Merge branch 'master' of https://Governor-Tarkin@bitbucket.org/Governor-Tarkin/swg...
[swg-src.git] / tools / inctree.pl
blob552e49df0ccb86cf6b8384c5a109b4f17488cf62
1 #!/usr/bin/perl
3 # Usage: inctree [options] [files]
5 # Configuration parameters.
7 $CPP = 'gcc -E';
8 # $CPP = "cc -P";
9 # $CPP = "/lib/cpp";
11 $shiftwidth = 4;
13 # Process switches.
15 while ($ARGV[0] =~ /^-/) {
16 $_ = shift;
17 if (/^-D(.*)/) {
18 $defines .= " -D" . ($1 ? $1 : shift);
20 elsif (/^-BW/) {
21 $CPP = 'cl /nologo /E';
22 $count = 0;
23 while (! chdir "build/win32")
25 die "could not find build/win32 in search backwards" if (++$count > 50);
26 chdir "..";
29 elsif (/^-I(.*)/) {
31 $include = ($1 ? $1 : shift);
33 if ($include =~ /\.rsp$/)
35 open(RSP, $include);
36 while (<RSP>)
38 chop;
39 $includes .= " -I" . $_;
41 close(RSP);
43 else {
44 $includes .= " -I" . $include;
47 elsif (/^-m(.*)/) {
48 push(@pats, $1 ? $1 : shift);
50 elsif (/^-l/) {
51 $lines++;
53 else {
54 die "Unrecognized switch: $_\n";
58 # Build a subroutine to scan for any specified patterns.
60 if (@pats) {
61 $sub = "sub pats {\n";
62 foreach $pat (@pats) {
63 $sub .= " print '>>>>>>> ',\$_ if m$pat;\n";
65 $sub .= "}\n";
66 eval $sub;
67 ++$pats;
70 # Now process each file on the command line.
72 foreach $file (@ARGV) {
73 open(CPP,"$CPP $defines $includes $file|")
74 || die "Can't run cpp: $!\n";
75 $line = 2;
77 while (<CPP>) {
78 ++$line;
79 &pats if $pats; # Avoid expensive call if we can.
80 s/^#line /# /; # handle dev-studio style line info as well
81 next unless /^#/;
82 next unless /^# \d/;
83 chop;
84 s/\\+/\\/g;
85 s/\\/\//g;
86 while (s#/\w+/../#/#) {}
87 ($junk,$newline,$filename) = split(/\s+/, $_, 3);
88 $filename =~ s/"//g;
89 $filename =~ s/^\s+//;
90 $filename =~ s/\s+$//;
92 # Now figure out if it's a push, a pop, or neither.
94 if ($stack[$#stack] eq $filename) { # Same file.
95 $line = $newline-1;
96 next;
99 if ($stack[$#stack-1] eq $filename) { # Leaving file.
100 $indent -= $shiftwidth;
101 $line = pop(@lines)-1;
102 pop(@stack);
104 else { # New file.
105 printf "%6d ", $line-2 if $lines;
106 push(@lines,$line);
107 $line = $newline;
108 print " " x ($indent), $filename;
109 print " DUPLICATE" if $seen{$filename}++;
110 print "\n";
111 $indent += $shiftwidth;
112 push(@stack,$filename);
115 close CPP;
116 $indent = 0;
117 %seen = ();
118 print "\n\n";
119 $line = 0;