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