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';

  }
  else {
    my $with_local = '';
    my $with_cpp11 = '';
    
    requires 'Alien::cmake3';
    requires 'Alien::MSYS' if $on_windows;
    #requires 'Alien::patchelf' if !($on_windows || $^O =~ /darwin/i);
    
    Alien::Build->log ("Proj library version is " . Alien::proj->version);
    die "Proj version must be at least 6.0.0"
      if not Alien::proj->atleast_version ('6.0.0');  

    plugin 'Build::SearchDep' => (
      aliens   => [grep {$_->install_type eq 'share'} @alien_deps],
      public_I => 1,
      public_l => 1,
    );
  
    start_url 'https://download.osgeo.org/gdal/CURRENT';
    #start_url 'https://download.osgeo.org/gdal/3.6.0';
    #start_url "file://$base_dir";  #  debug
    #  Handle release candidates in the regex.
    #  These are checked occasionally for debug purposes.
    my $RE_filter_version = qr/^gdal-([0-9\.]+)(?:rc\d\d)?\.tar\.gz$/;
    plugin Download => (
      filter  => $RE_filter_version,
      version => $RE_filter_version,
    );
    
    plugin Extract => (format => 'tar.gz');
  
    #plugin 'Build::Autoconf' => ();
    plugin 'Build::CMake' => ();
    
    #my $build_static = ($^O =~ /mswin/i) ? '' : '--disable-shared';
    #$build_static = '--enable-static=no';  #  override - HUGE files if static
    #$build_static = '' if $ENV{FORCE_DYNAMIC};
    my $build_static = '';
    
    
    if ($^O =~ /bsd|dragonfly/) {
      requires "Alien::gmake";
      plugin 'Build::Make' => 'gmake';
      #if (-d '/usr/local') {
          #$with_local = '--with-local=/usr/local';
      #}
      if (!-e '/usr/local/include/sqlite3.h' && Alien::sqlite->install_type eq 'system') {
        warn '/usr/local/include/sqlite3.h does not exist, '
           . 'you might need to install the sqlite package for your system, '
           . 'or install a share version of Alien::sqlite';
      }
    }
  
  
    my $make_cmd = '%{make}';
    my $make_inst_cmd = '%{make} install';  
    my @automated_rig_config_args;
    
    #  try not to exceed the cpan-testers log limits
    if ($on_automated_rig) {
      say "Running under CI or automated testing";
      $make_cmd      .= q/ | perl -ne "BEGIN {$|=1; open our $log, q|>|, q|build.log|};   print qq|\n| if 0 == ($. %% 100); print q|.|; print {$log} $_;" /;
      $make_inst_cmd .= q/ | perl -ne "BEGIN {$|=1; open our $log, q|>|, q|install.log|}; print qq|\n| if 0 == ($. %% 100); print q|.|; print {$log} $_;" /;
      if (!$on_windows) {
          $make_cmd =~ s/%%/%/;
          $make_cmd =~ s/"/'/g;
          $make_cmd .= ' || (E=$? && cat build.log && exit $E)';
          $make_inst_cmd =~ s/%%/%/;
          $make_inst_cmd =~ s/"/'/g;
          $make_inst_cmd .= ' || (E=$? && cat install.log && exit $E)';
      }
      else {
          #  fake exit codes on failure, not sure how to get originals
          $make_cmd      .= q/ || perl -e"system ('type', 'build.log'); exit 1"/;
          $make_inst_cmd .= q/ || perl -e"system ('type', 'install.log'); exit 1"/;
      }
  
      #  clean up the build dir on cpan testers etc
      plugin 'Cleanse::BuildDir';
    }
    
    my @with_args = (
      #https://gdal.org/build_hints.html#cmdoption-arg-GDAL_USE_-Packagename_in_upper_case-BOOL

alienfile  view on Meta::CPAN


  my @alien_rpaths;
  for my $alien (@alien_deps) {
    next if not $alien->install_type('share');
    push @alien_rpaths, $alien->dist_dir . '/lib';
  }
  if (!@alien_rpaths) {
    $build->log('No shared alien deps found, not updating rpaths');
    return;
  }

  use File::Which qw /which/;
  my $install_name_tool = which 'install_name_tool';
  if (!$install_name_tool) {
    $build->log('install_name_tool not found, not updating rpaths');
    return;
  }
  my $otool = which 'otool';
  if (!$install_name_tool) {
    $build->log('otool not found, not updating rpaths');
    return;
  }

  my $origin_string = '@loader_path';
  my @inst_tool_rpath_args
    = map {; '-add_rpath' => $_} (
       (map {"${origin_string}/../" . path ($_)->relative($install_path)->stringify} @alien_rpaths),
       @alien_rpaths,
      );

  my $dest_dir;
  if($build->meta_prop->{destdir})
  {
    $dest_dir = $ENV{DESTDIR};
  }
  $build->log('DEST DIR IS: ' . $dest_dir); 

  $build->log ("Prepending rpaths with args " . join ' ', @inst_tool_rpath_args);
  
  #  need to also do the bin dir - those files only need to have $ORIGIN/../lib added
  use File::Find::Rule;
  my (@so_files)
    = 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 = <>;
    }
}


sub patch_pkgconfig {
    #my $gdal_config_file = 'bin/gdal-config';
    #my $pkg_config_file  = 'lib/pkgconfig/gdal.pc';
    use File::Find::Rule;
    my @gdal_configs
      = File::Find::Rule->file()
                        ->name( 'gdal-config' )
                        ->in( '.' );
    my @pkg_configs
      = File::Find::Rule->file()
                        ->name( 'gdal.pc' )
                        ->in( '.' );
    say 'gdal-configs: ' . join ' ', @gdal_configs;
    say 'pkg-configs:  ' . join ' ', @pkg_configs;
    
    return if !@gdal_configs || !@pkg_configs;
    
    open my $gc_fh, '<', $gdal_configs[0] or die $!;
    my $dep_libs = '';
    while (defined (my $line = <$gc_fh>)) {
        if ($line =~ /CONFIG_DEP_LIBS=(.+)$/) {
            $dep_libs = $1;
            last;
        }
    }
    close $gc_fh;

    #  trim quotes (could do in prev check, but...)
    $dep_libs =~ s/^\"//;
    $dep_libs =~ s/\"$//;
    
    open my $pk_fh, '<', $pkg_configs[0] or die $!;
    my @pkg_conf;
    while (defined (my $line = <$pk_fh>)) {
        push @pkg_conf, $line;
    }
    close $pk_fh;

    #  change all (we should be more nuanced and do only the one that matters)
    foreach my $pkg_config_file (@pkg_configs) {
        say "Adding gdal dep_libs to $pkg_config_file";
        #  now add the dep libs to the pkg_conf file
        open $pk_fh, '>', $pkg_config_file or die $!;
        foreach my $line (@pkg_conf) {
            if ($line =~ /^CONFIG_INST_LIBS/) {
                chomp $line;
                $line .= " $dep_libs\n";
            }
            print {$pk_fh} $line;



( run in 0.440 second using v1.01-cache-2.11-cpan-02777c243ea )