Skip to content

Commit

Permalink
t/porting/cpphdrcheck.t: improve error reporting
Browse files Browse the repository at this point in the history
and make not finding a C++ compiler fail for now, to make it easier
to see the cases where we don't find one but expected to find one.
  • Loading branch information
tonycoz committed May 8, 2024
1 parent 82777c7 commit 435e917
Showing 1 changed file with 43 additions and 35 deletions.
78 changes: 43 additions & 35 deletions t/porting/cpphdrcheck.t
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,16 @@ CODE

note "test run for $ccpp";
my $out = test_run({ ccpp => $ccpp, code => \$ccpp_test_code }, $cfg);
unless ($out && $out->{stdout} && $out->{stdout} eq "OK\n") {
unless ($out && $out->{run_stdout} && $out->{run_stdout} eq "OK\n") {
diag "build: $out->{build_cmd}";
diag "build output: $out->{build_out}";
diag "build exit: $out->{build_exit}";
diag "run cmd: $out->{run_cmd}" if $out->{run_cmd};
diag "run stdout: $out->{run_stdout}" if $out->{run_stdout};
diag "run stderr: $out->{run_stderr}" if $out->{run_stderr};
diag "run exit: $out->{run_exit}" if defined $out->{run_exit};
# for now fail if no C++ compiler
die "No C++ compiler found";
return;
}

Expand Down Expand Up @@ -176,7 +185,7 @@ CODE
note "probe $ccpp for standard C++$std with $arg";
my $code = get_source("cpp$std");
my $out = test_run({ ccpp => $ccpp, code => \$code, opts => $arg }, $cfg);
if ($out && $out->{stdout} && $out->{stdout} eq "OK\n") {
if ($out && $out->{run_stdout} && $out->{run_stdout} eq "OK\n") {
note "found $std";
$stds{$std} = $arg;
}
Expand All @@ -186,31 +195,6 @@ CODE
return $cfg;
}

# perform a test run to see if a compiler works
# $conf can be empty to unix-like defaults, see test_build() for more
sub test_run ($job, $conf) {
my $dir = File::Temp->newdir();
chdir "$dir"
or die "Cannot chdir to temp directory '$dir': $!";
my $build = eval { _test_build($job, $conf); }
or note "test_build: $@";
my $result;
if ($build) {
my $exit = system "$build->{exe} >stdout.txt 2>stderr.txt";
$result =
+{
%$build,
exit => $exit,
stdout => scalar _slurp("stdout.txt"),
stderr => scalar _slurp("stderr.txt"),
};
}
chdir $cwd
or die "Cannot chdir back to '$cwd': $!";

$result;
}

sub ok_compile_only($job, $conf, $name) {
our $Level;
local $Level = $Level + 1;
Expand Down Expand Up @@ -268,9 +252,29 @@ sub _test_compile_only ($job, $conf) {
};
}

# perform a test run to see if a compiler works
# $conf can be empty to unix-like defaults, see test_build() for more
sub test_run ($job, $conf) {
my $dir = File::Temp->newdir();
chdir "$dir"
or die "Cannot chdir to temp directory '$dir': $!";
my $result = _test_build($job, $conf);
if ($result->{exe}) {
my $cmd = "$result->{exe} >stdout.txt 2>stderr.txt";
my $exit = system $cmd;
$result->{run_exit} = $exit;
$result->{run_cmd} = $cmd;
$result->{run_stdout} = scalar _slurp("stdout.txt");
$result->{run_stderr} = scalar _slurp("stderr.txt");
}
chdir $cwd
or die "Cannot chdir back to '$cwd': $!";

$result;
}

# build the supplied code to test we can invoke the compiler
# and so the caller can run it

sub _test_build ($job, $conf) {
$conf ||= { type => "unix" };

Expand All @@ -297,21 +301,25 @@ sub _test_build ($job, $conf) {

my $cmd = "$job->{ccpp} $opts $code 2>&1";
note "running '$cmd'";
my $out = `$cmd`;
my $result =
+{
build_cmd => "$cmd\n",
};
my $out = `$cmd` // "";
$result->{build_out} = $out;
$result->{build_exit} = $?;
unless ($? == 0) {
note "'$cmd' failed: $out";
return;
return $result;
}

my $exe = "a.out$_exe";
unless ($^O eq "MSWin32") {
$exe = "./$exe";
}
return
+{
exe => $exe,
out => $out,
};
$result->{exe} = $exe;

return $result;
}

sub _slurp ($filename) {
Expand Down

0 comments on commit 435e917

Please sign in to comment.