Alien-gdal

 view release on metacpan or  search on metacpan

lib/Alien/gdal.pm  view on Meta::CPAN

use List::Util qw /uniq/;
use Alien::proj;

our $VERSION = '1.44';

my ($have_geos, $have_proj, $have_spatialite);
my @have_aliens;
BEGIN {
    my @ld_lib_dirs;
    $have_geos = eval 'require Alien::geos::af';
    $have_spatialite = eval 'require Alien::spatialite';
    my @check_aliens
      = qw /Alien::geos::af Alien::sqlite Alien::proj
            Alien::freexl   Alien::libtiff Alien::spatialite/;
    foreach my $alien_lib (@check_aliens) {
        my $have_lib = eval "require $alien_lib";
        if ($have_lib && $alien_lib->install_type eq 'share') {
            push @have_aliens, $alien_lib;
            #  crude, but otherwise Geo::GDAL::FFI does not
            #  get fed all the needed info
            push @PATH, $alien_lib->bin_dir;
            push @ld_lib_dirs, $alien_lib->dist_dir . q{/lib};
        }
    }
    #if ($^O =~ /darwin/i) {
    #    @DYLD_LIBRARY_PATH = grep {defined} uniq (@DYLD_LIBRARY_PATH, @ld_lib_dirs);
    #}
    #elsif (not $^O =~ /mswin/i) {
    #    @LD_LIBRARY_PATH = grep {defined} uniq (@LD_LIBRARY_PATH, @ld_lib_dirs)
    #}

    #
    if ($^O =~ /mswin/i and !$ENV{PROJSO} and Alien::gdal->version lt 3) {
        my $libpath;
        $have_proj = eval 'require Alien::proj';
        if ($have_proj) {
            #  make sure we get the proj lib early in the search
            ($libpath) = Alien::proj->bin_dir;
        }
        my $proj_lib = FFI::CheckLib::find_lib (
            libpath => $libpath,
            lib     => 'proj',
        );
        #warn "PROJ_LIB FILE IS $proj_lib";
        $ENV{PROJSO} //= $proj_lib;
    }
    if (Alien::gdal->version ge '3') {
        push @PATH, 'Alien::proj'->bin_dirs
          if 'Alien::proj'->can('bin_dirs');
        if ($have_spatialite && Alien::spatialite->version ge 5) {
          push @PATH, 'Alien::spatialite'->bin_dirs
            if 'Alien::spatialite'->can('bin_dirs');
          push @have_aliens, 'Alien::spatialite';
        }
    }
}

sub version {
    my $self = shift;

    my $version = $self->SUPER::version;

    return $version if not $version =~ /CONFIG/;

    #  we have a busted version on Windows due to PkgConfig not handling pc vars
    #  system installs are on their own for now
    if ($self->is_share_install) {
        my $file = path($self->dist_dir, 'lib', 'pkgconfig', 'gdal.pc');
        my @lines = grep {/CONFIG_VERSION=/} $file->lines;
        $version = $lines[0];
        chomp $version;
        $version =~ s/CONFIG_VERSION=//;
    }

    return $version;
}

sub dynamic_libs {
    my $self = shift;
    
    #warn 'LD Path is: ' . join ' ', grep {defined} @LD_LIBRARY_PATH;
    #warn 'Bare env var: ' . ($ENV{LD_LIBRARY_PATH} // '');
    #warn 'DYLD Path is: ' . join ' ', grep {defined} @DYLD_LIBRARY_PATH;
    #warn 'Bare env var: ' . ($ENV{DYLD_LIBRARY_PATH} // '');

    my (@libs) = $self->SUPER::dynamic_libs;

    foreach my $alien (@have_aliens) {
        push @libs, $alien->dynamic_libs;
    }
    my (%seen, @libs2);
    foreach my $lib (@libs) {
        next if $seen{$lib};
        push @libs2, $lib;
        $seen{$lib}++;
    }
    
    return @libs2;
}

sub cflags {
    my $self = shift;
    
    my $cflags = $self->SUPER::cflags;
    
    if ($have_geos) {
        $cflags .= ' ' . (Alien::geos::af->cflags // '');
    }
    
    return $cflags;
}

sub libs {
    my $self = shift;
    
    my $cflags = $self->SUPER::libs;
    
    if ($have_geos) {
        $cflags .= ' ' . (Alien::geos::af->libs // '');
    }
    
    return $cflags;
}

#sub cflags_static {
#    my $self = shift;
#    
#    my $cflags = $self->SUPER::cflags_static;
#    
#    if ($have_geos) {
#        $cflags .= ' ' . Alien::geos::af->cflags_static;
#    }
#    
#    return $cflags;
#}
#
#sub libs_static {
#    my $self = shift;
#    
#    my $cflags = $self->SUPER::libs_static;
#    
#    if ($have_geos) {
#        $cflags .= ' ' . Alien::geos::af->libs_static;
#    }
#    
#    return $cflags;
#}

sub run_utility {
    my ($self, $utility, @args) = @_;

    my @alien_bins
      = grep {defined}
        map {$_->bin_dir}
        ($self, @have_aliens);
    push @alien_bins, Alien::proj->bin_dirs
      if Alien::proj->can ('bin_dirs');
    
    local $ENV{PATH} = $ENV{PATH};
    unshift @PATH, @alien_bins
      if @alien_bins;

    #  something of a hack
    local $ENV{LD_LIBRARY_PATH} = $ENV{LD_LIBRARY_PATH};
    push @LD_LIBRARY_PATH, Alien::gdal->dist_dir . '/lib';

    local $ENV{DYLD_LIBRARY_PATH} = $ENV{DYLD_LIBRARY_PATH};
    push @DYLD_LIBRARY_PATH, Alien::gdal->dist_dir . '/lib';

    if ($self->install_type eq 'share') {
        my @bin_dirs = $self->bin_dir;
        my $bin = $bin_dirs[0] // '';
        $utility = "$bin/$utility";  #  should strip path from $utility first?
    }
    #  handle spaces in path
    if ($^O =~ /mswin/i) {
        if ($utility =~ /\s/) {
            $utility = qq{"$utility"};
        }
    }
    else {
        $utility =~ s|(\s)|\$1|g;
    }


    #  user gets the pieces if it breaks
    capture {system $utility, @args};
}

sub data_dir {
    my $self = shift;
 
    my $path = $self->dist_dir . '/share/gdal';
    
    if (!-d $path) {
        #  try PkgConfig
        use PkgConfig;
        my %options;
        if (-d $self->dist_dir . '/lib/pkgconfig') {
            $options{search_path_override} = [ $self->dist_dir . '/lib/pkgconfig' ];



( run in 0.977 second using v1.01-cache-2.11-cpan-df04353d9ac )