1#!/usr/bin/perl -w
2use strict;
3use File::Temp qw/ tempdir /;
4my $prog = "reducer";
5
6die "$prog <code file> <error string> [optional command]\n" if ($#ARGV < 0);
7my $file = shift @ARGV;
8die "$prog: [error] cannot read file $file\n" if (! -r $file);
9
10my $magic = shift @ARGV;
11die "$prog: [error] no error string specified\n" if (! defined $magic);
12
13# Create a backup of the file.
14my $dir = tempdir( CLEANUP => 1 );
15print "$prog: created temporary directory '$dir'\n";
16my $srcFile = "$dir/$file";
17`cp $file $srcFile`;
18
19# Create the script.
20my $scriptFile = "$dir/script";
21open(OUT, ">$scriptFile") or die "$prog: cannot create '$scriptFile'\n";
22my $reduceOut = "$dir/reduceOut";
23
24my $command;
25if (scalar(@ARGV) > 0) { $command = \@ARGV; }
26else {
27  my $compiler = "clang";
28  $command = [$compiler, "-fsyntax-only", "-Wfatal-errors", "-Wno-deprecated-declarations", "-Wimplicit-function-declaration"];
29}
30push @$command, $srcFile;
31my $commandStr = "@$command";
32
33print OUT <<ENDTEXT;
34#!/usr/bin/perl -w
35use strict;
36my \$BAD = 1;
37my \$GOOD = 0;
38`rm -f $reduceOut`;
39my \$command = "$commandStr > $reduceOut 2>&1";
40system(\$command);
41open(IN, "$reduceOut") or exit(\$BAD);
42my \$found = 0;
43while(<IN>) {
44  if (/$magic/) { exit \$GOOD; }
45}
46exit \$BAD;
47ENDTEXT
48close(OUT);
49`chmod +x $scriptFile`;
50
51print "$prog: starting reduction\n";
52sub multidelta($) {
53    my ($level) = @_;
54    system("multidelta -level=$level $scriptFile $srcFile");
55}
56
57for (my $i = 1 ; $i <= 5; $i++) {
58  foreach my $level (0,0,1,1,2,2,10) {
59    multidelta($level);
60  }
61}
62
63# Copy the final file.
64`cp $srcFile $file.reduced`;
65print "$prog: generated '$file.reduced";
66