root/trunk/nt/makedepend.pl

Revision 2953, 5.9 kB (checked in by cvs2svn, 5 years ago)

3 copies to tags/branches

Line 
1 #!/usr/local/bin/perl
2 # makedepend.pl ---
3 #      Make dependency rule for make from C compiler output.
4 #                               Copyright (C) 1999, MIYASHITA Hisashi.
5 #
6 # This file is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 #
20
21 # Comment:
22 #   This file is perl script that is executable on Perl version 4 or 5.
23 #   This program extract dependency information from output of GCC or
24 #   C preprocessor that can print out '#line' information.  Then format
25 #   and print out the information into the expected file.
26
27 $makedepend_start = "### automatically generated dependency start\n";
28 $makedepend_end = "### automatically generated dependency end\n";
29 $usage = "makedepend.pl (-f|-F) OUTPUTFILE [-E] [-D] [-DO] [-ra] [[-rm REMOVE-RULE]  [-ag AGREEMENT-RULE] [-rp REPLACE-RULE] ...] [-cc COMPILER (msvc or gcc)] TARGETS...";
30 $remove_absolute_path = 0;
31 $generate_from_preprocess = 0;
32 $filetype_dos = 0;
33 $file_output_dos = 0;
34 $output_replace = 1;
35 $interpret_pattern = '^\#line[ \t]*(\d+)[ \t]+(\"[^\"]+\")$';
36
37 sub create_dep_gcc {
38     local ($file) = @_;
39
40     @out = ();
41     open(DEPFILE, $file);
42     while(<DEPFILE>) {
43         chop;
44         @sepline = split(/[\t ]/);
45
46         for ($j = 0;$j <= $#sepline;$j++) {
47             $str = $sepline[$j];
48             if (($str !~ /^[\t ]*\\[\t ]*$/)
49                 && ($str =~ /[^ \t]/)) {
50                 $str =~ s/:[ \t]*$//; # remove tail `:'
51                 if ($str =~ /[^ \t]/) {
52                     push(@out, $str);
53                 }
54             }
55         }
56     }
57     close(DEPFILE);
58
59     return @out;
60 }
61
62 sub create_dep_cpp {
63     local ($file) = @_;
64     local ($srcfile, $line);
65
66     @out = ();
67
68     if ($filetype_dos) {
69         $file =~ tr!\\!/!;
70     }
71     push (@out, $file);
72     open(DEPFILE, $file);
73     while(<DEPFILE>) {
74         s/\r\n$/\n/;
75         chop;
76         if (/$interpret_pattern/) {
77             $line = $1;
78             $srcfile = $2;
79
80             if ($srcfile !~ /^\"<(built-in|command line)>\"$/) {
81
82                 if ($srcfile =~ /\"[^\"]+\"$/) {
83                     eval '$srcfile = '.$srcfile;
84                 }
85
86                 if ($filetype_dos) {
87                     $srcfile =~ tr!\\!/!;
88                 }
89
90                 @list = grep(($srcfile eq $_), @out);
91                 if (!($#list >= 0)
92                     && ($srcfile =~ /[^ \t]/)) {
93                     push(@out, $srcfile);
94                 }
95             }
96         }
97     }
98     close(DEPFILE);
99
100     return @out;
101 }
102
103 sub check_path {
104     local ($file) = @_;
105     local ($pat, $repl, $action);
106
107     if ($remove_absolute_path) {
108         if ($filetype_dos) {
109             if ($elem =~/^(\w:)?\//) {
110                 return "";
111             }
112         }else{
113             if ($elem =~/^\//) {
114                 return "";
115             }
116         }
117     }
118     for ($k = 0;$k <= $#pathsep_rules;) {
119         $action = $pathsep_rules[$k++];
120         if ($action eq "REMOVE") {
121             $pat = $pathsep_rules[$k++];
122             print "$elem <RM- $pat\n";
123             return "" if ($elem =~ $pat);
124         } elsif ($action eq "REPLACE") {
125             $pat = $pathsep_rules[$k++];
126             $repl = $pathsep_rules[$k++];
127             eval "\$elem =~ s!$pat!$repl!";
128             print "$elem <RP- $pat + $repl\n";
129         } elsif ($action eq "AGREEMENT") {
130             $pat = $pathsep_rules[$k++];
131             print "$elem <AG- $pat\n";
132             return $elem if ($elem =~ $pat);
133         } else {
134             die("Internal error-I don't know such action:", $action);
135         }
136     }
137     return $elem;
138 }
139
140 sub output_dependency {
141     local ($file, $createdep, $replacep) = @_;
142     local ($skip_to_end, $output);
143
144     if ($replacep) {
145         $skip_to_end = 0;
146         $output = "";
147         open(INPUTFILE, $file);
148         while(<INPUTFILE>) {
149             s/\r\n$/\n/;
150             if ($_ eq $makedepend_start) {
151                 $output =  $output.$_.$createdep;
152                 $skip_to_end = 1;
153             }elsif ($_ eq $makedepend_end){
154                 $output =  $output.$_;
155                 $skip_to_end = 0;
156             }elsif ($skip_to_end == 0){
157                 $output =  $output.$_;
158             }
159         }
160         close(INPUTFILE);
161     } else {
162         $output = $createdep;
163     }
164     open(OUTPUTFILE, ">$file");
165     print OUTPUTFILE $output;
166     close(OUTPUTFILE);
167 }
168
169 ##
170 ## The end of function definition part---------------------------------------
171 ##
172
173 for($i = 0;$i <= $#ARGV;$i++){
174     if ($ARGV[$i] eq "-f"){
175         $output_filename = $ARGV[++$i];
176         $output_replace = 1;
177     }elsif ($ARGV[$i] eq "-F"){
178         $output_filename = $ARGV[++$i];
179         $output_replace = 0;
180     }elsif ($ARGV[$i] eq "-E"){
181         $generate_from_preprocess = 1;
182     }elsif ($ARGV[$i] eq "-D"){
183         $filetype_dos = 1;
184     }elsif ($ARGV[$i] eq "-DO"){
185         $file_output_dos = 1;
186     }elsif ($ARGV[$i] eq "-rp") {
187         split(/=/, $ARGV[++$i]);
188         if ($#_ != 1) {
189             die("-rp argument format must be ...=...:", $ARGV[$i]);
190         }
191         push(@pathsep_rules, "REPLACE");
192         push(@pathsep_rules, @_);
193     }elsif ($ARGV[$i] eq "-rm") {
194         push(@pathsep_rules, ("REMOVE", $ARGV[++$i]));
195     }elsif ($ARGV[$i] eq "-ag") {
196         push(@pathsep_rules, ("AGREEMENT", $ARGV[++$i]));
197     }elsif ($ARGV[$i] eq "-ra") {
198         $remove_absolute_path = 1;
199     }elsif ($ARGV[$i] eq "-cc") {
200         if ($ARGV[++$i] eq "gcc") {
201             $interpret_pattern = '^\#[ \t]+(\d+)[ \t]+(\"[^\"]+\") 1$';
202         } else {
203             $interpret_pattern = '^\#line[ \t]*(\d+)[ \t]+(\"[^\"]+\")$';
204         }
205     }else{
206         push(@depfiles, $ARGV[$i]);
207     }
208 }
209
210 if ($output_filename eq "") {
211     die "Specify output filename!";
212 }
213
214 $createdep = "\n";
215
216 for ($i = 0;$i <= $#depfiles;$i++){
217
218     if ($generate_from_preprocess) {
219         @deplist = &create_dep_cpp($depfiles[$i]);
220     } else {
221         @deplist = &create_dep_gcc($depfiles[$i]);
222     }
223
224     for ($j = 0;$j <= $#deplist;$j++) {
225         $elem = $deplist[$j];
226
227         $elem = &check_path($elem);
228         next if ($elem eq "");
229
230         if ($file_output_dos) {
231             $elem =~ tr!/!\\!;
232         }
233         if ($j == 0) {
234             $createdep = $createdep.$elem.':';
235         } else {
236             $createdep = $createdep." \\\n"."    ".$elem;
237         }
238     }
239     $createdep = $createdep."\n\n";
240 }
241
242 &output_dependency($output_filename, $createdep, $output_replace);
Note: See TracBrowser for help on using the browser.