Skip to content

Commit 439b571

Browse files
authored
Merge pull request #55 from iguessthislldo/igtd/cmake
Allow `cmake` Command to Fail Non-Fatally
2 parents 5c2dbb1 + 669678b commit 439b571

File tree

7 files changed

+84
-28
lines changed

7 files changed

+84
-28
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: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ sub Run ($)
5252

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

55+
main::PrintStatus (
56+
($self->{simple} && $options !~ /\W--build\W/) ? 'Configure' : 'Compile',
57+
$command_name);
58+
5559
# Get cmake_var_* Autobuild Variables
5660
my @cmake_vars = ();
5761
my $autobuild_var_cmake_var_re = qr/^cmake_var_(\w+)$/;
@@ -93,7 +97,7 @@ sub Run ($)
9397
else {
9498
print STDERR __FILE__,
9599
": unexpected arg name \"$name\" in $command_name command\n";
96-
return 0;
100+
return {failure => 'fatal'};
97101
}
98102
}
99103

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

111+
my $result = {};
112+
107113
# cmake_cmd commmand
108114
if ($self->{simple}) {
109-
return utility::run_command ("$cmake_command $options");
115+
utility::run_command ("$cmake_command $options", $result);
116+
return $result;
110117
}
111118
elsif (length ($options)) {
112119
print STDERR __FILE__,
113120
": options attribute not allowed for the cmake command\n";
114-
return 0;
121+
return {failure => 'fatal'};
115122
}
116123

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

125132
# Recreate Build Directory
126133
if (!utility::remove_tree ($build_dir)) {
127-
return 0;
134+
return {failure => 'fatal'};
128135
}
129136
if (!mkdir ($build_dir)) {
130137
print STDERR __FILE__, ": failed to make build directory \"$build_dir\": $!\n";
131-
return 0;
138+
return {failure => 'fatal'};
132139
}
133140

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

138145
# Run Configure CMake Command
139-
if (!utility::run_command ("$cmake_command $config_args")) {
140-
return 0;
146+
if (!utility::run_command ("$cmake_command $config_args", $result)) {
147+
return $result;
141148
}
142149

143150
# Run Build CMake Command
144-
if (!utility::run_command ("$cmake_command $build_args")) {
145-
return 0;
151+
if (!utility::run_command ("$cmake_command $build_args", $result)) {
152+
return $result;
146153
}
147154

148-
return 1;
155+
return $result;
149156
}
150157

151158
##############################################################################

command/print_cmake_version.pm

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ sub Run ($)
4646
my $cmake_command = main::GetVariable ('cmake_command');
4747
$cmake_command = "\"$cmake_command\" --version";
4848

49-
main::PrintStatus ('Config', "print CMake Version");
50-
51-
print "<h3>CMake version ($cmake_command)</h3>\n";
49+
main::PrintStatus ('Config', "CMake Version ($cmake_command)");
5250

5351
return utility::run_command ($cmake_command);
5452
}

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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@
1111

1212
<command name="log" options="on"/>
1313
<command name="print_cmake_version"/>
14-
<command name="log" options="off"/>
1514

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

18+
<!-- CMake command can fail without bringing down autobuild -->
19+
<command name="cmake">
20+
<arg name="build_dir">failed_build</arg>
21+
<arg name="add_build_args">--fail-on-purpose</arg>
22+
</command>
23+
1924
<!-- Override a cmake_var_ -->
2025
<command name="cmake" dir="subdir1">
2126
<arg name="var_CMAKE_C_COMPILER">super-fake-cc</arg>
@@ -32,4 +37,5 @@
3237
</command>
3338

3439
<command name="cmake_cmd" options="--cmake-cmd"/>
40+
<command name="log" options="off"/>
3541
</autobuild>

0 commit comments

Comments
 (0)