Fix bug #10252: Remove any trailing slashes from -C arguments (WINDOWS32).
[make/kirr.git] / tests / run_make_tests.pl
blob9b9104f02565675050fcf9cd19d8317274a48ae2
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
16 require "test_driver.pl";
18 #$SIG{INT} = sub { print STDERR "Caught a signal!\n"; die @_; };
20 sub valid_option
22 local($option) = @_;
24 if ($option =~ /^-make([-_]?path)?$/)
26 $make_path = shift @argv;
27 if (!-f $make_path)
29 print "$option $make_path: Not found.\n";
30 exit 0;
32 return 1;
35 if ($option =~ /^-valgrind$/i) {
36 $valgrind = 1;
37 return 1;
40 # This doesn't work--it _should_! Someone needs to fix this badly.
42 # elsif ($option =~ /^-work([-_]?dir)?$/)
43 # {
44 # $workdir = shift @argv;
45 # return 1;
46 # }
48 return 0;
52 # This is an "all-in-one" function. Arguments are as follows:
54 # [0] (string): The makefile to be tested. undef means use the last one.
55 # [1] (string): Arguments to pass to make.
56 # [2] (string): Answer we should get back.
57 # [3] (integer): Exit code we expect. A missing code means 0 (success)
59 $old_makefile = undef;
61 sub run_make_test
63 local ($makestring, $options, $answer, $err_code) = @_;
65 # If the user specified a makefile string, create a new makefile to contain
66 # it. If the first value is not defined, use the last one (if there is
67 # one).
69 if (! defined $makestring) {
70 defined $old_makefile
71 || die "run_make_test(undef) invoked before run_make_test('...')\n";
72 $makefile = $old_makefile;
73 } else {
74 if (! defined($makefile)) {
75 $makefile = &get_tmpfile();
78 # Make sure it ends in a newline.
79 $makestring && $makestring !~ /\n$/s and $makestring .= "\n";
81 # Replace @MAKEFILE@ with the makefile name and @MAKE@ with the path to
82 # make
83 $makestring =~ s/#MAKEFILE#/$makefile/g;
84 $makestring =~ s/#MAKE#/$make_name/g;
86 # Populate the makefile!
87 open(MAKEFILE, "> $makefile") || die "Failed to open $makefile: $!\n";
88 print MAKEFILE $makestring;
89 close(MAKEFILE) || die "Failed to write $makefile: $!\n";
92 # Do the same processing on $answer as we did on $makestring.
94 $answer && $answer !~ /\n$/s and $answer .= "\n";
95 $answer =~ s/#MAKEFILE#/$makefile/g;
96 $answer =~ s/#MAKE#/$make_name/g;
98 &run_make_with_options($makefile, $options, &get_logfile(0), $err_code);
99 &compare_output($answer, &get_logfile(1));
101 $old_makefile = $makefile;
102 $makefile = undef;
105 # The old-fashioned way...
106 sub run_make_with_options {
107 local ($filename,$options,$logname,$expected_code) = @_;
108 local($code);
109 local($command) = $make_path;
111 $expected_code = 0 unless defined($expected_code);
113 # Reset to reflect this one test.
114 $test_passed = 1;
116 if ($filename) {
117 $command .= " -f $filename";
120 if ($options) {
121 $command .= " $options";
124 if ($valgrind) {
125 print VALGRIND "\n\nExecuting: $command\n";
128 $code = &run_command_with_output($logname,$command);
130 # Check to see if we have Purify errors. If so, keep the logfile.
131 # For this to work you need to build with the Purify flag -exit-status=yes
133 if ($pure_log && -f $pure_log) {
134 if ($code & 0x7000) {
135 $code &= ~0x7000;
137 # If we have a purify log, save it
138 $tn = $pure_testname . ($num_of_logfiles ? ".$num_of_logfiles" : "");
139 print("Renaming purify log file to $tn\n") if $debug;
140 rename($pure_log, "$tn")
141 || die "Can't rename $log to $tn: $!\n";
142 ++$purify_errors;
143 } else {
144 unlink($pure_log);
148 if ($code != $expected_code) {
149 print "Error running $make_path (expected $expected_code; got $code): $command\n";
150 $test_passed = 0;
151 # If it's a SIGINT, stop here
152 if ($code & 127) {
153 print STDERR "\nCaught signal ".($code & 127)."!\n";
154 exit($code);
156 return 0;
159 if ($profile & $vos) {
160 system "add_profile $make_path";
166 sub print_usage
168 &print_standard_usage ("run_make_tests", "[-make_path make_pathname]");
171 sub print_help
173 &print_standard_help ("-make_path",
174 "\tYou may specify the pathname of the copy of make to run.");
177 sub get_this_pwd {
178 if ($vos) {
179 $delete_command = "delete_file";
180 $__pwd = `++(current_dir)`;
182 else {
183 $delete_command = "rm";
184 chop ($__pwd = `pwd`);
187 return $__pwd;
190 sub set_defaults
192 # $profile = 1;
193 $testee = "GNU make";
194 $make_path = "make";
195 $tmpfilesuffix = "mk";
196 $pwd = &get_this_pwd;
199 sub set_more_defaults
201 local($string);
202 local($index);
204 # Make sure we're in the C locale for those systems that support it,
205 # so sorting, etc. is predictable.
207 $ENV{LANG} = 'C';
209 # find the type of the port. We do this up front to have a single
210 # point of change if it needs to be tweaked.
212 # This is probably not specific enough.
214 if ($osname =~ /Windows/i) {
215 $port_type = 'W32';
217 # Bleah, the osname is so variable on DOS. This kind of bites.
218 # Well, as far as I can tell if we check for some text at the
219 # beginning of the line with either no spaces or a single space, then
220 # a D, then either "OS", "os", or "ev" and a space. That should
221 # match and be pretty specific.
222 elsif ($osname =~ /^([^ ]*|[^ ]* [^ ]*)D(OS|os|ev) /) {
223 $port_type = 'DOS';
225 # Check for OS/2
226 elsif ($osname =~ m%OS/2%) {
227 $port_type = 'OS/2';
229 # Everything else, right now, is UNIX. Note that we should integrate
230 # the VOS support into this as well and get rid of $vos; we'll do
231 # that next time.
232 else {
233 $port_type = 'UNIX';
236 # On DOS/Windows system the filesystem apparently can't track
237 # timestamps with second granularity (!!). Change the sleep time
238 # needed to force a file to be considered "old".
240 $wtime = $port_type eq 'UNIX' ? 1 : $port_type eq 'OS/2' ? 2 : 4;
242 # Find the full pathname of Make. For DOS systems this is more
243 # complicated, so we ask make itself.
245 $make_path = `sh -c 'echo "all:;\@echo \\\$(MAKE)" | $make_path -f-'`;
246 chop $make_path;
247 print "Make\t= `$make_path'\n" if $debug;
249 $string = `$make_path -v -f /dev/null 2> /dev/null`;
251 $string =~ /^(GNU Make [^,\n]*)/;
252 $testee_version = "$1\n";
254 $string = `sh -c "$make_path -f /dev/null 2>&1"`;
255 if ($string =~ /(.*): \*\*\* No targets\. Stop\./) {
256 $make_name = $1;
258 else {
259 if ($make_path =~ /$pathsep([^\n$pathsep]*)$/) {
260 $make_name = $1;
262 else {
263 $make_name = $make_path;
267 # prepend pwd if this is a relative path (ie, does not
268 # start with a slash, but contains one). Thanks for the
269 # clue, Roland.
271 if (index ($make_path, ":") != 1 && index ($make_path, "/") > 0)
273 $mkpath = "$pwd$pathsep$make_path";
275 else
277 $mkpath = $make_path;
280 # Get Purify log info--if any.
282 $ENV{PURIFYOPTIONS} =~ /.*-logfile=([^ ]+)/;
283 $pure_log = $1 || '';
284 $pure_log =~ s/%v/$make_name/;
285 $purify_errors = 0;
287 $string = `sh -c "$make_path -j 2 -f /dev/null 2>&1"`;
288 if ($string =~ /not supported/) {
289 $parallel_jobs = 0;
291 else {
292 $parallel_jobs = 1;
295 # Set up for valgrind, if requested.
297 if ($valgrind) {
298 # use POSIX qw(:fcntl_h);
299 # require Fcntl;
300 open(VALGRIND, "> valgrind.out")
301 || die "Cannot open valgrind.out: $!\n";
302 # -q --leak-check=yes
303 $make_path = "valgrind --num-callers=15 --logfile-fd=".fileno(VALGRIND)." $make_path";
304 # F_SETFD is 2
305 fcntl(VALGRIND, 2, 0) or die "fcntl(setfd) failed: $!\n";
306 system("echo Starting on `date` 1>&".fileno(VALGRIND));
307 print "Enabled valgrind support.\n";
311 sub setup_for_test
313 $makefile = &get_tmpfile;
314 if (-f $makefile) {
315 unlink $makefile;
318 # Get rid of any Purify logs.
319 if ($pure_log) {
320 ($pure_testname = $testname) =~ tr,/,_,;
321 $pure_testname = "$pure_log.$pure_testname";
322 system("rm -f $pure_testname*");
323 print("Purify testfiles are: $pure_testname*\n") if $debug;
327 exit !&toplevel;