| 1 | 8 | ▶ | use v5.40; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 8 | ▶ | use feature 'class'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 8 | ▶ | no warnings 'experimental::class'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | # | ||
| 5 | class Alien::Xrepo v1.0.11 { | ||
| 6 | 0 | ▶ | use Alien::Xmake; |
|
Subroutine
BEGIN: called CC=1 SCAR=6.9
| |||
| 7 | use Alien::Xrepo::PackageInfo; | ||
| 8 | use JSON::PP; | ||
| 9 | use Digest::SHA qw[sha1_hex]; | ||
| 10 | use File::Copy qw[move]; | ||
| 11 | use File::Temp (); | ||
| 12 | use Path::Tiny; | ||
| 13 | use Config; | ||
| 14 | use Capture::Tiny qw[capture capture_merged]; | ||
| 15 | use Cwd (); | ||
| 16 | # | ||
| 17 | # Guards for the throwaway package files written for `system` installs; each File::Temp::Dir removes its | ||
| 18 | # directory on DESTROY, so they are parked here for the life of the object. | ||
| 19 | field $pkgfile_guards : param //= []; | ||
| 20 | field $verbose : param //= 0; | ||
| 21 | field $diagnosis : param //= 0; | ||
| 22 | field $root : param //= undef; | ||
| 23 | field $theme : param //= 'plain'; | ||
| 24 | |||
| 25 | # Auto-confirm interactive xrepo/xmake prompts (e.g. -y / --confirm=yes). Useful when output is captured so a | ||
| 26 | # prompting install never hangs waiting on stdin. | ||
| 27 | field $yes : param //= 0; | ||
| 28 | field $confirm : param //= (); | ||
| 29 | field $xmake = Alien::Xmake->new; | ||
| 30 | |||
| 31 | # Default package kind (-k) for every action; a per-call `kind` wins. undef omits -k so installs match a bare | ||
| 32 | # `xrepo` command, matching cd52e9b behavior. | ||
| 33 | field $kind : param //= (); | ||
| 34 | |||
| 35 | # Warm-start resolution cache. install() memorizes a successful fetch result under this instance's store so a | ||
| 36 | # repeat launch skips xrepo entirely. Entries are LRU-bounded and a hit is only trusted while its recorded install | ||
| 37 | # dir still exists. Pass cache => 0 to disable (pure fetch-first behavior, one spawn per launch). | ||
| 38 | field $cache : param //= 1; | ||
| 39 | |||
| 40 | # Resolve which package store a call should operate on. A per-call `installdir` wins, then the `root` handed to the | ||
| 41 | # constructor, then whatever xrepo/the environment chooses (global store). | ||
| 42 | method _store_dir (%opts) { return $opts{installdir} // $root } | ||
| 43 | method blah ($msg) { return unless $verbose; say $msg; } | ||
| 44 | |||
| 45 | # Echo every command we are about to run | ||
| 46 | method _debug_cmd (@cmd) { | ||
| 47 | my $str = join( ' ', map { /\s/ ? qq{"$_"} : $_ } @cmd ); | ||
| 48 | my @env; | ||
| 49 | push @env, "XMAKE_THEME=$ENV{XMAKE_THEME}" if defined $ENV{XMAKE_THEME}; | ||
| 50 | push @env, "XMAKE_PKG_INSTALLDIR=$ENV{XMAKE_PKG_INSTALLDIR}" if defined $ENV{XMAKE_PKG_INSTALLDIR}; | ||
| 51 | push @env, "XMAKE_PKG_CACHEDIR=$ENV{XMAKE_PKG_CACHEDIR}" if defined $ENV{XMAKE_PKG_CACHEDIR}; | ||
| 52 | my $env = @env ? ' [ENV ' . join( ' ', @env ) . ']' : ''; | ||
| 53 | say "[XREPO] $str$env (cwd: @{[ Cwd::getcwd() ]})\n" if $verbose; | ||
| 54 | return; | ||
| 55 | } | ||
| 56 | |||
| 57 | # Compose the full xrepo spec for a package: `<pkg> <version>` when pinned, | ||
| 58 | # the bare name otherwise. One argv element either way, so a version never | ||
| 59 | # splits on whitespace. | ||
| 60 | method _full_spec ( $pkg_spec, $version ) { | ||
| 61 | return defined $version && length $version ? "$pkg_spec $version" : $pkg_spec; | ||
| 62 | } | ||
| 63 | |||
| 64 | method install ( $pkg_spec, $version //= (), %opts ) { | ||
| 65 | my $full_spec = $self->_full_spec( $pkg_spec, $version ); | ||
| 66 | local $ENV{XMAKE_THEME} = $self->_theme(%opts); | ||
| 67 | local $ENV{XMAKE_PKG_INSTALLDIR} = $self->_store_dir(%opts) if defined $self->_store_dir(%opts); | ||
| 68 | local $ENV{XMAKE_PKG_CACHEDIR} = $opts{cachedir} if defined $opts{cachedir}; | ||
| 69 | |||
| 70 | # An install that wants system packages is driven through a generated package file instead of a bare spec, | ||
| 71 | # because xrepo's shim decides `system` for us otherwise (see _package_file). The common arguments for | ||
| 72 | # install and fetch: the mutating install gets an implicit -y unless the caller opted out; fetch is query-only, | ||
| 73 | # so no confirm. configs ride along in the package file when there is one, since the shim drops --extra there. | ||
| 74 | # The install also carries the optional -v/-D verbosity flags (see _verbosity_args); they are only added here | ||
| 75 | # because install is the one action whose output is not captured and parsed. | ||
| 76 | my $pkgfile = $self->_package_file( $full_spec, \%opts ); | ||
| 77 | my @args = ( | ||
| 78 | $self->_confirm_args( \%opts ), | ||
| 79 | $self->_build_args( defined $pkgfile ? { %opts, configs => undef } : \%opts ), | ||
| 80 | $self->_verbosity_args( \%opts ) | ||
| 81 | ); | ||
| 82 | say "[*] xrepo: ensuring $full_spec is installed..." if $verbose; | ||
| 83 | |||
| 84 | # Warm path: a prior successful install+fetch is replayed straight from disk, so a repeat launch (eg/webui.pl) | ||
| 85 | # skips xrepo entirely. The LRU is validated against the recorded files (install dir and, when recorded, the | ||
| 86 | # first lib) before an entry is trusted. | ||
| 87 | my $use_cache = defined $opts{cache} ? $opts{cache} : $cache; | ||
| 88 | my $key = $use_cache ? $self->_cache_key( $full_spec, \%opts ) : undef; | ||
| 89 | my $meta = $use_cache ? ( $self->_cache_load(%opts) || { order => [], entries => {} } ) : undef; | ||
| 90 | if ( $meta && defined $key ) { | ||
| 91 | if ( my $hit = $self->_cache_get( $key, $meta ) ) { | ||
| 92 | my $info = eval { $self->_process_info( decode_json( $hit->{json} ) ) }; | ||
| 93 | if ( ref $info ) { | ||
| 94 | $self->_guard_store( $info, %opts ); | ||
| 95 | $self->_cache_save( $meta, %opts ); | ||
| 96 | return $info; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | # Fetch-first: an already-installed package answers with real paths immediately, so the mutating install is | ||
| 102 | # skipped (and the result is memorized for next launch). | ||
| 103 | my ( $data, $err ) = $self->_try_fetch( $full_spec, \%opts ); | ||
| 104 | if ( defined $data && $self->_info_installed($data) ) { | ||
| 105 | say "[*] xrepo: $full_spec already installed, reusing fetch result..." if $verbose; | ||
| 106 | if ( $meta && defined $key ) { | ||
| 107 | $self->_cache_put( $key, $meta, $self->_cache_entry($data) ); | ||
| 108 | $self->_cache_save( $meta, %opts ); | ||
| 109 | } | ||
| 110 | return $self->_finalize( $data, %opts ); | ||
| 111 | } | ||
| 112 | |||
| 113 | # Cold path: install, then fetch (which must succeed, since it tells us where things land). | ||
| 114 | my @install_cmd = $self->_argv( 'install', \@args, defined $pkgfile ? $pkgfile : $full_spec ); | ||
| 115 | $self->_debug_cmd(@install_cmd); | ||
| 116 | $self->blah("Running: @install_cmd"); | ||
| 117 | eval { system @install_cmd }; | ||
| 118 | if ( my $spawn_err = $@ ) { | ||
| 119 | chomp $spawn_err; | ||
| 120 | die "xrepo install failed for $full_spec\n" . | ||
| 121 | " command: @install_cmd\n" . | ||
| 122 | ( $^O eq 'MSWin32' ? " win32: $^E\n" : () ) . | ||
| 123 | " error: $spawn_err\n"; | ||
| 124 | } | ||
| 125 | if ( $? != 0 ) { | ||
| 126 | die "xrepo install failed for $full_spec\n" . " command: @install_cmd\n" . " exit: " . ( $? >> 8 ) . "\n"; | ||
| 127 | } | ||
| 128 | say "[*] xrepo: fetching paths..." if $verbose; | ||
| 129 | my $fresh = $self->_require_fetch( $full_spec, \%opts, $err ); | ||
| 130 | if ( $meta && defined $key ) { | ||
| 131 | $self->_cache_put( $key, $meta, $self->_cache_entry($fresh) ); | ||
| 132 | $self->_cache_save( $meta, %opts ); | ||
| 133 | } | ||
| 134 | $self->_finalize( $fresh, %opts ); | ||
| 135 | } | ||
| 136 | |||
| 137 | # LRU cap on cached resolutions. Entries are just the fetch JSON plus its install dir, so the bound keeps the file | ||
| 138 | # a few hundred KB even after a long debugging session. | ||
| 139 | sub _CACHE_MAX () {64} | ||
| 140 | |||
| 141 | # Where the resolution cache lives: alongside the caller's/instance's store when one is set, else the xmake global | ||
| 142 | # dir (~/.xmake) that xrepo itself uses for the default per-user store. | ||
| 143 | method _cache_dir (%opts) { | ||
| 144 | my $store = $self->_store_dir(%opts); | ||
| 145 | return path($store)->child('.alien-xmake') if defined $store && length $store; | ||
| 146 | my $home = $ENV{XMAKE_GLOBALDIR}; | ||
| 147 | $home //= $ENV{HOME}; | ||
| 148 | if ( !defined $home || !length $home ) { | ||
| 149 | $home = $^O eq 'MSWin32' ? "$ENV{HOMEDRIVE}$ENV{HOMEPATH}" : (); | ||
| 150 | } | ||
| 151 | return () if !defined $home || !length $home; | ||
| 152 | path($home)->child( '.xmake', '.alien-xmake' ); | ||
| 153 | } | ||
| 154 | |||
| 155 | # Cache identity: everything that can move the installed layout. configs values run through the same boolean | ||
| 156 | # stringifier as the CLI, so built-in true/false produce a stable key. | ||
| 157 | method _cache_key ( $full_spec, $opts ) { | ||
| 158 | my @parts = ( $full_spec, $opts->{kind} // '', $opts->{plat} // '', $opts->{arch} // '', $opts->{mode} // '' ); | ||
| 159 | |||
| 160 | # The private-build path and the system path can resolve a different package layout (one lands in this store, | ||
| 161 | # the other may be satisfied from outside it), so they must not share a cache entry. | ||
| 162 | push @parts, 'system' if $opts->{system}; | ||
| 163 | if ( my $c = $opts->{configs} ) { | ||
| 164 | my $str = ref $c eq 'HASH' ? join( ',', map { "$_=" . Alien::Xmake::_bool_str( $c->{$_} ) } sort keys %$c ) : "$c"; | ||
| 165 | push @parts, $str; | ||
| 166 | } | ||
| 167 | sha1_hex( join( "\0", @parts ) ); | ||
| 168 | } | ||
| 169 | |||
| 170 | method _cache_load (%opts) { | ||
| 171 | my $dir = $self->_cache_dir(%opts) or return (); | ||
| 172 | my $file = $dir->child('cache.json'); | ||
| 173 | return () unless -f $file; | ||
| 174 | my $raw = eval { $file->slurp_utf8 }; | ||
| 175 | return () unless defined $raw && length $raw; | ||
| 176 | my $meta = eval { decode_json($raw) }; | ||
| 177 | return () unless ref $meta eq 'HASH' && ref $meta->{order} eq 'ARRAY' && ref $meta->{entries} eq 'HASH'; | ||
| 178 | $meta; | ||
| 179 | } | ||
| 180 | |||
| 181 | method _cache_save ( $meta, %opts ) { | ||
| 182 | my $dir = $self->_cache_dir(%opts) or return; | ||
| 183 | my ( @order, %keep ); | ||
| 184 | for my $k ( @{ $meta->{order} // [] } ) { | ||
| 185 | my $e = $meta->{entries}{$k} or next; | ||
| 186 | next unless $self->_entry_alive($e); | ||
| 187 | push @order, $k; | ||
| 188 | $keep{$k} = $e; | ||
| 189 | } | ||
| 190 | while ( @order > _CACHE_MAX() ) { delete $keep{ pop @order }; } | ||
| 191 | $dir->mkpath; | ||
| 192 | my $file = $dir->child('cache.json'); | ||
| 193 | my $tmp = $dir->child('cache.json.tmp'); | ||
| 194 | $tmp->remove if $tmp->exists; # clear a tmp stranded by an interrupted save | ||
| 195 | $tmp->spew_utf8( encode_json( { version => 1, order => \@order, entries => \%keep } ) ); | ||
| 196 | $file->remove if $^O eq 'MSWin32' && $file->exists; | ||
| 197 | move( "$tmp", "$file" ); | ||
| 198 | } | ||
| 199 | |||
| 200 | method _entry_alive ($e) { | ||
| 201 | return () unless ref $e eq 'HASH'; | ||
| 202 | return -f $e->{libpath} if defined $e->{libpath} && length $e->{libpath}; | ||
| 203 | defined $e->{installdir} && length $e->{installdir} && -d $e->{installdir}; | ||
| 204 | } | ||
| 205 | |||
| 206 | method _cache_get ( $key, $meta ) { | ||
| 207 | my $e = $meta->{entries}{$key} or return (); | ||
| 208 | return () unless $self->_entry_alive($e); | ||
| 209 | $e->{last_used} = time; | ||
| 210 | $meta->{order} = [ $key, grep { $_ ne $key } @{ $meta->{order} } ]; | ||
| 211 | $e; | ||
| 212 | } | ||
| 213 | |||
| 214 | method _cache_put ( $key, $meta, $entry, $max //= _CACHE_MAX() ) { | ||
| 215 | $meta->{entries}{$key} = $entry; | ||
| 216 | $meta->{order} = [ $key, grep { $_ ne $key } @{ $meta->{order} } ]; | ||
| 217 | while ( @{ $meta->{order} } > $max ) { | ||
| 218 | delete $meta->{entries}{ pop @{ $meta->{order} } }; | ||
| 219 | } | ||
| 220 | $meta; | ||
| 221 | } | ||
| 222 | |||
| 223 | method _cache_entry ($data) { | ||
| 224 | my $info = ref $data eq 'ARRAY' ? $data->[0] : $data; | ||
| 225 | my %entry = ( json => encode_json($data), last_used => time ); | ||
| 226 | if ( ref $info eq 'HASH' ) { | ||
| 227 | my $art = $info->{artifacts}; | ||
| 228 | if ( ref $art eq 'HASH' && defined $art->{installdir} ) { $entry{installdir} = $art->{installdir}; } | ||
| 229 | elsif ( defined $info->{installdir} ) { $entry{installdir} = $info->{installdir}; } | ||
| 230 | my $libs = $info->{libfiles} // []; | ||
| 231 | $libs = [$libs] unless ref $libs eq 'ARRAY'; | ||
| 232 | if (@$libs) { $entry{libpath} = $libs->[0]; } | ||
| 233 | } | ||
| 234 | \%entry; | ||
| 235 | } | ||
| 236 | |||
| 237 | method _try_fetch ( $full_spec, $opts ) { | ||
| 238 | my @fetch_args = $self->_build_args($opts); | ||
| 239 | my @fetch_cmd = $self->_argv( 'fetch', [ '--json', @fetch_args ], $full_spec ); | ||
| 240 | $self->_debug_cmd(@fetch_cmd); | ||
| 241 | $self->blah("Running: @fetch_cmd"); | ||
| 242 | my ( $out, $err, $exit ) = capture { system @fetch_cmd }; | ||
| 243 | return ( undef, "Command: @fetch_cmd\nError:\n$err" ) if $exit != 0; | ||
| 244 | return ( undef, "Command: @fetch_cmd\nNo JSON output" ) unless defined $out && length $out; | ||
| 245 | my $data = eval { $self->_decode_json_output($out) }; | ||
| 246 | return ( undef, "Command: @fetch_cmd\n$@" ) if !defined $data; | ||
| 247 | ( $data, undef ); | ||
| 248 | } | ||
| 249 | |||
| 250 | # Mandatory fetch: after an install we have to know where the output landed, so a failure here is fatal. $why | ||
| 251 | # carries the earlier probe error so the die explains which attempt failed. | ||
| 252 | method _require_fetch ( $full_spec, $opts, $why //= () ) { | ||
| 253 | my ( $data, $err ) = $self->_try_fetch( $full_spec, $opts ); | ||
| 254 | unless ( defined $data ) { | ||
| 255 | die "xrepo fetch failed:\n$err\n" . ( defined $why && length $why ? "\n(an earlier fetch probe also failed:\n$why)" : () ); | ||
| 256 | } | ||
| 257 | $data; | ||
| 258 | } | ||
| 259 | |||
| 260 | # Verify a fetched record corresponds to files that actually exist on disk; xrepo fetch of a not-yet-installed | ||
| 261 | # package can still exit 0 with an empty/speculative record. | ||
| 262 | method _info_installed ($data) { | ||
| 263 | my $info = ref $data eq 'ARRAY' ? $data->[0] : $data; | ||
| 264 | return () unless ref $info eq 'HASH'; | ||
| 265 | my $art = $info->{artifacts}; | ||
| 266 | my $installdir = ref $art eq 'HASH' && defined $art->{installdir} ? $art->{installdir} : $info->{installdir}; | ||
| 267 | return 1 if defined $installdir && -d $installdir; | ||
| 268 | my $libs = $info->{libfiles} // []; | ||
| 269 | $libs = [$libs] unless ref $libs eq 'ARRAY'; | ||
| 270 | for my $f (@$libs) { return 1 if -f $f; } | ||
| 271 | my $bins = $info->{bindirs} // []; | ||
| 272 | $bins = [$bins] unless ref $bins eq 'ARRAY'; | ||
| 273 | for my $d (@$bins) { return 1 if -d $d; } | ||
| 274 | (); | ||
| 275 | } | ||
| 276 | |||
| 277 | # A pinned store (constructor `root` or per-call `installdir`) promises a self-contained layout but xmake can | ||
| 278 | # satisfy a requirement from the system (Homebrew, apt, ...) instead of building into the store. Such a resolution | ||
| 279 | # lives OUTSIDE the pinned store, so it would silently break the promise; it is rejected here rather than returned. | ||
| 280 | # A caller that explicitly asked for system packages (see _package_file) has traded that promise away on purpose, | ||
| 281 | # so for those the guard stands down. | ||
| 282 | method _finalize ( $data, %opts ) { | ||
| 283 | return () unless defined $data; | ||
| 284 | $data = $data->[0] if ref $data eq 'ARRAY'; | ||
| 285 | $self->_guard_store( $data, %opts ); | ||
| 286 | return $self->_process_info($data); | ||
| 287 | } | ||
| 288 | |||
| 289 | method _guard_store ( $info, %opts ) { | ||
| 290 | my $store = $self->_store_dir(%opts); | ||
| 291 | return $info unless defined $store && length $store; | ||
| 292 | |||
| 293 | # `system` asks xmake to prefer the machine's own packages (Homebrew, apt, vcpkg, ...); resolving outside the | ||
| 294 | # store is the point of the request, not a violation of it, so the pinned-store promise does not apply. | ||
| 295 | return $info if $opts{system}; | ||
| 296 | return $info if $self->_in_store( $info, $store ); | ||
| 297 | my $found = $self->_anchor_of($info) // 'an unknown location'; | ||
| 298 | die "xrepo resolved a package outside the requested store (wanted '$store', found '$found'). " . | ||
| 299 | 'xmake prefers system packages (e.g. Homebrew, apt) over building, so an isolated ' . | ||
| 300 | 'install is impossible for this package. Drop the root/installdir requirement or use ' . | ||
| 301 | "a package the system does not provide.\n"; | ||
| 302 | } | ||
| 303 | |||
| 304 | # True when the record's anchor (its install root, else the store layout implied by its first libfile/include dir) | ||
| 305 | # falls under the requested store. | ||
| 306 | method _in_store ( $info, $store ) { | ||
| 307 | my $anchor = $self->_anchor_of($info); | ||
| 308 | return 1 unless defined $anchor && length $anchor; | ||
| 309 | my ( $s, $a ) = map { path($_)->absolute->stringify } ( $store, $anchor ); | ||
| 310 | ( $s, $a ) = map { ( $_ // '' ) =~ s{\\}{/}gr } ( $s, $a ); | ||
| 311 | $s = lc($s) if $^O eq 'MSWin32'; | ||
| 312 | $a = lc($a) if $^O eq 'MSWin32'; | ||
| 313 | return 1 if index( $a, $s ) == 0 && ( length($a) == length($s) || substr( $a, length($s), 1 ) =~ m{[\\/]} ); | ||
| 314 | (); | ||
| 315 | } | ||
| 316 | |||
| 317 | # Where a resolution reports its output: the installdir when present, otherwise the store layout implied by the | ||
| 318 | # first libfile (mirroring _process_info) or include dir. | ||
| 319 | method _anchor_of ($info) { | ||
| 320 | my $ref = ref $info; | ||
| 321 | return $info->installdir if $ref eq 'Alien::Xrepo::PackageInfo' && defined $info->installdir; | ||
| 322 | return $info->libpath if $ref eq 'Alien::Xrepo::PackageInfo' && defined $info->libpath; | ||
| 323 | return () unless $ref eq 'HASH'; | ||
| 324 | my $art = $info->{artifacts}; | ||
| 325 | return $art->{installdir} if ref $art eq 'HASH' && defined $art->{installdir}; | ||
| 326 | return $info->{installdir} if defined $info->{installdir}; | ||
| 327 | my $libs = $info->{libfiles} // []; | ||
| 328 | $libs = [$libs] unless ref $libs eq 'ARRAY'; | ||
| 329 | my $incs = $info->{includedirs} // []; | ||
| 330 | $incs = [$incs] unless ref $incs eq 'ARRAY'; | ||
| 331 | return path( $libs->[0] )->parent->parent->stringify if @$libs; | ||
| 332 | return path( $incs->[0] )->parent->stringify if @$incs; | ||
| 333 | (); | ||
| 334 | } | ||
| 335 | |||
| 336 | method uninstall ( $pkg_spec, %opts ) { | ||
| 337 | local $ENV{XMAKE_THEME} = $self->_theme(%opts); | ||
| 338 | local $ENV{XMAKE_PKG_INSTALLDIR} = $self->_store_dir(%opts) if defined $self->_store_dir(%opts); | ||
| 339 | local $ENV{XMAKE_PKG_CACHEDIR} = $opts{cachedir} if defined $opts{cachedir}; | ||
| 340 | my @args = ( $self->_confirm_args( \%opts ), $self->_build_args( \%opts ) ); | ||
| 341 | push @args, '--all' if $opts{all}; | ||
| 342 | push @args, '--force' if $opts{force}; | ||
| 343 | say "[*] xrepo: uninstalling $pkg_spec..." if $verbose; | ||
| 344 | my @cmd = $self->_argv( 'remove', \@args, $pkg_spec ); | ||
| 345 | system @cmd; | ||
| 346 | } | ||
| 347 | |||
| 348 | # Parse C<xrepo search> (a.k.a. C<xmake require --search>) output. The layout per matched query term: | ||
| 349 | # | ||
| 350 | # The package names: (or "The addon names:" with --addon) | ||
| 351 | # <query>: | ||
| 352 | # -> <name>[-<version>]: <description> (in <repo>) | ||
| 353 | # | ||
| 354 | # The token before the first ": " is the package identity exactly as xrepo prints it. Names can contain '-', e.g. | ||
| 355 | # serial-tools-v1.0.4 is the package serial-tools at version 1.0.4, but zlib-ng-2.3.3 could read as name 'zlib-ng' | ||
| 356 | # OR name 'zlib-ng-2.3.3' with no version - the name/version boundary is not derivable from output alone, so | ||
| 357 | # callers receive the whole token (install() accepts it as a versioned spec). '::' namespaces (vcpkg::pcre) contain | ||
| 358 | # ':' but never ': '. | ||
| 359 | # | ||
| 360 | # xrepo iterates the per-query blocks in Lua hash order (unordered), but each call runs one query, so there is | ||
| 361 | # exactly one block and the within-block order is deterministic (Levenshtein-sorted by the matched name). | ||
| 362 | # | ||
| 363 | # xrepo will accept patches to export this to json but I'm lazy | ||
| 364 | method _parse_search ($out) { | ||
| 365 | my %seen; | ||
| 366 | my @hits; | ||
| 367 | for my $line ( split /\r?\n/, $out // '' ) { | ||
| 368 | next unless $line =~ /^\s{6}->\s+(.+?): (.*)$/; | ||
| 369 | my $token = $1; | ||
| 370 | push @hits, $token unless $seen{$token}++; | ||
| 371 | } | ||
| 372 | return @hits; | ||
| 373 | } | ||
| 374 | |||
| 375 | method search ( $query, %opts ) { | ||
| 376 | local $ENV{XMAKE_THEME} = $self->_theme(%opts); | ||
| 377 | say "[*] xrepo: searching for $query..." if $verbose; | ||
| 378 | my @flags = $opts{addon} ? ('--addon') : (); | ||
| 379 | my @cmd = $self->_argv( 'search', \@flags, $query ); | ||
| 380 | $self->_debug_cmd(@cmd); | ||
| 381 | my ( $out, $err, $exit ) = capture { system @cmd }; | ||
| 382 | if ( $exit != 0 ) { | ||
| 383 | warn "Alien::Xrepo: search for '$query' failed (exit $exit): $err\n"; | ||
| 384 | return; | ||
| 385 | } | ||
| 386 | return $self->_parse_search($out); | ||
| 387 | } | ||
| 388 | |||
| 389 | method clean (%opts) { | ||
| 390 | local $ENV{XMAKE_THEME} = $self->_theme(%opts); | ||
| 391 | local $ENV{XMAKE_PKG_INSTALLDIR} = $self->_store_dir(%opts) if defined $self->_store_dir(%opts); | ||
| 392 | local $ENV{XMAKE_PKG_CACHEDIR} = $opts{cachedir} if defined $opts{cachedir}; | ||
| 393 | say '[*] xrepo: cleaning cache...' if $verbose; | ||
| 394 | my @cmd = $self->_argv( 'clean', ['-y'] ); | ||
| 395 | system @cmd; | ||
| 396 | } | ||
| 397 | |||
| 398 | method add_repo ( $name, $url, $branch //= () ) { | ||
| 399 | local $ENV{XMAKE_THEME} = $self->_theme; | ||
| 400 | say "[*] xrepo: adding repo $name..." if $verbose; | ||
| 401 | my @cmd = $self->_argv( 'add-repo', ['-y'], $name, $url, $branch ); | ||
| 402 | my ( $out, $err, $exit ) = capture { system @cmd }; | ||
| 403 | die "xrepo add-repo failed:\n$err" if $exit != 0; | ||
| 404 | 1; | ||
| 405 | } | ||
| 406 | |||
| 407 | method rm_repo ($name) { | ||
| 408 | local $ENV{XMAKE_THEME} = $self->_theme; | ||
| 409 | say "[*] xrepo: removing repo $name..." if $verbose; | ||
| 410 | my @cmd = $self->_argv( 'rm-repo', ['-y'], $name ); | ||
| 411 | system @cmd; | ||
| 412 | } | ||
| 413 | |||
| 414 | method update_repo ( $name //= () ) { | ||
| 415 | local $ENV{XMAKE_THEME} = $self->_theme; | ||
| 416 | say '[*] xrepo: updating repositories...' if $verbose; | ||
| 417 | my @cmd = $self->_argv( 'update-repo', ['-y'], $name ); | ||
| 418 | system @cmd; | ||
| 419 | } | ||
| 420 | |||
| 421 | # xrepo's install shim (xmake/modules/private/xrepo/action/install.lua) always re-execs | ||
| 422 | method _package_file ( $full_spec, $opts ) { | ||
| 423 | return () unless $opts->{system}; | ||
| 424 | my ($name) = split( /\s+/, $full_spec ); | ||
| 425 | my $dir = File::Temp->newdir( 'alien-xrepo-XXXXXXXX', TMPDIR => 1 ); | ||
| 426 | push @$pkgfile_guards, $dir; # File::Temp::Dir deletes itself on DESTROY, so hold it for the object's life | ||
| 427 | my $file = path("$dir")->child('xrepo.lua'); | ||
| 428 | |||
| 429 | # configs move out of the command line and into add_requireconfs, since with a package file the shim | ||
| 430 | # ignores --configs anyway. `kind` is not a config on the shim's path (it only feeds the dropped --extra), | ||
| 431 | # so fold the shared/static choice in explicitly. | ||
| 432 | my %configs = ref $opts->{configs} eq 'HASH' ? %{ $opts->{configs} } : (); | ||
| 433 | my $kind = defined $opts->{kind} && length $opts->{kind} ? $opts->{kind} : $kind; | ||
| 434 | $configs{shared} = $kind eq 'static' ? false : true if !exists $opts->{no_kind} && defined $kind && length $kind; | ||
| 435 | |||
| 436 | # NOTE: no `system` option at all. In xmake, `system = true` means system packages ONLY (it errors out | ||
| 437 | # rather than building from source when none is found), while leaving it unset means "prefer the system, | ||
| 438 | # fall back to building". Only the latter can restore add_extsources() without turning a source fallback | ||
| 439 | # into a hard failure, so a bare add_requires() is what we want here. | ||
| 440 | my @lines = ( 'add_requires(' . $self->_lua_str($full_spec) . ')' ); | ||
| 441 | push @lines, | ||
| 442 | 'add_requireconfs(' . | ||
| 443 | $self->_lua_str($name) . | ||
| 444 | ', {configs = {' . | ||
| 445 | join( ',', map { $self->_lua_key($_) . ' = ' . $self->_lua_value( $configs{$_} ) } sort keys %configs ) . '}})' | ||
| 446 | if %configs; | ||
| 447 | $file->spew_utf8( join( "\n", @lines ) . "\n" ); | ||
| 448 | say '[*] xrepo: system packages requested, driving install via a package file...' if $verbose; | ||
| 449 | return $file->stringify; | ||
| 450 | } | ||
| 451 | |||
| 452 | # Lua string literal for a package spec or name. | ||
| 453 | method _lua_str ($s) { | ||
| 454 | my $q = $s; | ||
| 455 | $q =~ s{(["\\])}{\\$1}g; | ||
| 456 | return qq{"$q"}; | ||
| 457 | } | ||
| 458 | |||
| 459 | # Render a configs value as Lua. Perl's v5.40 built-in true/false are refs, so compare against the same | ||
| 460 | # _bool_str the command line uses and never emit 1 / "". | ||
| 461 | method _lua_value ($v) { | ||
| 462 | |||
| 463 | # A JSON::PP::Boolean (including false, which is falsey but a ref) renders through the same | ||
| 464 | # _bool_str the command line uses, so built-in true/false never leak out as 1 / "". | ||
| 465 | return Alien::Xmake::_bool_str($v) if ref $v; | ||
| 466 | my $b = Alien::Xmake::_bool_str($v); | ||
| 467 | return 'false' if $b eq '' || $b eq 'false'; | ||
| 468 | return 'true' if $b eq 'true'; | ||
| 469 | return $b if $b =~ /\A-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?\z/; # 0/1 stay numbers, as --configs renders them | ||
| 470 | return $self->_lua_str($b); | ||
| 471 | } | ||
| 472 | method _lua_key ($k) { $k =~ /\A[A-Za-z_][A-Za-z0-9_]*\z/ ? $k : $self->_lua_str($k) } | ||
| 473 | # | ||
| 474 | method _build_args ( $opts, $extra //= [] ) { | ||
| 475 | my @args; | ||
| 476 | |||
| 477 | # Auto-confirm interactive xrepo/xmake prompts so captured installs never hang. | ||
| 478 | my $yes_c = $opts->{yes} // $yes; | ||
| 479 | my $confirm_c = $opts->{confirm} // $confirm; | ||
| 480 | push @args, '--confirm=' . $confirm_c if defined $confirm_c && length $confirm_c; | ||
| 481 | push @args, '-y' if $yes_c && !( defined $confirm_c && length $confirm_c ); | ||
| 482 | |||
| 483 | # Standard xmake/xrepo flags | ||
| 484 | push @args, '-p', $opts->{plat} if $opts->{plat}; # platform (iphoneos, android, etc) | ||
| 485 | push @args, '-a', $opts->{arch} if $opts->{arch}; # architecture (arm64, x86_64) | ||
| 486 | push @args, '-m', $opts->{mode} if $opts->{mode}; # debug/release | ||
| 487 | my $kind = defined $opts->{kind} && length $opts->{kind} ? $opts->{kind} : $kind; | ||
| 488 | push @args, '-k', $kind | ||
| 489 | if !exists $opts->{no_kind} && defined $kind && length $kind; # emit `-k` only when a consumer asks for a specific kind | ||
| 490 | push @args, '--toolchain=' . $opts->{toolchain} if $opts->{toolchain}; | ||
| 491 | push @args, '--toolchain_host=' . $opts->{toolchain_host} if $opts->{toolchain_host}; | ||
| 492 | |||
| 493 | # Visual Studio | ||
| 494 | push @args, '--vs=' . $opts->{vs} if $opts->{vs}; | ||
| 495 | push @args, '--vs_toolset=' . $opts->{vs_toolset} if $opts->{vs_toolset}; | ||
| 496 | push @args, '--vs_sdkver=' . $opts->{vs_sdkver} if $opts->{vs_sdkver}; | ||
| 497 | |||
| 498 | # Android NDK | ||
| 499 | push @args, '--ndk=' . $opts->{ndk} if $opts->{ndk}; | ||
| 500 | |||
| 501 | # Cross compilation | ||
| 502 | push @args, '--sdk=' . $opts->{sdk} if $opts->{sdk}; | ||
| 503 | push @args, '--mingw=' . $opts->{mingw} if $opts->{mingw}; | ||
| 504 | |||
| 505 | # Parallel builds / linking | ||
| 506 | push @args, '-j', $opts->{jobs} if $opts->{jobs}; | ||
| 507 | push @args, '--linkjobs=' . $opts->{linkjobs} if $opts->{linkjobs}; | ||
| 508 | |||
| 509 | # Other configuration | ||
| 510 | push @args, '--force' if $opts->{force}; | ||
| 511 | push @args, '--shallow' if $opts->{shallow}; | ||
| 512 | push @args, '--build' if $opts->{build}; | ||
| 513 | push @args, '--debugdir=' . $opts->{debugdir} if $opts->{debugdir}; | ||
| 514 | |||
| 515 | # Complex configs (passed as --configs='key=val,key2=val2') | ||
| 516 | if ( my $c = $opts->{configs} ) { | ||
| 517 | if ( ref $c eq 'HASH' ) { | ||
| 518 | my $str = join( ',', map { "$_=" . Alien::Xmake::_bool_str( $c->{$_} ) } sort keys %$c ); | ||
| 519 | push @args, "--configs=$str"; | ||
| 520 | } | ||
| 521 | else { | ||
| 522 | push @args, "--configs=$c"; | ||
| 523 | } | ||
| 524 | } | ||
| 525 | |||
| 526 | # Build Includes. xrepo splits the `--includes` value on the OS path separator and rejoins with path.joinenv | ||
| 527 | # into XMAKE_RCFILES, the contents of which are textually PREPENDED to the temp project xmake.lua. Only genuine | ||
| 528 | # root-scope rc content (add_toolchains, etc.) belongs here: a vendored *package recipe* (package() {...}) is | ||
| 529 | # invalid in that scope and dies with e.g. "unknown interface: add_rules()", so recipe overrides are not supported. | ||
| 530 | my @rc; | ||
| 531 | if ( my $i = $opts->{includes} ) { | ||
| 532 | my @incs = ref $i eq 'ARRAY' ? @$i : split( /\Q$Config{path_sep}\E/, $i ); | ||
| 533 | push @rc, map { path($_)->absolute->stringify } @incs; | ||
| 534 | push @args, '--includes=' . join( $Config{path_sep}, @rc ) if @rc; | ||
| 535 | } | ||
| 536 | |||
| 537 | # Extra per-action flags (e.g. --json, --cflags, --ldflags) | ||
| 538 | push @args, @$extra; | ||
| 539 | return @args; | ||
| 540 | } | ||
| 541 | |||
| 542 | # Theme used for xmake/xrepo output. Defaults to 'plain' (no ANSI); callers may pass `theme =>` per-call, or | ||
| 543 | # `theme =>` to the constructor. xmake reads $ENV{XMAKE_THEME}. | ||
| 544 | method _theme (%opts) { $opts{theme} // $theme } | ||
| 545 | |||
| 546 | # Assemble a full xrepo CLI argv | ||
| 547 | method _ensure_working_project () { | ||
| 548 | return unless $^O eq 'MSWin32'; | ||
| 549 | my $tmp = $ENV{TEMP}; | ||
| 550 | return unless defined $tmp && length $tmp && -d $tmp; | ||
| 551 | my ( undef, undef, undef, $mday, $mon, $year ) = localtime; | ||
| 552 | my $work = path($tmp)->child( '.xmake', sprintf( '%02d%02d%02d', $year % 100, $mon + 1, $mday ), 'xrepo', 'working' ); | ||
| 553 | $work->mkpath; | ||
| 554 | my $xmake_lua = $work->child('xmake.lua'); | ||
| 555 | if ( !$xmake_lua->exists || $xmake_lua->slurp =~ /\$\{/ ) { | ||
| 556 | |||
| 557 | # A failed create leaves a read-only raw template here; Path::Tiny's spew renames over it and dies | ||
| 558 | # Permission denied, so unlink first. | ||
| 559 | $xmake_lua->remove if $xmake_lua->exists; | ||
| 560 | $xmake_lua->spew(<<~'add_rules'); | ||
| 561 | add_rules("mode.debug", "mode.release") | ||
| 562 | target("working") | ||
| 563 | set_kind("binary") | ||
| 564 | add_files("src/*.cpp") | ||
| 565 | add_rules | ||
| 566 | my $main = $work->child( 'src', 'main.cpp' ); | ||
| 567 | $work->child('src')->mkpath unless $main->parent->is_dir; | ||
| 568 | $main->spew("int main() { return 0; }\n") unless $main->exists; | ||
| 569 | return; | ||
| 570 | } | ||
| 571 | } | ||
| 572 | |||
| 573 | method _ensure_templates_available() { | ||
| 574 | return unless $^O eq 'MSWin32'; | ||
| 575 | my $exe = eval { $xmake->exe }; | ||
| 576 | return unless defined $exe && length $exe && -e $exe; | ||
| 577 | my $src = path($exe)->parent->child('templates'); | ||
| 578 | return unless $src->is_dir; | ||
| 579 | |||
| 580 | # ExtUtils::Install ships these as 0444, and on Windows that attribute survives xmake's | ||
| 581 | # {writeable=true} copy, so `xmake create` dies "cannot open file ... Permission denied" while | ||
| 582 | # rewriting the copied xmake.lua. The installed tree under perl/lib should stay read-only, so mirror | ||
| 583 | # the built-in templates into the user's writable xmake global dir instead -- <globaldir>/templates, | ||
| 584 | # xmake's own per-user override, which it resolves before <programdir>/templates. Seeded once per | ||
| 585 | # installed xmake version; an upgrade recopies the mirror wholesale. | ||
| 586 | # | ||
| 587 | # The global dir comes from xmake itself: its `~` mapping (LOCALAPPDATA on Windows) and the | ||
| 588 | # %APPDATA% legacy fallback are not portable to replicate in Perl, but `lua -c` prints the very | ||
| 589 | # directory create will consult. Single-quoted Lua strings only: win32's naive argv quoting mangles | ||
| 590 | # embedded double quotes (which surfaces as a bogus "Can't spawn" warning and exit 255). | ||
| 591 | my $script = q{import('core.base.global'); print('XREPO_GLOBALDIR=' .. global.directory())}; | ||
| 592 | my $lines = capture { system $exe, 'lua', '-c', $script }; | ||
| 593 | my ($gldir) = $lines =~ /^XREPO_GLOBALDIR=(.+)$/m; | ||
| 594 | return unless defined $gldir; | ||
| 595 | $gldir =~ s/\s+$//; | ||
| 596 | my $dst = path($gldir)->child('templates'); | ||
| 597 | $dst->mkpath; | ||
| 598 | my $marker = $dst->child('.alien-xrepo-synced'); | ||
| 599 | my $ver = eval { $xmake->_getver } // ''; | ||
| 600 | my $synced = $marker->exists ? $marker->slurp : ''; | ||
| 601 | return if length $synced && ( !length $ver || $synced eq "$ver\n" ); | ||
| 602 | $src->visit( | ||
| 603 | sub { | ||
| 604 | my $file = shift; | ||
| 605 | return if $file->is_dir; | ||
| 606 | my $to = $dst->child( $file->relative($src) ); | ||
| 607 | $to->parent->mkpath; | ||
| 608 | |||
| 609 | # Windows copies carry the source's read-only attribute with them, so force the per-user mirror | ||
| 610 | # writable (it lives in <globaldir>/templates, xmake's own mutable per-user template override). | ||
| 611 | $file->copy($to); | ||
| 612 | $to->chmod( $to->stat->mode | 0200 ); | ||
| 613 | }, | ||
| 614 | { recurse => 1 } | ||
| 615 | ); | ||
| 616 | $marker->spew("$ver\n"); | ||
| 617 | } | ||
| 618 | |||
| 619 | method _argv ( $action, $flags, @spec ) { | ||
| 620 | $self->_ensure_working_project; | ||
| 621 | $self->_ensure_templates_available; | ||
| 622 | @spec = grep {defined} @spec; | ||
| 623 | return ( $xmake->exe, qw[lua private.xrepo], $action, @$flags, @spec ); | ||
| 624 | } | ||
| 625 | |||
| 626 | # Actions that mutate the package store auto-confirm (-y) unless the caller explicitly opts out. The constructor's | ||
| 627 | # `yes =>` (or a per-call `yes =>` / `confirm =>`) is honored by _build_args; this covers the "caller said nothing" | ||
| 628 | # case so a captured install never hangs on an interactive prompt. | ||
| 629 | method _confirm_args ($opts) { | ||
| 630 | return ('-y') if !$yes && !exists $opts->{yes} && !( defined $opts->{confirm} && length $opts->{confirm} ); | ||
| 631 | return (); | ||
| 632 | } | ||
| 633 | |||
| 634 | # xmake prints only the tail of a failed package's log and then points at the full install.txt (which lives on the | ||
| 635 | # build machine and is gone by the time a CPAN tester's report reaches you). `-v`/--verbose makes it print the whole | ||
| 636 | # log to the console and `-D`/--diagnosis adds the diagnostic detail; xrepo's install action forwards both to the | ||
| 637 | # inner `xmake require` (see xmake/modules/private/xrepo/action/install.lua), so they belong on *this* command line. | ||
| 638 | # install is the only action that runs uncaptured, so the flags are deliberately kept out of _build_args: the | ||
| 639 | # JSON-returning actions (fetch/info/scan/...) would have their parsed output polluted by the extra verbosity. | ||
| 640 | method _verbosity_args ($opts) { | ||
| 641 | my @args; | ||
| 642 | push @args, '-v' if $opts->{verbose} // $verbose; | ||
| 643 | push @args, '-D' if $opts->{diagnosis} // $diagnosis; | ||
| 644 | return @args; | ||
| 645 | } | ||
| 646 | |||
| 647 | # Run `xrepo fetch` and return a parsed PackageInfo (or raw flags). | ||
| 648 | method fetch ( $pkg_spec, $version //= (), %opts ) { | ||
| 649 | my $full_spec = $self->_full_spec( $pkg_spec, $version ); | ||
| 650 | local $ENV{XMAKE_THEME} = $self->_theme(%opts); | ||
| 651 | local $ENV{XMAKE_PKG_INSTALLDIR} = $self->_store_dir(%opts) if defined $self->_store_dir(%opts); | ||
| 652 | local $ENV{XMAKE_PKG_CACHEDIR} = $opts{cachedir} if defined $opts{cachedir}; | ||
| 653 | my @extra; | ||
| 654 | |||
| 655 | # Raw flag modes: return the string directly instead of a PackageInfo | ||
| 656 | my $flags_mode = $opts{cflags} || $opts{ldflags}; | ||
| 657 | push @extra, '--cflags' if $opts{cflags}; | ||
| 658 | push @extra, '--ldflags' if $opts{ldflags}; | ||
| 659 | push @extra, '--deps' if $opts{deps}; | ||
| 660 | push @extra, '--system' if $opts{system}; | ||
| 661 | push @extra, '-e' if $opts{external}; | ||
| 662 | my @args = ( '--json', $self->_build_args( \%opts, \@extra ) ); | ||
| 663 | my @fetch_cmd = $self->_argv( 'fetch', \@args, $full_spec ); | ||
| 664 | $self->blah("Running: @fetch_cmd"); | ||
| 665 | my ( $json_out, $json_err, $json_exit ) = capture { system @fetch_cmd }; | ||
| 666 | return if $json_exit != 0; | ||
| 667 | |||
| 668 | # cflags/ldflags mode: xrepo returns a plain flag string, not JSON | ||
| 669 | return $json_out if $flags_mode; | ||
| 670 | $self->blah("Raw fetch output:\n$json_out"); | ||
| 671 | my $data = $self->_decode_json_output($json_out); | ||
| 672 | |||
| 673 | # xrepo might return a single object or a list. | ||
| 674 | return $self->_finalize( $data, %opts ); | ||
| 675 | } | ||
| 676 | |||
| 677 | method info ( $pkg_spec, %opts ) { | ||
| 678 | local $ENV{XMAKE_THEME} = $self->_theme(%opts); | ||
| 679 | local $ENV{XMAKE_PKG_INSTALLDIR} = $self->_store_dir(%opts) if defined $self->_store_dir(%opts); | ||
| 680 | local $ENV{XMAKE_PKG_CACHEDIR} = $opts{cachedir} if defined $opts{cachedir}; | ||
| 681 | my @extra; | ||
| 682 | push @extra, '--depgraph' if $opts{depgraph}; | ||
| 683 | push @extra, '--format=' . $opts{format} if $opts{format}; | ||
| 684 | my @args = $self->_build_args( \%opts, \@extra ); | ||
| 685 | say "[*] xrepo: showing info for $pkg_spec..." if $verbose; | ||
| 686 | my @cmd = $self->_argv( 'info', \@args, $pkg_spec ); | ||
| 687 | my ( $out, $err, $exit ) = capture { system @cmd }; | ||
| 688 | return unless $exit == 0; | ||
| 689 | |||
| 690 | # If the caller asked for machine-readable output, parse and return it | ||
| 691 | if ( ( $opts{format} // '' ) eq 'json' && $out =~ /[\{\[]/ ) { | ||
| 692 | my $data; | ||
| 693 | try { $data = decode_json($out); } catch ($e) { | ||
| 694 | } | ||
| 695 | return $data if $data; | ||
| 696 | } | ||
| 697 | return $out; | ||
| 698 | } | ||
| 699 | |||
| 700 | method scan ( $pkg //= (), %opts ) { | ||
| 701 | local $ENV{XMAKE_THEME} = $self->_theme(%opts); | ||
| 702 | local $ENV{XMAKE_PKG_INSTALLDIR} = $self->_store_dir(%opts) if defined $self->_store_dir(%opts); | ||
| 703 | local $ENV{XMAKE_PKG_CACHEDIR} = $opts{cachedir} if defined $opts{cachedir}; | ||
| 704 | my @args = $self->_build_args( { %opts, no_kind => 1 } ); | ||
| 705 | say '[*] xrepo: scanning installed packages...' if $verbose; | ||
| 706 | my @cmd = $self->_argv( 'scan', \@args, ( defined $pkg && length $pkg ) ? $pkg : () ); | ||
| 707 | my ( $out, $err, $exit ) = capture { system @cmd }; | ||
| 708 | return () if $exit != 0; | ||
| 709 | $self->blah($out); | ||
| 710 | return split /\n/, $out; | ||
| 711 | } | ||
| 712 | |||
| 713 | method download ( $pkg_spec, $version //= (), %opts ) { | ||
| 714 | my $full_spec = defined $version && length $version ? "$pkg_spec $version" : $pkg_spec; | ||
| 715 | local $ENV{XMAKE_THEME} = $self->_theme(%opts); | ||
| 716 | local $ENV{XMAKE_PKG_INSTALLDIR} = $self->_store_dir(%opts) if defined $self->_store_dir(%opts); | ||
| 717 | local $ENV{XMAKE_PKG_CACHEDIR} = $opts{cachedir} if defined $opts{cachedir}; | ||
| 718 | my @extra; | ||
| 719 | push @extra, '-o', $opts{outputdir} if $opts{outputdir}; | ||
| 720 | my @args = ( $self->_confirm_args( \%opts ), $self->_build_args( \%opts, \@extra ) ); | ||
| 721 | say "[*] xrepo: downloading $full_spec..." if $verbose; | ||
| 722 | my @cmd = $self->_argv( 'download', \@args, $full_spec ); | ||
| 723 | $self->blah("Running: @cmd"); | ||
| 724 | my ( $out, $err, $exit ) = capture { system @cmd }; | ||
| 725 | return 0 if $exit != 0; | ||
| 726 | $self->blah($out); | ||
| 727 | return 1; | ||
| 728 | } | ||
| 729 | |||
| 730 | method list_repo () { | ||
| 731 | local $ENV{XMAKE_THEME} = $self->_theme; | ||
| 732 | say '[*] xrepo: listing remote repositories...' if $verbose; | ||
| 733 | my ( $out, $err, $exit ) = capture { system( $xmake->exe, qw[lua private.xrepo list-repo] ) }; | ||
| 734 | return () if $exit != 0; | ||
| 735 | $self->blah($out); | ||
| 736 | return split /\n/, $out; | ||
| 737 | } | ||
| 738 | |||
| 739 | method import_pkg ( $pkg_spec, $version //= (), %opts ) { | ||
| 740 | my $full_spec = defined $version && length $version ? "$pkg_spec $version" : $pkg_spec; | ||
| 741 | local $ENV{XMAKE_THEME} = $self->_theme(%opts); | ||
| 742 | local $ENV{XMAKE_PKG_INSTALLDIR} = $self->_store_dir(%opts) if defined $self->_store_dir(%opts); | ||
| 743 | local $ENV{XMAKE_PKG_CACHEDIR} = $opts{cachedir} if defined $opts{cachedir}; | ||
| 744 | my @extra; | ||
| 745 | push @extra, '-i', $opts{packagedir} if $opts{packagedir}; | ||
| 746 | my @args = ( $self->_confirm_args( \%opts ), $self->_build_args( \%opts, \@extra ) ); | ||
| 747 | say "[*] xrepo: importing $full_spec..." if $verbose; | ||
| 748 | my @cmd = $self->_argv( 'import', \@args, $full_spec ); | ||
| 749 | $self->blah("Running: @cmd"); | ||
| 750 | my ( $out, $err, $exit ) = capture { system @cmd }; | ||
| 751 | return 0 if $exit != 0; | ||
| 752 | $self->blah($out); | ||
| 753 | return 1; | ||
| 754 | } | ||
| 755 | |||
| 756 | method export ( $pkg_spec, $version //= (), %opts ) { | ||
| 757 | my $full_spec = defined $version && length $version ? "$pkg_spec $version" : $pkg_spec; | ||
| 758 | local $ENV{XMAKE_THEME} = $self->_theme(%opts); | ||
| 759 | local $ENV{XMAKE_PKG_INSTALLDIR} = $self->_store_dir(%opts) if defined $self->_store_dir(%opts); | ||
| 760 | local $ENV{XMAKE_PKG_CACHEDIR} = $opts{cachedir} if defined $opts{cachedir}; | ||
| 761 | my @extra; | ||
| 762 | push @extra, '-o', $opts{packagedir} if $opts{packagedir}; | ||
| 763 | my @args = ( $self->_confirm_args( \%opts ), $self->_build_args( \%opts, \@extra ) ); | ||
| 764 | say "[*] xrepo: exporting $full_spec..." if $verbose; | ||
| 765 | my @cmd = $self->_argv( 'export', \@args, $full_spec ); | ||
| 766 | $self->blah("Running: @cmd"); | ||
| 767 | my ( $out, $err, $exit ) = capture { system @cmd }; | ||
| 768 | return 0 if $exit != 0; | ||
| 769 | $self->blah($out); | ||
| 770 | return 1; | ||
| 771 | } | ||
| 772 | |||
| 773 | method env ( $program //= (), %opts ) { | ||
| 774 | local $ENV{XMAKE_THEME} = $self->_theme(%opts); | ||
| 775 | local $ENV{XMAKE_PKG_INSTALLDIR} = $self->_store_dir(%opts) if defined $self->_store_dir(%opts); | ||
| 776 | local $ENV{XMAKE_PKG_CACHEDIR} = $opts{cachedir} if defined $opts{cachedir}; | ||
| 777 | my @extra; | ||
| 778 | push @extra, '--show' if $opts{show}; | ||
| 779 | push @extra, '--add', $opts{add} if $opts{add}; | ||
| 780 | push @extra, '--remove', $opts{remove} if $opts{remove}; | ||
| 781 | push @extra, '-l' if $opts{list}; | ||
| 782 | push @extra, '-b', $opts{bind} if $opts{bind}; | ||
| 783 | my @args = $self->_build_args( { %opts, no_kind => 1 }, \@extra ); | ||
| 784 | my @cmd = $self->_argv( 'env', \@args, ( defined $program && length $program ) ? $program : (), @{ $opts{arguments} // [] } ); | ||
| 785 | say "[*] xrepo: running in package environment..." if $verbose; | ||
| 786 | system @cmd; | ||
| 787 | } | ||
| 788 | |||
| 789 | # Decode xrepo `--json` output. xrepo can prefix the JSON with non-JSON "checking for X" chatter (e.g. the | ||
| 790 | # MSVC/compiler check on first install on Windows), so we skip leading prose lines and decode starting at the first | ||
| 791 | # JSON value line instead of demanding the whole capture be pure JSON. | ||
| 792 | method _decode_json_output ($raw) { | ||
| 793 | die "xrepo produced no JSON: got empty output" unless defined $raw && length $raw; | ||
| 794 | my ( $rest, $found ); | ||
| 795 | for my $line ( split /\r?\n/, $raw ) { | ||
| 796 | if ( $line =~ /^\s*[\[{"]/ ) { $found = 1; } | ||
| 797 | next unless $found; | ||
| 798 | $rest .= $line . "\n"; | ||
| 799 | } | ||
| 800 | die "xrepo produced no JSON; the probe/check chatter did not yield any JSON value.\nOutput was:\n$raw" unless $found; | ||
| 801 | my $decoded = eval { decode_json($rest) }; | ||
| 802 | die "Failed to decode xrepo JSON output: $@\nOutput was:\n$raw" if !defined $decoded; | ||
| 803 | return $decoded; | ||
| 804 | } | ||
| 805 | |||
| 806 | # A shared library's own `libpath` is only half the story for an FFI consumer: the dynamic linker resolves | ||
| 807 | # its DT_NEEDED/import-table/LC_LOAD_DYLIB entries *before* dlopen returns, so any transitive library that | ||
| 808 | # is not on the loader's default search path (a vendored freetype behind libsdl3_ttf, say) has to be made | ||
| 809 | # findable first. xmake already knows how to read those tables (a native C reader in core.base.binutils, | ||
| 810 | # with dumpbin/ldd/otool fallbacks) and how to resolve @rpath/@loader_path/$ORIGIN/absolute names against a | ||
| 811 | # set of search paths. These methods expose that as a post-install diagnostic. | ||
| 812 | # | ||
| 813 | # The environment variable this platform's loader consults last (Windows) or at all (ELF/Mach-O). | ||
| 814 | method loader_env_var () { | ||
| 815 | return 'PATH' if $^O eq 'MSWin32'; | ||
| 816 | return 'DYLD_LIBRARY_PATH' if $^O eq 'darwin'; | ||
| 817 | return 'LD_LIBRARY_PATH'; | ||
| 818 | } | ||
| 819 | |||
| 820 | # `xrepo fetch --json --deps` emits one entry per package in the dependency closure (the package plus | ||
| 821 | # everything it transitively needs), each with its own libfiles/linkdirs/bindirs/installdir. The plain | ||
| 822 | # fetch collapses to the primary package, so this is what lets a caller find where a transitive provider | ||
| 823 | # (freetype behind libsdl3_ttf) lives. | ||
| 824 | method fetch_closure ( $pkg_spec, $version //= (), %opts ) { | ||
| 825 | my $full_spec = $self->_full_spec( $pkg_spec, $version ); | ||
| 826 | local $ENV{XMAKE_THEME} = $self->_theme(%opts); | ||
| 827 | local $ENV{XMAKE_PKG_INSTALLDIR} = $self->_store_dir(%opts) if defined $self->_store_dir(%opts); | ||
| 828 | local $ENV{XMAKE_PKG_CACHEDIR} = $opts{cachedir} if defined $opts{cachedir}; | ||
| 829 | my @args = ( '--json', $self->_build_args( \%opts, ['--deps'] ) ); | ||
| 830 | my @cmd = $self->_argv( 'fetch', \@args, $full_spec ); | ||
| 831 | $self->_debug_cmd(@cmd); | ||
| 832 | $self->blah("Running: @cmd"); | ||
| 833 | my ( $out, $err, $exit ) = capture { system @cmd }; | ||
| 834 | return () if $exit != 0; | ||
| 835 | my $data = eval { $self->_decode_json_output($out) }; | ||
| 836 | return () unless ref $data eq 'ARRAY'; | ||
| 837 | return grep { ref $_ eq 'HASH' } @$data; | ||
| 838 | } | ||
| 839 | |||
| 840 | # Ask xmake to list the direct runtime dependencies of $libpath, resolving each to an absolute path where | ||
| 841 | # possible using the supplied search directories. Returns the raw entries: an absolute path when resolved, | ||
| 842 | # otherwise the bare loader name (e.g. `KERNEL32.dll`). | ||
| 843 | method scan_runtime_deps ( $libpath, @search_dirs ) { | ||
| 844 | return () unless defined $libpath && length $libpath && -f $libpath; | ||
| 845 | my $script = File::Temp->new( SUFFIX => '.lua' ); | ||
| 846 | print {$script} <<~'LUA'; | ||
| 847 | import("core.base.json") | ||
| 848 | import("utils.binary.deplibs") | ||
| 849 | function main(lib, ...) | ||
| 850 | local dirs = {...} | ||
| 851 | local deps = deplibs(lib, {resolve_path = true, resolve_search_paths = dirs}) or {} | ||
| 852 | io.write(json.encode(deps or {}), "\n") | ||
| 853 | end | ||
| 854 | LUA | ||
| 855 | close $script; | ||
| 856 | local $ENV{XMAKE_THEME} = $theme; | ||
| 857 | my @cmd = ( $xmake->exe, 'l', '-q', $script->filename, $libpath, grep {defined} @search_dirs ); | ||
| 858 | $self->_debug_cmd(@cmd); | ||
| 859 | my ( $out, $err, $exit ) = capture { system @cmd }; | ||
| 860 | return () if $exit != 0; | ||
| 861 | my $deps = eval { $self->_decode_json_output($out) }; | ||
| 862 | return () unless ref $deps eq 'ARRAY'; | ||
| 863 | return @$deps; | ||
| 864 | } | ||
| 865 | |||
| 866 | # Scan $libpath and classify every direct dependency by where it lives: | ||
| 867 | # store - resolved into one of the provided store/closure directories (the consumer must make it findable) | ||
| 868 | # host - resolved somewhere else on the machine that is not a default system dir (Homebrew, /usr/local) | ||
| 869 | # system - a default OS library directory, or a loader-name that is plainly part of the C runtime | ||
| 870 | # unresolved - a bare name that matched no search dir and no system pattern (the genuinely missing case) | ||
| 871 | # Options: search_dirs => [...], store_dirs => [...], system_dirs => [...] (defaults to this OS's dirs). | ||
| 872 | method runtime_deps ( $libpath, %opts ) { | ||
| 873 | my @raw = $self->scan_runtime_deps( $libpath, @{ $opts{search_dirs} // [] } ); | ||
| 874 | return [] unless @raw; | ||
| 875 | my @store = @{ $opts{store_dirs} // [] }; | ||
| 876 | my @system = @{ $opts{system_dirs} // [ $self->_system_lib_dirs ] }; | ||
| 877 | my @out; | ||
| 878 | for my $dep (@raw) { | ||
| 879 | next unless defined $dep && length $dep; | ||
| 880 | my %r = ( raw => $dep ); | ||
| 881 | if ( path($dep)->is_absolute ) { | ||
| 882 | my $p = path($dep); | ||
| 883 | $r{path} = $p->stringify; | ||
| 884 | $r{name} = $p->basename; | ||
| 885 | $r{dir} = $p->parent->stringify; | ||
| 886 | $r{scope} = $self->_path_under( $r{path}, @store ) ? 'store' : $self->_path_under( $r{path}, @system ) ? 'system' : 'host'; | ||
| 887 | } | ||
| 888 | else { | ||
| 889 | $r{name} = $dep; | ||
| 890 | $r{scope} = $self->_looks_system_lib($dep) ? 'system' : 'unresolved'; | ||
| 891 | } | ||
| 892 | push @out, \%r; | ||
| 893 | } | ||
| 894 | return \@out; | ||
| 895 | } | ||
| 896 | |||
| 897 | # Directories to offer the loader when resolving a library's dependency table, plus the install roots used to | ||
| 898 | # recognise a resolved path as "ours". Input is a list of fetch/closure entries (each with an optional | ||
| 899 | # artifacts.installdir and libfiles / linkdirs / bindirs). Returns (\@search_dirs, \@store_dirs). | ||
| 900 | method dep_search_dirs ($entries) { | ||
| 901 | my ( @search, @store, %seen ); | ||
| 902 | my $add = sub { my $d = shift; return unless defined $d && length $d; push @search, $d unless $seen{$d}++ }; | ||
| 903 | for my $e ( grep { ref $_ eq 'HASH' } @{ $entries // [] } ) { | ||
| 904 | my $id = $e->{artifacts}{installdir}; | ||
| 905 | if ( defined $id && length $id ) { | ||
| 906 | push @store, $id; | ||
| 907 | $add->( path($id)->child($_)->stringify ) for qw[bin lib]; | ||
| 908 | } | ||
| 909 | for my $key (qw[libfiles linkdirs bindirs]) { | ||
| 910 | my $v = $e->{$key} // []; | ||
| 911 | $v = [$v] unless ref $v eq 'ARRAY'; | ||
| 912 | for my $p (@$v) { | ||
| 913 | next unless defined $p && length $p; | ||
| 914 | $add->( $key eq 'libfiles' ? path($p)->parent->stringify : $p ); | ||
| 915 | } | ||
| 916 | } | ||
| 917 | } | ||
| 918 | return ( \@search, \@store ); | ||
| 919 | } | ||
| 920 | |||
| 921 | # True when $child is $dir itself or sits underneath it (separator-bounded, case-insensitive on Windows). | ||
| 922 | method _path_under ( $child, @dirs ) { | ||
| 923 | my $c = path($child)->absolute->stringify; | ||
| 924 | ($c) = ( $c =~ s{\\}{/}gr ); | ||
| 925 | $c = lc $c if $^O eq 'MSWin32'; | ||
| 926 | for my $dir (@dirs) { | ||
| 927 | next unless defined $dir && length $dir; | ||
| 928 | my $d = path($dir)->absolute->stringify; | ||
| 929 | ($d) = ( $d =~ s{\\}{/}gr ); | ||
| 930 | $d = lc $d if $^O eq 'MSWin32'; | ||
| 931 | return 1 if index( $c, $d ) == 0 && ( length($c) == length($d) || substr( $c, length($d), 1 ) eq '/' ); | ||
| 932 | } | ||
| 933 | return 0; | ||
| 934 | } | ||
| 935 | |||
| 936 | # Default OS locations whose libraries the loader already finds without help. | ||
| 937 | method _system_lib_dirs () { | ||
| 938 | if ( $^O eq 'MSWin32' ) { | ||
| 939 | my $root = $ENV{SystemRoot} // 'C:\\Windows'; | ||
| 940 | return grep {-d} map { path($root)->child($_)->stringify } qw[System32 SysWOW64]; | ||
| 941 | } | ||
| 942 | if ( $^O eq 'darwin' ) { | ||
| 943 | return grep {-d} qw[/usr/lib /usr/lib/system /System/Library/Frameworks /System/Library/PrivateFrameworks /Library/Frameworks]; | ||
| 944 | } | ||
| 945 | my @dirs = qw[/lib /lib64 /usr/lib /usr/lib64 /usr/local/lib /usr/local/lib64]; | ||
| 946 | push @dirs, grep {-d} glob('/lib/*-linux-gnu'), glob('/usr/lib/*-linux-gnu'); | ||
| 947 | return grep {-d} @dirs; | ||
| 948 | } | ||
| 949 | |||
| 950 | # A bare loader name the OS itself provides: the Windows API sets/CRT, Mach-O system install names, and the | ||
| 951 | # usual ELF runtime sonames. Anything else that stayed unresolved is worth reporting. | ||
| 952 | method _looks_system_lib ($name) { | ||
| 953 | if ( $^O eq 'MSWin32' ) { | ||
| 954 | return 1 if $name =~ /\A(?:api-ms-win-|ext-ms-)/i; | ||
| 955 | my $sys | ||
| 956 | = qr/\A(?:ucrtbase|vcruntime\d*|msvcp\d*|msvcr\d*|kernel32|kernelbase|user32|gdi32|gdi32full|advapi32|shell32|ole32|oleaut32|ws2_32|ntdll|combase|bcrypt|bcryptprimitives|cryptbase|userenv|imm32|setupapi|version|winmm|shlwapi|rpcrt4|sechost|cfgmgr32|dwmapi|dcomp|d3d\d*|dxgi|opengl32|glu32|uxtheme|usp10|wtsapi32|mswsock|dnsapi|iphlpapi|winhttp|crypt32|ncrypt|propsys|dwrite|d2d1|comdlg32|winspool|wininet|urlmon|psapi|powrprof|profapi|mpr|netapi32|credui|sspicli|secur32|msvcrt|msimg32|comctl32|windowscodecs|avrt|hid|dbghelp|win32u|shcore|textinputframework|twinapi|uiautomationcore)\d*\.dll\z/i; | ||
| 957 | return 1 if $name =~ $sys; | ||
| 958 | return 0; | ||
| 959 | } | ||
| 960 | if ( $^O eq 'darwin' ) { | ||
| 961 | return 1 if $name =~ m{\A/usr/lib/|/System/Library/|/Library/}; | ||
| 962 | return 1 if $name =~ /\Alib(?:System|c\+\+|objc|resolv|iconv)\b/; | ||
| 963 | } | ||
| 964 | return 1 if $name =~ /\Alib(?:c|m|dl|pthread|rt|gcc_s|stdc\+\+|gomp|atomic|resolv|nsl|util|z|crypt|quadmath)\b/; | ||
| 965 | return 1 if $name =~ /\Ald-linux/; | ||
| 966 | return 0; | ||
| 967 | } | ||
| 968 | |||
| 969 | # Recover the library artifacts xrepo failed to list. A port that ships only static archives (a vcpkg zlib, say) | ||
| 970 | # can come back with an install tree but an empty `libfiles`, which used to drop the package into the header-only | ||
| 971 | # branch below and hand consumers an undef libpath for a package that plainly ships a library. Scans the linkdirs | ||
| 972 | # xrepo did report, then the conventional lib/lib64 under the install root, and returns ( libfiles, linkdirs ) so a | ||
| 973 | # directory that actually yielded something joins linkdirs and the -L flags agree with libfiles. Both come back | ||
| 974 | # untouched for a genuinely header-only package. | ||
| 975 | method _scan_libfiles ( $installdir, $linkdirs ) { | ||
| 976 | my $ext = $^O eq 'MSWin32' ? qr/\.(?:dll|lib)\z/i : qr/\.(?:dylib|a|so(?:\.[\d.]+)?)\z/i; | ||
| 977 | my @dirs = @$linkdirs; | ||
| 978 | push @dirs, map { path($installdir)->child($_)->stringify } qw[lib lib64] if defined $installdir && length $installdir; | ||
| 979 | my ( @libfiles, %found_in ); | ||
| 980 | for my $dir (@dirs) { | ||
| 981 | next unless -d $dir; | ||
| 982 | my @found = map { $_->stringify } grep { $_->is_file && $_->basename =~ $ext } path($dir)->children; | ||
| 983 | next unless @found; | ||
| 984 | push @libfiles, @found; | ||
| 985 | $found_in{$dir} = 1; | ||
| 986 | } | ||
| 987 | return ( [], $linkdirs ) if !@libfiles; | ||
| 988 | my @all = @$linkdirs; | ||
| 989 | for my $dir (@dirs) { | ||
| 990 | push @all, $dir if $found_in{$dir} && !grep { $_ eq $dir } @all; | ||
| 991 | } | ||
| 992 | return ( \@libfiles, \@all ); | ||
| 993 | } | ||
| 994 | |||
| 995 | method _process_info ($info) { | ||
| 996 | $info = $info->[0] if ref $info eq 'ARRAY'; | ||
| 997 | return () unless ref $info eq 'HASH'; | ||
| 998 | my $libfiles = $info->{libfiles} // []; | ||
| 999 | $libfiles = [$libfiles] unless ref $libfiles eq 'ARRAY'; | ||
| 1000 | my $incdirs = $info->{includedirs} // []; | ||
| 1001 | $incdirs = [$incdirs] unless ref $incdirs eq 'ARRAY'; | ||
| 1002 | my $linkdirs = $info->{linkdirs} // []; | ||
| 1003 | $linkdirs = [$linkdirs] unless ref $linkdirs eq 'ARRAY'; | ||
| 1004 | my $bindirs = $info->{bindirs} // []; | ||
| 1005 | $bindirs = [$bindirs] unless ref $bindirs eq 'ARRAY'; | ||
| 1006 | my $links = $info->{links} // []; | ||
| 1007 | $links = [$links] unless ref $links eq 'ARRAY'; | ||
| 1008 | my $installdir = $info->{artifacts}{installdir}; | ||
| 1009 | my $kind = $info->{kind}; | ||
| 1010 | |||
| 1011 | if ( !defined $installdir ) { | ||
| 1012 | my ( $probe, $depth ); | ||
| 1013 | if (@$libfiles) { ( $probe, $depth ) = ( $libfiles->[0], 2 ); } | ||
| 1014 | elsif (@$incdirs) { ( $probe, $depth ) = ( $incdirs->[0], 1 ); } | ||
| 1015 | if ( defined $probe ) { | ||
| 1016 | my $p = path($probe); | ||
| 1017 | $p = $p->parent for 1 .. $depth; | ||
| 1018 | $installdir = $p->stringify; | ||
| 1019 | } | ||
| 1020 | } | ||
| 1021 | unless (@$bindirs) { | ||
| 1022 | $bindirs = [ path( $installdir, 'bin' )->stringify ] if $installdir && -d path( $installdir, 'bin' ); | ||
| 1023 | } | ||
| 1024 | |||
| 1025 | # An install tree that plainly holds libraries but was reported without any is not header-only; fill the list | ||
| 1026 | # in before `kind` is inferred so the package is still classified as a library below. | ||
| 1027 | ( $libfiles, $linkdirs ) = $self->_scan_libfiles( $installdir, $linkdirs ) if !@$libfiles; | ||
| 1028 | |||
| 1029 | # A package that ships neither libraries nor headers but has an install root is a binary tool (ninja, cmake, | ||
| 1030 | # node, ...); anything else is a library (or header-only). xrepo only reports `kind` for the former. | ||
| 1031 | $kind = ( defined $installdir && !@$libfiles && !@$incdirs ) ? 'binary' : 'library' if !defined $kind || !length $kind; | ||
| 1032 | |||
| 1033 | # Validate that we actually got files back | ||
| 1034 | unless (@$libfiles) { | ||
| 1035 | $self->blah('[!] xrepo returned no library files. Package might be header-only.'); | ||
| 1036 | |||
| 1037 | # Return a generic object (likely header-only) | ||
| 1038 | return Alien::Xrepo::PackageInfo->new( | ||
| 1039 | includedirs => $incdirs, | ||
| 1040 | libfiles => [], | ||
| 1041 | libpath => undef, | ||
| 1042 | linkdirs => $linkdirs, | ||
| 1043 | links => $links, | ||
| 1044 | license => $info->{license} // undef, | ||
| 1045 | shared => $info->{shared} // 0, | ||
| 1046 | static => $info->{static} // 0, | ||
| 1047 | version => $info->{version} // undef, | ||
| 1048 | bindirs => $bindirs, | ||
| 1049 | deps => $info->{deps} // [], | ||
| 1050 | runtime_deps => $info->{runtime_deps} // [], | ||
| 1051 | installdir => $installdir, | ||
| 1052 | kind => $kind | ||
| 1053 | ); | ||
| 1054 | } | ||
| 1055 | |||
| 1056 | # Heuristic to find the Runtime Library (DLL/SO/DyLib) for FFI | ||
| 1057 | my $runtime_lib; | ||
| 1058 | if ( $^O eq 'MSWin32' ) { | ||
| 1059 | |||
| 1060 | # Check if the lib is already in libfiles (MinGW often does this). A package can ship several libs | ||
| 1061 | # (SDL3_image plus its optional codec DLLs, for one), and the scan order is the order the filesystem hands | ||
| 1062 | # them back - case-insensitive on NTFS, so libavif-16.dll comes before SDL3_image.dll. Prefer the DLL named | ||
| 1063 | # by the package's own `links` instead of taking whatever sorted first. | ||
| 1064 | my @dlls = grep {/\.dll$/i} @$libfiles; | ||
| 1065 | if (@dlls) { | ||
| 1066 | my $norm = sub { | ||
| 1067 | my $n = lc $_[0]; | ||
| 1068 | $n =~ s/^lib//; | ||
| 1069 | $n; | ||
| 1070 | }; | ||
| 1071 | my %want = map { $norm->($_) => 1 } grep { defined && length } @$links; | ||
| 1072 | ($runtime_lib) = grep { $want{ $norm->( path($_)->basename(qr/\.dll$/i) ) } } @dlls; | ||
| 1073 | $runtime_lib //= $dlls[0]; | ||
| 1074 | } | ||
| 1075 | |||
| 1076 | # If not, we must hunt for it in the 'bin' directory sibling to the 'lib' directory. | ||
| 1077 | unless ($runtime_lib) { | ||
| 1078 | my ($imp_lib) = grep {/\.lib$/i} @$libfiles; | ||
| 1079 | if ($imp_lib) { | ||
| 1080 | my $lib_path = path($imp_lib); | ||
| 1081 | my $basename = $lib_path->basename(qr/\.lib$/i); # e.g., 'zlib' from 'zlib.lib' | ||
| 1082 | |||
| 1083 | # Construct list of potential directories to search | ||
| 1084 | my @search_dirs = @$bindirs; | ||
| 1085 | |||
| 1086 | # Add standard relative paths: /path/to/lib/../bin | ||
| 1087 | push @search_dirs, $lib_path->parent->parent->child('bin'); | ||
| 1088 | push @search_dirs, $lib_path->parent->sibling('bin'); # Some layouts differ | ||
| 1089 | |||
| 1090 | # Search for the DLL | ||
| 1091 | for my $dir (@search_dirs) { | ||
| 1092 | next unless -d $dir; | ||
| 1093 | my $d = path($dir); | ||
| 1094 | |||
| 1095 | # Exact match: zlib.lib -> zlib.dll | ||
| 1096 | my $try = $d->child("$basename.dll"); | ||
| 1097 | if ( $try->exists ) { $runtime_lib = $try->stringify; last; } | ||
| 1098 | |||
| 1099 | # MSVC vs MinGW naming: libpng.lib -> libpng16.dll or png.dll | ||
| 1100 | # Scan directory for anything starting with the basename | ||
| 1101 | my ($fuzzy) = grep { /^$basename/i && /\.dll$/i } map { $_->basename } $d->children; | ||
| 1102 | if ($fuzzy) { $runtime_lib = $d->child($fuzzy)->stringify; last; } | ||
| 1103 | } | ||
| 1104 | } | ||
| 1105 | } | ||
| 1106 | } | ||
| 1107 | elsif ( $^O eq 'darwin' ) { | ||
| 1108 | |||
| 1109 | # macOS: Prefer .dylib, then .so | ||
| 1110 | ($runtime_lib) = grep {/\.dylib$/i} @$libfiles; | ||
| 1111 | ($runtime_lib) //= grep {/\.so$/i} @$libfiles; | ||
| 1112 | } | ||
| 1113 | else { | ||
| 1114 | # Linux/BSD: Prefer .so, .so.x.y, .so.x | ||
| 1115 | ($runtime_lib) = grep {/\.so(\.|-|\d|$)/} @$libfiles; | ||
| 1116 | } | ||
| 1117 | |||
| 1118 | # Fallback and Logging | ||
| 1119 | unless ($runtime_lib) { | ||
| 1120 | |||
| 1121 | # If we asked for shared but couldn't find a runtime binary, log a warning. We fall back to the first file | ||
| 1122 | # (likely a static .a/.lib) so that XS builds might still work, even if Affix or FFI::Platypus will fail. | ||
| 1123 | if ( $info->{shared} // 0 ) { | ||
| 1124 | $self->blah('[!] Warning: Package is marked "shared" but no Runtime Binary (dll/so/dylib) was detected.'); | ||
| 1125 | $self->blah( '[!] Libfiles returned: ' . join( ', ', @$libfiles ) ); | ||
| 1126 | } | ||
| 1127 | $runtime_lib = $libfiles->[0]; | ||
| 1128 | } | ||
| 1129 | $self->blah( '[*] Identified runtime library: ' . $runtime_lib ) if $runtime_lib; | ||
| 1130 | return Alien::Xrepo::PackageInfo->new( | ||
| 1131 | includedirs => $incdirs, | ||
| 1132 | libfiles => $libfiles, | ||
| 1133 | libpath => $runtime_lib, | ||
| 1134 | linkdirs => $linkdirs, | ||
| 1135 | links => $links, | ||
| 1136 | license => $info->{license} // undef, | ||
| 1137 | shared => $info->{shared} // 0, | ||
| 1138 | static => $info->{static} // 0, | ||
| 1139 | version => $info->{version} // undef, | ||
| 1140 | bindirs => $bindirs, | ||
| 1141 | deps => $info->{deps} // [], | ||
| 1142 | runtime_deps => $info->{runtime_deps} // [], | ||
| 1143 | installdir => $installdir, | ||
| 1144 | kind => $kind | ||
| 1145 | ); | ||
| 1146 | } | ||
| 1147 | }; | ||
| 1148 | # | ||
| 1149 | 1; | ||