- Add xcalloc() and call it
[make.git] / tests / run_make_tests.pl
blob2071b176b0d683681a59d9baaa0a36dc1c7da09a
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 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 require "test_driver.pl";
42 # Some target systems might not have the POSIX module...
43 $has_POSIX = eval { require "POSIX.pm" };
45 #$SIG{INT} = sub { print STDERR "Caught a signal!\n"; die @_; };
47 sub valid_option
49 local($option) = @_;
51 if ($option =~ /^-make([-_]?path)?$/)
53 $make_path = shift @argv;
54 if (!-f $make_path)
56 print "$option $make_path: Not found.\n";
57 exit 0;
59 return 1;
62 if ($option =~ /^-(valgrind|memcheck)$/i) {
63 $valgrind = 1;
64 $valgrind_args = $memcheck_args;
65 return 1;
68 if ($option =~ /^-massif$/i) {
69 $valgrind = 1;
70 $valgrind_args = $massif_args;
71 return 1;
74 # This doesn't work--it _should_! Someone badly needs to fix this.
76 # elsif ($option =~ /^-work([-_]?dir)?$/)
77 # {
78 # $workdir = shift @argv;
79 # return 1;
80 # }
82 return 0;
86 # This is an "all-in-one" function. Arguments are as follows:
88 # [0] (string): The makefile to be tested. undef means use the last one.
89 # [1] (string): Arguments to pass to make.
90 # [2] (string): Answer we should get back.
91 # [3] (integer): Exit code we expect. A missing code means 0 (success)
93 $old_makefile = undef;
95 sub run_make_test
97 local ($makestring, $options, $answer, $err_code, $timeout) = @_;
99 # If the user specified a makefile string, create a new makefile to contain
100 # it. If the first value is not defined, use the last one (if there is
101 # one).
103 if (! defined $makestring) {
104 defined $old_makefile
105 || die "run_make_test(undef) invoked before run_make_test('...')\n";
106 $makefile = $old_makefile;
107 } else {
108 if (! defined($makefile)) {
109 $makefile = &get_tmpfile();
112 # Make sure it ends in a newline.
113 $makestring && $makestring !~ /\n$/s and $makestring .= "\n";
115 # Replace @MAKEFILE@ with the makefile name and @MAKE@ with the path to
116 # make
117 $makestring =~ s/#MAKEFILE#/$makefile/g;
118 $makestring =~ s/#MAKEPATH#/$mkpath/g;
119 $makestring =~ s/#MAKE#/$make_name/g;
120 $makestring =~ s/#PWD#/$pwd/g;
122 # Populate the makefile!
123 open(MAKEFILE, "> $makefile") || die "Failed to open $makefile: $!\n";
124 print MAKEFILE $makestring;
125 close(MAKEFILE) || die "Failed to write $makefile: $!\n";
128 # Do the same processing on $answer as we did on $makestring.
130 $answer && $answer !~ /\n$/s and $answer .= "\n";
131 $answer =~ s/#MAKEFILE#/$makefile/g;
132 $answer =~ s/#MAKEPATH#/$mkpath/g;
133 $answer =~ s/#MAKE#/$make_name/g;
134 $answer =~ s/#PWD#/$pwd/g;
136 run_make_with_options($makefile, $options, &get_logfile(0),
137 $err_code, $timeout);
138 &compare_output($answer, &get_logfile(1));
140 $old_makefile = $makefile;
141 $makefile = undef;
144 # The old-fashioned way...
145 sub run_make_with_options {
146 local ($filename,$options,$logname,$expected_code,$timeout) = @_;
147 local($code);
148 local($command) = $make_path;
150 $expected_code = 0 unless defined($expected_code);
152 # Reset to reflect this one test.
153 $test_passed = 1;
155 if ($filename) {
156 $command .= " -f $filename";
159 if ($options) {
160 $command .= " $options";
163 $command_string = "$command\n";
165 if ($valgrind) {
166 print VALGRIND "\n\nExecuting: $command\n";
171 my $old_timeout = $test_timeout;
172 $timeout and $test_timeout = $timeout;
174 # If valgrind is enabled, turn off the timeout check
175 $valgrind and $test_timeout = 0;
177 $code = &run_command_with_output($logname,$command);
179 $test_timeout = $old_timeout;
182 # Check to see if we have Purify errors. If so, keep the logfile.
183 # For this to work you need to build with the Purify flag -exit-status=yes
185 if ($pure_log && -f $pure_log) {
186 if ($code & 0x7000) {
187 $code &= ~0x7000;
189 # If we have a purify log, save it
190 $tn = $pure_testname . ($num_of_logfiles ? ".$num_of_logfiles" : "");
191 print("Renaming purify log file to $tn\n") if $debug;
192 rename($pure_log, "$tn")
193 || die "Can't rename $log to $tn: $!\n";
194 ++$purify_errors;
195 } else {
196 unlink($pure_log);
200 if ($code != $expected_code) {
201 print "Error running $make_path (expected $expected_code; got $code): $command\n";
202 $test_passed = 0;
203 $runf = &get_runfile;
204 &create_file (&get_runfile, $command_string);
205 # If it's a SIGINT, stop here
206 if ($code & 127) {
207 print STDERR "\nCaught signal ".($code & 127)."!\n";
208 ($code & 127) == 2 and exit($code);
210 return 0;
213 if ($profile & $vos) {
214 system "add_profile $make_path";
217 return 1;
220 sub print_usage
222 &print_standard_usage ("run_make_tests",
223 "[-make_path make_pathname] [-memcheck] [-massif]",);
226 sub print_help
228 &print_standard_help (
229 "-make_path",
230 "\tYou may specify the pathname of the copy of make to run.",
231 "-valgrind",
232 "-memcheck",
233 "\tRun the test suite under valgrind's memcheck tool.",
234 "\tChange the default valgrind args with the VALGRIND_ARGS env var.",
235 "-massif",
236 "\tRun the test suite under valgrind's massif toool.",
237 "\tChange the default valgrind args with the VALGRIND_ARGS env var."
241 sub get_this_pwd {
242 $delete_command = 'rm -f';
243 if ($has_POSIX) {
244 $__pwd = POSIX::getcwd();
245 } elsif ($vos) {
246 $delete_command = "delete_file -no_ask";
247 $__pwd = `++(current_dir)`;
248 } else {
249 # No idea... just try using pwd as a last resort.
250 chop ($__pwd = `pwd`);
253 return $__pwd;
256 sub set_defaults
258 # $profile = 1;
259 $testee = "GNU make";
260 $make_path = "make";
261 $tmpfilesuffix = "mk";
262 $pwd = &get_this_pwd;
265 sub set_more_defaults
267 local($string);
268 local($index);
270 # find the type of the port. We do this up front to have a single
271 # point of change if it needs to be tweaked.
273 # This is probably not specific enough.
275 if ($osname =~ /Windows/i || $osname =~ /MINGW32/i || $osname =~ /CYGWIN_NT/i) {
276 $port_type = 'W32';
278 # Bleah, the osname is so variable on DOS. This kind of bites.
279 # Well, as far as I can tell if we check for some text at the
280 # beginning of the line with either no spaces or a single space, then
281 # a D, then either "OS", "os", or "ev" and a space. That should
282 # match and be pretty specific.
283 elsif ($osname =~ /^([^ ]*|[^ ]* [^ ]*)D(OS|os|ev) /) {
284 $port_type = 'DOS';
286 # Check for OS/2
287 elsif ($osname =~ m%OS/2%) {
288 $port_type = 'OS/2';
290 # Everything else, right now, is UNIX. Note that we should integrate
291 # the VOS support into this as well and get rid of $vos; we'll do
292 # that next time.
293 else {
294 $port_type = 'UNIX';
297 # On DOS/Windows system the filesystem apparently can't track
298 # timestamps with second granularity (!!). Change the sleep time
299 # needed to force a file to be considered "old".
300 $wtime = $port_type eq 'UNIX' ? 1 : $port_type eq 'OS/2' ? 2 : 4;
302 print "Port type: $port_type\n" if $debug;
303 print "Make path: $make_path\n" if $debug;
305 # Find the full pathname of Make. For DOS systems this is more
306 # complicated, so we ask make itself.
307 my $mk = `sh -c 'echo "all:;\@echo \\\$(MAKE)" | $make_path -f-'`;
308 chop $mk;
309 $mk or die "FATAL ERROR: Cannot determine the value of \$(MAKE):\n
310 'echo \"all:;\@echo \\\$(MAKE)\" | $make_path -f-' failed!\n";
311 $make_path = $mk;
312 print "Make\t= `$make_path'\n" if $debug;
314 $string = `$make_path -v -f /dev/null 2> /dev/null`;
316 $string =~ /^(GNU Make [^,\n]*)/;
317 $testee_version = "$1\n";
319 $string = `sh -c "$make_path -f /dev/null 2>&1"`;
320 if ($string =~ /(.*): \*\*\* No targets\. Stop\./) {
321 $make_name = $1;
323 else {
324 if ($make_path =~ /$pathsep([^\n$pathsep]*)$/) {
325 $make_name = $1;
327 else {
328 $make_name = $make_path;
332 # prepend pwd if this is a relative path (ie, does not
333 # start with a slash, but contains one). Thanks for the
334 # clue, Roland.
336 if (index ($make_path, ":") != 1 && index ($make_path, "/") > 0)
338 $mkpath = "$pwd$pathsep$make_path";
340 else
342 $mkpath = $make_path;
345 # Get Purify log info--if any.
347 if (exists $ENV{PURIFYOPTIONS}
348 && $ENV{PURIFYOPTIONS} =~ /.*-logfile=([^ ]+)/) {
349 $pure_log = $1 || '';
350 $pure_log =~ s/%v/$make_name/;
351 $purify_errors = 0;
354 $string = `sh -c "$make_path -j 2 -f /dev/null 2>&1"`;
355 if ($string =~ /not supported/) {
356 $parallel_jobs = 0;
358 else {
359 $parallel_jobs = 1;
362 # Set up for valgrind, if requested.
364 if ($valgrind) {
365 my $args = $valgrind_args;
366 open(VALGRIND, "> valgrind.out")
367 || die "Cannot open valgrind.out: $!\n";
368 # -q --leak-check=yes
369 exists $ENV{VALGRIND_ARGS} and $args = $ENV{VALGRIND_ARGS};
370 $make_path = "valgrind --log-fd=".fileno(VALGRIND)." $args $make_path";
371 # F_SETFD is 2
372 fcntl(VALGRIND, 2, 0) or die "fcntl(setfd) failed: $!\n";
373 system("echo Starting on `date` 1>&".fileno(VALGRIND));
374 print "Enabled valgrind support.\n";
378 sub setup_for_test
380 $makefile = &get_tmpfile;
381 if (-f $makefile) {
382 unlink $makefile;
385 # Get rid of any Purify logs.
386 if ($pure_log) {
387 ($pure_testname = $testname) =~ tr,/,_,;
388 $pure_testname = "$pure_log.$pure_testname";
389 system("rm -f $pure_testname*");
390 print("Purify testfiles are: $pure_testname*\n") if $debug;
394 exit !&toplevel;