DNS-Unbound

 view release on metacpan or  search on metacpan

Makefile.PL  view on Meta::CPAN

use File::Temp;
use File::Spec;
use JSON::PP;

my $ccpath = $ENV{'CC'} || $Config::Config{'cc'};
print "Your C compiler appears to be: $ccpath\n";

if ( eval { require ExtUtils::PkgConfig } ) {
    print "Oh good! You have ExtUtils::PkgConfig. :)\n";

    # These can fail because older libunbound versions (e.g., 1.4.22)
    # didn’t include a pkg-config file.
    $cflags = ExtUtils::PkgConfig->cflags($pkgconfig_name);
    $ldflags = ExtUtils::PkgConfig->libs($pkgconfig_name);
    $libdir = ExtUtils::PkgConfig->libs_only_L($pkgconfig_name);
}
else {
    print "Hmm. You don’t seem to have ExtUtils::PkgConfig.\n";
    print "I’ll try running `pkg-config` directly …\n";

    my $cmd = "pkg-config --cflags $pkgconfig_name";

    $cflags = `$cmd`;
    if ($?) {
        warn "`$cmd` failed (CHILD_ERROR=$?)\n";
    }
    else {
        print "Cool. It looks like pkg-config works.\n";

        $ldflags = `pkg-config --libs $pkgconfig_name`;
        $libdir = `pkg-config --libs-only-L $pkgconfig_name`;
    }

    $_ && chomp for $cflags, $ldflags, $libdir;
}

$libdir =~ s<\A-L><>;

# In case pkg-config didn’t give us anything.
if (!$ldflags) {
    warn "I didn’t find libunbound via pkg-config. :(\n";
    warn "Now I’ll look for libunbound via ExtUtils::Liblist …\n";

    my ($xtralibs, $bsloadlibs, $ldloadlibs, $ld_run_path, $where_ar) = ExtUtils::Liblist->ext('-lunbound', 0, 1);

    if (@$where_ar) {
        print "Libunbound found at: @$where_ar\n";

        my @pieces = File::Spec->splitdir($where_ar->[0]);
        pop @pieces;
        $libdir = File::Spec->catdir(@pieces);

        $ldflags = "-L$libdir -lunbound";

        if (!$cflags) {
            print "Looking for unbound.h …$/";

            require Config;
            my @incdirs = (
                $Config::Config{'usrinc'},
                map { split m<\s+> } (
                    $Config::Config{'incpth'},
                    $Config::Config{'locincpth'},
                ),
            );

            while (@pieces > 1) {
                pop @pieces;
                push @incdirs, File::Spec->catdir(@pieces, "include"),
            }

            my %checked;

            for my $dir (@incdirs) {
                next if !$dir;

                next if $checked{$dir};
                $checked{$dir}++;

                print "Checking $dir …$/";

                if (-s File::Spec->catdir($dir, 'unbound.h')) {
                    print "Found it!$/";
                    $cflags = "-I$dir";
                    last;
                }
                else {
                    print "… nope. :($/";
                }
            }

            if (!$cflags) {
                print "I didn’t find unbound.h, but maybe your compiler can?$/";
            }
        }
    }
    else {

        # Useful for Travis CI. Not sure if it’s relevant in production …

        warn "That didn’t work, either. This doesn’t look good. :-/\n";
        warn "As a last resort, let’s just try compiling with libunbound …\n";

        my ($tfh, $tpath) = File::Temp::tempfile( CLEANUP => 1 );
        print {$tfh} "#include <unbound.h>\nint main() { return 0; }\n";
        close $tfh;

        my $cmd = "$ccpath $cflags -xc -lunbound $tpath";
        print "Trying: `$cmd`\n";

        my $out = `$cmd`;
        if ($?) {
            die "$ccpath failed to use libunbound (CHILD_ERROR=$?): $out";
        }
        else {
            print "Huh, weird … the compiler can use and link libunbound.\n";
            print "Maybe there’s a bug in ExtUtils::Liblist?\n";
            print "Anyway, let’s get on with our business …\n";
        }
    }



( run in 1.301 second using v1.01-cache-2.11-cpan-71847e10f99 )