- New code capability: a read-only string cache. Start of solution for
[make.git] / tests / run_make_tests.pl
blob5c500ef5879054830927117b99ae593f13d835f5
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 $valgrind = 0; # invoke make with valgrind
15 $valgrind_args = '--num-callers=15 --tool=memcheck --leak-check=full';
16 $pure_log = undef;
18 require "test_driver.pl";
20 # Some target systems might not have the POSIX module...
21 $has_POSIX = eval { require "POSIX.pm" };
23 #$SIG{INT} = sub { print STDERR "Caught a signal!\n"; die @_; };
25 sub valid_option
27 local($option) = @_;
29 if ($option =~ /^-make([-_]?path)?$/)
31 $make_path = shift @argv;
32 if (!-f $make_path)
34 print "$option $make_path: Not found.\n";
35 exit 0;
37 return 1;
40 if ($option =~ /^-valgrind$/i) {
41 $valgrind = 1;
42 return 1;
45 # This doesn't work--it _should_! Someone badly needs to fix this.
47 # elsif ($option =~ /^-work([-_]?dir)?$/)
48 # {
49 # $workdir = shift @argv;
50 # return 1;
51 # }
53 return 0;
57 # This is an "all-in-one" function. Arguments are as follows:
59 # [0] (string): The makefile to be tested. undef means use the last one.
60 # [1] (string): Arguments to pass to make.
61 # [2] (string): Answer we should get back.
62 # [3] (integer): Exit code we expect. A missing code means 0 (success)
64 $old_makefile = undef;
66 sub run_make_test
68 local ($makestring, $options, $answer, $err_code) = @_;
70 # If the user specified a makefile string, create a new makefile to contain
71 # it. If the first value is not defined, use the last one (if there is
72 # one).
74 if (! defined $makestring) {
75 defined $old_makefile
76 || die "run_make_test(undef) invoked before run_make_test('...')\n";
77 $makefile = $old_makefile;
78 } else {
79 if (! defined($makefile)) {
80 $makefile = &get_tmpfile();
83 # Make sure it ends in a newline.
84 $makestring && $makestring !~ /\n$/s and $makestring .= "\n";
86 # Replace @MAKEFILE@ with the makefile name and @MAKE@ with the path to
87 # make
88 $makestring =~ s/#MAKEFILE#/$makefile/g;
89 $makestring =~ s/#MAKEPATH#/$mkpath/g;
90 $makestring =~ s/#MAKE#/$make_name/g;
91 $makestring =~ s/#PWD#/$pwd/g;
93 # Populate the makefile!
94 open(MAKEFILE, "> $makefile") || die "Failed to open $makefile: $!\n";
95 print MAKEFILE $makestring;
96 close(MAKEFILE) || die "Failed to write $makefile: $!\n";
99 # Do the same processing on $answer as we did on $makestring.
101 $answer && $answer !~ /\n$/s and $answer .= "\n";
102 $answer =~ s/#MAKEFILE#/$makefile/g;
103 $answer =~ s/#MAKEPATH#/$mkpath/g;
104 $answer =~ s/#MAKE#/$make_name/g;
105 $answer =~ s/#PWD#/$pwd/g;
107 &run_make_with_options($makefile, $options, &get_logfile(0), $err_code);
108 &compare_output($answer, &get_logfile(1));
110 $old_makefile = $makefile;
111 $makefile = undef;
114 # The old-fashioned way...
115 sub run_make_with_options {
116 local ($filename,$options,$logname,$expected_code) = @_;
117 local($code);
118 local($command) = $make_path;
120 $expected_code = 0 unless defined($expected_code);
122 # Reset to reflect this one test.
123 $test_passed = 1;
125 if ($filename) {
126 $command .= " -f $filename";
129 if ($options) {
130 $command .= " $options";
133 if ($valgrind) {
134 print VALGRIND "\n\nExecuting: $command\n";
137 $code = &run_command_with_output($logname,$command);
139 # Check to see if we have Purify errors. If so, keep the logfile.
140 # For this to work you need to build with the Purify flag -exit-status=yes
142 if ($pure_log && -f $pure_log) {
143 if ($code & 0x7000) {
144 $code &= ~0x7000;
146 # If we have a purify log, save it
147 $tn = $pure_testname . ($num_of_logfiles ? ".$num_of_logfiles" : "");
148 print("Renaming purify log file to $tn\n") if $debug;
149 rename($pure_log, "$tn")
150 || die "Can't rename $log to $tn: $!\n";
151 ++$purify_errors;
152 } else {
153 unlink($pure_log);
157 if ($code != $expected_code) {
158 print "Error running $make_path (expected $expected_code; got $code): $command\n";
159 $test_passed = 0;
160 # If it's a SIGINT, stop here
161 if ($code & 127) {
162 print STDERR "\nCaught signal ".($code & 127)."!\n";
163 exit($code);
165 return 0;
168 if ($profile & $vos) {
169 system "add_profile $make_path";
175 sub print_usage
177 &print_standard_usage ("run_make_tests", "[-make_path make_pathname]");
180 sub print_help
182 &print_standard_help ("-make_path",
183 "\tYou may specify the pathname of the copy of make to run.");
186 sub get_this_pwd {
187 $delete_command = 'rm -f';
188 if ($has_POSIX) {
189 $__pwd = POSIX::getcwd();
190 } elsif ($vos) {
191 $delete_command = "delete_file -no_ask";
192 $__pwd = `++(current_dir)`;
193 } else {
194 # No idea... just try using pwd as a last resort.
195 chop ($__pwd = `pwd`);
198 return $__pwd;
201 sub set_defaults
203 # $profile = 1;
204 $testee = "GNU make";
205 $make_path = "make";
206 $tmpfilesuffix = "mk";
207 $pwd = &get_this_pwd;
210 sub set_more_defaults
212 local($string);
213 local($index);
215 # Make sure we're in the C locale for those systems that support it,
216 # so sorting, etc. is predictable.
218 $ENV{LANG} = 'C';
220 # find the type of the port. We do this up front to have a single
221 # point of change if it needs to be tweaked.
223 # This is probably not specific enough.
225 if ($osname =~ /Windows/i || $osname =~ /MINGW32/i) {
226 $port_type = 'W32';
228 # Bleah, the osname is so variable on DOS. This kind of bites.
229 # Well, as far as I can tell if we check for some text at the
230 # beginning of the line with either no spaces or a single space, then
231 # a D, then either "OS", "os", or "ev" and a space. That should
232 # match and be pretty specific.
233 elsif ($osname =~ /^([^ ]*|[^ ]* [^ ]*)D(OS|os|ev) /) {
234 $port_type = 'DOS';
236 # Check for OS/2
237 elsif ($osname =~ m%OS/2%) {
238 $port_type = 'OS/2';
240 # Everything else, right now, is UNIX. Note that we should integrate
241 # the VOS support into this as well and get rid of $vos; we'll do
242 # that next time.
243 else {
244 $port_type = 'UNIX';
247 # On DOS/Windows system the filesystem apparently can't track
248 # timestamps with second granularity (!!). Change the sleep time
249 # needed to force a file to be considered "old".
250 $wtime = $port_type eq 'UNIX' ? 1 : $port_type eq 'OS/2' ? 2 : 4;
252 print "Port type: $port_type\n" if $debug;
253 print "Make path: $make_path\n" if $debug;
255 # Find the full pathname of Make. For DOS systems this is more
256 # complicated, so we ask make itself.
257 my $mk = `sh -c 'echo "all:;\@echo \\\$(MAKE)" | $make_path -f-'`;
258 chop $mk;
259 $mk or die "FATAL ERROR: Cannot determine the value of \$(MAKE):\n
260 'echo \"all:;\@echo \\\$(MAKE)\" | $make_path -f-' failed!\n";
261 $make_path = $mk;
262 print "Make\t= `$make_path'\n" if $debug;
264 $string = `$make_path -v -f /dev/null 2> /dev/null`;
266 $string =~ /^(GNU Make [^,\n]*)/;
267 $testee_version = "$1\n";
269 $string = `sh -c "$make_path -f /dev/null 2>&1"`;
270 if ($string =~ /(.*): \*\*\* No targets\. Stop\./) {
271 $make_name = $1;
273 else {
274 if ($make_path =~ /$pathsep([^\n$pathsep]*)$/) {
275 $make_name = $1;
277 else {
278 $make_name = $make_path;
282 # prepend pwd if this is a relative path (ie, does not
283 # start with a slash, but contains one). Thanks for the
284 # clue, Roland.
286 if (index ($make_path, ":") != 1 && index ($make_path, "/") > 0)
288 $mkpath = "$pwd$pathsep$make_path";
290 else
292 $mkpath = $make_path;
295 # Get Purify log info--if any.
297 if (exists $ENV{PURIFYOPTIONS}
298 && $ENV{PURIFYOPTIONS} =~ /.*-logfile=([^ ]+)/) {
299 $pure_log = $1 || '';
300 $pure_log =~ s/%v/$make_name/;
301 $purify_errors = 0;
304 $string = `sh -c "$make_path -j 2 -f /dev/null 2>&1"`;
305 if ($string =~ /not supported/) {
306 $parallel_jobs = 0;
308 else {
309 $parallel_jobs = 1;
312 # Set up for valgrind, if requested.
314 if ($valgrind) {
315 open(VALGRIND, "> valgrind.out")
316 || die "Cannot open valgrind.out: $!\n";
317 # -q --leak-check=yes
318 exists $ENV{VALGRIND_ARGS} and $valgrind_args = $ENV{VALGRIND_ARGS};
319 $make_path = "valgrind --log-fd=".fileno(VALGRIND)." $valgrind_args $make_path";
320 # F_SETFD is 2
321 fcntl(VALGRIND, 2, 0) or die "fcntl(setfd) failed: $!\n";
322 system("echo Starting on `date` 1>&".fileno(VALGRIND));
323 print "Enabled valgrind support.\n";
327 sub setup_for_test
329 $makefile = &get_tmpfile;
330 if (-f $makefile) {
331 unlink $makefile;
334 # Get rid of any Purify logs.
335 if ($pure_log) {
336 ($pure_testname = $testname) =~ tr,/,_,;
337 $pure_testname = "$pure_log.$pure_testname";
338 system("rm -f $pure_testname*");
339 print("Purify testfiles are: $pure_testname*\n") if $debug;
343 exit !&toplevel;