Alien-gdal

 view release on metacpan or  search on metacpan

alienfile  view on Meta::CPAN

  
  return patch_rpaths_macos($build)
    if $^O =~ /darwin/i;

  my $h = get_alien_state_hash();
  my $install_path = $h->{install}{prefix};
  return if !defined $install_path;

  my @alien_rpaths;
  for my $alien (@alien_deps) {
    next if not $alien->install_type('share');
    my $libdir;
    for my $tgt (qw/lib lib64 lib32/) {
        $libdir = path ($alien->dist_dir, $tgt);
        last if -d $libdir;
    }
    $build->log ("Unable to locate lib dir for $alien, tried lib, lib64 and lib32.  Expect issues later on.")
      if !-d $libdir;
    push @alien_rpaths, $libdir;
  }
  if (!@alien_rpaths) {
    $build->log('No shared alien deps found, not updating rpaths');
    return;
  }

  eval 'require Alien::patchelf'
    or do {
      warn 'Unable to load Alien::patchelf, cannot update rpaths';
      return;
    };
  my $pe = Alien::patchelf->new;


  my $origin_string = $^O =~ /darwin/ ? '@loader_path' : '$ORIGIN';
  my $alien_rpath_text
    = join ':', (
       (map {$origin_string . '/../' . path ($_)->relative($install_path)->stringify} @alien_rpaths),
       @alien_rpaths
      );

  $build->log ("Prepending rpaths with $alien_rpath_text");


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

  #  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.so/ )
              ->in( $dest_dir );
  
  foreach my $so_file (@so_files) {
    my ($old_rpath, $result, $stderr, @errors);
    ($old_rpath, $stderr, @errors)
      = $pe->patchelf ('--print-rpath', $so_file);
    $old_rpath //= '';
    #  prepend our paths
    my $rpath = $alien_rpath_text . ($old_rpath ? (':' . $old_rpath) : '');
    $build->log("Updating rpath for $so_file to $rpath, was $old_rpath");
    ($result, $stderr, @errors)
      = $pe->patchelf ('--set-rpath', $rpath, $so_file);
    warn $stderr if $stderr;
  }

#my $xy = <STDIN>;
  return;
}

sub patch_rpaths_macos {
  my ($build) = @_;
  
  #  just for safety
  return if not $^O =~ /darwin/i;

  my $h = get_alien_state_hash();
  my $install_path = $h->{install}{prefix};
  return if !defined $install_path;

  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);
}



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