- Fix Savannah bug #18124
[make.git] / tests / run_make_tests.pl
blob0adb17200a3b1345e00cd07bf111a2ff3aed1983
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 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/>.
31 $valgrind = 0; # invoke make with valgrind
32 $valgrind_args = '--num-callers=15 --tool=memcheck --leak-check=full';
33 $pure_log = undef;
35 require "test_driver.pl";
37 # Some target systems might not have the POSIX module...
38 $has_POSIX = eval { require "POSIX.pm" };
40 #$SIG{INT} = sub { print STDERR "Caught a signal!\n"; die @_; };
42 sub valid_option
44 local($option) = @_;
46 if ($option =~ /^-make([-_]?path)?$/)
48 $make_path = shift @argv;
49 if (!-f $make_path)
51 print "$option $make_path: Not found.\n";
52 exit 0;
54 return 1;
57 if ($option =~ /^-valgrind$/i) {
58 $valgrind = 1;
59 return 1;
62 # This doesn't work--it _should_! Someone badly needs to fix this.
64 # elsif ($option =~ /^-work([-_]?dir)?$/)
65 # {
66 # $workdir = shift @argv;
67 # return 1;
68 # }
70 return 0;
74 # This is an "all-in-one" function. Arguments are as follows:
76 # [0] (string): The makefile to be tested. undef means use the last one.
77 # [1] (string): Arguments to pass to make.
78 # [2] (string): Answer we should get back.
79 # [3] (integer): Exit code we expect. A missing code means 0 (success)
81 $old_makefile = undef;
83 sub run_make_test
85 local ($makestring, $options, $answer, $err_code, $timeout) = @_;
87 # If the user specified a makefile string, create a new makefile to contain
88 # it. If the first value is not defined, use the last one (if there is
89 # one).
91 if (! defined $makestring) {
92 defined $old_makefile
93 || die "run_make_test(undef) invoked before run_make_test('...')\n";
94 $makefile = $old_makefile;
95 } else {
96 if (! defined($makefile)) {
97 $makefile = &get_tmpfile();
100 # Make sure it ends in a newline.
101 $makestring && $makestring !~ /\n$/s and $makestring .= "\n";
103 # Replace @MAKEFILE@ with the makefile name and @MAKE@ with the path to
104 # make
105 $makestring =~ s/#MAKEFILE#/$makefile/g;
106 $makestring =~ s/#MAKEPATH#/$mkpath/g;
107 $makestring =~ s/#MAKE#/$make_name/g;
108 $makestring =~ s/#PWD#/$pwd/g;
110 # Populate the makefile!
111 open(MAKEFILE, "> $makefile") || die "Failed to open $makefile: $!\n";
112 print MAKEFILE $makestring;
113 close(MAKEFILE) || die "Failed to write $makefile: $!\n";
116 # Do the same processing on $answer as we did on $makestring.
118 $answer && $answer !~ /\n$/s and $answer .= "\n";
119 $answer =~ s/#MAKEFILE#/$makefile/g;
120 $answer =~ s/#MAKEPATH#/$mkpath/g;
121 $answer =~ s/#MAKE#/$make_name/g;
122 $answer =~ s/#PWD#/$pwd/g;
124 run_make_with_options($makefile, $options, &get_logfile(0),
125 $err_code, $timeout);
126 &compare_output($answer, &get_logfile(1));
128 $old_makefile = $makefile;
129 $makefile = undef;
132 # The old-fashioned way...
133 sub run_make_with_options {
134 local ($filename,$options,$logname,$expected_code,$timeout) = @_;
135 local($code);
136 local($command) = $make_path;
138 $expected_code = 0 unless defined($expected_code);
140 # Reset to reflect this one test.
141 $test_passed = 1;
143 if ($filename) {
144 $command .= " -f $filename";
147 if ($options) {
148 $command .= " $options";
151 if ($valgrind) {
152 print VALGRIND "\n\nExecuting: $command\n";
157 my $old_timeout = $test_timeout;
158 $test_timeout = $timeout if $timeout;
160 $code = &run_command_with_output($logname,$command);
162 $test_timeout = $old_timeout;
165 # Check to see if we have Purify errors. If so, keep the logfile.
166 # For this to work you need to build with the Purify flag -exit-status=yes
168 if ($pure_log && -f $pure_log) {
169 if ($code & 0x7000) {
170 $code &= ~0x7000;
172 # If we have a purify log, save it
173 $tn = $pure_testname . ($num_of_logfiles ? ".$num_of_logfiles" : "");
174 print("Renaming purify log file to $tn\n") if $debug;
175 rename($pure_log, "$tn")
176 || die "Can't rename $log to $tn: $!\n";
177 ++$purify_errors;
178 } else {
179 unlink($pure_log);
183 if ($code != $expected_code) {
184 print "Error running $make_path (expected $expected_code; got $code): $command\n";
185 $test_passed = 0;
186 # If it's a SIGINT, stop here
187 if ($code & 127) {
188 print STDERR "\nCaught signal ".($code & 127)."!\n";
189 exit($code);
191 return 0;
194 if ($profile & $vos) {
195 system "add_profile $make_path";
201 sub print_usage
203 &print_standard_usage ("run_make_tests",
204 "[-make_path make_pathname] [-valgrind]",);
207 sub print_help
209 &print_standard_help ("-make_path",
210 "\tYou may specify the pathname of the copy of make to run.");
213 sub get_this_pwd {
214 $delete_command = 'rm -f';
215 if ($has_POSIX) {
216 $__pwd = POSIX::getcwd();
217 } elsif ($vos) {
218 $delete_command = "delete_file -no_ask";
219 $__pwd = `++(current_dir)`;
220 } else {
221 # No idea... just try using pwd as a last resort.
222 chop ($__pwd = `pwd`);
225 return $__pwd;
228 sub set_defaults
230 # $profile = 1;
231 $testee = "GNU make";
232 $make_path = "make";
233 $tmpfilesuffix = "mk";
234 $pwd = &get_this_pwd;
237 sub set_more_defaults
239 local($string);
240 local($index);
242 # find the type of the port. We do this up front to have a single
243 # point of change if it needs to be tweaked.
245 # This is probably not specific enough.
247 if ($osname =~ /Windows/i || $osname =~ /MINGW32/i || $osname =~ /CYGWIN_NT/i) {
248 $port_type = 'W32';
250 # Bleah, the osname is so variable on DOS. This kind of bites.
251 # Well, as far as I can tell if we check for some text at the
252 # beginning of the line with either no spaces or a single space, then
253 # a D, then either "OS", "os", or "ev" and a space. That should
254 # match and be pretty specific.
255 elsif ($osname =~ /^([^ ]*|[^ ]* [^ ]*)D(OS|os|ev) /) {
256 $port_type = 'DOS';
258 # Check for OS/2
259 elsif ($osname =~ m%OS/2%) {
260 $port_type = 'OS/2';
262 # Everything else, right now, is UNIX. Note that we should integrate
263 # the VOS support into this as well and get rid of $vos; we'll do
264 # that next time.
265 else {
266 $port_type = 'UNIX';
269 # On DOS/Windows system the filesystem apparently can't track
270 # timestamps with second granularity (!!). Change the sleep time
271 # needed to force a file to be considered "old".
272 $wtime = $port_type eq 'UNIX' ? 1 : $port_type eq 'OS/2' ? 2 : 4;
274 print "Port type: $port_type\n" if $debug;
275 print "Make path: $make_path\n" if $debug;
277 # Find the full pathname of Make. For DOS systems this is more
278 # complicated, so we ask make itself.
279 my $mk = `sh -c 'echo "all:;\@echo \\\$(MAKE)" | $make_path -f-'`;
280 chop $mk;
281 $mk or die "FATAL ERROR: Cannot determine the value of \$(MAKE):\n
282 'echo \"all:;\@echo \\\$(MAKE)\" | $make_path -f-' failed!\n";
283 $make_path = $mk;
284 print "Make\t= `$make_path'\n" if $debug;
286 $string = `$make_path -v -f /dev/null 2> /dev/null`;
288 $string =~ /^(GNU Make [^,\n]*)/;
289 $testee_version = "$1\n";
291 $string = `sh -c "$make_path -f /dev/null 2>&1"`;
292 if ($string =~ /(.*): \*\*\* No targets\. Stop\./) {
293 $make_name = $1;
295 else {
296 if ($make_path =~ /$pathsep([^\n$pathsep]*)$/) {
297 $make_name = $1;
299 else {
300 $make_name = $make_path;
304 # prepend pwd if this is a relative path (ie, does not
305 # start with a slash, but contains one). Thanks for the
306 # clue, Roland.
308 if (index ($make_path, ":") != 1 && index ($make_path, "/") > 0)
310 $mkpath = "$pwd$pathsep$make_path";
312 else
314 $mkpath = $make_path;
317 # Get Purify log info--if any.
319 if (exists $ENV{PURIFYOPTIONS}
320 && $ENV{PURIFYOPTIONS} =~ /.*-logfile=([^ ]+)/) {
321 $pure_log = $1 || '';
322 $pure_log =~ s/%v/$make_name/;
323 $purify_errors = 0;
326 $string = `sh -c "$make_path -j 2 -f /dev/null 2>&1"`;
327 if ($string =~ /not supported/) {
328 $parallel_jobs = 0;
330 else {
331 $parallel_jobs = 1;
334 # Set up for valgrind, if requested.
336 if ($valgrind) {
337 open(VALGRIND, "> valgrind.out")
338 || die "Cannot open valgrind.out: $!\n";
339 # -q --leak-check=yes
340 exists $ENV{VALGRIND_ARGS} and $valgrind_args = $ENV{VALGRIND_ARGS};
341 $make_path = "valgrind --log-fd=".fileno(VALGRIND)." $valgrind_args $make_path";
342 # F_SETFD is 2
343 fcntl(VALGRIND, 2, 0) or die "fcntl(setfd) failed: $!\n";
344 system("echo Starting on `date` 1>&".fileno(VALGRIND));
345 print "Enabled valgrind support.\n";
349 sub setup_for_test
351 $makefile = &get_tmpfile;
352 if (-f $makefile) {
353 unlink $makefile;
356 # Get rid of any Purify logs.
357 if ($pure_log) {
358 ($pure_testname = $testname) =~ tr,/,_,;
359 $pure_testname = "$pure_log.$pure_testname";
360 system("rm -f $pure_testname*");
361 print("Purify testfiles are: $pure_testname*\n") if $debug;
365 exit !&toplevel;