1 # Copyright 2010, INRIA, University of Copenhagen
2 # Julia Lawall, Rene Rydhof Hansen, Gilles Muller, Nicolas Palix
3 # Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
4 # Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
5 # This file is part of Coccinelle.
7 # Coccinelle is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, according to version 2 of the License.
11 # Coccinelle is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
19 # The authors reserve the right to distribute this or future versions of
20 # Coccinelle under other licenses.
26 sub pr2
{ print "$_[0]\n"; }
27 sub mylog
{ print @_;}
30 # to be launched from the git directory
31 die "usage: $0 commithashafter [commithashbefore]"
32 if(@ARGV <= 0 || @ARGV >= 3);
34 # update: now I also extract the headers files, the one
35 # that were modified in the commit and the one that are
36 # locally used and that may contain useful type information
39 # update: now I also extract some include/linux/header.h files, the
40 # one having the same name of one of the driver.
42 my $target_dir = "/tmp/extract_c_and_res/$ARGV[0]";
43 `mkdir -p $target_dir`;
44 my $old_dir = "/tmp/extract_c_and_res/$ARGV[0]_old";
46 my $new_dir = "/tmp/extract_c_and_res/$ARGV[0]_new";
49 my $commit_new = $ARGV[0];
50 my $commit_old = $ARGV[1] || "$commit_new^"; # default parent
52 my $gitfile = "$target_dir/$commit_new.gitinfo";
53 my $makefile = "$target_dir/Makefile";
55 `git show $commit_new > $gitfile `;
58 # processing the patch
62 my @driverheaders_in_include = ();
65 open FILE
, "$gitfile" or die "$!";
68 # allow other dir ? # fs|mm there is drivers under arch/ too
69 if(/^diff --git a\/((drivers
|sound
)\
/.*?\.[ch]) b/){
76 elsif(/^diff --git a\/(include\
/.*?\.h) b/) {
77 mylog
"potential header driver $1\n";
78 push @driverheaders_in_include, $1;
80 elsif(/^diff --git a\//) {
81 mylog
" not driver:$_";
84 die "PB: strange diff line: $_";
88 # extracting the .c and .h of the patch
92 # to be able to later find the corresponding local included header file
93 my $kerneldir_of_file = {};
98 foreach my $f (@files) {
99 my ($base) = `basename $f`;
102 if($base =~ /\.c$/) {
103 $res =~ s/\.c$/.res/;
105 if($base =~ /\.h$/) {
106 $res =~ s/\.h$/.h.res/;
109 pr2
"processing: $f $base $res";
110 if(-e
"$target_dir/$base") {
112 $base = "${counter}_$base";
113 $res = "${counter}_$res";
114 pr2
"try transform one file because already exist: $base";
115 if($base =~ /\.h$/) {
116 die "PB: Two header files share the same name: $base.";
120 die "PB: one of the file already exist: $base" if (-e
"$target_dir/$base");
122 `git cat-file blob $commit_old:$f > $target_dir/$base`;
123 `git cat-file blob $commit_new:$f > $target_dir/$res`;
125 `git cat-file blob $commit_old:$f > $old_dir/$base`;
126 `git cat-file blob $commit_new:$f > $new_dir/$base`;
128 $kerneldir_of_file->{$base} = `dirname $f`;
129 chomp $kerneldir_of_file->{$base};
131 push @finalcfiles, $base;
132 $finalcfiles->{$base} = 1;
139 open MAKE
, ">$makefile" or die "$!";
140 print MAKE
"CEDESCRIPTION=\"\"\n";
141 print MAKE
"SP=foo.cocci\n";
142 print MAKE
"SOURCES = ";
143 my $last = shift @finalcfiles;
144 foreach my $f (@finalcfiles) {
145 print MAKE
"$f \\\n\t";
147 print MAKE
"$last\n";
152 include \$(TOP)/generic_makefile
157 # process potential driver headers of include/
159 foreach my $f (@driverheaders_in_include) {
160 my $base = `basename $f`;
164 } else { die "PB: internal error"; }
167 # julia want all .h that were in the patch, not just the headers
168 # of our heuristic. Hence the comment.
171 # if(defined($finalcfiles->{$base})) {
173 # pr2 "found header of driver in include/: $f of $base";
174 my $dir = `dirname $f`;
176 `mkdir -p $target_dir/$dir`;
177 `git cat-file blob $commit_old:$f > $target_dir/$f`;
178 `git cat-file blob $commit_new:$f > $target_dir/$f.res`;
180 `mkdir -p $old_dir/$dir`;
181 `mkdir -p $new_dir/$dir`;
182 `git cat-file blob $commit_old:$f > $old_dir/$f`;
183 `git cat-file blob $commit_new:$f > $new_dir/$f`;
188 # compute other linux headers not in the patch
190 my @linuxheaders = `cd $target_dir; grep -E \"#include +\<[^>]*\>\" *.c *.h`;
191 foreach my $line (@linuxheaders) {
194 if($line =~ /^(.*)?:#include *\<([^>]*)\>/) {
195 my ($_file, $f) = ($1, $2);
197 my $base = `basename $f`;
201 } else { die "PB: internal error"; }
203 if(defined($finalcfiles->{$base}) && ! -e
"$target_dir/include/$f") {
204 pr2
"found header of driver in include/: $f of $base";
205 my $dir = `dirname $f`;
207 `mkdir -p $target_dir/include/$dir`;
208 `git cat-file blob $commit_old:include/$f > $target_dir/include/$f`;
210 } else { pr2
"pb regexp: $line"; }
214 # compute other local headers not in the patch
216 my @headers = `cd $target_dir; grep -E \"#include +\\".*\\"\" *.c *.h`;
219 foreach my $line (@headers) {
222 if($line =~ /^(.*)?:#include *"(.*)"/) {
224 my ($file, $header) = ($1, $2);
225 my $dir = $kerneldir_of_file->{$file};
227 my $fullheader = "$dir/$header";
230 if($files->{$fullheader}) {
231 pr2
"INFO: $fullheader was already in commit";
233 $hfiles->{$fullheader} = 1;
236 } else { pr2
"pb regexp: $line"; }
240 foreach my $h (keys %{$hfiles}) {
241 my ($base) = `basename $h`;
243 pr2
"processing additionnal header file: $h $base";
245 if(-e
"$target_dir/$base") {
246 pr2
"-------------------------------------";
247 pr2
"PB: local header (not modified in the git) $base already exists";
248 pr2
"BUT I CONTINUE, but may have more .failed in the end";
249 pr2
"-------------------------------------";
251 `git cat-file blob $commit_old:$h > $target_dir/$base`;