Ensure private variables are not used when appending target-specific
[make.git] / tests / run_make_tests.pl
blob7291c55728e8390b55ec7d6945b5925aa512201f
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, 2010 Free Software
16 # Foundation, Inc.
17 # This file is part of GNU Make.
19 # GNU Make is free software; you can redistribute it and/or modify it under
20 # the terms of the GNU General Public License as published by the Free Software
21 # Foundation; either version 3 of the License, or (at your option) any later
22 # version.
24 # GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
25 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
26 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
27 # details.
29 # You should have received a copy of the GNU General Public License along with
30 # this program. If not, see <http://www.gnu.org/licenses/>.
32 %FEATURES = ();
34 $valgrind = 0; # invoke make with valgrind
35 $valgrind_args = '';
36 $memcheck_args = '--num-callers=15 --tool=memcheck --leak-check=full';
37 $massif_args = '--num-callers=15 --tool=massif --alloc-fn=xmalloc --alloc-fn=xcalloc --alloc-fn=xrealloc --alloc-fn=xstrdup --alloc-fn=xstrndup';
38 $pure_log = undef;
40 $command_string = '';
42 $all_tests = 0;
44 require "test_driver.pl";
46 # Some target systems might not have the POSIX module...
47 $has_POSIX = eval { require "POSIX.pm" };
49 #$SIG{INT} = sub { print STDERR "Caught a signal!\n"; die @_; };
51 sub valid_option
53 local($option) = @_;
55 if ($option =~ /^-make([-_]?path)?$/i) {
56 $make_path = shift @argv;
57 if (!-f $make_path) {
58 print "$option $make_path: Not found.\n";
59 exit 0;
61 return 1;
64 if ($option =~ /^-all([-_]?tests)?$/i) {
65 $all_tests = 1;
66 return 1;
69 if ($option =~ /^-(valgrind|memcheck)$/i) {
70 $valgrind = 1;
71 $valgrind_args = $memcheck_args;
72 return 1;
75 if ($option =~ /^-massif$/i) {
76 $valgrind = 1;
77 $valgrind_args = $massif_args;
78 return 1;
81 # This doesn't work--it _should_! Someone badly needs to fix this.
83 # elsif ($option =~ /^-work([-_]?dir)?$/)
84 # {
85 # $workdir = shift @argv;
86 # return 1;
87 # }
89 return 0;
93 # This is an "all-in-one" function. Arguments are as follows:
95 # [0] (string): The makefile to be tested. undef means use the last one.
96 # [1] (string): Arguments to pass to make.
97 # [2] (string): Answer we should get back.
98 # [3] (integer): Exit code we expect. A missing code means 0 (success)
100 $old_makefile = undef;
102 sub run_make_test
104 local ($makestring, $options, $answer, $err_code, $timeout) = @_;
106 # If the user specified a makefile string, create a new makefile to contain
107 # it. If the first value is not defined, use the last one (if there is
108 # one).
110 if (! defined $makestring) {
111 defined $old_makefile
112 || die "run_make_test(undef) invoked before run_make_test('...')\n";
113 $makefile = $old_makefile;
114 } else {
115 if (! defined($makefile)) {
116 $makefile = &get_tmpfile();
119 # Make sure it ends in a newline.
120 $makestring && $makestring !~ /\n$/s and $makestring .= "\n";
122 # Replace @MAKEFILE@ with the makefile name and @MAKE@ with the path to
123 # make
124 $makestring =~ s/#MAKEFILE#/$makefile/g;
125 $makestring =~ s/#MAKEPATH#/$mkpath/g;
126 $makestring =~ s/#MAKE#/$make_name/g;
127 $makestring =~ s/#PERL#/$perl_name/g;
128 $makestring =~ s/#PWD#/$pwd/g;
130 # Populate the makefile!
131 open(MAKEFILE, "> $makefile") || die "Failed to open $makefile: $!\n";
132 print MAKEFILE $makestring;
133 close(MAKEFILE) || die "Failed to write $makefile: $!\n";
136 # Do the same processing on $answer as we did on $makestring.
138 $answer && $answer !~ /\n$/s and $answer .= "\n";
139 $answer =~ s/#MAKEFILE#/$makefile/g;
140 $answer =~ s/#MAKEPATH#/$mkpath/g;
141 $answer =~ s/#MAKE#/$make_name/g;
142 $answer =~ s/#PERL#/$perl_name/g;
143 $answer =~ s/#PWD#/$pwd/g;
145 run_make_with_options($makefile, $options, &get_logfile(0),
146 $err_code, $timeout);
147 &compare_output($answer, &get_logfile(1));
149 $old_makefile = $makefile;
150 $makefile = undef;
153 # The old-fashioned way...
154 sub run_make_with_options {
155 local ($filename,$options,$logname,$expected_code,$timeout) = @_;
156 local($code);
157 local($command) = $make_path;
159 $expected_code = 0 unless defined($expected_code);
161 # Reset to reflect this one test.
162 $test_passed = 1;
164 if ($filename) {
165 $command .= " -f $filename";
168 if ($options) {
169 $command .= " $options";
172 $command_string = "$command\n";
174 if ($valgrind) {
175 print VALGRIND "\n\nExecuting: $command\n";
180 my $old_timeout = $test_timeout;
181 $timeout and $test_timeout = $timeout;
183 # If valgrind is enabled, turn off the timeout check
184 $valgrind and $test_timeout = 0;
186 $code = &run_command_with_output($logname,$command);
188 $test_timeout = $old_timeout;
191 # Check to see if we have Purify errors. If so, keep the logfile.
192 # For this to work you need to build with the Purify flag -exit-status=yes
194 if ($pure_log && -f $pure_log) {
195 if ($code & 0x7000) {
196 $code &= ~0x7000;
198 # If we have a purify log, save it
199 $tn = $pure_testname . ($num_of_logfiles ? ".$num_of_logfiles" : "");
200 print("Renaming purify log file to $tn\n") if $debug;
201 rename($pure_log, "$tn")
202 || die "Can't rename $log to $tn: $!\n";
203 ++$purify_errors;
204 } else {
205 unlink($pure_log);
209 if ($code != $expected_code) {
210 print "Error running $make_path (expected $expected_code; got $code): $command\n";
211 $test_passed = 0;
212 $runf = &get_runfile;
213 &create_file (&get_runfile, $command_string);
214 # If it's a SIGINT, stop here
215 if ($code & 127) {
216 print STDERR "\nCaught signal ".($code & 127)."!\n";
217 ($code & 127) == 2 and exit($code);
219 return 0;
222 if ($profile & $vos) {
223 system "add_profile $make_path";
226 return 1;
229 sub print_usage
231 &print_standard_usage ("run_make_tests",
232 "[-make_path make_pathname] [-memcheck] [-massif]",);
235 sub print_help
237 &print_standard_help (
238 "-make_path",
239 "\tYou may specify the pathname of the copy of make to run.",
240 "-valgrind",
241 "-memcheck",
242 "\tRun the test suite under valgrind's memcheck tool.",
243 "\tChange the default valgrind args with the VALGRIND_ARGS env var.",
244 "-massif",
245 "\tRun the test suite under valgrind's massif toool.",
246 "\tChange the default valgrind args with the VALGRIND_ARGS env var."
250 sub get_this_pwd {
251 $delete_command = 'rm -f';
252 if ($has_POSIX) {
253 $__pwd = POSIX::getcwd();
254 } elsif ($vos) {
255 $delete_command = "delete_file -no_ask";
256 $__pwd = `++(current_dir)`;
257 } else {
258 # No idea... just try using pwd as a last resort.
259 chop ($__pwd = `pwd`);
262 return $__pwd;
265 sub set_defaults
267 # $profile = 1;
268 $testee = "GNU make";
269 $make_path = "make";
270 $tmpfilesuffix = "mk";
271 $pwd = &get_this_pwd;
274 sub set_more_defaults
276 local($string);
277 local($index);
279 # find the type of the port. We do this up front to have a single
280 # point of change if it needs to be tweaked.
282 # This is probably not specific enough.
284 if ($osname =~ /Windows/i || $osname =~ /MINGW32/i || $osname =~ /CYGWIN_NT/i) {
285 $port_type = 'W32';
287 # Bleah, the osname is so variable on DOS. This kind of bites.
288 # Well, as far as I can tell if we check for some text at the
289 # beginning of the line with either no spaces or a single space, then
290 # a D, then either "OS", "os", or "ev" and a space. That should
291 # match and be pretty specific.
292 elsif ($osname =~ /^([^ ]*|[^ ]* [^ ]*)D(OS|os|ev) /) {
293 $port_type = 'DOS';
295 # Check for OS/2
296 elsif ($osname =~ m%OS/2%) {
297 $port_type = 'OS/2';
299 # Everything else, right now, is UNIX. Note that we should integrate
300 # the VOS support into this as well and get rid of $vos; we'll do
301 # that next time.
302 else {
303 $port_type = 'UNIX';
306 # On DOS/Windows system the filesystem apparently can't track
307 # timestamps with second granularity (!!). Change the sleep time
308 # needed to force a file to be considered "old".
309 $wtime = $port_type eq 'UNIX' ? 1 : $port_type eq 'OS/2' ? 2 : 4;
311 print "Port type: $port_type\n" if $debug;
312 print "Make path: $make_path\n" if $debug;
314 # Find the full pathname of Make. For DOS systems this is more
315 # complicated, so we ask make itself.
316 my $mk = `sh -c 'echo "all:;\@echo \\\$(MAKE)" | $make_path -f-'`;
317 chop $mk;
318 $mk or die "FATAL ERROR: Cannot determine the value of \$(MAKE):\n
319 'echo \"all:;\@echo \\\$(MAKE)\" | $make_path -f-' failed!\n";
320 $make_path = $mk;
321 print "Make\t= `$make_path'\n" if $debug;
323 $string = `$make_path -v -f /dev/null 2> /dev/null`;
325 $string =~ /^(GNU Make [^,\n]*)/;
326 $testee_version = "$1\n";
328 $string = `sh -c "$make_path -f /dev/null 2>&1"`;
329 if ($string =~ /(.*): \*\*\* No targets\. Stop\./) {
330 $make_name = $1;
332 else {
333 if ($make_path =~ /$pathsep([^\n$pathsep]*)$/) {
334 $make_name = $1;
336 else {
337 $make_name = $make_path;
341 # prepend pwd if this is a relative path (ie, does not
342 # start with a slash, but contains one). Thanks for the
343 # clue, Roland.
345 if (index ($make_path, ":") != 1 && index ($make_path, "/") > 0)
347 $mkpath = "$pwd$pathsep$make_path";
349 else
351 $mkpath = $make_path;
354 # Get Purify log info--if any.
356 if (exists $ENV{PURIFYOPTIONS}
357 && $ENV{PURIFYOPTIONS} =~ /.*-logfile=([^ ]+)/) {
358 $pure_log = $1 || '';
359 $pure_log =~ s/%v/$make_name/;
360 $purify_errors = 0;
363 $string = `sh -c "$make_path -j 2 -f /dev/null 2>&1"`;
364 if ($string =~ /not supported/) {
365 $parallel_jobs = 0;
367 else {
368 $parallel_jobs = 1;
371 %FEATURES = map { $_ => 1 } split /\s+/, `sh -c "echo '\\\$(info \\\$(.FEATURES))' | $make_path -f- 2>/dev/null"`;
373 # Set up for valgrind, if requested.
375 if ($valgrind) {
376 my $args = $valgrind_args;
377 open(VALGRIND, "> valgrind.out")
378 || die "Cannot open valgrind.out: $!\n";
379 # -q --leak-check=yes
380 exists $ENV{VALGRIND_ARGS} and $args = $ENV{VALGRIND_ARGS};
381 $make_path = "valgrind --log-fd=".fileno(VALGRIND)." $args $make_path";
382 # F_SETFD is 2
383 fcntl(VALGRIND, 2, 0) or die "fcntl(setfd) failed: $!\n";
384 system("echo Starting on `date` 1>&".fileno(VALGRIND));
385 print "Enabled valgrind support.\n";
389 sub setup_for_test
391 $makefile = &get_tmpfile;
392 if (-f $makefile) {
393 unlink $makefile;
396 # Get rid of any Purify logs.
397 if ($pure_log) {
398 ($pure_testname = $testname) =~ tr,/,_,;
399 $pure_testname = "$pure_log.$pure_testname";
400 system("rm -f $pure_testname*");
401 print("Purify testfiles are: $pure_testname*\n") if $debug;
405 exit !&toplevel;