view release on metacpan or search on metacpan
my $repo = Alien::Xrepo->new();
# my $repo = Alien::Xrepo->new( cache => 0 ); # live resolution, skip the warmed cache
# Add a custom repository (optional)
# $repo->add_repo( 'my-repo', 'https://github.com/my/repo.git' );
# Install a shared lib with automatic configuration
my $ogg = $repo->install('libvorbis');
# Install a library with specific configuration
# equivalent to: xrepo install -p windows -a x86_64 -m debug --configs='shared=true,vs_runtime=MD' libpng
my $pkg = $repo->install(
'libpng', '1.6.x',
plat => 'windows',
arch => 'x64',
mode => 'debug',
configs => { vs_runtime => 'MD' }
);
die 'Install failed' unless $pkg;
# Or install a binary tool and run it (ninja, cmake, python, node, ...)
my $ninja = $repo->install('ninja');
my ($ninja_exe) = map { path($_)->child($^O eq 'MSWin32' ? 'ninja.exe' : 'ninja') } $ninja->bin_dir;
system $ninja_exe, '--version';
# Wrap a single function from sqlite3 with Affix
- `plat`
Target platform (e.g., `windows`, `linux`, `macosx`, `android`, `iphoneos`, `wasm`).
- `arch`
Target architecture (e.g., `x86_64`, `arm64`, `riscv64`).
- `mode`
Build mode: `debug` or `release`.
- `kind`
Library kind: `shared` or `static`. If omitted entirely, the package's own default applies, so a bare install behaves
exactly like `xrepo install`.
_Note: For FFI, you almost always want `shared`, but `static` is available if you are linking archives with, say, an
XS module._
- `toolchain`
_install/download_: force reinstall/redownload all packages. _remove_: force removal even when still depended on.
- `shallow`
Do not install/download dependent packages.
- `build`
Always build and install from source.
- `debugdir`
Source directory used for debugging; enables `force` and `shallow` by default.
- `configs( ... )`
A hashref or string of package-specific configurations.
```perl
configs => { openssl => 'true', shared => 'true' }
# becomes --configs='openssl=true,shared=true'
```
```perl
use v5.40;
use Alien::Xrepo;
my $repo = Alien::Xrepo->new;
$repo->install(
'libpng', '1.6.x',
plat => 'windows',
arch => 'x64',
mode => 'debug',
kind => 'shared'
);
my $cflags = $repo->fetch( 'libpng', undef, cflags => 1 ); # "-Ic:/.../include ..."
my $ldflags = $repo->fetch( 'libpng', undef, ldflags => 1 ); # "-Lc:/.../lib -l... .."
say "CFLAGS: $cflags";
say "LDFLAGS: $ldflags";
```
### Remove a package from the cache
eg/alien_xrepo.pl view on Meta::CPAN
say for $repo->scan('libpng');
# Fetch info for an already-installed package without installing again
# my $pkg = $repo->fetch('zlib');
# Add a custom repository (optional)
# $repo->add_repo( 'my-repo', 'https://github.com/my/repo.git' );
# Install a shared lib with an automatic configuration
my $ogg = $repo->install( 'libvorbis', undef, kind => 'shared' );
# Install a library with specific configuration
# equivalent to: xrepo install -p windows -a x86_64 -m debug --configs='shared=true,vs_runtime=MD' libpng
my $pkg = $repo->install( 'libpng', '1.6.x', kind => 'shared', plat => 'windows', arch => 'x64', mode => 'debug', configs => { vs_runtime => 'MD' } );
die 'Install failed' unless $pkg;
{ # Bind a single zlib function with Affix (fast; Affix::Wrap can block for a very long time parsing zlib.h on some platforms)
use Affix;
my $zlib = $repo->install( 'zlib', undef, kind => 'shared' );
affix $zlib->libpath, 'zlibVersion', [], String;
say 'zlib version: ' . zlibVersion();
}
{ # Wrap a single function from sqlite3 with Affix
use Affix;
my $sqlite3 = $repo->install( 'sqlite3', undef, kind => 'shared' );
eg/examples/Exotic-SDL3/recipes/packages/l/libsdl3_ttf/xmake.lua view on Meta::CPAN
if package:config("harfbuzz") then
package:add("deps", "harfbuzz")
end
if package:config("plutosvg") then
package:add("deps", "plutosvg", "plutovg")
end
end)
on_install(function (package)
local configs = {"-DSDLTTF_SAMPLES=OFF", "-DSDLTTF_VENDORED=OFF"}
table.insert(configs, "-DCMAKE_BUILD_TYPE=" .. (package:is_debug() and "Debug" or "Release"))
table.insert(configs, "-DBUILD_SHARED_LIBS=" .. (package:config("shared") and "ON" or "OFF"))
table.insert(configs, "-DSDLTTF_HARFBUZZ=" .. (package:config("harfbuzz") and "ON" or "OFF"))
table.insert(configs, "-DSDLTTF_PLUTOSVG=" .. (package:config("plutosvg") and "ON" or "OFF"))
local freetype = package:dep("freetype")
if freetype then
local fetchinfo = freetype:fetch()
if fetchinfo then
local includedirs = table.wrap(fetchinfo.includedirs or fetchinfo.sysincludedirs)
if #includedirs > 0 then
-- Freetype fix: see the header comment at the top of this file.
lib/Alien/Xrepo.pm view on Meta::CPAN
# repeat launch skips xrepo entirely. Entries are LRU-bounded and a hit is only trusted while its recorded install
# dir still exists. Pass cache => 0 to disable (pure fetch-first behavior, one spawn per launch).
field $cache : param //= 1;
# Resolve which package store a call should operate on. A per-call `installdir` wins, then the `root` handed to the
# constructor, then whatever xrepo/the environment chooses (global store).
method _store_dir (%opts) { return $opts{installdir} // $root }
method blah ($msg) { return unless $verbose; say $msg; }
# Echo every command we are about to run
method _debug_cmd (@cmd) {
my $str = join( ' ', map { /\s/ ? qq{"$_"} : $_ } @cmd );
my @env;
push @env, "XMAKE_THEME=$ENV{XMAKE_THEME}" if defined $ENV{XMAKE_THEME};
push @env, "XMAKE_PKG_INSTALLDIR=$ENV{XMAKE_PKG_INSTALLDIR}" if defined $ENV{XMAKE_PKG_INSTALLDIR};
push @env, "XMAKE_PKG_CACHEDIR=$ENV{XMAKE_PKG_CACHEDIR}" if defined $ENV{XMAKE_PKG_CACHEDIR};
my $env = @env ? ' [ENV ' . join( ' ', @env ) . ']' : '';
say "[XREPO] $str$env (cwd: @{[ Cwd::getcwd() ]})\n" if $verbose;
return;
}
lib/Alien/Xrepo.pm view on Meta::CPAN
say "[*] xrepo: $full_spec already installed, reusing fetch result..." if $verbose;
if ( $meta && defined $key ) {
$self->_cache_put( $key, $meta, $self->_cache_entry($data) );
$self->_cache_save( $meta, %opts );
}
return $self->_finalize( $data, %opts );
}
# Cold path: install, then fetch (which must succeed, since it tells us where things land).
my @install_cmd = $self->_argv( 'install', \@args, defined $pkgfile ? $pkgfile : $full_spec );
$self->_debug_cmd(@install_cmd);
$self->blah("Running: @install_cmd");
eval { system @install_cmd };
if ( my $spawn_err = $@ ) {
chomp $spawn_err;
die "xrepo install failed for $full_spec\n" .
" command: @install_cmd\n" .
( $^O eq 'MSWin32' ? " win32: $^E\n" : () ) .
" error: $spawn_err\n";
}
if ( $? != 0 ) {
lib/Alien/Xrepo.pm view on Meta::CPAN
say "[*] xrepo: fetching paths..." if $verbose;
my $fresh = $self->_require_fetch( $full_spec, \%opts, $err );
if ( $meta && defined $key ) {
$self->_cache_put( $key, $meta, $self->_cache_entry($fresh) );
$self->_cache_save( $meta, %opts );
}
$self->_finalize( $fresh, %opts );
}
# LRU cap on cached resolutions. Entries are just the fetch JSON plus its install dir, so the bound keeps the file
# a few hundred KB even after a long debugging session.
sub _CACHE_MAX () {64}
# Where the resolution cache lives: alongside the caller's/instance's store when one is set, else the xmake global
# dir (~/.xmake) that xrepo itself uses for the default per-user store.
method _cache_dir (%opts) {
my $store = $self->_store_dir(%opts);
return path($store)->child('.alien-xmake') if defined $store && length $store;
my $home = $ENV{XMAKE_GLOBALDIR};
$home //= $ENV{HOME};
if ( !defined $home || !length $home ) {
lib/Alien/Xrepo.pm view on Meta::CPAN
my $libs = $info->{libfiles} // [];
$libs = [$libs] unless ref $libs eq 'ARRAY';
if (@$libs) { $entry{libpath} = $libs->[0]; }
}
\%entry;
}
method _try_fetch ( $full_spec, $opts ) {
my @fetch_args = $self->_build_args($opts);
my @fetch_cmd = $self->_argv( 'fetch', [ '--json', @fetch_args ], $full_spec );
$self->_debug_cmd(@fetch_cmd);
$self->blah("Running: @fetch_cmd");
my ( $out, $err, $exit ) = capture { system @fetch_cmd };
return ( undef, "Command: @fetch_cmd\nError:\n$err" ) if $exit != 0;
return ( undef, "Command: @fetch_cmd\nNo JSON output" ) unless defined $out && length $out;
my $data = eval { $self->_decode_json_output($out) };
return ( undef, "Command: @fetch_cmd\n$@" ) if !defined $data;
( $data, undef );
}
# Mandatory fetch: after an install we have to know where the output landed, so a failure here is fatal. $why
lib/Alien/Xrepo.pm view on Meta::CPAN
push @hits, $token unless $seen{$token}++;
}
return @hits;
}
method search ( $query, %opts ) {
local $ENV{XMAKE_THEME} = $self->_theme(%opts);
say "[*] xrepo: searching for $query..." if $verbose;
my @flags = $opts{addon} ? ('--addon') : ();
my @cmd = $self->_argv( 'search', \@flags, $query );
$self->_debug_cmd(@cmd);
my ( $out, $err, $exit ) = capture { system @cmd };
if ( $exit != 0 ) {
warn "Alien::Xrepo: search for '$query' failed (exit $exit): $err\n";
return;
}
return $self->_parse_search($out);
}
method clean (%opts) {
local $ENV{XMAKE_THEME} = $self->_theme(%opts);
lib/Alien/Xrepo.pm view on Meta::CPAN
# Auto-confirm interactive xrepo/xmake prompts so captured installs never hang.
my $yes_c = $opts->{yes} // $yes;
my $confirm_c = $opts->{confirm} // $confirm;
push @args, '--confirm=' . $confirm_c if defined $confirm_c && length $confirm_c;
push @args, '-y' if $yes_c && !( defined $confirm_c && length $confirm_c );
# Standard xmake/xrepo flags
push @args, '-p', $opts->{plat} if $opts->{plat}; # platform (iphoneos, android, etc)
push @args, '-a', $opts->{arch} if $opts->{arch}; # architecture (arm64, x86_64)
push @args, '-m', $opts->{mode} if $opts->{mode}; # debug/release
my $kind = defined $opts->{kind} && length $opts->{kind} ? $opts->{kind} : $kind;
push @args, '-k', $kind
if !exists $opts->{no_kind} && defined $kind && length $kind; # emit `-k` only when a consumer asks for a specific kind
push @args, '--toolchain=' . $opts->{toolchain} if $opts->{toolchain};
push @args, '--toolchain_host=' . $opts->{toolchain_host} if $opts->{toolchain_host};
# Visual Studio
push @args, '--vs=' . $opts->{vs} if $opts->{vs};
push @args, '--vs_toolset=' . $opts->{vs_toolset} if $opts->{vs_toolset};
push @args, '--vs_sdkver=' . $opts->{vs_sdkver} if $opts->{vs_sdkver};
lib/Alien/Xrepo.pm view on Meta::CPAN
push @args, '--mingw=' . $opts->{mingw} if $opts->{mingw};
# Parallel builds / linking
push @args, '-j', $opts->{jobs} if $opts->{jobs};
push @args, '--linkjobs=' . $opts->{linkjobs} if $opts->{linkjobs};
# Other configuration
push @args, '--force' if $opts->{force};
push @args, '--shallow' if $opts->{shallow};
push @args, '--build' if $opts->{build};
push @args, '--debugdir=' . $opts->{debugdir} if $opts->{debugdir};
# Complex configs (passed as --configs='key=val,key2=val2')
if ( my $c = $opts->{configs} ) {
if ( ref $c eq 'HASH' ) {
my $str = join( ',', map { "$_=" . Alien::Xmake::_bool_str( $c->{$_} ) } sort keys %$c );
push @args, "--configs=$str";
}
else {
push @args, "--configs=$c";
}
lib/Alien/Xrepo.pm view on Meta::CPAN
my ( undef, undef, undef, $mday, $mon, $year ) = localtime;
my $work = path($tmp)->child( '.xmake', sprintf( '%02d%02d%02d', $year % 100, $mon + 1, $mday ), 'xrepo', 'working' );
$work->mkpath;
my $xmake_lua = $work->child('xmake.lua');
if ( !$xmake_lua->exists || $xmake_lua->slurp =~ /\$\{/ ) {
# A failed create leaves a read-only raw template here; Path::Tiny's spew renames over it and dies
# Permission denied, so unlink first.
$xmake_lua->remove if $xmake_lua->exists;
$xmake_lua->spew(<<~'add_rules');
add_rules("mode.debug", "mode.release")
target("working")
set_kind("binary")
add_files("src/*.cpp")
add_rules
my $main = $work->child( 'src', 'main.cpp' );
$work->child('src')->mkpath unless $main->parent->is_dir;
$main->spew("int main() { return 0; }\n") unless $main->exists;
return;
}
}
lib/Alien/Xrepo.pm view on Meta::CPAN
# everything it transitively needs), each with its own libfiles/linkdirs/bindirs/installdir. The plain
# fetch collapses to the primary package, so this is what lets a caller find where a transitive provider
# (freetype behind libsdl3_ttf) lives.
method fetch_closure ( $pkg_spec, $version //= (), %opts ) {
my $full_spec = $self->_full_spec( $pkg_spec, $version );
local $ENV{XMAKE_THEME} = $self->_theme(%opts);
local $ENV{XMAKE_PKG_INSTALLDIR} = $self->_store_dir(%opts) if defined $self->_store_dir(%opts);
local $ENV{XMAKE_PKG_CACHEDIR} = $opts{cachedir} if defined $opts{cachedir};
my @args = ( '--json', $self->_build_args( \%opts, ['--deps'] ) );
my @cmd = $self->_argv( 'fetch', \@args, $full_spec );
$self->_debug_cmd(@cmd);
$self->blah("Running: @cmd");
my ( $out, $err, $exit ) = capture { system @cmd };
return () if $exit != 0;
my $data = eval { $self->_decode_json_output($out) };
return () unless ref $data eq 'ARRAY';
return grep { ref $_ eq 'HASH' } @$data;
}
# Ask xmake to list the direct runtime dependencies of $libpath, resolving each to an absolute path where
# possible using the supplied search directories. Returns the raw entries: an absolute path when resolved,
lib/Alien/Xrepo.pm view on Meta::CPAN
import("utils.binary.deplibs")
function main(lib, ...)
local dirs = {...}
local deps = deplibs(lib, {resolve_path = true, resolve_search_paths = dirs}) or {}
io.write(json.encode(deps or {}), "\n")
end
LUA
close $script;
local $ENV{XMAKE_THEME} = $theme;
my @cmd = ( $xmake->exe, 'l', '-q', $script->filename, $libpath, grep {defined} @search_dirs );
$self->_debug_cmd(@cmd);
my ( $out, $err, $exit ) = capture { system @cmd };
return () if $exit != 0;
my $deps = eval { $self->_decode_json_output($out) };
return () unless ref $deps eq 'ARRAY';
return @$deps;
}
# Scan $libpath and classify every direct dependency by where it lives:
# store - resolved into one of the provided store/closure directories (the consumer must make it findable)
# host - resolved somewhere else on the machine that is not a default system dir (Homebrew, /usr/local)
lib/Alien/Xrepo.pod view on Meta::CPAN
my $repo = Alien::Xrepo->new();
# my $repo = Alien::Xrepo->new( cache => 0 ); # live resolution, skip the warmed cache
# Add a custom repository (optional)
# $repo->add_repo( 'my-repo', 'https://github.com/my/repo.git' );
# Install a shared lib with automatic configuration
my $ogg = $repo->install('libvorbis');
# Install a library with specific configuration
# equivalent to: xrepo install -p windows -a x86_64 -m debug --configs='shared=true,vs_runtime=MD' libpng
my $pkg = $repo->install(
'libpng', '1.6.x',
plat => 'windows',
arch => 'x64',
mode => 'debug',
configs => { vs_runtime => 'MD' }
);
die 'Install failed' unless $pkg;
# Or install a binary tool and run it (ninja, cmake, python, node, ...)
my $ninja = $repo->install('ninja');
my ($ninja_exe) = map { path($_)->child($^O eq 'MSWin32' ? 'ninja.exe' : 'ninja') } $ninja->bin_dir;
system $ninja_exe, '--version';
# Wrap a single function from sqlite3 with Affix
lib/Alien/Xrepo.pod view on Meta::CPAN
=item C<plat>
Target platform (e.g., C<windows>, C<linux>, C<macosx>, C<android>, C<iphoneos>, C<wasm>).
=item C<arch>
Target architecture (e.g., C<x86_64>, C<arm64>, C<riscv64>).
=item C<mode>
Build mode: C<debug> or C<release>.
=item C<kind>
Library kind: C<shared> or C<static>. If omitted entirely, the package's own default applies, so a bare install behaves
exactly like C<xrepo install>.
I<Note: For FFI, you almost always want C<shared>, but C<static> is available if you are linking archives with, say, an
XS module.>
=item C<toolchain>
lib/Alien/Xrepo.pod view on Meta::CPAN
I<install/download>: force reinstall/redownload all packages. I<remove>: force removal even when still depended on.
=item C<shallow>
Do not install/download dependent packages.
=item C<build>
Always build and install from source.
=item C<debugdir>
Source directory used for debugging; enables C<force> and C<shallow> by default.
=item C<configs( ... )>
A hashref or string of package-specific configurations.
configs => { openssl => 'true', shared => 'true' }
# becomes --configs='openssl=true,shared=true'
Perl's built-in boolean scalars (C<use feature 'true'/'false'>, enabled by C<use v5.36+>) are normalized to the literal
strings C<'true'> / C<'false'>, so C<< configs => { shared => true } >> produces C<--configs='shared=true'>.
lib/Alien/Xrepo.pod view on Meta::CPAN
C<fetch> the C<-I...> / C<-L.../-l...> flags you would pass to your own compiler:
use v5.40;
use Alien::Xrepo;
my $repo = Alien::Xrepo->new;
$repo->install(
'libpng', '1.6.x',
plat => 'windows',
arch => 'x64',
mode => 'debug',
kind => 'shared'
);
my $cflags = $repo->fetch( 'libpng', undef, cflags => 1 ); # "-Ic:/.../include ..."
my $ldflags = $repo->fetch( 'libpng', undef, ldflags => 1 ); # "-Lc:/.../lib -l... .."
say "CFLAGS: $cflags";
say "LDFLAGS: $ldflags";
=head3 Remove a package from the cache
lib/Alien/Xrepo/Build/Recipe.pm view on Meta::CPAN
for my $root ( keys %$pkg_roots ) {
my $v = $pkg_roots->{$root};
die "Recipe: pkg_roots '$root' must be a plain string" if ref $v || !defined $v;
}
}
my %DEF_KEYS = map { $_ => 1 } qw[
name version required
plat arch mode kind no_kind toolchain toolchain_host
vs vs_toolset vs_sdkver ndk sdk mingw
jobs linkjobs force shallow build debugdir
yes confirm theme cachedir installdir
configs includes
system verbose diagnosis
];
method version_for ($name) { $defs{$name}{version} // undef }
method required ($name) { $defs{$name}{required} // 1 }
method opts_for ( $name, %ambient ) {
my $def = $defs{$name};
return %ambient unless ref $def eq 'HASH';
t/002_argv.t view on Meta::CPAN
my ($flag_bool) = grep {/^--configs=/} @bool;
is $flag_bool, '--configs=shared=true', 'built-in true renders as --configs=shared=true';
my @boolf = $repo->_build_args( { configs => { shared => false } } );
my ($flag_boolf) = grep {/^--configs=/} @boolf;
is $flag_boolf, '--configs=shared=false', 'built-in false renders as --configs=shared=false';
my @mixed = $repo->_build_args( { configs => { shared => true, vs_runtime => 'MD' } } );
my ($flag_mixed) = grep {/^--configs=/} @mixed;
is $flag_mixed, '--configs=shared=true,vs_runtime=MD', 'booleans and strings mix in key order';
# Plain numbers / strings are historical behavior: values pass through untouched.
my @vec = $repo->_build_args( { configs => { legacy => 1, mode => 'debug' } } );
my ($flag_vec) = grep {/^--configs=/} @vec;
is $flag_vec, '--configs=legacy=1,mode=debug', 'non-boolean config values pass through unchanged';
};
subtest '_package_file is only generated for a system install' => sub {
# Without `system`, an install must stay on the bare-spec path: xrepo's shim forces
# system=false there, which is what keeps an isolated/pinned install inside its own store.
ok !scalar $repo->_package_file( 'zlib', {} ), 'no package file without system';
# With it, the install is driven through a package file so our own add_requires decides system.
my $file = $repo->_package_file( 'libsdl3_ttf 3.2.2', { system => 1, kind => 'shared' } );
ok defined $file && -f $file, 'package file is created';
t/053_install_cache.t view on Meta::CPAN
subtest '_cache_key is deterministic and option-sensitive' => sub {
my $a = $repo->_cache_key( 'webui', { kind => 'shared', configs => { shared => true } } );
my $b = $repo->_cache_key( 'webui', { kind => 'shared', configs => { shared => true } } );
is $a, $b, 'same spec+opts yield the same key';
isnt $a, $repo->_cache_key( 'webui', { kind => 'static' } ), 'kind changes the key';
isnt $a, $repo->_cache_key( 'webui 2.4.2', { kind => 'shared' } ), 'version changes the key';
isnt $a, $repo->_cache_key( 'webui', { kind => 'shared', plat => 'windows' } ), 'plat changes the key';
isnt $a, $repo->_cache_key( 'webui', { kind => 'shared', arch => 'arm64' } ), 'arch changes the key';
is $a, $repo->_cache_key( 'webui', { kind => 'shared', configs => { shared => 'true' } } ),
'built-in boolean and the literal string hash identical (stable bool stringification)';
isnt $a, $repo->_cache_key( 'webui', { kind => 'shared', mode => 'debug' } ), 'mode changes the key';
};
subtest 'LRU eviction drops the least recently used entry' => sub {
my $meta = { order => [], entries => {} };
$repo->_cache_put( 'a', $meta, { installdir => 'x', last_used => time }, 3 );
$repo->_cache_put( 'b', $meta, { installdir => 'x', last_used => time }, 3 );
$repo->_cache_put( 'c', $meta, { installdir => 'x', last_used => time }, 3 );
is [ @{ $meta->{order} } ], [qw[c b a]], 'newest entries are first';
$repo->_cache_put( 'a', $meta, $meta->{entries}{a}, 3 );
is [ @{ $meta->{order} } ], [qw[a c b]], 're-putting an entry bumps it to the front';
$repo->_cache_put( 'd', $meta, { installdir => 'x', last_used => time }, 3 );