| 1 |
#!/usr/bin/perl -w |
| 2 |
# multidelta; see License.txt for copyright and terms of use |
| 3 |
|
| 4 |
# **************** |
| 5 |
# driver for delta, which works with multiple input files |
| 6 |
# Scott McPeak [email protected] |
| 7 |
|
| 8 |
# the goal here is to be able to minimize in-place: given |
| 9 |
# a fragment of a build process which exhibits some failure, |
| 10 |
# minimize the relevant files without disturbing the build |
| 11 |
# process fragment itself |
| 12 |
|
| 13 |
use strict 'subs'; |
| 14 |
use FindBin; |
| 15 |
|
| 16 |
$delta = "$FindBin::Bin/delta"; |
| 17 |
$topformflat = "$FindBin::Bin/topformflat"; |
| 18 |
$log = "multidelta.log"; |
| 19 |
|
| 20 |
$level = 0; |
| 21 |
$undo = 0; |
| 22 |
$cpp = 0; # bool: use the cpp preprocessor? |
| 23 |
|
| 24 |
if (@ARGV == 0) { |
| 25 |
print(<<"EOF"); |
| 26 |
Multidelta version 2003.7.14 |
| 27 |
|
| 28 |
usage: $0 [options] test-script file [file [...]] |
| 29 |
|
| 30 |
The collection of input files is minimized as much as possible, such |
| 31 |
that the test-script continues to return true. |
| 32 |
|
| 33 |
When the script is run with a source file as an argument, it should do |
| 34 |
any single-file integrity checks on that file. Then, it should |
| 35 |
proceed to check the entire build, on the assumption that all other |
| 36 |
files have already passed their integrity checks. |
| 37 |
|
| 38 |
When the script is run with no arguments, it should check integrity of |
| 39 |
all files and then check the build. |
| 40 |
|
| 41 |
So that the script does not need to have the list of input files |
| 42 |
hard-coded in, the environment variable "multidelta_all_files" is |
| 43 |
always set to a space-separated list of the filenames no matter how |
| 44 |
the script is run. |
| 45 |
|
| 46 |
Options: |
| 47 |
-level=n When topformflattening, flatten to level n [$level]. |
| 48 |
-u Undo the last invocation, by copying the *.bak files |
| 49 |
onto the original copies. |
| 50 |
-cpp Before flattening run through the cpp preprocessor. |
| 51 |
|
| 52 |
EOF |
| 53 |
exit(0); |
| 54 |
} |
| 55 |
|
| 56 |
die "$topformflat doesn't exit or isn't executable. Did you run 'make'?\n" |
| 57 |
unless (-x $topformflat); |
| 58 |
|
| 59 |
while ($ARGV[0] =~ m"^-") { |
| 60 |
if ($ARGV[0] =~ m"^-level=") { |
| 61 |
($level) = ($ARGV[0] =~ m"=(\d+)"); |
| 62 |
} |
| 63 |
elsif ($ARGV[0] eq "-u") { |
| 64 |
$undo = 1; |
| 65 |
} |
| 66 |
elsif ($ARGV[0] eq "-cpp") { |
| 67 |
$cpp = 1; |
| 68 |
} |
| 69 |
else { |
| 70 |
die ("unknown option: $ARGV[0]\n"); |
| 71 |
} |
| 72 |
shift(@ARGV); |
| 73 |
} |
| 74 |
|
| 75 |
$script = $ARGV[0]; |
| 76 |
shift(@ARGV); |
| 77 |
@files = @ARGV; |
| 78 |
for my $f(@files) { |
| 79 |
die "Input file contains a space '$f'\n" |
| 80 |
if $f=~/ /; # multidelta_all_files won't work if filenames contain spaces. |
| 81 |
} |
| 82 |
# So the script knows all the files even if it is called in a mode |
| 83 |
# where not all of them are passed in. |
| 84 |
$ENV{"multidelta_all_files"} = join(" ", @files); |
| 85 |
|
| 86 |
|
| 87 |
if ($undo) { |
| 88 |
# copy the .bak files back |
| 89 |
for $fn (@files) { |
| 90 |
diagnostic("reverting to backup copy of $fn"); |
| 91 |
run("cp -f ${fn}.bak $fn"); |
| 92 |
} |
| 93 |
exit(0); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
diagnostic("verify the script passes now"); |
| 98 |
if (simpleRun($script) != 0) { |
| 99 |
die "Initial test fails\n"; |
| 100 |
} |
| 101 |
|
| 102 |
# for every input file, make a backup copy, and produce |
| 103 |
# another version with all comments and preprocessor |
| 104 |
# directives stripped, and toplevel forms flattened |
| 105 |
for $fn (@files) { |
| 106 |
diagnostic("making a backup copy of $fn"); |
| 107 |
run("cp -f $fn ${fn}.bak"); |
| 108 |
|
| 109 |
if ($cpp) { |
| 110 |
diagnostic("preprocessing and flattening $fn"); |
| 111 |
# -P: don't emit #line directives |
| 112 |
run("cpp -P ${fn}.bak | $topformflat $level >$fn"); |
| 113 |
} else { |
| 114 |
diagnostic("flattening $fn"); |
| 115 |
run("$topformflat $level <${fn}.bak >$fn"); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
diagnostic("verify still passes script"); |
| 120 |
if (simpleRun($script) != 0) { |
| 121 |
$actions = "topformflat"; |
| 122 |
if ($cpp) { |
| 123 |
$actions = "cpp and " . $actions; |
| 124 |
} |
| 125 |
die "Test fails after $actions\n"; |
| 126 |
} |
| 127 |
|
| 128 |
# one by one, apply delta |
| 129 |
for $fn (@files) { |
| 130 |
diagnostic("applying delta to $fn"); |
| 131 |
run("$delta -in_place -test=$script $fn"); |
| 132 |
|
| 133 |
if (-f "DELTA-STOP") { |
| 134 |
diagnostic("Stopping because DELTA-STOP exists"); |
| 135 |
exit 1; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
print("multidelta is finished\n"); |
| 140 |
exit(0); |
| 141 |
|
| 142 |
|
| 143 |
# -------------------- subroutines -------------------- |
| 144 |
sub diagnostic { |
| 145 |
print(@_, "\n"); |
| 146 |
|
| 147 |
open (LOG, ">>$log"); |
| 148 |
print LOG (@_, "\n"); |
| 149 |
close (LOG); |
| 150 |
} |
| 151 |
|
| 152 |
sub simpleRun { |
| 153 |
my $ret = system(@_); |
| 154 |
# from perldoc -f system |
| 155 |
my $signal = $ret & 127; |
| 156 |
my $exitValue = $ret >> 8; |
| 157 |
if ($signal) { |
| 158 |
die "$0 exiting due to signal $signal\n"; |
| 159 |
} |
| 160 |
return $exitValue; |
| 161 |
} |
| 162 |
|
| 163 |
sub run { |
| 164 |
diagnostic(@_); |
| 165 |
my $exitValue = simpleRun(@_); |
| 166 |
if ($exitValue) { |
| 167 |
die "stopping because delta exited with exit value $exitValue: " . join(' ', @_) . "\n"; |
| 168 |
} |
| 169 |
} |