Debian Codegraph @

tests/run_make_tests.pl

raw
1 #!/usr/bin/env perl
2 # -*-perl-*-
3
4 # Test driver for the Make test suite
5
6 # Usage: run_make_tests [testname]
7 # [-debug]
8 # [-help]
9 # [-verbose]
10 # [-keep]
11 # [-make <make prog>]
12 # (and others)
13
14 # Copyright (C) 1992-2020 Free Software Foundation, Inc.
15 # This file is part of GNU Make.
16 #
17 # GNU Make is free software; you can redistribute it and/or modify it under
18 # the terms of the GNU General Public License as published by the Free Software
19 # Foundation; either version 3 of the License, or (at your option) any later
20 # version.
21 #
22 # GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
23 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
24 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
25 # details.
26 #
27 # You should have received a copy of the GNU General Public License along with
28 # this program. If not, see <http://www.gnu.org/licenses/>.
29
30 # Add the working directory to @INC and load the test driver
31 use FindBin;
32 use lib "$FindBin::Bin";
33
34 our $testsroot = $FindBin::Bin;
35
36 require "test_driver.pl";
37
38 use File::Spec;
39
40 use Cwd;
41 $cwdpath = cwd();
42 ($cwdvol, $cwddir, $_) = File::Spec->splitpath($cwdpath, 1);
43
44 # Some target systems might not have the POSIX module...
45 $has_POSIX = eval { require "POSIX.pm" };
46
47 %FEATURES = ();
48
49 $valgrind = 0; # invoke make with valgrind
50 $valgrind_args = '';
51 $memcheck_args = '--num-callers=15 --tool=memcheck --leak-check=full --suppressions=guile.supp';
52 $massif_args = '--num-callers=15 --tool=massif --alloc-fn=xmalloc --alloc-fn=xcalloc --alloc-fn=xrealloc --alloc-fn=xstrdup --alloc-fn=xstrndup';
53 $pure_log = undef;
54
55 # The location of the GNU make source directory
56 $srcdir = '';
57
58 $command_string = '';
59
60 $all_tests = 0;
61
62 # Shell commands
63
64 $sh_name = '/bin/sh';
65 $is_posix_sh = 1;
66
67 $CMD_rmfile = 'rm -f';
68
69 %CONFIG_FLAGS = ();
70
71 # Find the strings that will be generated for various error codes.
72 # We want them from the C locale regardless of our current locale.
73
74 $ERR_no_such_file = undef;
75 $ERR_read_only_file = undef;
76 $ERR_unreadable_file = undef;
77 $ERR_nonexe_file = undef;
78 $ERR_exe_dir = undef;
79
80 {
81 use locale;
82
83 my $loc = undef;
84 if ($has_POSIX) {
85 POSIX->import(qw(locale_h));
86 # Windows has POSIX locale, but only LC_ALL not LC_MESSAGES
87 $loc = POSIX::setlocale(&POSIX::LC_ALL);
88 POSIX::setlocale(&POSIX::LC_ALL, 'C');
89 }
90
91 if (open(my $F, '<', 'file.none')) {
92 print "Opened non-existent file! Skipping related tests.\n";
93 } else {
94 $ERR_no_such_file = "$!";
95 }
96
97 unlink('file.out');
98 touch('file.out');
99
100 chmod(0444, 'file.out');
101 if (open(my $F, '>', 'file.out')) {
102 print "Opened read-only file! Skipping related tests.\n";
103 close($F);
104 } else {
105 $ERR_read_only_file = "$!";
106 }
107
108 $_ = `./file.out 2>/dev/null`;
109 if ($? == 0) {
110 print "Executed non-executable file! Skipping related tests.\n";
111 } else {
112 $ERR_nonexe_file = "$!";
113 }
114
115 $_ = `./. 2>/dev/null`;
116 if ($? == 0) {
117 print "Executed directory! Skipping related tests.\n";
118 } else {
119 $ERR_exe_dir = "$!";
120 }
121
122 chmod(0000, 'file.out');
123 if (open(my $F, '<', 'file.out')) {
124 print "Opened unreadable file! Skipping related tests.\n";
125 close($F);
126 } else {
127 $ERR_unreadable_file = "$!";
128 }
129
130 unlink('file.out') or die "Failed to delete file.out: $!\n";
131
132 $loc and POSIX::setlocale(&POSIX::LC_ALL, $loc);
133 }
134
135 use FindBin;
136 use lib "$FindBin::Bin";
137
138 require "test_driver.pl";
139 require "config-flags.pm";
140
141 # Some target systems might not have the POSIX module...
142 $has_POSIX = eval { require "POSIX.pm" };
143
144 #$SIG{INT} = sub { print STDERR "Caught a signal!\n"; die @_; };
145
146 sub valid_option
147 {
148 local($option) = @_;
149
150 if ($option =~ /^-make([-_]?path)?$/i) {
151 $make_path = shift @argv;
152 if (!-f $make_path) {
153 print "$option $make_path: Not found.\n";
154 exit 0;
155 }
156 return 1;
157 }
158
159 if ($option =~ /^-srcdir$/i) {
160 $srcdir = shift @argv;
161 if (! -f "$srcdir/gnuremake.h") {
162 print "$option $srcdir: Not a valid GNU make source directory.\n";
163 exit 0;
164 }
165 return 1;
166 }
167
168 if ($option =~ /^-all([-_]?tests)?$/i) {
169 $all_tests = 1;
170 return 1;
171 }
172
173 if ($option =~ /^-(valgrind|memcheck)$/i) {
174 $valgrind = 1;
175 $valgrind_args = $memcheck_args;
176 return 1;
177 }
178
179 if ($option =~ /^-massif$/i) {
180 $valgrind = 1;
181 $valgrind_args = $massif_args;
182 return 1;
183 }
184
185 # This doesn't work--it _should_! Someone badly needs to fix this.
186 #
187 # elsif ($option =~ /^-work([-_]?dir)?$/)
188 # {
189 # $workdir = shift @argv;
190 # return 1;
191 # }
192
193 return 0;
194 }
195
196
197 # This is an "all-in-one" function. Arguments are as follows:
198 #
199 # [0] (string): The makefile to be tested. undef means use the last one.
200 # [1] (string): Arguments to pass to make.
201 # [2] (string): Answer we should get back.
202 # [3] (integer): Exit code we expect. A missing code means 0 (success)
203
204 $old_makefile = undef;
205
206 sub subst_make_string
207 {
208 local $_ = shift;
209 $makefile and s/#MAKEFILE#/$makefile/g;
210 s/#MAKEPATH#/$mkpath/g;
211 s/#MAKE#/$make_name/g;
212 s/#PERL#/$perl_name/g;
213 s/#PWD#/$pwd/g;
214 return $_;
215 }
216
217 sub run_make_test
218 {
219 local ($makestring, $options, $answer, $err_code, $timeout) = @_;
220 my @call = caller;
221
222 # If the user specified a makefile string, create a new makefile to contain
223 # it. If the first value is not defined, use the last one (if there is
224 # one).
225
226 if (! defined $makestring) {
227 defined $old_makefile
228 || die "run_make_test(undef) invoked before run_make_test('...')\n";
229 $makefile = $old_makefile;
230 } else {
231 if (! defined($makefile)) {
232 $makefile = &get_tmpfile();
233 }
234
235 # Make sure it ends in a newline and substitute any special tokens.
236 $makestring && $makestring !~ /\n$/s and $makestring .= "\n";
237 $makestring = subst_make_string($makestring);
238
239 # Populate the makefile!
240 open(MAKEFILE, "> $makefile") || die "Failed to open $makefile: $!\n";
241 print MAKEFILE $makestring;
242 close(MAKEFILE) || die "Failed to write $makefile: $!\n";
243 }
244
245 # Do the same processing on $answer as we did on $makestring.
246 if (defined $answer) {
247 $answer && $answer !~ /\n$/s and $answer .= "\n";
248 $answer = subst_make_string($answer);
249 }
250
251 run_make_with_options($makefile, $options, &get_logfile(0),
252 $err_code, $timeout, @call);
253 &compare_output($answer, &get_logfile(1));
254
255 $old_makefile = $makefile;
256 $makefile = undef;
257 }
258
259 sub add_options {
260 my $cmd = shift;
261
262 foreach (@_) {
263 if (ref($cmd)) {
264 push(@$cmd, ref($_) ? @$_ : $_);
265 } else {
266 $cmd .= ' '.(ref($_) ? "@$_" : $_);
267 }
268 }
269
270 return $cmd;
271 }
272
273 sub create_command {
274 return !$_[0] || ref($_[0]) ? [$make_path] : $make_path;
275 }
276
277 # The old-fashioned way...
278 # $options can be a scalar (string) or a ref to an array of options
279 # If it's a scalar the entire argument is passed to system/exec etc. as
280 # a single string. If it's a ref then the array is passed to system/exec.
281 # Using a ref should be preferred as it's more portable but all the older
282 # invocations use strings.
283 sub run_make_with_options {
284 my ($filename,$options,$logname,$expected_code,$timeout,@call) = @_;
285 @call = caller unless @call;
286 my $code;
287 my $command = create_command($options);
288
289 $expected_code = 0 unless defined($expected_code);
290
291 # Reset to reflect this one test.
292 $test_passed = 1;
293
294 if ($filename) {
295 $command = add_options($command, '-f', $filename);
296 }
297
298 if ($options) {
299 if (!ref($options) && $^O eq 'VMS') {
300 # Try to make sure arguments are properly quoted.
301 # This does not handle all cases.
302 # We should convert the tests to use array refs not strings
303
304 # VMS uses double quotes instead of single quotes.
305 $options =~ s/\'/\"/g;
306
307 # If the leading quote is inside non-whitespace, then the
308 # quote must be doubled, because it will be enclosed in another
309 # set of quotes.
310 $options =~ s/(\S)(\".*\")/$1\"$2\"/g;
311
312 # Options must be quoted to preserve case if not already quoted.
313 $options =~ s/(\S+)/\"$1\"/g;
314
315 # Special fixup for embedded quotes.
316 $options =~ s/(\"\".+)\"(\s+)\"(.+\"\")/$1$2$3/g;
317
318 $options =~ s/(\A)(?:\"\")(.+)(?:\"\")/$1\"$2\"/g;
319
320 # Special fixup for misc/general4 test.
321 $options =~ s/""\@echo" "cc""/\@echo cc"/;
322 $options =~ s/"\@echo link"""/\@echo link"/;
323
324 # Remove shell escapes expected to be removed by bash
325 if ($options !~ /path=pre/) {
326 $options =~ s/\\//g;
327 }
328
329 # special fixup for options/eval
330 $options =~ s/"--eval=\$\(info" "eval/"--eval=\$\(info eval/;
331
332 print ("Options fixup = -$options-\n") if $debug;
333 }
334
335 $command = add_options($command, $options);
336 }
337
338 my $cmdstr = ref($command) ? "'".join("' '", @$command)."'" : $command;
339
340 if (@call) {
341 $command_string = "#$call[1]:$call[2]\n$cmdstr\n";
342 } else {
343 $command_string = $cmdstr;
344 }
345
346 if ($valgrind) {
347 print VALGRIND "\n\nExecuting: $cmdstr\n";
348 }
349
350 {
351 my $old_timeout = $test_timeout;
352 $timeout and $test_timeout = $timeout;
353
354 # If valgrind is enabled, turn off the timeout check
355 $valgrind and $test_timeout = 0;
356
357 if (ref($command)) {
358 $code = run_command_with_output($logname, @$command);
359 } else {
360 $code = run_command_with_output($logname, $command);
361 }
362 $test_timeout = $old_timeout;
363 }
364
365 # Check to see if we have Purify errors. If so, keep the logfile.
366 # For this to work you need to build with the Purify flag -exit-status=yes
367
368 if ($pure_log && -f $pure_log) {
369 if ($code & 0x7000) {
370 $code &= ~0x7000;
371
372 # If we have a purify log, save it
373 $tn = $pure_testname . ($num_of_logfiles ? ".$num_of_logfiles" : "");
374 print("Renaming purify log file to $tn\n") if $debug;
375 rename($pure_log, "$tn")
376 || die "Can't rename $log to $tn: $!\n";
377 ++$purify_errors;
378 } else {
379 unlink($pure_log);
380 }
381 }
382
383 if ($code != $expected_code) {
384 print "Error running $make_path (expected $expected_code; got $code): $cmdstr\n";
385 $test_passed = 0;
386 $runf = &get_runfile;
387 &create_file (&get_runfile, $command_string);
388 # If it's a SIGINT, stop here
389 if ($code & 127) {
390 print STDERR "\nCaught signal ".($code & 127)."!\n";
391 ($code & 127) == 2 and exit($code);
392 }
393 return 0;
394 }
395
396 if ($profile & $vos) {
397 system "add_profile $make_path";
398 }
399
400 return 1;
401 }
402
403 sub print_usage
404 {
405 &print_standard_usage ("run_make_tests",
406 "[-make MAKE_PATHNAME] [-srcdir SRCDIR] [-memcheck] [-massif]",);
407 }
408
409 sub print_help
410 {
411 &print_standard_help (
412 "-make",
413 "\tYou may specify the pathname of the copy of make to run.",
414 "-srcdir",
415 "\tSpecify the make source directory.",
416 "-valgrind",
417 "-memcheck",
418 "\tRun the test suite under valgrind's memcheck tool.",
419 "\tChange the default valgrind args with the VALGRIND_ARGS env var.",
420 "-massif",
421 "\tRun the test suite under valgrind's massif toool.",
422 "\tChange the default valgrind args with the VALGRIND_ARGS env var."
423 );
424 }
425
426 sub get_this_pwd {
427 if ($has_POSIX) {
428 $__pwd = POSIX::getcwd();
429 } elsif ($vos) {
430 $__pwd = `++(current_dir)`;
431 } else {
432 # No idea... just try using pwd as a last resort.
433 chop ($__pwd = `pwd`);
434 }
435
436 return $__pwd;
437 }
438
439 sub set_defaults
440 {
441 # $profile = 1;
442 $testee = "GNU make";
443 $make_path = "make";
444 $tmpfilesuffix = "mk";
445 $pwd = &get_this_pwd;
446 }
447
448 sub set_more_defaults
449 {
450 local($string);
451 local($index);
452
453 # On DOS/Windows system the filesystem apparently can't track
454 # timestamps with second granularity (!!). Change the sleep time
455 # needed to force a file to be considered "old".
456 $wtime = $port_type eq 'UNIX' ? 1 : $port_type eq 'OS/2' ? 2 : 4;
457
458 # Find the full pathname of Make. For DOS systems this is more
459 # complicated, so we ask make itself.
460 if ($osname eq 'VMS') {
461 $port_type = 'VMS-DCL' unless defined $ENV{"SHELL"};
462 # On VMS pre-setup make to be found with simply 'make'.
463 $make_path = 'make';
464 } else {
465 create_file('make.mk', 'all:;$(info $(MAKE))');
466 my $mk = `$make_path -sf make.mk`;
467 unlink('make.mk');
468 chop $mk;
469 $mk or die "FATAL ERROR: Cannot determine the value of \$(MAKE)\n";
470 $make_path = $mk;
471 }
472
473 # Ask make what shell to use
474 create_file('shell.mk', 'all:;$(info $(SHELL))');
475 $sh_name = `$make_path -sf shell.mk`;
476 unlink('shell.mk');
477 chop $sh_name;
478 if (! $sh_name) {
479 print "Cannot determine shell\n";
480 $is_posix_sh = 0;
481 } else {
482 my $o = `$sh_name -c ': do nothing' 2>&1`;
483 $is_posix_sh = $? == 0 && $o == '';
484 }
485
486 $string = `$make_path -v`;
487 $string =~ /^(GNU Make [^,\n]*)/ or die "$make_path is not GNU make. Version:\n$string";
488 $testee_version = "$1\n";
489
490 create_file('null.mk', '');
491
492 my $redir = '2>&1';
493 $redir = '' if os_name eq 'VMS';
494 $string = `sh -c "$make_path -f null.mk $redir"`;
495 if ($string =~ /(.*): \*\*\* No targets\. Stop\./) {
496 $make_name = $1;
497 }
498 else {
499 $make_path =~ /^(?:.*$pathsep)?(.+)$/;
500 $make_name = $1;
501 }
502
503 # prepend pwd if this is a relative path (ie, does not
504 # start with a slash, but contains one). Thanks for the
505 # clue, Roland.
506
507 if (index ($make_path, ":") != 1 && index ($make_path, "/") > 0)
508 {
509 $mkpath = "$pwd$pathsep$make_path";
510 }
511 else
512 {
513 $mkpath = $make_path;
514 }
515
516 # If srcdir wasn't provided on the command line, see if the
517 # location of the make program gives us a clue. Don't fail if not;
518 # we'll assume it's been installed into /usr/include or wherever.
519 if (! $srcdir) {
520 $make_path =~ /^(.*$pathsep)?/;
521 my $d = $1 || '../';
522 -f "${d}gnumake.h" and $srcdir = $d;
523 }
524
525 # Not with the make program, so see if we can get it out of the makefile
526 if (! $srcdir && open(MF, "< ../Makefile")) {
527 local $/ = undef;
528 $_ = <MF>;
529 close(MF);
530 /^abs_srcdir\s*=\s*(.*?)\s*$/m;
531 -f "$1/gnumake.h" and $srcdir = $1;
532 }
533
534 # Get Purify log info--if any.
535
536 if (exists $ENV{PURIFYOPTIONS}
537 && $ENV{PURIFYOPTIONS} =~ /.*-logfile=([^ ]+)/) {
538 $pure_log = $1 || '';
539 $pure_log =~ s/%v/$make_name/;
540 $purify_errors = 0;
541 }
542
543 $string = `sh -c "$make_path -j 2 -f null.mk $redir"`;
544 if ($string =~ /not supported/) {
545 $parallel_jobs = 0;
546 }
547 else {
548 $parallel_jobs = 1;
549 }
550
551 unlink('null.mk');
552
553 create_file('features.mk', 'all:;$(info $(.FEATURES))');
554 %FEATURES = map { $_ => 1 } split /\s+/, `$make_path -sf features.mk`;
555 unlink('features.mk');
556
557 # Set up for valgrind, if requested.
558
559 $make_command = $make_path;
560
561 if ($valgrind) {
562 my $args = $valgrind_args;
563 open(VALGRIND, "> valgrind.out")
564 || die "Cannot open valgrind.out: $!\n";
565 # -q --leak-check=yes
566 exists $ENV{VALGRIND_ARGS} and $args = $ENV{VALGRIND_ARGS};
567 $make_path = "valgrind --log-fd=".fileno(VALGRIND)." $args $make_path";
568 # F_SETFD is 2
569 fcntl(VALGRIND, 2, 0) or die "fcntl(setfd) failed: $!\n";
570 system("echo Starting on `date` 1>&".fileno(VALGRIND));
571 print "Enabled valgrind support.\n";
572 }
573
574 if ($debug) {
575 print "Port type: $port_type\n";
576 print "Make path: $make_path\n";
577 print "Shell path: $sh_name".($is_posix_sh ? ' (POSIX)' : '')."\n";
578 print "#PWD#: $pwd\n";
579 print "#PERL#: $perl_name\n";
580 print "#MAKEPATH#: $mkpath\n";
581 print "#MAKE#: $make_name\n";
582 }
583 }
584
585 sub setup_for_test
586 {
587 $makefile = &get_tmpfile;
588 if (-f $makefile) {
589 unlink $makefile;
590 }
591
592 # Get rid of any Purify logs.
593 if ($pure_log) {
594 ($pure_testname = $testname) =~ tr,/,_,;
595 $pure_testname = "$pure_log.$pure_testname";
596 system("rm -f $pure_testname*");
597 print("Purify testfiles are: $pure_testname*\n") if $debug;
598 }
599 }
600
601 exit !&toplevel;