Imported Upstream version 4.0
[platform/upstream/make.git] / tests / run_make_tests.pl
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-2013 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 %FEATURES = ();
31
32 $valgrind = 0;              # invoke make with valgrind
33 $valgrind_args = '';
34 $memcheck_args = '--num-callers=15 --tool=memcheck --leak-check=full --suppressions=guile.supp';
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;
37
38 # The location of the GNU make source directory
39 $srcdir = '';
40
41 $command_string = '';
42
43 $all_tests = 0;
44
45 require "test_driver.pl";
46
47 # Some target systems might not have the POSIX module...
48 $has_POSIX = eval { require "POSIX.pm" };
49
50 #$SIG{INT} = sub { print STDERR "Caught a signal!\n"; die @_; };
51
52 sub valid_option
53 {
54    local($option) = @_;
55
56    if ($option =~ /^-make([-_]?path)?$/i) {
57        $make_path = shift @argv;
58        if (!-f $make_path) {
59            print "$option $make_path: Not found.\n";
60            exit 0;
61        }
62        return 1;
63    }
64
65    if ($option =~ /^-srcdir$/i) {
66        $srcdir = shift @argv;
67        if (! -f "$srcdir/gnumake.h") {
68            print "$option $srcdir: Not a valid GNU make source directory.\n";
69            exit 0;
70        }
71        return 1;
72    }
73
74    if ($option =~ /^-all([-_]?tests)?$/i) {
75        $all_tests = 1;
76        return 1;
77    }
78
79    if ($option =~ /^-(valgrind|memcheck)$/i) {
80        $valgrind = 1;
81        $valgrind_args = $memcheck_args;
82        return 1;
83    }
84
85    if ($option =~ /^-massif$/i) {
86        $valgrind = 1;
87        $valgrind_args = $massif_args;
88        return 1;
89    }
90
91 # This doesn't work--it _should_!  Someone badly needs to fix this.
92 #
93 #   elsif ($option =~ /^-work([-_]?dir)?$/)
94 #   {
95 #      $workdir = shift @argv;
96 #      return 1;
97 #   }
98
99    return 0;
100 }
101
102
103 # This is an "all-in-one" function.  Arguments are as follows:
104 #
105 #  [0] (string):  The makefile to be tested.  undef means use the last one.
106 #  [1] (string):  Arguments to pass to make.
107 #  [2] (string):  Answer we should get back.
108 #  [3] (integer): Exit code we expect.  A missing code means 0 (success)
109
110 $old_makefile = undef;
111
112 sub subst_make_string
113 {
114     local $_ = shift;
115     $makefile and s/#MAKEFILE#/$makefile/g;
116     s/#MAKEPATH#/$mkpath/g;
117     s/#MAKE#/$make_name/g;
118     s/#PERL#/$perl_name/g;
119     s/#PWD#/$pwd/g;
120     return $_;
121 }
122
123 sub run_make_test
124 {
125   local ($makestring, $options, $answer, $err_code, $timeout) = @_;
126
127   # If the user specified a makefile string, create a new makefile to contain
128   # it.  If the first value is not defined, use the last one (if there is
129   # one).
130
131   if (! defined $makestring) {
132     defined $old_makefile
133       || die "run_make_test(undef) invoked before run_make_test('...')\n";
134     $makefile = $old_makefile;
135   } else {
136     if (! defined($makefile)) {
137       $makefile = &get_tmpfile();
138     }
139
140     # Make sure it ends in a newline and substitute any special tokens.
141     $makestring && $makestring !~ /\n$/s and $makestring .= "\n";
142     $makestring = subst_make_string($makestring);
143
144     # Populate the makefile!
145     open(MAKEFILE, "> $makefile") || die "Failed to open $makefile: $!\n";
146     print MAKEFILE $makestring;
147     close(MAKEFILE) || die "Failed to write $makefile: $!\n";
148   }
149
150   # Do the same processing on $answer as we did on $makestring.
151   if (defined $answer) {
152       $answer && $answer !~ /\n$/s and $answer .= "\n";
153       $answer = subst_make_string($answer);
154   }
155
156   run_make_with_options($makefile, $options, &get_logfile(0),
157                         $err_code, $timeout);
158   &compare_output($answer, &get_logfile(1));
159
160   $old_makefile = $makefile;
161   $makefile = undef;
162 }
163
164 # The old-fashioned way...
165 sub run_make_with_options {
166   local ($filename,$options,$logname,$expected_code,$timeout) = @_;
167   local($code);
168   local($command) = $make_path;
169
170   $expected_code = 0 unless defined($expected_code);
171
172   # Reset to reflect this one test.
173   $test_passed = 1;
174
175   if ($filename) {
176     $command .= " -f $filename";
177   }
178
179   if ($options) {
180     $command .= " $options";
181   }
182
183   $command_string = "$command\n";
184
185   if ($valgrind) {
186     print VALGRIND "\n\nExecuting: $command\n";
187   }
188
189
190   {
191       my $old_timeout = $test_timeout;
192       $timeout and $test_timeout = $timeout;
193
194       # If valgrind is enabled, turn off the timeout check
195       $valgrind and $test_timeout = 0;
196
197       $code = &run_command_with_output($logname,$command);
198
199       $test_timeout = $old_timeout;
200   }
201
202   # Check to see if we have Purify errors.  If so, keep the logfile.
203   # For this to work you need to build with the Purify flag -exit-status=yes
204
205   if ($pure_log && -f $pure_log) {
206     if ($code & 0x7000) {
207       $code &= ~0x7000;
208
209       # If we have a purify log, save it
210       $tn = $pure_testname . ($num_of_logfiles ? ".$num_of_logfiles" : "");
211       print("Renaming purify log file to $tn\n") if $debug;
212       rename($pure_log, "$tn")
213         || die "Can't rename $log to $tn: $!\n";
214       ++$purify_errors;
215     } else {
216       unlink($pure_log);
217     }
218   }
219
220   if ($code != $expected_code) {
221     print "Error running $make_path (expected $expected_code; got $code): $command\n";
222     $test_passed = 0;
223     $runf = &get_runfile;
224     &create_file (&get_runfile, $command_string);
225     # If it's a SIGINT, stop here
226     if ($code & 127) {
227       print STDERR "\nCaught signal ".($code & 127)."!\n";
228       ($code & 127) == 2 and exit($code);
229     }
230     return 0;
231   }
232
233   if ($profile & $vos) {
234     system "add_profile $make_path";
235   }
236
237   return 1;
238 }
239
240 sub print_usage
241 {
242    &print_standard_usage ("run_make_tests",
243                           "[-make MAKE_PATHNAME] [-srcdir SRCDIR] [-memcheck] [-massif]",);
244 }
245
246 sub print_help
247 {
248    &print_standard_help (
249         "-make",
250         "\tYou may specify the pathname of the copy of make to run.",
251         "-srcdir",
252         "\tSpecify the make source directory.",
253         "-valgrind",
254         "-memcheck",
255         "\tRun the test suite under valgrind's memcheck tool.",
256         "\tChange the default valgrind args with the VALGRIND_ARGS env var.",
257         "-massif",
258         "\tRun the test suite under valgrind's massif toool.",
259         "\tChange the default valgrind args with the VALGRIND_ARGS env var."
260        );
261 }
262
263 sub get_this_pwd {
264   $delete_command = 'rm -f';
265   if ($has_POSIX) {
266     $__pwd = POSIX::getcwd();
267   } elsif ($vos) {
268     $delete_command = "delete_file -no_ask";
269     $__pwd = `++(current_dir)`;
270   } else {
271     # No idea... just try using pwd as a last resort.
272     chop ($__pwd = `pwd`);
273   }
274
275   return $__pwd;
276 }
277
278 sub set_defaults
279 {
280    # $profile = 1;
281    $testee = "GNU make";
282    $make_path = "make";
283    $tmpfilesuffix = "mk";
284    $pwd = &get_this_pwd;
285 }
286
287 sub set_more_defaults
288 {
289    local($string);
290    local($index);
291
292    # find the type of the port.  We do this up front to have a single
293    # point of change if it needs to be tweaked.
294    #
295    # This is probably not specific enough.
296    #
297    if ($osname =~ /Windows/i || $osname =~ /MINGW32/i || $osname =~ /CYGWIN_NT/i) {
298      $port_type = 'W32';
299    }
300    # Bleah, the osname is so variable on DOS.  This kind of bites.
301    # Well, as far as I can tell if we check for some text at the
302    # beginning of the line with either no spaces or a single space, then
303    # a D, then either "OS", "os", or "ev" and a space.  That should
304    # match and be pretty specific.
305    elsif ($osname =~ /^([^ ]*|[^ ]* [^ ]*)D(OS|os|ev) /) {
306      $port_type = 'DOS';
307    }
308    # Check for OS/2
309    elsif ($osname =~ m%OS/2%) {
310      $port_type = 'OS/2';
311    }
312    # Everything else, right now, is UNIX.  Note that we should integrate
313    # the VOS support into this as well and get rid of $vos; we'll do
314    # that next time.
315    else {
316      $port_type = 'UNIX';
317    }
318
319    # On DOS/Windows system the filesystem apparently can't track
320    # timestamps with second granularity (!!).  Change the sleep time
321    # needed to force a file to be considered "old".
322    $wtime = $port_type eq 'UNIX' ? 1 : $port_type eq 'OS/2' ? 2 : 4;
323
324    print "Port type: $port_type\n" if $debug;
325    print "Make path: $make_path\n" if $debug;
326
327    # Find the full pathname of Make.  For DOS systems this is more
328    # complicated, so we ask make itself.
329    my $mk = `sh -c 'echo "all:;\@echo \\\$(MAKE)" | $make_path -f-'`;
330    chop $mk;
331    $mk or die "FATAL ERROR: Cannot determine the value of \$(MAKE):\n
332 'echo \"all:;\@echo \\\$(MAKE)\" | $make_path -f-' failed!\n";
333    $make_path = $mk;
334    print "Make\t= '$make_path'\n" if $debug;
335
336    $string = `$make_path -v -f /dev/null 2> /dev/null`;
337
338    $string =~ /^(GNU Make [^,\n]*)/;
339    $testee_version = "$1\n";
340
341    $string = `sh -c "$make_path -f /dev/null 2>&1"`;
342    if ($string =~ /(.*): \*\*\* No targets\.  Stop\./) {
343      $make_name = $1;
344    }
345    else {
346      $make_path =~ /^(?:.*$pathsep)?(.+)$/;
347      $make_name = $1;
348    }
349
350    # prepend pwd if this is a relative path (ie, does not
351    # start with a slash, but contains one).  Thanks for the
352    # clue, Roland.
353
354    if (index ($make_path, ":") != 1 && index ($make_path, "/") > 0)
355    {
356       $mkpath = "$pwd$pathsep$make_path";
357    }
358    else
359    {
360       $mkpath = $make_path;
361    }
362
363    # If srcdir wasn't provided on the command line, see if the
364    # location of the make program gives us a clue.  Don't fail if not;
365    # we'll assume it's been installed into /usr/include or wherever.
366    if (! $srcdir) {
367        $make_path =~ /^(.*$pathsep)?/;
368        my $d = $1 || '../';
369        -f "${d}gnumake.h" and $srcdir = $d;
370    }
371
372    # Not with the make program, so see if we can get it out of the makefile
373    if (! $srcdir && open(MF, "< ../Makefile")) {
374        local $/ = undef;
375        $_ = <MF>;
376        close(MF);
377        /^abs_srcdir\s*=\s*(.*?)\s*$/m;
378        -f "$1/gnumake.h" and $srcdir = $1;
379    }
380
381    # Get Purify log info--if any.
382
383    if (exists $ENV{PURIFYOPTIONS}
384        && $ENV{PURIFYOPTIONS} =~ /.*-logfile=([^ ]+)/) {
385      $pure_log = $1 || '';
386      $pure_log =~ s/%v/$make_name/;
387      $purify_errors = 0;
388    }
389
390    $string = `sh -c "$make_path -j 2 -f /dev/null 2>&1"`;
391    if ($string =~ /not supported/) {
392      $parallel_jobs = 0;
393    }
394    else {
395      $parallel_jobs = 1;
396    }
397
398    %FEATURES = map { $_ => 1 } split /\s+/, `sh -c "echo '\\\$(info \\\$(.FEATURES))' | $make_path -f- 2>/dev/null"`;
399
400    # Set up for valgrind, if requested.
401
402    if ($valgrind) {
403      my $args = $valgrind_args;
404      open(VALGRIND, "> valgrind.out")
405        || die "Cannot open valgrind.out: $!\n";
406      #  -q --leak-check=yes
407      exists $ENV{VALGRIND_ARGS} and $args = $ENV{VALGRIND_ARGS};
408      $make_path = "valgrind --log-fd=".fileno(VALGRIND)." $args $make_path";
409      # F_SETFD is 2
410      fcntl(VALGRIND, 2, 0) or die "fcntl(setfd) failed: $!\n";
411      system("echo Starting on `date` 1>&".fileno(VALGRIND));
412      print "Enabled valgrind support.\n";
413    }
414 }
415
416 sub setup_for_test
417 {
418   $makefile = &get_tmpfile;
419   if (-f $makefile) {
420     unlink $makefile;
421   }
422
423   # Get rid of any Purify logs.
424   if ($pure_log) {
425     ($pure_testname = $testname) =~ tr,/,_,;
426     $pure_testname = "$pure_log.$pure_testname";
427     system("rm -f $pure_testname*");
428     print("Purify testfiles are: $pure_testname*\n") if $debug;
429   }
430 }
431
432 exit !&toplevel;