Alien-Xrepo
view release on metacpan or search on metacpan
lib/Alien/Xrepo/Build.pm view on Meta::CPAN
# shallow <share>/<pkg> dir, so clear installdir and fall back to the engine's store root.
my %fetch_opts = %opts;
delete $fetch_opts{installdir};
my @entries = eval { $r->fetch_closure( $name, $recipe->version_for($name), %fetch_opts ) };
my ( $search, $store ) = $r->can('dep_search_dirs') ? $r->dep_search_dirs( \@entries ) : ( [], [] );
my %seen = map { $_ => 1 } @$search;
my $add = sub { my $d = shift; return unless defined $d && length $d; push @$search, $d unless $seen{$d}++ };
# Everything already recorded in this run is also fair game to resolve against.
for my $other ( values %{ $runtime_prop->{packages} // {} } ) {
my $id = $other->{installdir};
if ( defined $id && length $id ) {
push @$store, $id;
$add->( path($id)->child($_)->stringify ) for qw[bin lib];
}
$add->($_) for @{ $other->{linkdirs} // [] }, @{ $other->{bindirs} // [] };
}
$add->( path($libpath)->parent->stringify );
push @$store, $data->{installdir} if defined $data->{installdir} && length $data->{installdir};
my $deps = eval { $r->runtime_deps( $libpath, search_dirs => $search, store_dirs => $store ) };
return unless ref $deps eq 'ARRAY';
$data->{runtime_deps} = $deps;
my @missing = grep { ( $_->{scope} // '' ) eq 'unresolved' } @$deps;
warn "[!] $name: unresolved runtime dependencies (not found in the store or system paths): " .
join( ', ', map { $_->{name} } @missing ) . "\n"
if @missing;
return;
}
method export () {
return if $stage_done->{export};
$self->run_hooks('export');
if ( my $dir = $export_dir ) {
for my $name ( $recipe->packages ) {
next if exists $runtime_prop->{errors}{$name};
say "Exporting $name into $dir..." if $verbose;
eval { $r->export( $name, $recipe->version_for($name), packagedir => path($dir)->child($name)->absolute ); };
warn "[!] $name could not be exported: $@\n" if $@;
}
}
if ($snapshot) {
my $packages = $runtime_prop->{packages} // {};
if ( defined $share_dir ) {
my %rebased;
for my $name ( keys %$packages ) {
my %data = %{ $packages->{$name} // {} };
$self->_share_rebase( $name, \%data );
$rebased{$name} = \%data;
}
$packages = \%rebased;
}
my $data = {
dist_name => $recipe->name,
install_type => $install_type,
pkg_roots => $recipe->pkg_roots,
packages => $packages,
errors => $runtime_prop->{errors} // {},
digest => $self->_config_digest
};
path($snapshot)->parent->mkpath if defined $snapshot;
path($snapshot)->spew_utf8( encode_json($data) . "\n" );
say "Wrote runtime snapshot $snapshot" if $verbose;
}
$stage_done->{export} = 1;
$self->_checkpoint;
$self;
}
method test () {
return if $stage_done->{test};
$self->run_hooks('test');
$stage_done->{test} = 1;
$self;
}
method _version_for_pkg ($name) { $recipe->version_for($name) }
method _opts_for_pkg ( $name, %opts ) { $recipe->opts_for( $name, %opts ) }
method _config_digest () {
require Digest::SHA;
my @parts;
for my $name ( $recipe->packages ) {
my $version = $recipe->version_for($name);
my %opts = $recipe->opts_for( $name, %{ $install_prop->{profile} // {} } );
push @parts, $name, ( defined $version ? $version : '' );
push @parts, map { $_ . '=' . ( defined $opts{$_} ? $opts{$_} : '' ) } sort keys %opts;
}
Digest::SHA::sha256_hex( join "\x1f", @parts );
}
method _probe_satisfied ( $found, $expected ) {
return 0 unless defined $found && length $found;
return 1 unless defined $expected && length $expected;
return $found eq $expected;
}
# Recipe hook modules are loaded once per engine and invited to register their own stage hooks the same way code
# can: module->register_hooks($self).
method _load_recipe_hooks () {
for my $mod ( @{ $recipe->hooks || [] } ) {
next if $loaded_hooks->{$mod};
my $file = $mod;
$file =~ s{::}{/}g;
$file .= '.pm';
require $file;
my $register = $mod->can('register_hooks');
die "Alien::Xrepo::Build: recipe hook module '$mod' must expose register_hooks(\$build), got '$mod'" unless $register;
$register->($self);
$loaded_hooks->{$mod} = 1;
}
return;
}
# pkg_roots mirror Alien::Build's: name => environment variable whose value holds the package root. When the
# variable is set and points at an existing directory the package is treated as system-provided there and xrepo is
# never consulted (probe reports it satisfied; install/gather record it directly).
method _pkg_root ($name) {
my $pr = $recipe->pkg_roots->{$name};
return () unless defined $pr && length $pr;
my $dir = $ENV{$pr};
return () unless defined $dir && length $dir && -d $dir;
return path($dir)->absolute->stringify;
lib/Alien/Xrepo/Build.pm view on Meta::CPAN
$env{VCPKG_ROOT} = $dir if $ns eq 'vcpkg';
return %env;
}
# Shallow share install: when a share_dir is set, every package is installed
# into <share_dir>/<pkg> (its own little store) so the dist ships its own copy
# of the libraries and never depends on the xmake cache at runtime. The
# snapshot records those paths share-relative (see export/_share_rebase).
method _pkg_installdir ($name) {
return () unless defined $share_dir && length $share_dir;
return () if $name =~ /::/;
return path($share_dir)->absolute->child($name)->stringify;
}
# Rewrite an installed package's recorded paths to be relative to share_dir
# (so they survive `./Build install` relocating the share dir). Only paths
# that actually live under the share dir are made relative.
method _share_rebase ( $name, $data ) {
my $share = path($share_dir)->absolute->stringify;
for my $k (qw[includedirs libfiles linkdirs bindirs]) {
next unless ref $data->{$k} eq 'ARRAY';
$data->{$k} = [ map { $self->_under_share( $share, $_ ) } @{ $data->{$k} } ];
}
for my $k (qw[libpath installdir]) {
$data->{$k} = $self->_under_share( $share, $data->{$k} ) if defined $data->{$k};
}
if ( ref $data->{runtime_deps} eq 'ARRAY' ) {
for my $dep ( @{ $data->{runtime_deps} } ) {
next unless ref $dep eq 'HASH';
for my $k (qw[path dir]) {
$dep->{$k} = $self->_under_share( $share, $dep->{$k} ) if defined $dep->{$k};
}
}
}
return $data;
}
method _under_share ( $share, $p ) {
return $p unless defined $p && length $p;
my ( $s, $n ) = map { ( $_ // '' ) =~ s{\\}{/}gr } ( $share, $p );
$s =~ s{/$}{};
$s = lc($s) if $^O eq 'MSWin32';
$n = lc($n) if $^O eq 'MSWin32';
return $p unless index( $n, "$s/" ) == 0;
return substr( $p, length($share) + 1 );
}
# Callers mark the stage done *before* checkpointing. The file has to record the
# stage that just finished, or a resumed Build re-enters work that is already
# complete -- on a long stage such as install, repeating it in full.
method _checkpoint () {
return unless defined $checkpoint;
path($checkpoint)->parent->mkpath;
my $data = {
install_type => $install_type,
stage_done => $stage_done,
meta_prop => $meta_prop,
install_prop => $install_prop,
runtime_prop => $runtime_prop
};
path($checkpoint)->spew_utf8( encode_json($data) . "\n" );
return;
}
#
method _load_checkpoint () {
return 0 unless defined $checkpoint && -e $checkpoint;
my $data = eval { decode_json( path($checkpoint)->slurp_utf8 ) };
return 0 unless ref $data eq 'HASH';
$install_type = $data->{install_type} // 'system';
$stage_done = $data->{stage_done} // {};
$meta_prop = $data->{meta_prop} // {};
$install_prop = $data->{install_prop} // {};
$runtime_prop = $data->{runtime_prop} // {};
return 1;
}
};
#
1;
__END__
Copyright (C) Sanko Robinson.
This library is free software; you can redistribute it and/or modify it under the terms found in
the Artistic License 2. Other copyrights, terms, and conditions may apply to data transmitted
through this module.
( run in 1.240 second using v1.01-cache-2.11-cpan-234fa220139 )