Alien-libmaxminddb

 view release on metacpan or  search on metacpan

libmaxminddb.PL  view on Meta::CPAN

use 5.014;
use warnings;
use utf8;

my $DIST_NAME       = 'Alien-libmaxminddb';
my $BUNDLED_VERSION = '1.13.3';

package PgkConf;

use File::Spec qw();

sub new {
    my ($class, %attrs) = @_;

    my $self = bless \%attrs, $class;

    $self->{pkgconf} = $self->find_pkgconf(qw(pkgconf pkg-config));

    return $self;
}

sub find_pkgconf {
    my ($self, @programs) = @_;

    my $devnull = File::Spec->devnull;

    for my $pkgconf (@programs) {
        my $output = `$pkgconf --version 2>$devnull`;
        if ($? == 0) {
            return $pkgconf;
        }
    }

    return;
}

sub pkgconf {
    my ($self, @options) = @_;

    my $module = $self->{module};

    my $devnull = File::Spec->devnull;

    my $pkgconf = $self->{pkgconf};
    if (defined $pkgconf) {
        my $cmd    = $pkgconf . q{ } . join(q{ }, @options) . q{ } . $module;
        my $output = `$cmd 2>$devnull`;
        if ($? == 0) {
            chomp $output;
            return 1, $output;
        }
    }

    return 0, undef;
}

sub cflags {
    my ($self) = @_;

    return $self->pkgconf('--cflags');
}

sub libs {
    my ($self) = @_;

    return $self->pkgconf('--libs');
}

sub modversion {
    my ($self) = @_;

    return $self->pkgconf('--modversion');
}

sub config {
    my ($self) = @_;

    my ($ok_cflags, $cflags) = $self->cflags;
    return if !$ok_cflags;

    my ($ok_libs, $libs) = $self->libs;
    return if !$ok_libs;

    my ($ok_modversion, $modversion) = $self->modversion;
    return if !$ok_modversion;

    my $config = {
        cflags       => $cflags,
        install_type => 'system',
        libs         => $libs,
        version      => $modversion,
    };

    return $config;
}

package LibraryBuilder;

use Config;
use ExtUtils::CBuilder;
use File::Copy qw();
use File::Path qw();
use File::Spec qw();
use File::Temp qw();

sub new {
    my ($class, %attrs) = @_;

    my $self = bless \%attrs, $class;

    my $builder = ExtUtils::CBuilder->new;
    if (!$builder->have_compiler) {
        warn "Error: No C compiler found\n";
        die "OS unsupported\n";
    }

    my $is_little_endian = 1;
    my $byteorder        = $Config{byteorder};
    if (!defined $byteorder) {
        warn "\$Config{byteorder} is undefined\n";
        die "OS unsupported\n";
    }
    if ($byteorder == 4321 || $byteorder == 87654321) {
        $is_little_endian = 0;
    }
    elsif (!($byteorder == 1234 || $byteorder == 12345678)) {
        warn "Unknown byte order: $byteorder\n";
        die "OS unsupported\n";
    }

    $self->{builder}          = $builder;
    $self->{is_little_endian} = $is_little_endian;

    return $self;
}

sub compile {
    my ($self, $cflags_ref, $code) = @_;

    my ($out, $src_file) = File::Temp::tempfile('testXXXX', SUFFIX => '.c')
        or die 'Cannot create temporary source code file';
    print {$out} $code;
    close $out
        or die "Cannot write to $src_file";

    my $obj_file = eval {
        $self->{builder}->compile(
            source               => $src_file,
            extra_compiler_flags => $cflags_ref,
        );
    };

    unlink $src_file;

    return $obj_file;
}

sub can_compile {
    my ($self, $cflags_ref, $code) = @_;

    my $ok = 0;

    my $obj_file = $self->compile($cflags_ref, $code);
    if (defined $obj_file) {
        unlink $obj_file;
        $ok = 1;
    }

    return $ok;

libmaxminddb.PL  view on Meta::CPAN

        @{$self->check_libm($cflags_ref, [])},
        @{$self->check_libsocket($cflags_ref, [])},
    ];

    my @src_files = map { File::Spec->catfile(qw(maxminddb src), $_) }
        qw(maxminddb.c data-pool.c);

    my $hdr_file = File::Spec->catfile(qw(maxminddb include), 'maxminddb.h');

    File::Copy::copy($hdr_file, 'maxminddb.h')
        or die "Cannot copy $hdr_file";

    $self->create_config_h('config.h', $cflags_ref)
        or die 'Cannot create config.h';

    $self->create_maxminddb_config_h('maxminddb_config.h', $cflags_ref)
        or die 'Cannot create maxminddb_config.h';

    my @inc_dirs = File::Spec->curdir();

    my @obj_files = map {
        $self->{builder}->compile(
            source               => $_,
            include_dirs         => \@inc_dirs,
            extra_compiler_flags => [@{$cflags_ref}, '-DHAVE_CONFIG_H=1'],
        ) or die "Cannot compile $_";
    } @src_files;

    my $lib_file = $self->create_library('maxminddb', \@obj_files);

    unlink @obj_files;

    $self->{hdr_files} = [qw(maxminddb.h maxminddb_config.h)];
    $self->{lib_files} = [$lib_file];

    $self->{config} = {
        cflags       => join(q{ }, @{$cflags_ref}),
        install_type => 'share',
        libs         => join(q{ }, @{$libs_ref}),
        version      => $BUNDLED_VERSION,
    };

    return $self;
}

sub config {
    my ($self) = @_;

    return $self->{config};
}

package ConfigWriter;

use File::Path qw();
use File::Spec qw();
use JSON::PP   qw();

sub new {
    my ($class, %attrs) = @_;

    my $self = bless \%attrs, $class;

    return $self;
}

sub install {
    my ($self, $dist_dir) = @_;

    my $alien_dir = File::Spec->catdir($dist_dir, '_alien');
    File::Path::make_path($alien_dir);

    my $json_file = File::Spec->catfile($alien_dir, 'alien.json');
    open my $out, '>', $json_file
        or die "Cannot create $json_file";
    print ${out} JSON::PP->new->pretty->encode($self->{config});
    close $out
        or die "Cannot write to $json_file";

    return $self;
}

sub done {
    open my $out, '>', 'done.txt';
    close $out;
    return;
}

package main;

my $dist_dir = File::Spec->catdir(qw(blib lib auto share dist), $DIST_NAME);

my $config = PgkConf->new(module => 'libmaxminddb')->config;
if (!defined $config) {
    $config = LibraryBuilder->new->build->install($dist_dir)->config;
}
ConfigWriter->new(config => $config)->install($dist_dir)->done;



( run in 0.474 second using v1.01-cache-2.11-cpan-411bb0df24b )