Update copyright years.
[make.git] / tests / run_make_tests.pl
blobdabeae9180edd5a90715aa95560e0a21951210b7
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, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
15 # 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
16 # This file is part of GNU Make.
18 # GNU Make is free software; you can redistribute it and/or modify it under
19 # the terms of the GNU General Public License as published by the Free Software
20 # Foundation; either version 3 of the License, or (at your option) any later
21 # version.
23 # GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
24 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
25 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
26 # details.
28 # You should have received a copy of the GNU General Public License along with
29 # this program. If not, see <http://www.gnu.org/licenses/>.
32 $valgrind = 0; # invoke make with valgrind
33 $valgrind_args = '';
34 $memcheck_args = '--num-callers=15 --tool=memcheck --leak-check=full';
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/#PWD#/$pwd/g;
127 # Populate the makefile!
128 open(MAKEFILE, "> $makefile") || die "Failed to open $makefile: $!\n";
129 print MAKEFILE $makestring;
130 close(MAKEFILE) || die "Failed to write $makefile: $!\n";
133 # Do the same processing on $answer as we did on $makestring.
135 $answer && $answer !~ /\n$/s and $answer .= "\n";
136 $answer =~ s/#MAKEFILE#/$makefile/g;
137 $answer =~ s/#MAKEPATH#/$mkpath/g;
138 $answer =~ s/#MAKE#/$make_name/g;
139 $answer =~ s/#PWD#/$pwd/g;
141 run_make_with_options($makefile, $options, &get_logfile(0),
142 $err_code, $timeout);
143 &compare_output($answer, &get_logfile(1));
145 $old_makefile = $makefile;
146 $makefile = undef;
149 # The old-fashioned way...
150 sub run_make_with_options {
151 local ($filename,$options,$logname,$expected_code,$timeout) = @_;
152 local($code);
153 local($command) = $make_path;
155 $expected_code = 0 unless defined($expected_code);
157 # Reset to reflect this one test.
158 $test_passed = 1;
160 if ($filename) {
161 $command .= " -f $filename";
164 if ($options) {
165 $command .= " $options";
168 $command_string = "$command\n";
170 if ($valgrind) {
171 print VALGRIND "\n\nExecuting: $command\n";
176 my $old_timeout = $test_timeout;
177 $timeout and $test_timeout = $timeout;
179 # If valgrind is enabled, turn off the timeout check
180 $valgrind and $test_timeout = 0;
182 $code = &run_command_with_output($logname,$command);
184 $test_timeout = $old_timeout;
187 # Check to see if we have Purify errors. If so, keep the logfile.
188 # For this to work you need to build with the Purify flag -exit-status=yes
190 if ($pure_log && -f $pure_log) {
191 if ($code & 0x7000) {
192 $code &= ~0x7000;
194 # If we have a purify log, save it
195 $tn = $pure_testname . ($num_of_logfiles ? ".$num_of_logfiles" : "");
196 print("Renaming purify log file to $tn\n") if $debug;
197 rename($pure_log, "$tn")
198 || die "Can't rename $log to $tn: $!\n";
199 ++$purify_errors;
200 } else {
201 unlink($pure_log);
205 if ($code != $expected_code) {
206 print "Error running $make_path (expected $expected_code; got $code): $command\n";
207 $test_passed = 0;
208 $runf = &get_runfile;
209 &create_file (&get_runfile, $command_string);
210 # If it's a SIGINT, stop here
211 if ($code & 127) {
212 print STDERR "\nCaught signal ".($code & 127)."!\n";
213 ($code & 127) == 2 and exit($code);
215 return 0;
218 if ($profile & $vos) {
219 system "add_profile $make_path";
222 return 1;
225 sub print_usage
227 &print_standard_usage ("run_make_tests",
228 "[-make_path make_pathname] [-memcheck] [-massif]",);
231 sub print_help
233 &print_standard_help (
234 "-make_path",
235 "\tYou may specify the pathname of the copy of make to run.",
236 "-valgrind",
237 "-memcheck",
238 "\tRun the test suite under valgrind's memcheck tool.",
239 "\tChange the default valgrind args with the VALGRIND_ARGS env var.",
240 "-massif",
241 "\tRun the test suite under valgrind's massif toool.",
242 "\tChange the default valgrind args with the VALGRIND_ARGS env var."
246 sub get_this_pwd {
247 $delete_command = 'rm -f';
248 if ($has_POSIX) {
249 $__pwd = POSIX::getcwd();
250 } elsif ($vos) {
251 $delete_command = "delete_file -no_ask";
252 $__pwd = `++(current_dir)`;
253 } else {
254 # No idea... just try using pwd as a last resort.
255 chop ($__pwd = `pwd`);
258 return $__pwd;
261 sub set_defaults
263 # $profile = 1;
264 $testee = "GNU make";
265 $make_path = "make";
266 $tmpfilesuffix = "mk";
267 $pwd = &get_this_pwd;
270 sub set_more_defaults
272 local($string);
273 local($index);
275 # find the type of the port. We do this up front to have a single
276 # point of change if it needs to be tweaked.
278 # This is probably not specific enough.
280 if ($osname =~ /Windows/i || $osname =~ /MINGW32/i || $osname =~ /CYGWIN_NT/i) {
281 $port_type = 'W32';
283 # Bleah, the osname is so variable on DOS. This kind of bites.
284 # Well, as far as I can tell if we check for some text at the
285 # beginning of the line with either no spaces or a single space, then
286 # a D, then either "OS", "os", or "ev" and a space. That should
287 # match and be pretty specific.
288 elsif ($osname =~ /^([^ ]*|[^ ]* [^ ]*)D(OS|os|ev) /) {
289 $port_type = 'DOS';
291 # Check for OS/2
292 elsif ($osname =~ m%OS/2%) {
293 $port_type = 'OS/2';
295 # Everything else, right now, is UNIX. Note that we should integrate
296 # the VOS support into this as well and get rid of $vos; we'll do
297 # that next time.
298 else {
299 $port_type = 'UNIX';
302 # On DOS/Windows system the filesystem apparently can't track
303 # timestamps with second granularity (!!). Change the sleep time
304 # needed to force a file to be considered "old".
305 $wtime = $port_type eq 'UNIX' ? 1 : $port_type eq 'OS/2' ? 2 : 4;
307 print "Port type: $port_type\n" if $debug;
308 print "Make path: $make_path\n" if $debug;
310 # Find the full pathname of Make. For DOS systems this is more
311 # complicated, so we ask make itself.
312 my $mk = `sh -c 'echo "all:;\@echo \\\$(MAKE)" | $make_path -f-'`;
313 chop $mk;
314 $mk or die "FATAL ERROR: Cannot determine the value of \$(MAKE):\n
315 'echo \"all:;\@echo \\\$(MAKE)\" | $make_path -f-' failed!\n";
316 $make_path = $mk;
317 print "Make\t= `$make_path'\n" if $debug;
319 $string = `$make_path -v -f /dev/null 2> /dev/null`;
321 $string =~ /^(GNU Make [^,\n]*)/;
322 $testee_version = "$1\n";
324 $string = `sh -c "$make_path -f /dev/null 2>&1"`;
325 if ($string =~ /(.*): \*\*\* No targets\. Stop\./) {
326 $make_name = $1;
328 else {
329 if ($make_path =~ /$pathsep([^\n$pathsep]*)$/) {
330 $make_name = $1;
332 else {
333 $make_name = $make_path;
337 # prepend pwd if this is a relative path (ie, does not
338 # start with a slash, but contains one). Thanks for the
339 # clue, Roland.
341 if (index ($make_path, ":") != 1 && index ($make_path, "/") > 0)
343 $mkpath = "$pwd$pathsep$make_path";
345 else
347 $mkpath = $make_path;
350 # Get Purify log info--if any.
352 if (exists $ENV{PURIFYOPTIONS}
353 && $ENV{PURIFYOPTIONS} =~ /.*-logfile=([^ ]+)/) {
354 $pure_log = $1 || '';
355 $pure_log =~ s/%v/$make_name/;
356 $purify_errors = 0;
359 $string = `sh -c "$make_path -j 2 -f /dev/null 2>&1"`;
360 if ($string =~ /not supported/) {
361 $parallel_jobs = 0;
363 else {
364 $parallel_jobs = 1;
367 # Set up for valgrind, if requested.
369 if ($valgrind) {
370 my $args = $valgrind_args;
371 open(VALGRIND, "> valgrind.out")
372 || die "Cannot open valgrind.out: $!\n";
373 # -q --leak-check=yes
374 exists $ENV{VALGRIND_ARGS} and $args = $ENV{VALGRIND_ARGS};
375 $make_path = "valgrind --log-fd=".fileno(VALGRIND)." $args $make_path";
376 # F_SETFD is 2
377 fcntl(VALGRIND, 2, 0) or die "fcntl(setfd) failed: $!\n";
378 system("echo Starting on `date` 1>&".fileno(VALGRIND));
379 print "Enabled valgrind support.\n";
383 sub setup_for_test
385 $makefile = &get_tmpfile;
386 if (-f $makefile) {
387 unlink $makefile;
390 # Get rid of any Purify logs.
391 if ($pure_log) {
392 ($pure_testname = $testname) =~ tr,/,_,;
393 $pure_testname = "$pure_log.$pure_testname";
394 system("rm -f $pure_testname*");
395 print("Purify testfiles are: $pure_testname*\n") if $debug;
399 exit !&toplevel;