| 1 | use v5.40; | ||
| 2 | use feature qw[class try]; | ||
| 3 | no warnings 'experimental::class'; | ||
| 4 | # | ||
| 5 | class Alien::Xrepo::Runtime v1.0.10 { | ||
| 6 | use Alien::Xrepo; | ||
| 7 | use Alien::Xrepo::Build::Recipe; | ||
| 8 | use Alien::Xrepo::Runtime::Alt; | ||
| 9 | use Config (); | ||
| 10 | use JSON::PP qw[decode_json]; | ||
| 11 | use Path::Tiny; | ||
| 12 | use File::ShareDir (); | ||
| 13 | # | ||
| 14 | field $pkg_name : param = undef; | ||
| 15 | field $recipe : param = undef; # Recipe object (or file/dir) - alternative to inline pkg_name | ||
| 16 | field $root : param = undef; | ||
| 17 | field $verbose : param = 0; | ||
| 18 | field $diagnosis : param = 0; # forwarded to Alien::Xrepo->diagnosis (xmake -D on installs) | ||
| 19 | field $repo : param //= undef; # injectable engine | ||
| 20 | field $snapshot : param //= undef; # path to a snapshot JSON (hermetic mode) | ||
| 21 | field $autodetect_snapshot : param //= 1; # look for dist auto/share snapshots | ||
| 22 | field $install_opts : param = {}; # ambient profile merged under every package def | ||
| 23 | field $cache : param //= 1; # forward the engine's warm-start cache to Alien::Xrepo | ||
| 24 | |||
| 25 | # | ||
| 26 | field $recipe_obj = undef; # resolved Alien::Xrepo::Build::Recipe | ||
| 27 | field $infos = {}; # package => Alien::Xrepo::PackageInfo | ||
| 28 | field $snap = {}; # package => raw snapshot data | ||
| 29 | field $r = undef; # resolved Alien::Xrepo engine | ||
| 30 | field $snap_install_type = undef; | ||
| 31 | field $snap_file : reader(snapshot_path) = undef; # path of the snapshot actually loaded, if any | ||
| 32 | field $snap_errors : reader(snapshot_errors) = {}; # package => build error recorded in that snapshot | ||
| 33 | field $rdeps = {}; # package => discovered runtime deps (aref) | ||
| 34 | field $closure = {}; # package => raw fetch --deps closure (aref) | ||
| 35 | field $check_runtime_deps : param //= 1; # surface the diagnostic through package_info | ||
| 36 | |||
| 37 | # | ||
| 38 | ADJUST { | ||
| 39 | if ( !defined $pkg_name && !defined $recipe ) { | ||
| 40 | if ( $self->can('recipe') ) { | ||
| 41 | $recipe = $self->recipe; | ||
| 42 | } | ||
| 43 | elsif ( $self->can('pkg_name') ) { | ||
| 44 | $pkg_name = $self->pkg_name; | ||
| 45 | } | ||
| 46 | } | ||
| 47 | die "pkg_name, recipe, or a subclass recipe() is required" if !defined $pkg_name && !defined $recipe; | ||
| 48 | if ( !keys %$install_opts && $self->can('install_opts') ) { | ||
| 49 | $install_opts = { $self->install_opts }; | ||
| 50 | } | ||
| 51 | $r = $repo // Alien::Xrepo->new( root => $root, verbose => $verbose, diagnosis => $diagnosis, cache => $cache ); | ||
| 52 | if ( defined $recipe ) { | ||
| 53 | $recipe_obj | ||
| 54 | = ref $recipe eq 'HASH' ? Alien::Xrepo::Build::Recipe->new(%$recipe) : | ||
| 55 | ref $recipe ? $recipe : | ||
| 56 | Alien::Xrepo::Build::Recipe->new( defined $recipe && -d $recipe ? ( dir => $recipe ) : ( file => $recipe ), ); | ||
| 57 | } | ||
| 58 | else { | ||
| 59 | $recipe_obj = Alien::Xrepo::Build::Recipe->new( packages => $pkg_name ); | ||
| 60 | } | ||
| 61 | $self->_load_snapshot; | ||
| 62 | } | ||
| 63 | # | ||
| 64 | method package_names () { $recipe_obj->packages } | ||
| 65 | method package_defs () { $recipe_obj->package_defs } | ||
| 66 | # | ||
| 67 | method has_snapshot () { defined $snap_file } | ||
| 68 | # | ||
| 69 | # Hermetic mode: a snapshot file wins entirely (no xrepo subprocess). | ||
| 70 | method _load_snapshot () { | ||
| 71 | my $file = $snapshot; | ||
| 72 | if ( !defined $file && $autodetect_snapshot ) { | ||
| 73 | for my $cand ( __PACKAGE__->_snapshot_candidates_for( ref $self ) ) { | ||
| 74 | if ( -e $cand ) { $file = $cand; last; } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | return unless defined $file && -e $file; | ||
| 78 | my $data = eval { decode_json( path($file)->slurp_utf8 ) }; | ||
| 79 | return unless ref $data eq 'HASH'; | ||
| 80 | $snap = $data->{packages} // {}; | ||
| 81 | $snap_install_type = $data->{install_type}; | ||
| 82 | $snap_file = "$file"; | ||
| 83 | $snap_errors = $data->{errors} // {}; | ||
| 84 | my $base = path($file)->parent; | ||
| 85 | |||
| 86 | for my $name ( keys %$snap ) { | ||
| 87 | my %pkg = %{ $snap->{$name} }; | ||
| 88 | $self->_rebase_relative( $base, \%pkg ); | ||
| 89 | $infos->{$name} = Alien::Xrepo::PackageInfo->new(%pkg); | ||
| 90 | } | ||
| 91 | return; | ||
| 92 | } | ||
| 93 | |||
| 94 | method _rebase_relative ( $base, $data ) { | ||
| 95 | $data->{$_} = [ map { $self->_snap_abs( $base, $_ ) } @{ $data->{$_} } ] for qw[includedirs libfiles linkdirs bindirs]; | ||
| 96 | for my $k (qw[libpath installdir]) { | ||
| 97 | $data->{$k} = $self->_snap_abs( $base, $data->{$k} ) if defined $data->{$k}; | ||
| 98 | } | ||
| 99 | if ( ref $data->{runtime_deps} eq 'ARRAY' ) { | ||
| 100 | for my $dep ( @{ $data->{runtime_deps} } ) { | ||
| 101 | next unless ref $dep eq 'HASH'; | ||
| 102 | for my $k (qw[path dir]) { | ||
| 103 | $dep->{$k} = $self->_snap_abs( $base, $dep->{$k} ) if defined $dep->{$k}; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | } | ||
| 107 | return; | ||
| 108 | } | ||
| 109 | |||
| 110 | method _snap_abs ( $base, $p ) { | ||
| 111 | return $p if !defined $p || length $p == 0 || path($p)->is_absolute; | ||
| 112 | return $base->child($p)->stringify; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub _snapshot_candidates_for ( $class, $instance ) { | ||
| 116 | ( my $dist = $instance ) =~ s{::}{-}g; | ||
| 117 | my @cand = path( 'blib', 'lib', 'auto', 'share', 'dist', $dist )->child('xrepo-snapshot.json')->stringify; | ||
| 118 | my $share = eval { File::ShareDir::dist_dir($dist) }; | ||
| 119 | unshift @cand, path($share)->child('xrepo-snapshot.json')->stringify if $share; | ||
| 120 | return @cand; | ||
| 121 | } | ||
| 122 | |||
| 123 | # Lazy resolution: so long as we are not serving a snapshot, fetch paths for a package the first time it is asked | ||
| 124 | # for and remember them. An unresolvable package settles at undef (safe defaults) instead of killing the caller, so | ||
| 125 | # a cold checkout (no snapshot, no store) yields safe defaults. | ||
| 126 | method _ensure_info ($pkg) { | ||
| 127 | return $infos->{$pkg} if exists $infos->{$pkg}; | ||
| 128 | my $info = $infos->{$pkg}; | ||
| 129 | return $info if defined $info; | ||
| 130 | my $version = $recipe_obj->version_for($pkg); | ||
| 131 | my %ambient = ( %{ $recipe_obj->defaults // {} }, %$install_opts ); | ||
| 132 | my %opts = $recipe_obj->opts_for( $pkg, %ambient ); | ||
| 133 | my $resolved; | ||
| 134 | try { $resolved = $r->fetch( $pkg, $version, %opts ) } | ||
| 135 | catch ($e) { | ||
| 136 | warn "[!] could not resolve $pkg: $e\n" if $verbose; | ||
| 137 | $resolved = undef; | ||
| 138 | } | ||
| 139 | $infos->{$pkg} = $resolved; | ||
| 140 | return $resolved; | ||
| 141 | } | ||
| 142 | # | ||
| 143 | method _pkg_info ( $pkg = undef ) { | ||
| 144 | my $name = $pkg // ( $recipe_obj->packages )[0]; | ||
| 145 | die "Unknown package '$name'. Known: @{[ $recipe_obj->packages ]}" unless grep { $_ eq $name } $recipe_obj->packages; | ||
| 146 | return $self->_ensure_info($name); | ||
| 147 | } | ||
| 148 | # | ||
| 149 | method libpath ( $pkg = undef ) { my $i = $self->_pkg_info($pkg); $i ? $i->libpath : undef } | ||
| 150 | method ffi_lib ( $pkg = undef ) { $self->libpath($pkg) } | ||
| 151 | method bin_dir ( $pkg = undef ) { my $i = $self->_pkg_info($pkg); $i ? $i->bin_dir : () } | ||
| 152 | |||
| 153 | # Prepend the package's shippable binary directories to PATH so a consumer | ||
| 154 | # can spawn the tool (e.g. ninja) without knowing where xrepo put it. | ||
| 155 | method prepend_to_path ( $pkg = undef ) { | ||
| 156 | my @dirs = $self->bin_dir($pkg); | ||
| 157 | return () unless @dirs; | ||
| 158 | $ENV{PATH} = join( $Config::Config{path_sep} // ':', @dirs, ( $ENV{PATH} // () ) ); | ||
| 159 | return @dirs; | ||
| 160 | } | ||
| 161 | method version ( $pkg = undef ) { my $i = $self->_pkg_info($pkg); $i ? $i->version : undef } | ||
| 162 | method kind ( $pkg = undef ) { my $i = $self->_pkg_info($pkg); $i ? $i->kind : undef } | ||
| 163 | # | ||
| 164 | method cflags ( $pkg = undef ) { | ||
| 165 | my $i = $self->_pkg_info($pkg); | ||
| 166 | return '' unless $i; | ||
| 167 | return join ' ', map {"-I$_"} @{ $i->includedirs }; | ||
| 168 | } | ||
| 169 | method cflags_static ( $pkg = undef ) { $self->cflags($pkg) } | ||
| 170 | # | ||
| 171 | method libs ( $pkg = undef ) { | ||
| 172 | my $i = $self->_pkg_info($pkg); | ||
| 173 | return '' unless $i; | ||
| 174 | my $libs = join ' ', map {"-L$_"} @{ $i->linkdirs // [] }; | ||
| 175 | $libs .= ' ' . join ' ', map {"-l$_"} @{ $i->links // [] }; | ||
| 176 | return $libs; | ||
| 177 | } | ||
| 178 | method libs_static ( $pkg = undef ) { $self->libs($pkg) } | ||
| 179 | # | ||
| 180 | method dynamic_libs ( $pkg = undef ) { | ||
| 181 | my $i = $self->_pkg_info($pkg); | ||
| 182 | return () unless $i; | ||
| 183 | return map { $_ // () } @{ $i->libfiles // [] }; | ||
| 184 | } | ||
| 185 | # | ||
| 186 | method dist_dir ( $pkg = undef ) { | ||
| 187 | my $i = $self->_pkg_info($pkg); | ||
| 188 | return $i ? $i->installdir : undef; | ||
| 189 | } | ||
| 190 | # | ||
| 191 | method install_type ( $pkg = undef ) { | ||
| 192 | return $snap_install_type if defined $snap_install_type; | ||
| 193 | return $self->_pkg_info($pkg) ? 'share' : 'system'; | ||
| 194 | } | ||
| 195 | # | ||
| 196 | method split_flags ( $flags, $pkg = undef ) { | ||
| 197 | return () unless defined $flags; | ||
| 198 | return grep {length} split ' ', $flags; | ||
| 199 | } | ||
| 200 | # | ||
| 201 | method alt ( $name = undef ) { | ||
| 202 | my $primary = ( $recipe_obj->packages )[0]; | ||
| 203 | my $pkg = $name // $primary; | ||
| 204 | die "Unknown package '$pkg'. Known: @{[ $recipe_obj->packages ]}" unless grep { $_ eq $pkg } $recipe_obj->packages; | ||
| 205 | return $self if $pkg eq $primary; | ||
| 206 | return Alien::Xrepo::Runtime::Alt->new( base => $self, pkg => $pkg ); | ||
| 207 | } | ||
| 208 | # | ||
| 209 | method find_header ( $filename, $pkg = undef ) { | ||
| 210 | my $i = $self->_pkg_info($pkg); | ||
| 211 | return $i ? $i->find_header($filename) : undef; | ||
| 212 | } | ||
| 213 | |||
| 214 | # An Alien hands Perl a library to dlopen; the dynamic linker resolves that library's *own* dependencies | ||
| 215 | # before our symbols are usable, so a vendored provider (freetype behind libsdl3_ttf) has to be findable | ||
| 216 | # first. The engine can read those tables (see Alien::Xrepo::runtime_deps); these accessors wrap that with | ||
| 217 | # the package dependency closure, so a consumer learns "you also need <dir> on <loader var>" instead of an | ||
| 218 | # opaque dlopen failure. All are cached and degrade to empty for spy engines / failures. | ||
| 219 | # | ||
| 220 | # Raw fetch entries for the package's dependency closure (package + every transitive provider). | ||
| 221 | method _closure_entries ($name) { | ||
| 222 | return $closure->{$name} if exists $closure->{$name}; | ||
| 223 | return $closure->{$name} = [] unless $r->can('fetch_closure'); | ||
| 224 | my %ambient = ( %{ $recipe_obj->defaults // {} }, %$install_opts ); | ||
| 225 | my %opts = $recipe_obj->opts_for( $name, %ambient ); | ||
| 226 | my $version = $recipe_obj->version_for($name); | ||
| 227 | my @entries; | ||
| 228 | try { @entries = $r->fetch_closure( $name, $version, %opts ) } | ||
| 229 | catch ($e) { | ||
| 230 | warn "[!] could not fetch the dependency closure for $name: $e\n" if $verbose; | ||
| 231 | @entries = (); | ||
| 232 | } | ||
| 233 | return $closure->{$name} = \@entries; | ||
| 234 | } | ||
| 235 | |||
| 236 | # The closure's library directories (search paths) and install roots (for recognising "ours"). | ||
| 237 | method _closure_dirs ($name) { | ||
| 238 | return ( [], [] ) unless $r->can('dep_search_dirs'); | ||
| 239 | return $r->dep_search_dirs( $self->_closure_entries($name) ); | ||
| 240 | } | ||
| 241 | |||
| 242 | # Classified direct runtime dependencies of the package's FFI library, e.g. | ||
| 243 | # { name => 'libfreetype.6.dylib', path => '/store/f/freetype/.../lib/...', dir => '...', scope => 'store' } | ||
| 244 | # scope is one of store / host / system / unresolved (see Alien::Xrepo::runtime_deps). Cached. | ||
| 245 | method runtime_deps ( $pkg = undef ) { | ||
| 246 | my $name = $pkg // ( $recipe_obj->packages )[0]; | ||
| 247 | die "Unknown package '$name'. Known: @{[ $recipe_obj->packages ]}" unless grep { $_ eq $name } $recipe_obj->packages; | ||
| 248 | return $rdeps->{$name} if exists $rdeps->{$name}; | ||
| 249 | return $rdeps->{$name} = [] unless $r->can('runtime_deps'); | ||
| 250 | my $info = $self->_pkg_info($name); | ||
| 251 | my $libpath = $info ? $info->libpath : undef; | ||
| 252 | return $rdeps->{$name} = [] unless defined $libpath && length $libpath && -f $libpath; | ||
| 253 | my ( $search, $store ) = $self->_closure_dirs($name); | ||
| 254 | |||
| 255 | if ( $info->libfiles && @{ $info->libfiles } ) { | ||
| 256 | unshift @$search, map { path($_)->parent->stringify } @{ $info->libfiles }; | ||
| 257 | } | ||
| 258 | unshift @$search, map { path($_)->stringify } $info->bin_dir; | ||
| 259 | unshift @$store, $info->installdir if defined $info->installdir && length $info->installdir; | ||
| 260 | my $deps = $r->runtime_deps( $libpath, search_dirs => $search, store_dirs => $store ); | ||
| 261 | return $rdeps->{$name} = ( $deps // [] ); | ||
| 262 | } | ||
| 263 | |||
| 264 | # Everything the loader would NOT find on its own (store / host / unresolved). | ||
| 265 | method foreign_runtime_deps ( $pkg = undef ) { | ||
| 266 | return grep { ( $_->{scope} // '' ) ne 'system' } @{ $self->runtime_deps($pkg) }; | ||
| 267 | } | ||
| 268 | |||
| 269 | # Directories to put on the loader search path so foreign_runtime_deps resolve. | ||
| 270 | method runtime_dep_dirs ( $pkg = undef ) { | ||
| 271 | my %seen; | ||
| 272 | return grep { defined && length && !$seen{$_}++ } map { $_->{dir} } $self->foreign_runtime_deps($pkg); | ||
| 273 | } | ||
| 274 | |||
| 275 | # The env var this platform's loader consults at dlopen time. | ||
| 276 | method loader_env_var () { | ||
| 277 | return $r->loader_env_var if $r->can('loader_env_var'); | ||
| 278 | return 'PATH' if $^O eq 'MSWin32'; | ||
| 279 | return 'DYLD_LIBRARY_PATH' if $^O eq 'darwin'; | ||
| 280 | return 'LD_LIBRARY_PATH'; | ||
| 281 | } | ||
| 282 | |||
| 283 | # A post-install summary of the extra libraries a consumer must make loadable, or '' when none. | ||
| 284 | method runtime_dep_report ( $pkg = undef ) { | ||
| 285 | my @d = $self->foreign_runtime_deps($pkg); | ||
| 286 | return '' unless @d; | ||
| 287 | my @lines = ('runtime dependencies that must be loadable before this library is dlopen\'d:'); | ||
| 288 | push @lines, sprintf( ' - %s [%s]%s', $_->{name}, $_->{scope}, defined $_->{path} ? " -> $_->{path}" : '' ) for @d; | ||
| 289 | my @dirs = $self->runtime_dep_dirs($pkg); | ||
| 290 | push @lines, ' add to ' . $self->loader_env_var . ': ' . join( ( $^O eq 'MSWin32' ? ';' : ':' ), @dirs ) if @dirs; | ||
| 291 | return join "\n", @lines; | ||
| 292 | } | ||
| 293 | |||
| 294 | # PackageInfo, with the runtime dependency diagnostic attached (unless already recorded in a snapshot, or | ||
| 295 | # disabled via `check_runtime_deps => 0`). Spy engines that cannot scan simply yield the plain object. | ||
| 296 | method package_info ( $pkg = undef ) { | ||
| 297 | my $name = $pkg // ( $recipe_obj->packages )[0]; | ||
| 298 | my $info = $self->_pkg_info($name); | ||
| 299 | return $info unless $info; | ||
| 300 | return $info if @{ $info->runtime_deps // [] }; | ||
| 301 | return $info if $self->has_snapshot; # a snapshot is authoritative (and must stay hermetic) | ||
| 302 | return $info unless $check_runtime_deps; | ||
| 303 | return $info unless defined $info->libpath && length $info->libpath; | ||
| 304 | my $deps = $self->runtime_deps($name); | ||
| 305 | return $info unless $deps && @$deps; | ||
| 306 | return $info->with_runtime_deps($deps); | ||
| 307 | } | ||
| 308 | }; | ||
| 309 | # | ||
| 310 | 1; | ||
| 311 | __END__ |