Alien-gdal

 view release on metacpan or  search on metacpan

alienfile  view on Meta::CPAN

use 5.010;
use warnings;
use alienfile;
use Sort::Versions;
use Env qw /@PATH @LD_LIBRARY_PATH @DYLD_LIBRARY_PATH @PROJ_LIB/;
use Path::Tiny qw /path/;
use List::Util;
use FFI::CheckLib;
use Config;

my $on_windows = $^O =~ /mswin/i;
my $on_automated_rig
  =  $ENV{PERL_CPAN_REPORTER_DIR}
  || $ENV{PERL_CPAN_REPORTER_CONFIG}
  || $ENV{AUTOMATED_TESTING};
  #|| $ENV{TRAVIS}
  #|| $ENV{APPVEYOR}
  #|| $ENV{CI};

my @alien_deps = qw(
        Alien::geos::af
        Alien::proj
        Alien::sqlite
        Alien::libtiff
  );

#  make sure we get GEOS, PROJ and other support
foreach my $alien (@alien_deps) {
  eval "require $alien";
  my $e = $@;
  warn $e if $e;
  if (!$e && $alien->install_type eq 'share') {
    log "Prepending $alien bin dir to path";
    unshift @PATH, $alien->bin_dir;
  }
}
my $have_spatialite = eval 'require
  Alien::spatialite';
if ($have_spatialite && Alien::spatialite->version ge 5) {
  if (!$on_windows and Alien::spatialite->install_type('share')
      and $Alien::spatialite::VERSION lt 1.06) {
    $have_spatialite = undef;  #  rpath issues before this
  }
  else {
    log "Prepending Alien::spatialite bin dir to path";
    unshift @PATH, Alien::spatialite->bin_dir;
    push @alien_deps, 'Alien::spatialite' if Alien::spatialite->install_type eq 'share';
  }
}
else {
  $have_spatialite = undef;
}

