Skip to content

Commit c500930

Browse files
Allow CMake Command to Fail Non-Fatally
Created a new command error return mechanism that distinguishes between fatal and non-fatal errors. This is indented to work with the `required` attribute from #4.
1 parent 5c2dbb1 commit c500930

File tree

6 files changed

+78
-24
lines changed

6 files changed

+78
-24
lines changed

autobuild.pl

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -853,15 +853,44 @@ (\%)
853853
}
854854
}
855855

856-
if ($command_table{$NAME}->Run ($OPTIONS, $args) == 0) {
856+
my $result = $command_table{$NAME}->Run ($OPTIONS, $args);
857+
my $result_type = ref ($result);
858+
my $failure = undef;
859+
if ($result_type eq '') {
860+
# This is the traditional command return mechanism. 0 is a fatal error
861+
# and other values (usually 1) are a success.
862+
$failure = 'fatal' if $result == 0;
863+
}
864+
elsif ($result_type eq 'HASH') {
865+
# Newer command return mechanism:
866+
# {} is success
867+
# {failure => 'fatal'} is a fatal error intended for when something
868+
# is probably fundamentally wrong with autobuild xml file and/or
869+
# the command couldn't function correctly.
870+
# {failure => 'non-fatal'} is a non-fatal error intended for when the
871+
# command failed, but in a "normal" or at least possibly expected
872+
# way, like if a test failed.
873+
# (and others?) are a success.
874+
if (exists ($result->{failure})) {
875+
$failure = $result->{failure};
876+
if ($failure ne 'fatal' && $failure ne 'non-fatal') {
877+
print STDERR "ERROR: $CMD $CMD2 " .
878+
"set \"fail\" to unexpected value \"$failure\"\n";
879+
$failure = 'fatal';
880+
}
881+
}
882+
}
883+
if (defined ($failure)) {
857884
print STDERR "ERROR: While $CMD $CMD2:\n" if ($verbose <= 1);
858885
print STDERR " The command failed";
859-
$status = 1;
860-
if (!$keep_going) {
861-
print STDERR ", exiting.\n";
862-
chdir ($starting_dir);
863-
ChangeENV (%originalENV);
864-
next INPFILE;
886+
if ($failure eq 'fatal') {
887+
$status = 1;
888+
if (!$keep_going) {
889+
print STDERR ", exiting.\n";
890+
chdir ($starting_dir);
891+
ChangeENV (%originalENV);
892+
next INPFILE;
893+
}
865894
}
866895
print STDERR "!\n";
867896
}

command/cmake.pm

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ sub Run ($)
9393
else {
9494
print STDERR __FILE__,
9595
": unexpected arg name \"$name\" in $command_name command\n";
96-
return 0;
96+
return {failure => 'fatal'};
9797
}
9898
}
9999

@@ -104,14 +104,17 @@ sub Run ($)
104104
$config_args .= " -G \"$cmake_generator\"";
105105
}
106106

107+
my $result = {};
108+
107109
# cmake_cmd commmand
108110
if ($self->{simple}) {
109-
return utility::run_command ("$cmake_command $options");
111+
utility::run_command ("$cmake_command $options", $result);
112+
return $result;
110113
}
111114
elsif (length ($options)) {
112115
print STDERR __FILE__,
113116
": options attribute not allowed for the cmake command\n";
114-
return 0;
117+
return {failure => 'fatal'};
115118
}
116119

117120
# Insert cmake_var_* Autobuild Variables and var_* Arguments
@@ -124,28 +127,28 @@ sub Run ($)
124127

125128
# Recreate Build Directory
126129
if (!utility::remove_tree ($build_dir)) {
127-
return 0;
130+
return {failure => 'fatal'};
128131
}
129132
if (!mkdir ($build_dir)) {
130133
print STDERR __FILE__, ": failed to make build directory \"$build_dir\": $!\n";
131-
return 0;
134+
return {failure => 'fatal'};
132135
}
133136

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

138141
# Run Configure CMake Command
139-
if (!utility::run_command ("$cmake_command $config_args")) {
140-
return 0;
142+
if (!utility::run_command ("$cmake_command $config_args", $result)) {
143+
return $result;
141144
}
142145

143146
# Run Build CMake Command
144-
if (!utility::run_command ("$cmake_command $build_args")) {
145-
return 0;
147+
if (!utility::run_command ("$cmake_command $build_args", $result)) {
148+
return $result;
146149
}
147150

148-
return 1;
151+
return $result;
149152
}
150153

151154
##############################################################################

common/utility.pm

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ package utility;
66
use File::Path qw(rmtree);
77

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

1817
if ($main::verbose) {
1918
print ("===== Running Command: $command\n");
@@ -24,16 +23,25 @@ sub run_command ($;$)
2423
my $error_message;
2524
if ($? == -1) {
2625
$error_message = "Failed to Run: $!";
26+
if (defined ($ab_command_result)) {
27+
$ab_command_result->{failure} = 'fatal';
28+
}
2729
}
2830
elsif ($signal) {
2931
$error_message = sprintf ("Exited on Signal %d, %s coredump",
3032
$signal, ($? & 128) ? 'with' : 'without');
33+
if (defined ($ab_command_result)) {
34+
$ab_command_result->{failure} = 'non-fatal';
35+
}
3136
}
3237
else {
3338
$error_message = sprintf ("Returned %d", $? >> 8);
39+
if (defined ($ab_command_result)) {
40+
$ab_command_result->{failure} = 'non-fatal';
41+
}
3442
}
3543
print STDERR "Command \"$command\" $error_message\n";
36-
return $ignore_failure;
44+
return 0;
3745
}
3846
return 1;
3947
}

tests/autobuild/cmake/fake_cmake.pl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
print ("fake_cmake.pl version 123.456.789\n");
1111
}
1212

13+
if ($args =~ "<<--fail-on-purpose>>") {
14+
exit (1);
15+
}
16+
1317
my $file = "cmake_runs.txt";
1418
open (my $fd, ">>$file") or die ("Couldn't open $file: $!");
1519
print $fd "$args\n";

tests/autobuild/cmake/run_test.pl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ sub dump_log
4949
"<<--build>> <<.>>\n",
5050
"build/cmake_runs.txt");
5151

52+
expect_file_contents (
53+
"<<..>> <<-G>> <<Fake Generator>> <<-DCMAKE_C_COMPILER=fake-cc>>\n",
54+
"failed_build/cmake_runs.txt");
55+
5256
expect_file_contents (
5357
"<<..>> <<-G>> <<Fake Generator>> " .
5458
"<<-DCMAKE_C_COMPILER=super-fake-cc>> " .

tests/autobuild/cmake/test.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
<!-- All Defaults -->
1717
<command name="cmake"/>
1818

19+
<!-- CMake command can fail without bringing down autobuild -->
20+
<command name="cmake">
21+
<arg name="build_dir">failed_build</arg>
22+
<arg name="add_build_args">--fail-on-purpose</arg>
23+
</command>
24+
1925
<!-- Override a cmake_var_ -->
2026
<command name="cmake" dir="subdir1">
2127
<arg name="var_CMAKE_C_COMPILER">super-fake-cc</arg>

0 commit comments

Comments
 (0)