Alien-Build-Plugin-Build-Xrepo

 view release on metacpan or  search on metacpan

t/101_end2end.t  view on Meta::CPAN

    if ( !$alien ) {
        _diagnose_failed_installs($started);

        #die 'alien build failed';
    }
    alien_ok $alien;
    subtest 'consumer flags and dirs' => sub {
        my @dyn = $alien->dynamic_libs;
        ok scalar @dyn >= 1, 'dynamic libs found';
        like $alien->cflags, qr[-I], 'cflags present';
        like $alien->libs,   qr[-L], 'libs present';
    };
}

# The capstone FFI check: load the dynamic libs the plugin gathered and call a real exported symbol
# to prove the whole chain down to the native library works.
ffi_ok with_subtest {
    my ($ffi) = @_;
    my $func  = $ffi->function( zlibVersion => [] => 'string' );
    my $v     = $func->call;
    ok defined $v && length $v, 'zlibVersion called via gathered dynamic_libs: ' . ( $v // '(undef)' );
    like $v, qr[^\d+\.\d+\.\d+], 'zlibVersion returned a semver string';
};
#
done_testing;

# xmake writes a failed package install's full output to
# <cachedir>/packages/<...>/<version>/installdir.failed/logs/install.txt and only prints a short tail plus the
# path.  Test::Alien already shows the captured build output when the build dies, but the log file itself is
# left behind on the build machine's cache, so read it back into the TAP diagnostics while it still exists.
sub _diagnose_failed_installs ($since) {
    my $cache = _xmake_cachedir();
    if ( !defined $cache ) {
        diag 'xmake cache directory unknown; skipped failed-install logs';
        return;
    }
    my $packages = path($cache)->child('packages');
    if ( !$packages->is_dir ) {
        diag "no xmake package cache at $packages; skipped failed-install logs";
        return;
    }
    my %logs;
    $packages->visit(
        sub ( $file, $state ) {
            diag "$file";
            return if $file->is_dir;
            return unless $file->basename eq 'install.txt';
            return unless $file->parent->basename eq 'logs';
            return unless $file->parent->parent->basename eq 'installdir.failed';
            my $mtime = eval { $file->stat->mtime };
            return if defined $mtime && $mtime < $since - 5;
            $logs{ $file->stringify } = 1;
        },
        { recurse => 1 }
    );
    if ( !%logs ) {
        diag "no recent failed xmake install log under $packages (the failure may have happened before xmake ran)";
        return;
    }
    for my $log ( sort keys %logs ) {
        my @lines = eval { path($log)->lines_utf8( { chomp => 1 } ) };
        if ($@) {
            @lines = eval { path($log)->lines( { chomp => 1 } ) };
            if ($@) {
                diag "could not read xmake install log $log: $@";
                next;
            }
        }
        s/\r$// for @lines;
        my $max = 2000;
        if ( @lines > $max ) {
            my $skipped = @lines - $max;
            diag "xmake install log: $log ($skipped earlier line(s) omitted)";
            @lines = @lines[ -$max .. -1 ];
        }
        else {
            diag "xmake install log: $log";
        }
        diag "  $_" for @lines;
    }
    return;
}

# Ask xmake itself for the cache directory: the XMAKE_GLOBALDIR / %APPDATA% / ~ resolution is not portable to
# duplicate here, and this mirrors what Alien::Xrepo does when it seeds xmake's templates.
sub _xmake_cachedir {
    my $exe = eval { Alien::Xmake->new->exe };
    return undef unless defined $exe && length $exe && -e $exe;
    my $script = q{import('core.base.global'); print('XREPO_CACHEDIR=' .. global.cachedir())};
    my ($out)  = capture { system $exe, 'lua', '-c', $script };
    my ($dir)  = ( defined $out ? $out : '' ) =~ /^XREPO_CACHEDIR=(.+)$/m;
    return undef unless defined $dir;
    $dir =~ s/\s+$//;
    return $dir;
}

sub _which_in_path ($exec) {
    my $sep  = ( $^O eq 'MSWin32' ) ? ';'                : ':';
    my @exts = ( $^O eq 'MSWin32' ) ? qw(.exe .bat .cmd) : ('');
    for my $dir ( split /\Q$sep\E/, $ENV{PATH} ) {
        next if !length $dir;
        for my $ext (@exts) {
            my $cand = File::Spec->catfile( $dir, "$exec$ext" );
            return $cand if -e $cand && !-d $cand;
        }
    }
    return undef;
}



( run in 1.912 second using v1.01-cache-2.11-cpan-234fa220139 )