1 # Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
2 # Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
3 # This file is part of Coccinelle.
5 # Coccinelle is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, according to version 2 of the License.
9 # Coccinelle is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
17 # The authors reserve the right to distribute this or future versions of
18 # Coccinelle under other licenses.
24 sub pr2
{ print "$_[0]\n"; }
25 sub mylog
{ print @_;}
28 # to be launched from the git directory
29 die "usage: $0 commithashafter [commithashbefore]"
30 if(@ARGV <= 0 || @ARGV >= 3);
32 # update: now I also extract the headers files, the one
33 # that were modified in the commit and the one that are
34 # locally used and that may contain useful type information
37 # update: now I also extract some include/linux/header.h files, the
38 # one having the same name of one of the driver.
40 my $target_dir = "/tmp/extract_c_and_res/$ARGV[0]";
41 `mkdir -p $target_dir`;
42 my $old_dir = "/tmp/extract_c_and_res/$ARGV[0]_old";
44 my $new_dir = "/tmp/extract_c_and_res/$ARGV[0]_new";
47 my $commit_new = $ARGV[0];
48 my $commit_old = $ARGV[1] || "$commit_new^"; # default parent
50 my $gitfile = "$target_dir/$commit_new.gitinfo";
51 my $makefile = "$target_dir/Makefile";
53 `git show $commit_new > $gitfile `;
56 # processing the patch
60 my @driverheaders_in_include = ();
63 open FILE
, "$gitfile" or die "$!";
66 # allow other dir ? # fs|mm there is drivers under arch/ too
67 if(/^diff --git a\/((drivers
|sound
)\
/.*?\.[ch]) b/){
74 elsif(/^diff --git a\/(include\
/.*?\.h) b/) {
75 mylog
"potential header driver $1\n";
76 push @driverheaders_in_include, $1;
78 elsif(/^diff --git a\//) {
79 mylog
" not driver:$_";
82 die "PB: strange diff line: $_";
86 # extracting the .c and .h of the patch
90 # to be able to later find the corresponding local included header file
91 my $kerneldir_of_file = {};
96 foreach my $f (@files) {
97 my ($base) = `basename $f`;
100 if($base =~ /\.c$/) {
101 $res =~ s/\.c$/.res/;
103 if($base =~ /\.h$/) {
104 $res =~ s/\.h$/.h.res/;
107 pr2
"processing: $f $base $res";
108 if(-e
"$target_dir/$base") {
110 $base = "${counter}_$base";
111 $res = "${counter}_$res";
112 pr2
"try transform one file because already exist: $base";
113 if($base =~ /\.h$/) {
114 die "PB: Two header files share the same name: $base.";
118 die "PB: one of the file already exist: $base" if (-e
"$target_dir/$base");
120 `git-cat-file blob $commit_old:$f > $target_dir/$base`;
121 `git-cat-file blob $commit_new:$f > $target_dir/$res`;
123 `git-cat-file blob $commit_old:$f > $old_dir/$base`;
124 `git-cat-file blob $commit_new:$f > $new_dir/$base`;
126 $kerneldir_of_file->{$base} = `dirname $f`;
127 chomp $kerneldir_of_file->{$base};
129 push @finalcfiles, $base;
130 $finalcfiles->{$base} = 1;
137 open MAKE
, ">$makefile" or die "$!";
138 print MAKE
"CEDESCRIPTION=\"\"\n";
139 print MAKE
"SP=foo.cocci\n";
140 print MAKE
"SOURCES = ";
141 my $last = shift @finalcfiles;
142 foreach my $f (@finalcfiles) {
143 print MAKE
"$f \\\n\t";
145 print MAKE
"$last\n";
150 include \$(TOP)/generic_makefile
155 # process potential driver headers of include/
157 foreach my $f (@driverheaders_in_include) {
158 my $base = `basename $f`;
162 } else { die "PB: internal error"; }
165 # julia want all .h that were in the patch, not just the headers
166 # of our heuristic. Hence the comment.
169 # if(defined($finalcfiles->{$base})) {
171 # pr2 "found header of driver in include/: $f of $base";
172 my $dir = `dirname $f`;
174 `mkdir -p $target_dir/$dir`;
175 `git-cat-file blob $commit_old:$f > $target_dir/$f`;
176 `git-cat-file blob $commit_new:$f > $target_dir/$f.res`;
178 `mkdir -p $old_dir/$dir`;
179 `mkdir -p $new_dir/$dir`;
180 `git-cat-file blob $commit_old:$f > $old_dir/$f`;
181 `git-cat-file blob $commit_new:$f > $new_dir/$f`;
186 # compute other linux headers not in the patch
188 my @linuxheaders = `cd $target_dir; grep -E \"#include +\<[^>]*\>\" *.c *.h`;
189 foreach my $line (@linuxheaders) {
192 if($line =~ /^(.*)?:#include *\<([^>]*)\>/) {
193 my ($_file, $f) = ($1, $2);
195 my $base = `basename $f`;
199 } else { die "PB: internal error"; }
201 if(defined($finalcfiles->{$base}) && ! -e
"$target_dir/include/$f") {
202 pr2
"found header of driver in include/: $f of $base";
203 my $dir = `dirname $f`;
205 `mkdir -p $target_dir/include/$dir`;
206 `git-cat-file blob $commit_old:include/$f > $target_dir/include/$f`;
208 } else { pr2
"pb regexp: $line"; }
212 # compute other local headers not in the patch
214 my @headers = `cd $target_dir; grep -E \"#include +\\".*\\"\" *.c *.h`;
217 foreach my $line (@headers) {
220 if($line =~ /^(.*)?:#include *"(.*)"/) {
222 my ($file, $header) = ($1, $2);
223 my $dir = $kerneldir_of_file->{$file};
225 my $fullheader = "$dir/$header";
228 if($files->{$fullheader}) {
229 pr2
"INFO: $fullheader was already in commit";
231 $hfiles->{$fullheader} = 1;
234 } else { pr2
"pb regexp: $line"; }
238 foreach my $h (keys %{$hfiles}) {
239 my ($base) = `basename $h`;
241 pr2
"processing additionnal header file: $h $base";
243 if(-e
"$target_dir/$base") {
244 pr2
"-------------------------------------";
245 pr2
"PB: local header (not modified in the git) $base already exists";
246 pr2
"BUT I CONTINUE, but may have more .failed in the end";
247 pr2
"-------------------------------------";
249 `git-cat-file blob $commit_old:$h > $target_dir/$base`;