#  configure script does not seem to detect the proj lib distributed with Strawberry perl
#use FFI::CheckLib;
#my $proj_lib = FFI::CheckLib::find_lib (lib => 'proj');
Alien::Build->log ('$ENV{PROJ_LIB} IS ' . ($ENV{PROJ_LIB} // ''));


#  make libtool noisy for debug purposes
#$ENV{LTFLAGS} = "--debug --verbose" if $on_windows;


use Cwd;
my $base_dir = getcwd();

my $min_target_version = '3.1.0';

plugin 'PkgConfig' => (
    pkg_name => 'gdal',
    minimum_version => $min_target_version,
);


share {

  #  add existing alien files to path etc
  #  not very elegant...
  #  disable for now - config tests fail
  #  it looks to be already noted in https://github.com/Perl5-Alien/Alien-Build/issues/12
  #  might also be able to access via old hash via https://metacpan.org/pod/Alien::Build#install_prop
  my $have_alien_gdal = eval 'require Alien::gdal';
  my $copy_from_dir;
  if ($ENV{ALIEN_SHARE_RECYCLE} && $have_alien_gdal && Alien::gdal->install_type eq 'share') {
      my $ag_version = Alien::gdal->version;
      say "Found existing gdal via alien ($ag_version) in " .  Alien::gdal->dist_dir;
      #  append the relevant path
      if (versioncmp($ag_version, $min_target_version) >= 0) {
        $copy_from_dir = Alien::gdal->dist_dir;
      }
  }

  if ($copy_from_dir) {
    #  files are copied in the extract hook
    meta->register_hook(download => sub {
      Path::Tiny->new ('fauxdownload.txt')->touch;
    });

    if($^O eq 'MSWin32') {
      meta->register_hook(extract => sub {
        my($build) = @_;
        $copy_from_dir =~ s|/|\\|g;  #  it seems that xcopy needs backslashes in paths
        $build->system("xcopy $copy_from_dir . /E /Q");
      });
    }
    else {
      meta->register_hook(extract => sub {
        my ($build) = @_;
        "cp -aR $copy_from_dir .";
      });
    }

    meta->after_hook( extract => sub {
      my($build) = @_;
      $build->log('CURRENTLY IN ' . cwd());
      File::Path::rmtree '_alien';
    });

    plugin 'Build::Copy';

  }

alienfile  view on Meta::CPAN

    = grep {not -l $_}
      File::Find::Rule
              ->file()
              ->name( qr/^libgdal[.\d]*.dylib/ )
              ->in( $dest_dir );
  
  foreach my $so_file (@so_files) {
    use Capture::Tiny qw /capture/;
    $build->log ("Updating rpath for $so_file");
    my ($otool_text, $old_rpath, $result, $stderr, @errors);
    $build ->log ("Calling: $otool, '-l', $so_file");
    #eval {
      ($otool_text, $stderr, @errors)
        = capture {system $otool, '-l', $so_file};
    #};
    #$build->log ($@) if $@;
    #$build->log (@errors) if @errors;
    $build->log ($stderr) if $stderr;
    #$build->log($otool_text);  #  debug
    
    #  now we need to extract the rpaths
    my @lines = split "\n", $otool_text;
    my @existing_paths;
    while (defined (my $line = shift @lines)) {
        if ($line =~ /RPATH/) {
            shift @lines;
            $line = shift @lines;
            $line =~ s/^\s+path\s//;
            $line =~ s/\s\(offset.+$//;
            push @existing_paths, $line;
        }
    }
    #  remove existing
    if (@existing_paths) {
      $build->log ("removing existing rpaths " . join ' ', @existing_paths);
      my @delete_args = map {; '-delete_rpath' => $_} @existing_paths;
      ($result, $stderr, @errors)
        = capture {system $install_name_tool, @delete_args, $so_file};
      #$build->log($result);
      $build->log($stderr) if $stderr;
    }

    #  now append the existing ones
    push @inst_tool_rpath_args, map {; '-add_rpath' => $_} @existing_paths;
    #  prepend our paths
    ($result, $stderr, @errors)
      = capture {system $install_name_tool, @inst_tool_rpath_args, $so_file};
    #$build->log($result);
    $build->log($stderr) if $stderr;
  }

  return;
}


#  we can get dangling -L values
#  bandaid until we find the source
sub cleanup_ldflags {
    my ($build, @args) = @_;

    if ($ENV{LDFLAGS} && $ENV{LDFLAGS} =~ /\s*-L\s*$/) {
        $build->log("Trimming trailing -L from $ENV{LDFLAGS}");
        $ENV{LDFLAGS} =~ s/\s*-L\s*$//;
    }

    #$orig->($build, @args);
    return;
}


sub update_pkg_conf_path {
    return if !$on_windows;
    use Env qw /@PKG_CONFIG_PATH/;
    say 'Modifying drive paths in PKG_CONFIG_PATH';
    say $ENV{PKG_CONFIG_PATH};
    #  msys-ificate drive paths
    @PKG_CONFIG_PATH = map {my $x = $_; $x =~ s{^([a-z]):}{/$1}i; $x} @PKG_CONFIG_PATH;
    say $ENV{PKG_CONFIG_PATH};
    return;
}


#  git for windows clashes with MSYS
#  if its /usr/bin dir is in the path
sub remove_gitfw_from_path {
  my ($orig, $build, @args) = @_;

  return $orig->($build, @args)
    if !$on_windows;

  local $ENV{PATH} = $ENV{PATH};

  my $msys_path = eval {
    path('Alien::MSYS'->msys_path())
  };
  return if !defined $msys_path;
  my $count = @PATH;

  @PATH
    = grep {path($_)->stringify =~ m|/usr/bin$| && path($_) ne $msys_path ? () : $_}
      @PATH;

  my $removed = $count - @PATH;
  if ($removed) {
    $build->log ("$removed additional .../usr/bin dirs were removed from the path for compilation");
  }

  $orig->($build, @args);
}

sub pause {
    return;  #  re-enable in case of debug
#    return if $on_automated_rig;
#    return if !$on_windows;

    say "CONTINUE?";
    my $response = <>;
    while (not $response =~ /yes/) {
        $response = <>;
    }
}




( run in 0.784 second using v1.01-cache-2.11-cpan-d8267643d1d )