Keep the command line on the heap to avoid stack overflow.
[make.git] / tests / run_make_tests.pl
blob477b1176721156996f631b666ae6689d9af2d728
1 #!/usr/bin/env perl
2 # -*-perl-*-
4 # Test driver for the Make test suite
6 # Usage: run_make_tests [testname]
7 # [-debug]
8 # [-help]
9 # [-verbose]
10 # [-keep]
11 # [-make <make prog>]
12 # (and others)
14 # Copyright (C) 1992-2012 Free Software Foundation, Inc.
15 # This file is part of GNU Make.
17 # GNU Make is free software; you can redistribute it and/or modify it under
18 # the terms of the GNU General Public License as published by the Free Software
19 # Foundation; either version 3 of the License, or (at your option) any later
20 # version.
22 # GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
23 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
24 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
25 # details.
27 # You should have received a copy of the GNU General Public License along with
28 # this program. If not, see <http://www.gnu.org/licenses/>.
30 %FEATURES = ();
32 $valgrind = 0; # invoke make with valgrind
33 $valgrind_args = '';
34 $memcheck_args = '--num-callers=15 --tool=memcheck --leak-check=full --suppressions=guile.supp';
35 $massif_args = '--num-callers=15 --tool=massif --alloc-fn=xmalloc --alloc-fn=xcalloc --alloc-fn=xrealloc --alloc-fn=xstrdup --alloc-fn=xstrndup';
36 $pure_log = undef;
38 $command_string = '';
40 $all_tests = 0;
42 require "test_driver.pl";
44 # Some target systems might not have the POSIX module...
45 $has_POSIX = eval { require "POSIX.pm" };
47 #$SIG{INT} = sub { print STDERR "Caught a signal!\n"; die @_; };
49 sub valid_option
51 local($option) = @_;
53 if ($option =~ /^-make([-_]?path)?$/i) {
54 $make_path = shift @argv;
55 if (!-f $make_path) {
56 print "$option $make_path: Not found.\n";
57 exit 0;
59 return 1;
62 if ($option =~ /^-all([-_]?tests)?$/i) {
63 $all_tests = 1;
64 return 1;
67 if ($option =~ /^-(valgrind|memcheck)$/i) {
68 $valgrind = 1;
69 $valgrind_args = $memcheck_args;
70 return 1;
73 if ($option =~ /^-massif$/i) {
74 $valgrind = 1;
75 $valgrind_args = $massif_args;
76 return 1;
79 # This doesn't work--it _should_! Someone badly needs to fix this.
81 # elsif ($option =~ /^-work([-_]?dir)?$/)
82 # {
83 # $workdir = shift @argv;
84 # return 1;
85 # }
87 return 0;
91 # This is an "all-in-one" function. Arguments are as follows:
93 # [0] (string): The makefile to be tested. undef means use the last one.
94 # [1] (string): Arguments to pass to make.
95 # [2] (string): Answer we should get back.
96 # [3] (integer): Exit code we expect. A missing code means 0 (success)
98 $old_makefile = undef;
100 sub run_make_test
102 local ($makestring, $options, $answer, $err_code, $timeout) = @_;
104 # If the user specified a makefile string, create a new makefile to contain
105 # it. If the first value is not defined, use the last one (if there is
106 # one).
108 if (! defined $makestring) {
109 defined $old_makefile
110 || die "run_make_test(undef) invoked before run_make_test('...')\n";
111 $makefile = $old_makefile;
112 } else {
113 if (! defined($makefile)) {
114 $makefile = &get_tmpfile();
117 # Make sure it ends in a newline.
118 $makestring && $makestring !~ /\n$/s and $makestring .= "\n";
120 # Replace @MAKEFILE@ with the makefile name and @MAKE@ with the path to
121 # make
122 $makestring =~ s/#MAKEFILE#/$makefile/g;
123 $makestring =~ s/#MAKEPATH#/$mkpath/g;
124 $makestring =~ s/#MAKE#/$make_name/g;
125 $makestring =~ s/#PERL#/$perl_name/g;
126 $makestring =~ s/#PWD#/$pwd/g;
128 # Populate the makefile!
129 open(MAKEFILE, "> $makefile") || die "Failed to open $makefile: $!\n";
130 print MAKEFILE $makestring;
131 close(MAKEFILE) || die "Failed to write $makefile: $!\n";
134 # Do the same processing on $answer as we did on $makestring.
136 $answer && $answer !~ /\n$/s and $answer .= "\n";
137 $answer =~ s/#MAKEFILE#/$makefile/g;
138 $answer =~ s/#MAKEPATH#/$mkpath/g;
139 $answer =~ s/#MAKE#/$make_name/g;
140 $answer =~ s/#PERL#/$perl_name/g;
141 $answer =~ s/#PWD#/$pwd/g;
143 run_make_with_options($makefile, $options, &get_logfile(0),
144 $err_code, $timeout);
145 &compare_output($answer, &get_logfile(1));
147 $old_makefile = $makefile;
148 $makefile = undef;
151 # The old-fashioned way...
152 sub run_make_with_options {
153 local ($filename,$options,$logname,$expected_code,$timeout) = @_;
154 local($code);
155 local($command) = $make_path;
157 $expected_code = 0 unless defined($expected_code);
159 # Reset to reflect this one test.
160 $test_passed = 1;
162 if ($filename) {
163 $command .= " -f $filename";
166 if ($options) {
167 $command .= " $options";
170 $command_string = "$command\n";
172 if ($valgrind) {
173 print VALGRIND "\n\nExecuting: $command\n";
178 my $old_timeout = $test_timeout;
179 $timeout and $test_timeout = $timeout;
181 # If valgrind is enabled, turn off the timeout check
182 $valgrind and $test_timeout = 0;
184 $code = &run_command_with_output($logname,$command);
186 $test_timeout = $old_timeout;
189 # Check to see if we have Purify errors. If so, keep the logfile.
190 # For this to work you need to build with the Purify flag -exit-status=yes
192 if ($pure_log && -f $pure_log) {
193 if ($code & 0x7000) {
194 $code &= ~0x7000;
196 # If we have a purify log, save it
197 $tn = $pure_testname . ($num_of_logfiles ? ".$num_of_logfiles" : "");
198 print("Renaming purify log file to $tn\n") if $debug;
199 rename($pure_log, "$tn")
200 || die "Can't rename $log to $tn: $!\n";
201 ++$purify_errors;
202 } else {
203 unlink($pure_log);
207 if ($code != $expected_code) {
208 print "Error running $make_path (expected $expected_code; got $code): $command\n";
209 $test_passed = 0;
210 $runf = &get_runfile;
211 &create_file (&get_runfile, $command_string);
212 # If it's a SIGINT, stop here
213 if ($code & 127) {
214 print STDERR "\nCaught signal ".($code & 127)."!\n";
215 ($code & 127) == 2 and exit($code);
217 return 0;
220 if ($profile & $vos) {
221 system "add_profile $make_path";
224 return 1;
227 sub print_usage
229 &print_standard_usage ("run_make_tests",
230 "[-make_path make_pathname] [-memcheck] [-massif]",);
233 sub print_help
235 &print_standard_help (
236 "-make_path",
237 "\tYou may specify the pathname of the copy of make to run.",
238 "-valgrind",
239 "-memcheck",
240 "\tRun the test suite under valgrind's memcheck tool.",
241 "\tChange the default valgrind args with the VALGRIND_ARGS env var.",
242 "-massif",
243 "\tRun the test suite under valgrind's massif toool.",
244 "\tChange the default valgrind args with the VALGRIND_ARGS env var."
248 sub get_this_pwd {
249 $delete_command = 'rm -f';
250 if ($has_POSIX) {
251 $__pwd = POSIX::getcwd();
252 } elsif ($vos) {
253 $delete_command = "delete_file -no_ask";
254 $__pwd = `++(current_dir)`;
255 } else {
256 # No idea... just try using pwd as a last resort.
257 chop ($__pwd = `pwd`);
260 return $__pwd;
263 sub set_defaults
265 # $profile = 1;
266 $testee = "GNU make";
267 $make_path = "make";
268 $tmpfilesuffix = "mk";
269 $pwd = &get_this_pwd;
272 sub set_more_defaults
274 local($string);
275 local($index);
277 # find the type of the port. We do this up front to have a single
278 # point of change if it needs to be tweaked.
280 # This is probably not specific enough.
282 if ($osname =~ /Windows/i || $osname =~ /MINGW32/i || $osname =~ /CYGWIN_NT/i) {
283 $port_type = 'W32';
285 # Bleah, the osname is so variable on DOS. This kind of bites.
286 # Well, as far as I can tell if we check for some text at the
287 # beginning of the line with either no spaces or a single space, then
288 # a D, then either "OS", "os", or "ev" and a space. That should
289 # match and be pretty specific.
290 elsif ($osname =~ /^([^ ]*|[^ ]* [^ ]*)D(OS|os|ev) /) {
291 $port_type = 'DOS';
293 # Check for OS/2
294 elsif ($osname =~ m%OS/2%) {
295 $port_type = 'OS/2';
297 # Everything else, right now, is UNIX. Note that we should integrate
298 # the VOS support into this as well and get rid of $vos; we'll do
299 # that next time.
300 else {
301 $port_type = 'UNIX';
304 # On DOS/Windows system the filesystem apparently can't track
305 # timestamps with second granularity (!!). Change the sleep time
306 # needed to force a file to be considered "old".
307 $wtime = $port_type eq 'UNIX' ? 1 : $port_type eq 'OS/2' ? 2 : 4;
309 print "Port type: $port_type\n" if $debug;
310 print "Make path: $make_path\n" if $debug;
312 # Find the full pathname of Make. For DOS systems this is more
313 # complicated, so we ask make itself.
314 my $mk = `sh -c 'echo "all:;\@echo \\\$(MAKE)" | $make_path -f-'`;
315 chop $mk;
316 $mk or die "FATAL ERROR: Cannot determine the value of \$(MAKE):\n
317 'echo \"all:;\@echo \\\$(MAKE)\" | $make_path -f-' failed!\n";
318 $make_path = $mk;
319 print "Make\t= '$make_path'\n" if $debug;
321 $string = `$make_path -v -f /dev/null 2> /dev/null`;
323 $string =~ /^(GNU Make [^,\n]*)/;
324 $testee_version = "$1\n";
326 $string = `sh -c "$make_path -f /dev/null 2>&1"`;
327 if ($string =~ /(.*): \*\*\* No targets\. Stop\./) {
328 $make_name = $1;
330 else {
331 if ($make_path =~ /$pathsep([^\n$pathsep]*)$/) {
332 $make_name = $1;
334 else {
335 $make_name = $make_path;
339 # prepend pwd if this is a relative path (ie, does not
340 # start with a slash, but contains one). Thanks for the
341 # clue, Roland.
343 if (index ($make_path, ":") != 1 && index ($make_path, "/") > 0)
345 $mkpath = "$pwd$pathsep$make_path";
347 else
349 $mkpath = $make_path;
352 # Get Purify log info--if any.
354 if (exists $ENV{PURIFYOPTIONS}
355 && $ENV{PURIFYOPTIONS} =~ /.*-logfile=([^ ]+)/) {
356 $pure_log = $1 || '';
357 $pure_log =~ s/%v/$make_name/;
358 $purify_errors = 0;
361 $string = `sh -c "$make_path -j 2 -f /dev/null 2>&1"`;
362 if ($string =~ /not supported/) {
363 $parallel_jobs = 0;
365 else {
366 $parallel_jobs = 1;
369 %FEATURES = map { $_ => 1 } split /\s+/, `sh -c "echo '\\\$(info \\\$(.FEATURES))' | $make_path -f- 2>/dev/null"`;
371 # Set up for valgrind, if requested.
373 if ($valgrind) {
374 my $args = $valgrind_args;
375 open(VALGRIND, "> valgrind.out")
376 || die "Cannot open valgrind.out: $!\n";
377 # -q --leak-check=yes
378 exists $ENV{VALGRIND_ARGS} and $args = $ENV{VALGRIND_ARGS};
379 $make_path = "valgrind --log-fd=".fileno(VALGRIND)." $args $make_path";
380 # F_SETFD is 2
381 fcntl(VALGRIND, 2, 0) or die "fcntl(setfd) failed: $!\n";
382 system("echo Starting on `date` 1>&".fileno(VALGRIND));
383 print "Enabled valgrind support.\n";
387 sub setup_for_test
389 $makefile = &get_tmpfile;
390 if (-f $makefile) {
391 unlink $makefile;
394 # Get rid of any Purify logs.
395 if ($pure_log) {
396 ($pure_testname = $testname) =~ tr,/,_,;
397 $pure_testname = "$pure_log.$pure_testname";
398 system("rm -f $pure_testname*");
399 print("Purify testfiles are: $pure_testname*\n") if $debug;
403 exit !&toplevel;