Skip to content

Allow cmake Command to Fail Non-Fatally #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions autobuild.pl
Original file line number Diff line number Diff line change
Expand Up @@ -853,15 +853,44 @@ (\%)
}
}

if ($command_table{$NAME}->Run ($OPTIONS, $args) == 0) {
my $result = $command_table{$NAME}->Run ($OPTIONS, $args);
my $result_type = ref ($result);
my $failure = undef;
if ($result_type eq '') {
# This is the traditional command return mechanism. 0 is a fatal error
# and other values (usually 1) are a success.
$failure = 'fatal' if $result == 0;
}
elsif ($result_type eq 'HASH') {
# Newer command return mechanism:
# {} is success
# {failure => 'fatal'} is a fatal error intended for when something
# is probably fundamentally wrong with autobuild xml file and/or
# the command couldn't function correctly.
# {failure => 'non-fatal'} is a non-fatal error intended for when the
# command failed, but in a "normal" or at least possibly expected
# way, like if a test failed.
# (and others?) are a success.
if (exists ($result->{failure})) {
$failure = $result->{failure};
if ($failure ne 'fatal' && $failure ne 'non-fatal') {
print STDERR "ERROR: $CMD $CMD2 " .
"set \"fail\" to unexpected value \"$failure\"\n";
$failure = 'fatal';
}
}
}
if (defined ($failure)) {
print STDERR "ERROR: While $CMD $CMD2:\n" if ($verbose <= 1);
print STDERR " The command failed";
$status = 1;
if (!$keep_going) {
print STDERR ", exiting.\n";
chdir ($starting_dir);
ChangeENV (%originalENV);
next INPFILE;
if ($failure eq 'fatal') {
$status = 1;
if (!$keep_going) {
print STDERR ", exiting.\n";
chdir ($starting_dir);
ChangeENV (%originalENV);
next INPFILE;
}
}
print STDERR "!\n";
}
Expand Down
29 changes: 18 additions & 11 deletions command/cmake.pm
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ sub Run ($)

my $command_name = $self->{simple} ? "cmake_cmd" : "cmake";

main::PrintStatus (
($self->{simple} && $options !~ /\W--build\W/) ? 'Configure' : 'Compile',
$command_name);

# Get cmake_var_* Autobuild Variables
my @cmake_vars = ();
my $autobuild_var_cmake_var_re = qr/^cmake_var_(\w+)$/;
Expand Down Expand Up @@ -93,7 +97,7 @@ sub Run ($)
else {
print STDERR __FILE__,
": unexpected arg name \"$name\" in $command_name command\n";
return 0;
return {failure => 'fatal'};
}
}

Expand All @@ -104,14 +108,17 @@ sub Run ($)
$config_args .= " -G \"$cmake_generator\"";
}

my $result = {};

# cmake_cmd commmand
if ($self->{simple}) {
return utility::run_command ("$cmake_command $options");
utility::run_command ("$cmake_command $options", $result);
return $result;
}
elsif (length ($options)) {
print STDERR __FILE__,
": options attribute not allowed for the cmake command\n";
return 0;
return {failure => 'fatal'};
}

# Insert cmake_var_* Autobuild Variables and var_* Arguments
Expand All @@ -124,28 +131,28 @@ sub Run ($)

# Recreate Build Directory
if (!utility::remove_tree ($build_dir)) {
return 0;
return {failure => 'fatal'};
}
if (!mkdir ($build_dir)) {
print STDERR __FILE__, ": failed to make build directory \"$build_dir\": $!\n";
return 0;
return {failure => 'fatal'};
}

# Change to Build Directory
my $build_cd = ChangeDir->new({dir => $build_dir});
return 0 unless ($build_cd);
return {failure => 'fatal'} unless ($build_cd);

