Debian Codegraph @

scripts/localize_headers

raw
1 #!/usr/bin/perl -w
2 ##
3 ## Copyright (c) 2015 The University of Utah
4 ## All rights reserved.
5 ##
6 ## This file is distributed under the University of Illinois Open Source
7 ## License. See the file COPYING for details.
8
9 ###############################################################################
10
11 use strict;
12 use File::Spec;
13 use File::Copy;
14 use File::Basename;
15 use File::Compare;
16 use Cwd;
17
18 # TODO might need to support __has_include_next, __has_include
19
20 sub usage() {
21 print "usage: $0 [-debug] compiler 'compiler args' source-file\n";
22 print "the file should not be in the cwd\n";
23 print "the file will be assumed to be C++ unless it ends with .c\n";
24 exit(-1);
25 }
26
27 my $debug = 0;
28 if ($ARGV[0] eq "-debug") {
29 shift @ARGV;
30 $debug = 1;
31 }
32
33 usage() unless scalar(@ARGV)==3;
34 my $COMP = $ARGV[0];
35 my $CFLAGS = $ARGV[1];
36 my $FILE = $ARGV[2];
37
38 # TODO bail if file is in cwd
39
40 my $EXT;
41 my $CPP;
42 if ($FILE =~ /\.c$/) {
43 $EXT = ".c";
44 $CPP = "";
45 print "looks like '$FILE' is C code\n";
46 } else {
47 $EXT = ".cpp";
48 $CPP = "-x c++";
49 print "looks like '$FILE' is C++ code (if not you'll need to edit this script)\n";
50 }
51
52 my @exts = (".c", ".cpp");
53
54 sub runit ($) {
55 my $cmd = shift;
56 print "[ $cmd ]\n";
57 die if ((system "$cmd") == -1);
58 my $exit_value = $? >> 8;
59 return $exit_value;
60 }
61
62 my @includes;
63 my %deps;
64 my $srcfile_copy;
65
66 # get the include path
67 open INF, "echo | $COMP $CFLAGS -v $CPP -E - 2>&1 |" or die;
68 my $go = 0;
69 print "compiler search path:\n";
70 while (my $line = <INF>) {
71 chomp $line;
72 if ($line eq "#include <...> search starts here:") {
73 $go = 1;
74 next;
75 }
76 if ($line eq "End of search list.") {
77 $go = 0;
78 next;
79 }
80 if ($go) {
81 $line =~ s/^\s*//;
82 $line =~ s/\s*$//;
83 # WHY DO YOU DO THIS, OS X?
84 $line =~ s/ \(framework directory\)$//;
85 my $dir = Cwd::realpath($line);
86 print " $line\n";
87 die "oops '$line'" unless -d $line;
88 push @includes, $line;
89 next;
90 }
91 }
92 close INF;
93
94 # get the list of dependencies
95 open INF2, "$COMP $CFLAGS -c -w $CPP $FILE -M |" or die;
96 my $base = basename($FILE, @exts);
97 open OF, ">orig_deps.txt" or die;
98 while (my $f = <INF2>) {
99 chomp $f;
100 $f =~ s/$base\.o://g;
101 $f =~ s/^\s*//;
102 $f =~ s/\s*\\\s*//g;
103 my @l = split /\s+/, $f;
104 foreach my $g (@l) {
105 print OF "$g ";
106 # assume the actual src file is the first dependency
107 if (!defined($srcfile_copy)) {
108 $srcfile_copy = basename($g);
109 }
110 die unless -f $g;
111 my $r = Cwd::realpath($g);
112 die unless -f $r;
113 # this hash needs to contain canonical paths
114 $deps{$r} = 1;
115 }
116 }
117 close INF2;
118 close OF;
119
120 my %map;
121 my %rmap;
122
123 sub substitute($$$$) {
124 (my $file, my $lref, my $next, my $line) = @_;
125 my @list = @{$lref};
126
127 print " we're trying to localize '$file'\n" if $debug;
128 print " looking for next after $next\n" if $debug && $next ne "";
129
130 foreach my $dir (@list) {
131 my $path = File::Spec->catfile($dir, $file);
132 print " concatenating '$dir' + '$file'\n" if $debug;
133 print " to get '$path'\n" if $debug;
134 if (-e $path) {
135 if ($next ne "" && $path eq $next) {
136 $next = "";
137 } else {
138 $path = Cwd::realpath($path);
139 if (exists $map{$path}) {
140 my $f = $map{$path};
141 print " found: -> '$dir' '$f'\n" if $debug;
142 print OUTF "#pragma \"$path\"\n";
143 print OUTF "#include \"$f\"";
144 print OUTF "\n";
145 return;
146 }
147 }
148 }
149 }
150 print "not found '$file'\n" if $debug;
151 print OUTF "#error localize_headers should not have tried to include $file\n";
152 }
153
154 # make a local copy of a file with its include paths munged to refer
155 # to other copies -- when this is finished there should be no includes
156 # outside of the cwd
157 sub rewrite($$) {
158 (my $absfile, my $target) = @_;
159 open INF, "<$absfile" or die;
160 open OUTF, ">$target" or die;
161 my $n = 0;
162 my($fnxxx, $dir, $suffixxx) = fileparse($absfile);
163 my @dotincludes = @includes;
164 unshift @dotincludes, $dir;
165 while (my $line = <INF>) {
166 chomp $line;
167 $n++;
168 if ($line =~ /^\s*#\s*include\s+\"(.*?)\"/) {
169 substitute($1, \@dotincludes, "", $line);
170 next;
171 }
172 if ($line =~ /^\s*#\s*include\s+\<(.*?)\>/) {
173 substitute($1, \@includes, "", $line);
174 next;
175 }
176 if ($line =~ /^\s*#\s*include_next\s+\<(.*?)\>/) {
177 substitute($1, \@includes, $absfile, $line);
178 next;
179 }
180 print OUTF $line."\n";
181 }
182 close INF;
183 close OUTF;
184 }
185
186 foreach my $dep (sort keys %deps) {
187 my($file, $dirs, $suffix) = fileparse($dep);
188
189 my $s1 = scalar(keys %map);
190 my $s2 = scalar(keys %rmap);
191
192 my $count = 0;
193 my $renamed = $file;
194 while (exists($rmap{$renamed})) {
195 $count++;
196 $renamed = $file."_".$count;
197 }
198
199 print "adding dep '$dep' -> '$renamed'\n" if $debug;
200
201 $map{$dep} = $renamed;
202 $rmap{$renamed} = $dep;
203
204 die unless ($s1 < scalar(keys %map));
205 die unless ($s2 < scalar(keys %rmap));
206 }
207
208 # copy over all dependencies
209 my $all_deps = "";
210 my $n = 0;
211 foreach my $dep (sort keys %deps) {
212 print "processing '$dep'\n" if $debug;
213 rewrite($dep, $map{$dep});
214 $all_deps .= "$map{$dep} ";
215 $n++;
216 }
217
218 print "\n\'files_to_reduce.txt\' contains a list of all files copied to this dir\n\n";
219
220 open OUTF, ">files_to_reduce.txt" or die;
221 print OUTF "$all_deps\n";
222 close OUTF;
223
224 my $res;
225
226 $res = runit ("$COMP $CFLAGS -E $FILE > out1$EXT");
227 die unless $res == 0;
228 runit ("grep -v '^# ' out1$EXT | grep -v '^\\s*\$' > out1strip$EXT");
229
230 $res = runit ("$COMP $CFLAGS -E $srcfile_copy > out2$EXT");
231 die unless $res == 0;
232 runit ("grep -v '^# ' out2$EXT | grep -v '^\\s*\$' > out2strip$EXT");
233
234 print "\n";
235 print "original src file, preprocessed, is in out1$EXT, stripped of # lines as out1strip$EXT\n";
236 print "transformed src file, preprocessed, is in out2$EXT, stripped of # lines as out2strip$EXT\n";
237
238 print "\n";
239
240 if (compare("out1strip$EXT", "out2strip$EXT") == 0) {
241 print "the stripped files are the same, which means this script might well have worked\n";
242 } else {
243 print "the stripped files are different -- this script might have worked, but please\n";
244 print "inspect the diffs manually\n";
245 }
246
247 print "\nreduce like this:\n";
248 print " creduce interestingness_test `cat files_to_reduce.txt`\n\n";