summaryrefslogtreecommitdiff
path: root/scripts/fixincludes
Unidiff
Diffstat (limited to 'scripts/fixincludes') (more/less context) (ignore whitespace changes)
-rwxr-xr-xscripts/fixincludes61
1 files changed, 3 insertions, 58 deletions
diff --git a/scripts/fixincludes b/scripts/fixincludes
index da8613c..f07965e 100755
--- a/scripts/fixincludes
+++ b/scripts/fixincludes
@@ -1,587 +1,532 @@
1#!/usr/bin/perl -w 1#!/usr/bin/perl -w
2# tries to reduce the number of includes in KDE source files 2# tries to reduce the number of includes in KDE source files
3# (c) 2001 Dirk Mueller <mueller@kde.org> 3# (c) 2001 Dirk Mueller <mueller@kde.org>
4 4
5use File::Basename; 5use File::Basename;
6use Cwd; 6use Cwd;
7 7
8# declaration of useful subroutines 8# declaration of useful subroutines
9sub find_fixable_sources ($); 9sub find_fixable_sources ($);
10sub find_fixable_headers($); 10sub find_fixable_headers($);
11sub find_removable_includes ($); 11sub find_removable_includes ($);
12sub warn_before_modifying ($); 12sub warn_before_modifying ($);
13sub remove_include ($$$); 13sub remove_include ($$$);
14sub replace_include ($$$); 14sub replace_include ($$$);
15sub fix_duplicates($); 15sub fix_duplicates($);
16sub fix_compat_includes($); 16sub fix_compat_includes($);
17sub fix_unnecessary($); 17sub fix_unnecessary($);
18sub copy_file($$); 18sub copy_file($$);
19sub process_source_file($); 19sub process_source_file($);
20 20
21# some global variables 21# some global variables
22$verbose = 0; # turns on debugging 22$verbose = 1; # turns on debugging
23$modify = 0; # if 1 it should try to fix the files as well 23$modify = 1; # if 1 it should try to fix the files as well
24$experimental = 0; # try&error if an include is obsolete (slow!!) 24$experimental = 1; # try&error if an include is obsolete (slow!!)
25@explicitfiles = (); # filled in if passing files on the command line 25@explicitfiles = (); # filled in if passing files on the command line
26 26
27# statistic variables 27# statistic variables
28$exp_success = 0; 28$exp_success = 0;
29$exp_failure = 0; 29$exp_failure = 0;
30 30
31while (defined ($ARGV[0])) 31while (defined ($ARGV[0]))
32{ 32{
33 $_ = shift; 33 $_ = shift;
34 if (/^--help$|^-h$/) { 34 if (/^--help$|^-h$/) {
35 print "Usage: fixincludes [--verbose | -v] [--experimental | -e ] [--modify | -m ]\n"; 35 print "Usage: fixincludes [--verbose | -v] [--experimental | -e ] [--modify | -m ]\n";
36 exit 0; 36 exit 0;
37 } 37 }
38 elsif (/^--verbose$|^-v$/) { 38 elsif (/^--verbose$|^-v$/) {
39 $verbose = 1; # Oh is there a problem...? 39 $verbose = 1; # Oh is there a problem...?
40 } 40 }
41 elsif (/^--modify$|^-m$/) { 41 elsif (/^--modify$|^-m$/) {
42 $modify = 1; 42 $modify = 1;
43 } 43 }
44 elsif (/^--experimental$|^-e$/) { 44 elsif (/^--experimental$|^-e$/) {
45 $modify = 1; 45 $modify = 1;
46 $experimental = 1; 46 $experimental = 1;
47 } 47 }
48 elsif (!/^-/) { 48 elsif (!/^-/) {
49 push @explicitfiles, $_; 49 push @explicitfiles, $_;
50 } 50 }
51} 51}
52 52
53$cppExt = "(cpp|cc|cxx|C|c\\+\\+)"; 53$cppExt = "(cpp|cc|cxx|C|c\\+\\+)";
54$hExt = "(h|H|hh|hxx|hpp|h\\+\\+)"; 54$hExt = "(h|H|hh|hxx|hpp|h\\+\\+)";
55 55
56# list of compat headers. scroll down ... much of boring stuff here.. 56# list of compat headers. scroll down ... much of boring stuff here..
57%compatmap = ( 57%compatmap = (
58 'qapp.h' => "qapplication.h",
59 #'qarray.h' => "qmemarray.h",
60 #'qbitarry.h' => "qbitarray.h",
61 'qbttngrp.h' => "qbuttongroup.h",
62 #'qchkbox.h' => "qcheckbox.h",
63 'qclipbrd.h' => "qclipboard.h",
64 #'qcollect.h' => "qptrcollection.h",
65 #'qcollection.h' => "qptrcollection.h",
66 'qcombo.h' => "qcombobox.h",
67 'qconnect.h' => "qconnection.h",
68 'qdatetm.h' => "qdatetime.h",
69 'qdrawutl.h' => "qdrawutil.h",
70 'qdstream.h' => "qdatastream.h",
71 #'qfiledef.h' => "private/qfiledefs_p.h",
72 'qfiledlg.h' => "qfiledialog.h",
73 'qfileinf.h' => "qfileinfo.h",
74 'qfontdta.h' => "qfontdata.h",
75 'qfontinf.h' => "qfontinfo.h",
76 'qfontmet.h' => "qfontmetrics.h",
77 'qgrpbox.h' => "qgroupbox.h",
78 'qintcach.h' => "qintcache.h",
79 'qiodev.h' => "qiodevice.h",
80 'qlcdnum.h' => "qlcdnumber.h",
81 'qlined.h' => "qlineedit.h",
82 #'qlist.h' => "qptrlist.h",
83 'qmenudta.h' => "qmenudata.h",
84 'qmetaobj.h' => "qmetaobject.h",
85 'qmlined.h' => "qtmultilineedit.h",
86 'qmsgbox.h' => "qmessagebox.h",
87 'qmultilinedit.h' => "qmultilineedit.h",
88 'qobjcoll.h' => "qobjectlist.h>\n\#include <qobjectdict.h",
89 'qobjdefs.h' => "qobjectdefs.h",
90 'qpaintd.h' => "qpaintdevice.h",
91 'qpaintdc.h' => "qpaintdevicedefs.h",
92 'qpdevmet.h' => "qpaintdevicemetrics.h",
93 'qpmcache.h' => "qpixmapcache.h",
94 'qpntarry.h' => "qpointarray.h",
95 'qpopmenu.h' => "qpopupmenu.h",
96 'qprndlg.h' => "qprintdialog.h",
97 'qprogbar.h' => "qprogressbar.h",
98 'qprogdlg.h' => "qprogressdialog.h",
99 'qpsprn.h' => "<private/qpsprinter_p.h>",
100 'qpushbt.h' => "qpushbutton.h",
101 'qqueue.h' => "qptrqueue.h",
102 'qradiobt.h' => "qradiobutton.h",
103 'qrangect.h' => "qrangecontrol.h",
104 'qscrbar.h' => "qscrollbar.h",
105 'qsocknot.h' => "qsocketnotifier.h",
106# 'qstack.h' => "qptrstack.h",
107 'qtabdlg.h' => "qtabdialog.h",
108 'qtstream.h' => "qtextstream.h",
109# 'qvector.h' => "qptrvector.h",
110 'qwidcoll.h' => "qwidgetlist.h\n\#include <qwidgetintdict.h",
111 'qwindefs.h' => "qwindowdefs.h",
112
113# and now the KDE specific compat includes 58# and now the KDE specific compat includes
114# 'kapp.h' => "kapplication.h", 59# 'kapp.h' => "kapplication.h",
115# 'kstddirs.h' => "kstandarddirs.h", 60# 'kstddirs.h' => "kstandarddirs.h",
116# 'kuniqueapp.h' => "kuniqueapplication.h", 61# 'kuniqueapp.h' => "kuniqueapplication.h",
117# 'ktmainwindow.h'=> "kmainwindow.h", 62# 'ktmainwindow.h'=> "kmainwindow.h",
118# 'kcolorbtn.h' => "kcolorbutton.h", 63# 'kcolorbtn.h' => "kcolorbutton.h",
119# 'kcolordlg.h' => "kcolordialog.h", 64# 'kcolordlg.h' => "kcolordialog.h",
120# 'kxmlgui.h' => "kxmlguifactory.h", 65# 'kxmlgui.h' => "kxmlguifactory.h",
121); 66);
122 67
123 68
124# now it starts to get interesting again 69# now it starts to get interesting again
125 70
126# Look for source files in the given directory ($dir, first parameter) 71# Look for source files in the given directory ($dir, first parameter)
127sub find_fixable_sources ($) 72sub find_fixable_sources ($)
128{ 73{
129 # for now I grep the directory (requires srcdir==builddir) 74 # for now I grep the directory (requires srcdir==builddir)
130 # actually it should read the Makefile and 75 # actually it should read the Makefile and
131 # find the _SOURCES / _OBJECTS tags that are put there by 76 # find the _SOURCES / _OBJECTS tags that are put there by
132 # automake and am_edit, but thats an excercise to the reader ;-) 77 # automake and am_edit, but thats an excercise to the reader ;-)
133 78
134 my ( $dir ) = @_; 79 my ( $dir ) = @_;
135 80
136 opendir (DIR, "$dir") || die "Couldn't read '$dir'\n"; 81 opendir (DIR, "$dir") || die "Couldn't read '$dir'\n";
137 my @sources = grep { /^.*\.$cppExt$/o } readdir(DIR); 82 my @sources = grep { /^.*\.$cppExt$/o } readdir(DIR);
138 closedir(DIR); 83 closedir(DIR);
139 84
140 print "found sources: [ " . join(' ', @sources) . " ] in $dir\n" if ($verbose); 85 print "found sources: [ " . join(' ', @sources) . " ] in $dir\n" if ($verbose);
141 86
142 # prefix them with $dir 87 # prefix them with $dir
143 my @retsources = (); 88 my @retsources = ();
144 foreach $source(@sources) { 89 foreach $source(@sources) {
145 push @retsources, "$dir/$source"; 90 push @retsources, "$dir/$source";
146 } 91 }
147 92
148 return @retsources; 93 return @retsources;
149} 94}
150 95
151# Look for header files in the given directory ($dir, first parameter) 96# Look for header files in the given directory ($dir, first parameter)
152sub find_fixable_headers ($) 97sub find_fixable_headers ($)
153{ 98{
154 # for now I grep the directory (requires srcdir==builddir) 99 # for now I grep the directory (requires srcdir==builddir)
155 # actually it should read the Makefile and 100 # actually it should read the Makefile and
156 # find the _HEADERS tags that are put there by 101 # find the _HEADERS tags that are put there by
157 # automake and am_edit, but thats an excercise to the reader ;-) 102 # automake and am_edit, but thats an excercise to the reader ;-)
158 103
159 my ( $dir ) = @_; 104 my ( $dir ) = @_;
160 105
161 opendir (DIR, "$dir") || die "Couldn't read '$dir'\n"; 106 opendir (DIR, "$dir") || die "Couldn't read '$dir'\n";
162 my @headers = grep { /^.*\.$hExt$/o } readdir(DIR); 107 my @headers = grep { /^.*\.$hExt$/o } readdir(DIR);
163 closedir(DIR); 108 closedir(DIR);
164 109
165 print "found headers: [ " . join(' ', @headers) . " ] in $dir\n" if ($verbose); 110 print "found headers: [ " . join(' ', @headers) . " ] in $dir\n" if ($verbose);
166 111
167 # prefix them with $dir 112 # prefix them with $dir
168 my @retheaders = (); 113 my @retheaders = ();
169 foreach $source(@headers) { 114 foreach $source(@headers) {
170 push @retheaders, "$dir/$source"; 115 push @retheaders, "$dir/$source";
171 } 116 }
172 117
173 return @retheaders; 118 return @retheaders;
174} 119}
175 120
176sub find_removable_includes ($) 121sub find_removable_includes ($)
177{ 122{
178 my $srcfile = shift @_; 123 my $srcfile = shift @_;
179 open(SRC, "< $srcfile") || die "find_removable_includes: couldn't open '$srcfile'\n"; 124 open(SRC, "< $srcfile") || die "find_removable_includes: couldn't open '$srcfile'\n";
180 125
181 my @includes = (); 126 my @includes = ();
182 127
183 # we skip all includes that are somehow ifdefed 128 # we skip all includes that are somehow ifdefed
184 129
185 my $cpplevel = 0; 130 my $cpplevel = 0;
186 $cpplevel = -1 if ($srcfile=~m/^.*\.$hExt$/); # plan for header-protection #ifndef/#define/#endif 131 $cpplevel = -1 if ($srcfile=~m/^.*\.$hExt$/); # plan for header-protection #ifndef/#define/#endif
187 while (<SRC>) { 132 while (<SRC>) {
188 if ($_ =~ m/^\#if/) { 133 if ($_ =~ m/^\#if/) {
189 $cpplevel = $cpplevel + 1; 134 $cpplevel = $cpplevel + 1;
190 next; 135 next;
191 } 136 }
192 if ($_ =~ m/^\#endif/) { 137 if ($_ =~ m/^\#endif/) {
193 $cpplevel = $cpplevel - 1; 138 $cpplevel = $cpplevel - 1;
194 next; 139 next;
195 } 140 }
196 #if ($cpplevel == 0 && $_ =~ m/^\#include\s*[\"<]([qk]\S*\.h)[\">]\S*/) { 141 #if ($cpplevel == 0 && $_ =~ m/^\#include\s*[\"<]([qk]\S*\.h)[\">]\S*/) {
197 if ($cpplevel == 0 && (($_ =~ m/^\#include\s*\"(\S+\.h)\"\S*/) || ($_ =~ m/^\#include\s*\<([qk]\S+.h)\>\S*/))) { 142 if ($cpplevel == 0 && (($_ =~ m/^\#include\s*\"(\S+\.h)\"\S*/) || ($_ =~ m/^\#include\s*\<([qk]\S+.h)\>\S*/))) {
198 push @includes, $1; 143 push @includes, $1;
199 next; 144 next;
200 } 145 }
201 } 146 }
202 close SRC; 147 close SRC;
203 148
204 print "No fixable includes found in $srcfile\n" if ($verbose and not @includes); 149 print "No fixable includes found in $srcfile\n" if ($verbose and not @includes);
205 print "found includes: [ " . join(' ', @includes) . " ]\n" if ($verbose and @includes); 150 print "found includes: [ " . join(' ', @includes) . " ]\n" if ($verbose and @includes);
206 151
207 return @includes; 152 return @includes;
208} 153}
209 154
210# first parameter: srcfile 155# first parameter: srcfile
211# second parameter: include to remove 156# second parameter: include to remove
212# third parameter is the duplicate level: this include is removed $level times 157# third parameter is the duplicate level: this include is removed $level times
213sub remove_include ($$$) 158sub remove_include ($$$)
214{ 159{
215 my $srcfile = shift @_; 160 my $srcfile = shift @_;
216 my $include = quotemeta(shift @_); 161 my $include = quotemeta(shift @_);
217 my $level = shift @_; 162 my $level = shift @_;
218 163
219 die "$srcfile is not read/writeable!\n" if( ! -r $srcfile || ! -w $srcfile); 164 die "$srcfile is not read/writeable!\n" if( ! -r $srcfile || ! -w $srcfile);
220 open(I, "< $srcfile") or die "remove_include: couldn't open '$srcfile'\n"; 165 open(I, "< $srcfile") or die "remove_include: couldn't open '$srcfile'\n";
221 my @contents = <I>; 166 my @contents = <I>;
222 close(I); 167 close(I);
223 168
224 # ok, CPU time doesn't count so we do it the lazy way 169 # ok, CPU time doesn't count so we do it the lazy way
225 # we should remove the last occurence of the include in the 170 # we should remove the last occurence of the include in the
226 # file because in case its a duplicate removing the first 171 # file because in case its a duplicate removing the first
227 # one could make a difference. 172 # one could make a difference.
228 my @revcontents = reverse @contents; 173 my @revcontents = reverse @contents;
229 @contents = (); 174 @contents = ();
230 175
231 # we skip all inludes that are somehow ifdefed 176 # we skip all inludes that are somehow ifdefed
232 # note the logic is reversed because it operates 177 # note the logic is reversed because it operates
233 # on reversed lines :) 178 # on reversed lines :)
234 my $cpplevel = 0; 179 my $cpplevel = 0;
235 $cpplevel = -1 if ($srcfile=~m/^.*\.$hExt$/); # plan for header-protection #ifndef/#define/#endif 180 $cpplevel = -1 if ($srcfile=~m/^.*\.$hExt$/); # plan for header-protection #ifndef/#define/#endif
236 foreach $line (@revcontents) { 181 foreach $line (@revcontents) {
237 if ($line =~ m/^\#if/) { 182 if ($line =~ m/^\#if/) {
238 $cpplevel = $cpplevel - 1; 183 $cpplevel = $cpplevel - 1;
239 push @contents, $line; 184 push @contents, $line;
240 next; 185 next;
241 } 186 }
242 187
243 if ($line =~ m/^\#endif/) { 188 if ($line =~ m/^\#endif/) {
244 $cpplevel = $cpplevel + 1; 189 $cpplevel = $cpplevel + 1;
245 push @contents, $line; 190 push @contents, $line;
246 next; 191 next;
247 } 192 }
248 193
249 #if ($cpplevel == 0 && $level > 0 && $line =~ m/^\#include\s*[\"<]$include[\">]\S*$/) { 194 #if ($cpplevel == 0 && $level > 0 && $line =~ m/^\#include\s*[\"<]$include[\">]\S*$/) {
250 if ($cpplevel == 0 && (($line =~ m/^\#include\s*\"$include\"\S*/) || ($line =~ m/^\#include\s*\<$include\>\S*/))) { 195 if ($cpplevel == 0 && (($line =~ m/^\#include\s*\"$include\"\S*/) || ($line =~ m/^\#include\s*\<$include\>\S*/))) {
251 $level = $level - 1; 196 $level = $level - 1;
252 # skipping the line.. 197 # skipping the line..
253 next; 198 next;
254 } 199 }
255 200
256 push @contents, $line; 201 push @contents, $line;
257 } 202 }
258 203
259 # now we have the fixed contents in @contents, although in wrong order 204 # now we have the fixed contents in @contents, although in wrong order
260 open(O, "> $srcfile") || die "remove_include: couldn't open '$srcfile' for writing\n"; 205 open(O, "> $srcfile") || die "remove_include: couldn't open '$srcfile' for writing\n";
261 print O reverse @contents; 206 print O reverse @contents;
262 close (O); 207 close (O);
263} 208}
264 209
265# first parameter: srcfile 210# first parameter: srcfile
266# second parameter: include to replace 211# second parameter: include to replace
267# third parameter the include file to replace it with 212# third parameter the include file to replace it with
268sub replace_include ($$$) 213sub replace_include ($$$)
269{ 214{
270 my $srcfile = shift @_; 215 my $srcfile = shift @_;
271 my $include = quotemeta(shift @_); 216 my $include = quotemeta(shift @_);
272 my $destinclude = shift @_; 217 my $destinclude = shift @_;
273 218
274 die "$srcfile is not read/writeable!\n" if( ! -r $srcfile || ! -w $srcfile); 219 die "$srcfile is not read/writeable!\n" if( ! -r $srcfile || ! -w $srcfile);
275 open(I, "< $srcfile") or die "replace_include: couldn't open '$srcfile'\n"; 220 open(I, "< $srcfile") or die "replace_include: couldn't open '$srcfile'\n";
276 my @contents = <I>; 221 my @contents = <I>;
277 close(I); 222 close(I);
278 223
279 # ok, CPU time doesn't count so we do it the lazy way 224 # ok, CPU time doesn't count so we do it the lazy way
280 my @revcontents = reverse @contents; 225 my @revcontents = reverse @contents;
281 @contents = (); 226 @contents = ();
282 227
283 # we skip all inludes that are somehow ifdefed 228 # we skip all inludes that are somehow ifdefed
284 # note the logic is reversed because it operates 229 # note the logic is reversed because it operates
285 # on reversed lines :) 230 # on reversed lines :)
286 my $cpplevel = 0; 231 my $cpplevel = 0;
287 $cpplevel = -1 if ($srcfile=~m/^.*\.$hExt$/); # plan for header-protection #ifndef/#define/#endif 232 $cpplevel = -1 if ($srcfile=~m/^.*\.$hExt$/); # plan for header-protection #ifndef/#define/#endif
288 foreach $line (@revcontents) { 233 foreach $line (@revcontents) {
289 if ($line =~ m/^\#if/) { 234 if ($line =~ m/^\#if/) {
290 $cpplevel = $cpplevel - 1; 235 $cpplevel = $cpplevel - 1;
291 push @contents, $line; 236 push @contents, $line;
292 next; 237 next;
293 } 238 }
294 239
295 if ($line =~ m/^\#endif/) { 240 if ($line =~ m/^\#endif/) {
296 $cpplevel = $cpplevel + 1; 241 $cpplevel = $cpplevel + 1;
297 push @contents, $line; 242 push @contents, $line;
298 next; 243 next;
299 } 244 }
300 245
301 #if ($cpplevel == 0 && $line =~ m/^\#include\s*[\"<]$include[\">]\S*/) { 246 #if ($cpplevel == 0 && $line =~ m/^\#include\s*[\"<]$include[\">]\S*/) {
302 if ($cpplevel == 0 && (($line =~ m/^\#include\s*\"$include\"\S*/) || ($line =~ m/^\#include\s*\<$include\>\S*/))) { 247 if ($cpplevel == 0 && (($line =~ m/^\#include\s*\"$include\"\S*/) || ($line =~ m/^\#include\s*\<$include\>\S*/))) {
303 print "HAH! found $include to replace in $srcfile!\n" if($verbose); 248 print "HAH! found $include to replace in $srcfile!\n" if($verbose);
304 $line =~ s/(\#include\s*[\"<])$include([\">]\S*)/$1$destinclude$2/; 249 $line =~ s/(\#include\s*[\"<])$include([\">]\S*)/$1$destinclude$2/;
305 } 250 }
306 251
307 push @contents, $line; 252 push @contents, $line;
308 } 253 }
309 254
310 # now we have the fixed contents in @contents 255 # now we have the fixed contents in @contents
311 open(O, "> $srcfile") || die "replace_include: couldn't open '$srcfile' for writing\n"; 256 open(O, "> $srcfile") || die "replace_include: couldn't open '$srcfile' for writing\n";
312 print O reverse @contents; 257 print O reverse @contents;
313 close (O); 258 close (O);
314} 259}
315 260
316sub fix_duplicates($) 261sub fix_duplicates($)
317{ 262{
318 my $srcfile = shift @_; 263 my $srcfile = shift @_;
319 264
320 my @includes = &find_removable_includes($srcfile); 265 my @includes = &find_removable_includes($srcfile);
321 266
322 my %inclMap = (); 267 my %inclMap = ();
323 268
324 # initialize 269 # initialize
325 foreach $include (@includes) { 270 foreach $include (@includes) {
326 $inclMap{$include} = 0; 271 $inclMap{$include} = 0;
327 } 272 }
328 273
329 # count number of occurences 274 # count number of occurences
330 foreach $include (@includes) { 275 foreach $include (@includes) {
331 $inclMap{$include} = $inclMap{$include} + 1; 276 $inclMap{$include} = $inclMap{$include} + 1;
332 } 277 }
333 278
334 # check for duplicates 279 # check for duplicates
335 foreach $include (keys %inclMap) { 280 foreach $include (keys %inclMap) {
336 next if $inclMap{$include} <= 1; 281 next if $inclMap{$include} <= 1;
337 282
338 print "$srcfile: duplicate level ". $inclMap{$include} .": ". $include ."\n"; 283 print "$srcfile: duplicate level ". $inclMap{$include} .": ". $include ."\n";
339 284
340 &remove_include($srcfile, $include, $inclMap{$include} - 1) if($modify); 285 &remove_include($srcfile, $include, $inclMap{$include} - 1) if($modify);
341 } 286 }
342} 287}
343 288
344sub fix_compat_includes($) 289sub fix_compat_includes($)
345{ 290{
346 my $srcfile = shift @_; 291 my $srcfile = shift @_;
347 292
348 my @includes = &find_removable_includes($srcfile); 293 my @includes = &find_removable_includes($srcfile);
349 294
350 my %inclMap = (); 295 my %inclMap = ();
351 296
352 # initialize 297 # initialize
353 foreach $include (@includes) { 298 foreach $include (@includes) {
354 $inclMap{$include} = 0; 299 $inclMap{$include} = 0;
355 } 300 }
356 301
357 # count number of occurences 302 # count number of occurences
358 foreach $include (@includes) { 303 foreach $include (@includes) {
359 $inclMap{$include} = $inclMap{$include} + 1; 304 $inclMap{$include} = $inclMap{$include} + 1;
360 } 305 }
361 306
362 # check for compat headers 307 # check for compat headers
363 foreach $include (keys %inclMap) { 308 foreach $include (keys %inclMap) {
364 if( defined $compatmap{$include}) { 309 if( defined $compatmap{$include}) {
365 print "$srcfile: compat header: $include, to be replaced by ". $compatmap{$include} ."\n"; 310 print "$srcfile: compat header: $include, to be replaced by ". $compatmap{$include} ."\n";
366 &replace_include($srcfile, $include, $compatmap{$include}) if($modify); 311 &replace_include($srcfile, $include, $compatmap{$include}) if($modify);
367 } 312 }
368 } 313 }
369} 314}
370 315
371# copies a file from src to dest, overwrites destination if exists 316# copies a file from src to dest, overwrites destination if exists
372sub copy_file($$) 317sub copy_file($$)
373{ 318{
374 my $src = shift(@_); 319 my $src = shift(@_);
375 my $dst = shift(@_); 320 my $dst = shift(@_);
376 321
377 open(I, "< $src") or die "copy_file: can't open $src for input\n"; 322 open(I, "< $src") or die "copy_file: can't open $src for input\n";
378 my @fcontents = <I>; 323 my @fcontents = <I>;
379 close(I); 324 close(I);
380 open(O, "> $dst") or die "copy_file: can't open $dst for output\n"; 325 open(O, "> $dst") or die "copy_file: can't open $dst for output\n";
381 print O @fcontents; 326 print O @fcontents;
382 close(O); 327 close(O);
383} 328}
384 329
385# interrupt handler for fix_unnecessary 330# interrupt handler for fix_unnecessary
386sub sighandler_fix_unnecessary() 331sub sighandler_fix_unnecessary()
387{ 332{
388 my($sig) = @_; 333 my($sig) = @_;
389 print "Caught a SIG$sig--shutting down after restoring $srcfile\n"; 334 print "Caught a SIG$sig--shutting down after restoring $srcfile\n";
390 chdir($srcdir); 335 chdir($srcdir);
391 unlink $srcfile || warn "couldn't unlink $srcfile"; 336 unlink $srcfile || warn "couldn't unlink $srcfile";
392 rename $localbackup, $srcfile || warn "couldn't rename $localbackup to $srcfile"; 337 rename $localbackup, $srcfile || warn "couldn't rename $localbackup to $srcfile";
393 exit(1); 338 exit(1);
394} 339}
395 340
396sub fix_unnecessary($) 341sub fix_unnecessary($)
397{ 342{
398 local $srcfile = shift @_; 343 local $srcfile = shift @_;
399 local $srcdir = dirname($srcfile); 344 local $srcdir = dirname($srcfile);
400 345
401 # find canonical path for srcdir 346 # find canonical path for srcdir
402 my $origdir = cwd; 347 my $origdir = cwd;
403 chdir($srcdir); 348 chdir($srcdir);
404 $srcdir = cwd; 349 $srcdir = cwd;
405 print "srcdir=$srcdir\n" if($verbose); 350 print "srcdir=$srcdir\n" if($verbose);
406 351
407 my $builddir = $srcdir; 352 my $builddir = $srcdir;
408 my $makecmd = "make"; 353 my $makecmd = "make";
409 if (defined $ENV{"OBJ_REPLACEMENT"}) 354 if (defined $ENV{"OBJ_REPLACEMENT"})
410 { 355 {
411 # we have to use sed here, because perl can't do s#a#b# 356 # we have to use sed here, because perl can't do s#a#b#
412 $builddir = `echo $srcdir | sed -e \$OBJ_REPLACEMENT`; 357 $builddir = `echo $srcdir | sed -e \$OBJ_REPLACEMENT`;
413 chomp $builddir; 358 chomp $builddir;
414 $makecmd = "makeobj"; 359 $makecmd = "makeobj";
415 } 360 }
416 print "builddir=$builddir\n" if($verbose); 361 print "builddir=$builddir\n" if($verbose);
417 362
418 my $tot = $exp_success + $exp_failure; 363 my $tot = $exp_success + $exp_failure;
419 print "=============== $srcfile (successes: $exp_success; total: $tot)\n"; 364 print "=============== $srcfile (successes: $exp_success; total: $tot)\n";
420 365
421 $srcfile = basename($srcfile); 366 $srcfile = basename($srcfile);
422 367
423 # first figure out some details 368 # first figure out some details
424 my @includes = &find_removable_includes($srcfile); 369 my @includes = &find_removable_includes($srcfile);
425 370
426 my $blanksrc = $srcfile; 371 my $blanksrc = $srcfile;
427 $blanksrc =~ s/(.*)\.[^\.]+/$1/; 372 $blanksrc =~ s/(.*)\.[^\.]+/$1/;
428 373
429 print "Checking for initial compilation: "; 374 print "Checking for initial compilation: ";
430 chdir($builddir); 375 chdir($builddir);
431 my $objextension = "BUG"; 376 my $objextension = "BUG";
432 unlink "$blanksrc.lo"; 377 unlink "$blanksrc.lo";
433 my $output = `$makecmd $blanksrc.lo 2>&1`; 378 my $output = `$makecmd $blanksrc.lo 2>&1`;
434 $objextension = ".lo" if ( 0 == ($? >> 8)); 379 $objextension = ".lo" if ( 0 == ($? >> 8));
435 if($objextension eq "BUG") { 380 if($objextension eq "BUG") {
436 print "failed with .lo... "; 381 print "failed with .lo... ";
437 unlink "$blanksrc.o"; 382 unlink "$blanksrc.o";
438 $output = `$makecmd $blanksrc.o 2>&1`; 383 $output = `$makecmd $blanksrc.o 2>&1`;
439 $objextension = ".o" if ( 0 == ($? >> 8)); 384 $objextension = ".o" if ( 0 == ($? >> 8));
440 } 385 }
441 if($objextension eq "BUG") { 386 if($objextension eq "BUG") {
442 warn "can't figure out right compile command for $srcfile :-(\n??? unused, or didn't compile in the first place?\n"; 387 warn "can't figure out right compile command for $srcfile :-(\n??? unused, or didn't compile in the first place?\n";
443 warn "$output"; 388 warn "$output";
444 chdir($origdir); 389 chdir($origdir);
445 return; 390 return;
446 } 391 }
447 392
448 print "worked with $objextension\n"; 393 print "worked with $objextension\n";
449 394
450 # now try to drop some includes 395 # now try to drop some includes
451 foreach $include (@includes) { 396 foreach $include (@includes) {
452 # kdatastream is special because 397 # kdatastream is special because
453 # it will break the application if removed even 398 # it will break the application if removed even
454 # if it continues to compile 399 # if it continues to compile
455 next if( $include eq "kdatastream.h"); 400 next if( $include eq "kdatastream.h");
456 # I also like to have kdebug.h still in 401 # I also like to have kdebug.h still in
457 # so that it's easy to add kdDebug calls 402 # so that it's easy to add kdDebug calls
458 next if( $include eq "kdebug.h"); 403 next if( $include eq "kdebug.h");
459 # avoid this one as it might cause 404 # avoid this one as it might cause
460 # certain code parts to be disabled from compilation 405 # certain code parts to be disabled from compilation
461 next if( $include eq "qmodules.h"); 406 next if( $include eq "qmodules.h");
462 # check if it is its own header file 407 # check if it is its own header file
463 my $blankhdr = $include; 408 my $blankhdr = $include;
464 $blankhdr =~ s/(.*)\.[^\.]+/$1/; 409 $blankhdr =~ s/(.*)\.[^\.]+/$1/;
465 next if ($blankhdr eq $blanksrc); 410 next if ($blankhdr eq $blanksrc);
466 411
467 chdir($srcdir); 412 chdir($srcdir);
468 413
469 local $localbackup = $srcfile . "#fixincludes"; 414 local $localbackup = $srcfile . "#fixincludes";
470 415
471 # preserve timestamp if possible for CVS 416 # preserve timestamp if possible for CVS
472 unlink $localbackup; 417 unlink $localbackup;
473 rename $srcfile, $localbackup; 418 rename $srcfile, $localbackup;
474 copy_file($localbackup, $srcfile); 419 copy_file($localbackup, $srcfile);
475 420
476 # revert to backup in case of interrupt (Ctrl+C) 421 # revert to backup in case of interrupt (Ctrl+C)
477 $SIG{'INT'} = \&sighandler_fix_unnecessary; 422 $SIG{'INT'} = \&sighandler_fix_unnecessary;
478 423
479 # check if it still compiles 424 # check if it still compiles
480 if($verbose) { 425 if($verbose) {
481 chdir($builddir); 426 chdir($builddir);
482 unlink "$builddir/$blanksrc$objextension"; 427 unlink "$builddir/$blanksrc$objextension";
483 `$makecmd $blanksrc$objextension 2>&1`; 428 `$makecmd $blanksrc$objextension 2>&1`;
484 die "unexpected error $output\nexitcode=" . ($? >> 8) if($? >> 8); 429 die "unexpected error $output\nexitcode=" . ($? >> 8) if($? >> 8);
485 chdir($srcdir); 430 chdir($srcdir);
486 } 431 }
487 432
488 # duplicates have to be nuked here , so it will be dropped maximum once 433 # duplicates have to be nuked here , so it will be dropped maximum once
489 print "trying without $include: "; 434 print "trying without $include: ";
490 &remove_include($srcfile, $include, 1); 435 &remove_include($srcfile, $include, 1);
491 436
492 chdir($builddir); 437 chdir($builddir);
493 438
494 # try if it compiles 439 # try if it compiles
495 unlink "$builddir/$blanksrc$objextension"; 440 unlink "$builddir/$blanksrc$objextension";
496 $output=`$makecmd $blanksrc$objextension 2>&1`; 441 $output=`$makecmd $blanksrc$objextension 2>&1`;
497 my $retcode = ($? >> 8); 442 my $retcode = ($? >> 8);
498 #print "retcode=$retcode\n$output" if ($verbose); 443 #print "retcode=$retcode\n$output" if ($verbose);
499 444
500 chdir($srcdir); 445 chdir($srcdir);
501 if($retcode == 0) { 446 if($retcode == 0) {
502 # wow, it worked, lets continue! 447 # wow, it worked, lets continue!
503 print "SUCCESS!\n"; 448 print "SUCCESS!\n";
504 $SIG{'INT'} = 'DEFAULT'; 449 $SIG{'INT'} = 'DEFAULT';
505 unlink $localbackup; 450 unlink $localbackup;
506 $exp_success = $exp_success + 1; 451 $exp_success = $exp_success + 1;
507 } 452 }
508 else { 453 else {
509 # better luck next time 454 # better luck next time
510 print "failed\n"; 455 print "failed\n";
511 unlink $srcfile; 456 unlink $srcfile;
512 rename $localbackup, $srcfile; 457 rename $localbackup, $srcfile;
513 $SIG{'INT'} = 'DEFAULT'; 458 $SIG{'INT'} = 'DEFAULT';
514 459
515 $exp_failure = $exp_failure + 1; 460 $exp_failure = $exp_failure + 1;
516 } 461 }
517 } 462 }
518 463
519 print "\n"; 464 print "\n";
520 465
521 chdir($origdir); 466 chdir($origdir);
522} 467}
523 468
524sub process_source_file($) 469sub process_source_file($)
525{ 470{
526 local $file = shift @_; 471 local $file = shift @_;
527 print "Checking: $file\n" if($verbose); 472 print "Checking: $file\n" if($verbose);
528 &fix_compat_includes($file); 473 &fix_compat_includes($file);
529 &fix_duplicates($file); 474 &fix_duplicates($file);
530 if ($experimental) { 475 if ($experimental) {
531 &fix_unnecessary($file); 476 &fix_unnecessary($file);
532 } 477 }
533 print "\n" if ($verbose); 478 print "\n" if ($verbose);
534} 479}
535 480
536# here is the main logic 481# here is the main logic
537 482
538# warn about modified files 483# warn about modified files
539if($modify) { 484if($modify) {
540 `cvscheck | grep '^[MmC]'`; 485 `cvscheck | grep '^[MmC]'`;
541 print "WARNING: you have pending local changes.You might commit them by accident!\n" if($? >> 8 == 0); 486 print "WARNING: you have pending local changes.You might commit them by accident!\n" if($? >> 8 == 0);
542} 487}
543 488
544# process files from the command line, if any 489# process files from the command line, if any
545if ( $#explicitfiles >= 0 ) { 490if ( $#explicitfiles >= 0 ) {
546 foreach $file( @explicitfiles ) { 491 foreach $file( @explicitfiles ) {
547 &process_source_file( $file ); 492 &process_source_file( $file );
548 } 493 }
549 exit 0; 494 exit 0;
550} 495}
551 496
552# first generate a list of subdirectories 497# first generate a list of subdirectories
553@dirlist = ("."); 498@dirlist = (".");
554foreach $dir ( @dirlist ) { 499foreach $dir ( @dirlist ) {
555 opendir (DIR, "$dir") || warn "Couldn't read '$dir'"; 500 opendir (DIR, "$dir") || warn "Couldn't read '$dir'";
556 my $subdir = ""; 501 my $subdir = "";
557 while( $subdir = readdir(DIR)) { 502 while( $subdir = readdir(DIR)) {
558 next if ($subdir =~ /^\..*/); 503 next if ($subdir =~ /^\..*/);
559 push @dirlist, "$dir/$subdir" if ( -d "$dir/$subdir"); 504 push @dirlist, "$dir/$subdir" if ( -d "$dir/$subdir");
560 } 505 }
561 closedir(DIR); 506 closedir(DIR);
562} 507}
563 508
564# now iterate over all subdirs 509# now iterate over all subdirs
565foreach $dir(@dirlist) { 510foreach $dir(@dirlist) {
566 511
567 # check if this directory wants not to be fixed 512 # check if this directory wants not to be fixed
568 if(open(M, "$dir/Makefile.am")) { 513 if(open(M, "$dir/Makefile.am")) {
569 my @mcontents = grep /(\-UQT_NO_COMPAT|\-UKDE_NO_COMPAT)/, <M>; 514 my @mcontents = grep /(\-UQT_NO_COMPAT|\-UKDE_NO_COMPAT)/, <M>;
570 close(M); 515 close(M);
571 if ( @mcontents ) { 516 if ( @mcontents ) {
572 print "Skipping directory: $dir\n"; 517 print "Skipping directory: $dir\n";
573 next; 518 next;
574 } 519 }
575 } 520 }
576 521
577 @headers = &find_fixable_headers($dir); 522 @headers = &find_fixable_headers($dir);
578 foreach $file(@headers) { 523 foreach $file(@headers) {
579 print "Checking: $file\n" if($verbose); 524 print "Checking: $file\n" if($verbose);
580 &fix_compat_includes($file); 525 &fix_compat_includes($file);
581 &fix_duplicates($file); 526 &fix_duplicates($file);
582 } 527 }
583 @sources = &find_fixable_sources($dir); 528 @sources = &find_fixable_sources($dir);
584 foreach $file(@sources) { 529 foreach $file(@sources) {
585 &process_source_file($file); 530 &process_source_file($file);
586 } 531 }
587} 532}