# Run Configure CMake Command
if (!utility::run_command ("$cmake_command $config_args")) {
return 0;
if (!utility::run_command ("$cmake_command $config_args", $result)) {
return $result;
}

# Run Build CMake Command
if (!utility::run_command ("$cmake_command $build_args")) {
return 0;
if (!utility::run_command ("$cmake_command $build_args", $result)) {
return $result;
}

return 1;
return $result;
}

##############################################################################
Expand Down
4 changes: 1 addition & 3 deletions command/print_cmake_version.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ sub Run ($)
my $cmake_command = main::GetVariable ('cmake_command');
$cmake_command = "\"$cmake_command\" --version";

main::PrintStatus ('Config', "print CMake Version");

print "<h3>CMake version ($cmake_command)</h3>\n";
main::PrintStatus ('Config', "CMake Version ($cmake_command)");

return utility::run_command ($cmake_command);
}
Expand Down
20 changes: 14 additions & 6 deletions common/utility.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ package utility;
use File::Path qw(rmtree);

# Run command, returns 0 if there was an error. If the second argument is
# passed and is true, then it always returns 1.
# passed, it's assumed to be an autobuild command result hashref and "failure"
# will be set to "fatal" if it is a total failure is fatal and "non-fatal" if
# the exit status result is just non-zero.
sub run_command ($;$)
{
my $command = shift;
my $ignore_failure = shift;
if (!defined $ignore_failure) {
$ignore_failure = 0;
}
my $ab_command_result = shift;

if ($main::verbose) {
print ("===== Running Command: $command\n");
Expand All @@ -24,16 +23,25 @@ sub run_command ($;$)
my $error_message;
if ($? == -1) {
$error_message = "Failed to Run: $!";
if (defined ($ab_command_result)) {
$ab_command_result->{failure} = 'fatal';
}
}
elsif ($signal) {
$error_message = sprintf ("Exited on Signal %d, %s coredump",
$signal, ($? & 128) ? 'with' : 'without');
if (defined ($ab_command_result)) {
$ab_command_result->{failure} = 'non-fatal';
}
}
else {
$error_message = sprintf ("Returned %d", $? >> 8);
if (defined ($ab_command_result)) {
$ab_command_result->{failure} = 'non-fatal';
}
}
print STDERR "Command \"$command\" $error_message\n";
return $ignore_failure;
return 0;
}
return 1;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/autobuild/cmake/fake_cmake.pl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
print ("fake_cmake.pl version 123.456.789\n");
}

if ($args =~ "<<--fail-on-purpose>>") {
exit (1);
}

my $file = "cmake_runs.txt";
open (my $fd, ">>$file") or die ("Couldn't open $file: $!");
print $fd "$args\n";
4 changes: 4 additions & 0 deletions tests/autobuild/cmake/run_test.pl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ sub dump_log
"<<--build>> <<.>>\n",
"build/cmake_runs.txt");

expect_file_contents (
"<<..>> <<-G>> <<Fake Generator>> <<-DCMAKE_C_COMPILER=fake-cc>>\n",
"failed_build/cmake_runs.txt");

expect_file_contents (
"<<..>> <<-G>> <<Fake Generator>> " .
"<<-DCMAKE_C_COMPILER=super-fake-cc>> " .
Expand Down
8 changes: 7 additions & 1 deletion tests/autobuild/cmake/test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@

<command name="log" options="on"/>
<command name="print_cmake_version"/>
<command name="log" options="off"/>

<!-- All Defaults -->
<command name="cmake"/>

<!-- CMake command can fail without bringing down autobuild -->
<command name="cmake">
<arg name="build_dir">failed_build</arg>
<arg name="add_build_args">--fail-on-purpose</arg>
</command>

<!-- Override a cmake_var_ -->
<command name="cmake" dir="subdir1">
<arg name="var_CMAKE_C_COMPILER">super-fake-cc</arg>
Expand All @@ -32,4 +37,5 @@
</command>

<command name="cmake_cmd" options="--cmake-cmd"/>
<command name="log" options="off"/>
</autobuild>