maint-tool revision 1.9 1 1.1 christos #!/usr/bin/perl
2 1.1 christos # -*- perl -*-
3 1.1 christos
4 1.9 christos # Copyright (C) 2001-2024 Free Software Foundation, Inc.
5 1.1 christos #
6 1.1 christos # This file is part of the libiberty library.
7 1.1 christos # Libiberty is free software; you can redistribute it and/or
8 1.1 christos # modify it under the terms of the GNU Library General Public
9 1.1 christos # License as published by the Free Software Foundation; either
10 1.1 christos # version 2 of the License, or (at your option) any later version.
11 1.1 christos #
12 1.1 christos # Libiberty is distributed in the hope that it will be useful,
13 1.1 christos # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 1.1 christos # Library General Public License for more details.
16 1.1 christos #
17 1.1 christos # You should have received a copy of the GNU Library General Public
18 1.1 christos # License along with libiberty; see the file COPYING.LIB. If not,
19 1.1 christos # write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 1.1 christos # Boston, MA 02110-1301, USA.
21 1.1 christos #
22 1.1 christos # Originally written by DJ Delorie <dj (at] redhat.com>
23 1.1 christos
24 1.1 christos
25 1.1 christos # This is a trivial script which checks the lists of C and O files in
26 1.1 christos # the Makefile for consistency.
27 1.1 christos
28 1.1 christos $mode = shift;
29 1.1 christos $srcdir = ".";
30 1.1 christos
31 1.1 christos if ($mode eq "-s") {
32 1.1 christos $srcdir = shift;
33 1.1 christos $mode = shift;
34 1.1 christos }
35 1.1 christos
36 1.1 christos &missing() if $mode eq "missing";
37 1.1 christos &undoc() if $mode eq "undoc";
38 1.1 christos &deps() if $mode eq "deps";
39 1.1 christos
40 1.1 christos exit 0;
41 1.1 christos
42 1.1 christos format STDOUT =
43 1.1 christos ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~
44 1.1 christos $out
45 1.1 christos ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
46 1.1 christos $out
47 1.1 christos .
48 1.1 christos
49 1.1 christos ######################################################################
50 1.1 christos
51 1.1 christos sub missing {
52 1.1 christos
53 1.1 christos opendir(S, $srcdir);
54 1.1 christos while ($f = readdir S) {
55 1.1 christos $have{$f} = 1;
56 1.1 christos }
57 1.1 christos closedir(S);
58 1.1 christos opendir(S, ".");
59 1.1 christos while ($f = readdir S) {
60 1.1 christos $have{$f} = 1;
61 1.1 christos }
62 1.1 christos closedir(S);
63 1.1 christos
64 1.1 christos for $a (@ARGV) {
65 1.1 christos $listed{$a} = 1;
66 1.1 christos $have{$a} = 0;
67 1.1 christos }
68 1.1 christos
69 1.1 christos for $f (sort keys %have) {
70 1.1 christos next unless $have{$f};
71 1.1 christos if ($f =~ /\.c$/) {
72 1.1 christos print "S $f\n";
73 1.1 christos }
74 1.1 christos }
75 1.1 christos for $f (sort keys %listed) {
76 1.1 christos if ($f =~ /(.*)\.c$/) {
77 1.1 christos $base = $1;
78 1.1 christos if (! $listed{"./$base.o"}) {
79 1.1 christos print "O $f\n";
80 1.1 christos }
81 1.1 christos }
82 1.1 christos }
83 1.1 christos }
84 1.1 christos
85 1.1 christos ######################################################################
86 1.1 christos
87 1.1 christos sub undoc {
88 1.1 christos
89 1.1 christos opendir(S, $srcdir);
90 1.1 christos while ($file = readdir S) {
91 1.1 christos if ($file =~ /\.texi$/) {
92 1.1 christos open(T, "$srcdir/$file");
93 1.1 christos while (<T>) {
94 1.1 christos if (/^\@deftype[^\(]* ([^\s\(]+) *\(/) {
95 1.1 christos $documented{$1} = 1;
96 1.1 christos }
97 1.1 christos }
98 1.1 christos close(T);
99 1.1 christos }
100 1.1 christos if ($file =~ /\.c$/) {
101 1.1 christos open(C, "$srcdir/$file");
102 1.1 christos while (<C>) {
103 1.1 christos if (/\@undocumented (\S+)/) {
104 1.1 christos $documented{$1} = 1;
105 1.1 christos }
106 1.1 christos if (/^static /) {
107 1.1 christos if (! /[\(;]/) {
108 1.1 christos s/[\r\n]+$/ /;
109 1.1 christos $_ .= <C>;
110 1.1 christos }
111 1.1 christos while ($_ =~ /\([^\)]*$/) {
112 1.1 christos s/[\r\n]+$/ /;
113 1.1 christos $_ .= <C>;
114 1.1 christos }
115 1.1 christos }
116 1.1 christos s/ VPARAMS([ \(])/$1/;
117 1.1 christos s/PREFIX\(([^\)]*)\)/byte_$1/;
118 1.1 christos if (/^static [^\(]* ([^\s\(]+) *\(.*\)$/) {
119 1.1 christos $documented{$1} = 1;
120 1.1 christos }
121 1.1 christos }
122 1.1 christos }
123 1.1 christos }
124 1.1 christos closedir(D);
125 1.1 christos
126 1.1 christos # $out = join(' ', sort keys %documented);
127 1.1 christos # write;
128 1.1 christos # print "\n";
129 1.1 christos
130 1.1 christos system "etags $srcdir/*.c $srcdir/../include/*.h";
131 1.1 christos open(TAGS, "TAGS");
132 1.1 christos while (<TAGS>) {
133 1.1 christos s/[\r\n]+$//;
134 1.1 christos if (/^\014$/) {
135 1.1 christos $filename = <TAGS>;
136 1.1 christos $filename =~ s/[\r\n]+$//;
137 1.1 christos $filename =~ s/,\d+$//;
138 1.1 christos $filename =~ s@.*[/\\]@@;
139 1.1 christos next;
140 1.1 christos }
141 1.1 christos if ($filename =~ /\.c$/ ) {
142 1.1 christos next unless /^[_a-zA-Z]/;
143 1.1 christos } else {
144 1.1 christos next unless /^\# *define/;
145 1.1 christos s/\# *define *//;
146 1.1 christos }
147 1.1 christos
148 1.1 christos s/ VPARAMS//;
149 1.1 christos s/ *\177.*//;
150 1.1 christos s/,$//;
151 1.1 christos s/DEFUN\(//;
152 1.1 christos s/\(//;
153 1.1 christos
154 1.1 christos next if /^static /;
155 1.1 christos next if /\s/;
156 1.1 christos next if /^_/;
157 1.1 christos next if $documented{$_};
158 1.1 christos next if /_H_?$/;
159 1.1 christos
160 1.1 christos if ($seen_in{$_} ne $filename) {
161 1.1 christos $saw{$_} ++;
162 1.1 christos }
163 1.1 christos $seen_in{$_} = $filename;
164 1.1 christos }
165 1.1 christos
166 1.1 christos for $k (keys %saw) {
167 1.1 christos delete $saw{$k} if $saw{$k} > 1;
168 1.1 christos }
169 1.1 christos
170 1.1 christos for $k (sort keys %saw) {
171 1.1 christos $fromfile{$seen_in{$k}} .= " " if $fromfile{$seen_in{$k}};
172 1.1 christos $fromfile{$seen_in{$k}} .= $k;
173 1.1 christos }
174 1.1 christos
175 1.1 christos for $f (sort keys %fromfile) {
176 1.1 christos $out = "$f: $fromfile{$f}";
177 1.1 christos write;
178 1.1 christos }
179 1.1 christos }
180 1.1 christos
181 1.1 christos ######################################################################
182 1.1 christos
183 1.1 christos sub deps_for {
184 1.1 christos my($f) = @_;
185 1.1 christos my(%d);
186 1.1 christos open(F, $f);
187 1.1 christos %d = ();
188 1.1 christos while (<F>) {
189 1.1 christos if (/^#\s*include\s+["<](.*)[">]/) {
190 1.1 christos $d{$1} = 1;
191 1.1 christos }
192 1.1 christos }
193 1.1 christos close(F);
194 1.1 christos return keys %d;
195 1.1 christos }
196 1.1 christos
197 1.1 christos sub canonicalize {
198 1.1 christos my ($p) = @_;
199 1.1 christos 0 while $p =~ s@/\./@/@g;
200 1.1 christos 0 while $p =~ s@^\./@@g;
201 1.1 christos 0 while $p =~ s@/[^/]+/\.\./@/@g;
202 1.1 christos return $p;
203 1.1 christos }
204 1.1 christos
205 1.1 christos sub locals_first {
206 1.1 christos my ($a,$b) = @_;
207 1.1 christos return -1 if $a eq "config.h";
208 1.1 christos return 1 if $b eq "config.h";
209 1.1 christos return $a cmp $b;
210 1.1 christos }
211 1.1 christos
212 1.1 christos sub deps {
213 1.1 christos
214 1.1 christos $crule = "\tif [ x\"\$(PICFLAG)\" != x ]; then \\\n";
215 1.1 christos $crule .= "\t \$(COMPILE.c) \$(PICFLAG) \$< -o pic/\$@; \\\n";
216 1.1 christos $crule .= "\telse true; fi\n";
217 1.3 christos $crule .= "\tif [ x\"\$(NOASANFLAG)\" != x ]; then \\\n";
218 1.3 christos $crule .= "\t \$(COMPILE.c) \$(PICFLAG) \$(NOASANFLAG) \$< -o noasan/\$@; \\\n";
219 1.3 christos $crule .= "\telse true; fi\n";
220 1.1 christos $crule .= "\t\$(COMPILE.c) \$< \$(OUTPUT_OPTION)\n";
221 1.1 christos $crule .= "\n";
222 1.1 christos
223 1.1 christos $incdir = shift @ARGV;
224 1.1 christos
225 1.1 christos opendir(INC, $incdir);
226 1.1 christos while ($f = readdir INC) {
227 1.3 christos next unless $f =~ /\.h$/ || $f =~ /\.def$/;
228 1.1 christos $mine{$f} = "\$(INCDIR)/$f";
229 1.1 christos $deps{$f} = join(' ', &deps_for("$incdir/$f"));
230 1.1 christos }
231 1.1 christos $mine{'config.h'} = "config.h";
232 1.1 christos
233 1.1 christos opendir(INC, $srcdir);
234 1.1 christos while ($f = readdir INC) {
235 1.1 christos next unless $f =~ /\.h$/;
236 1.1 christos $mine{$f} = "\$(srcdir)/$f";
237 1.1 christos $deps{$f} = join(' ', &deps_for("$srcdir/$f"));
238 1.1 christos }
239 1.1 christos $mine{'config.h'} = "config.h";
240 1.1 christos
241 1.1 christos open(IN, "$srcdir/Makefile.in");
242 1.1 christos open(OUT, ">$srcdir/Makefile.tmp");
243 1.1 christos while (<IN>) {
244 1.1 christos last if /remainder of this file/;
245 1.1 christos print OUT;
246 1.1 christos }
247 1.1 christos print OUT "# The dependencies in the remainder of this file are automatically\n";
248 1.1 christos print OUT "# generated by \"make maint-deps\". Manual edits will be lost.\n\n";
249 1.1 christos
250 1.1 christos opendir(S, $srcdir);
251 1.1 christos for $f (sort readdir S) {
252 1.1 christos if ($f =~ /\.c$/) {
253 1.1 christos
254 1.1 christos %scanned = ();
255 1.1 christos @pending = &deps_for("$srcdir/$f");
256 1.1 christos while (@pending) {
257 1.1 christos @tmp = @pending;
258 1.1 christos @pending = ();
259 1.1 christos for $p (@tmp) {
260 1.1 christos next unless $mine{$p};
261 1.1 christos if (!$scanned{$p}) {
262 1.1 christos push(@pending, split(' ', $deps{$p}));
263 1.1 christos $scanned{$p} = 1;
264 1.1 christos }
265 1.1 christos }
266 1.1 christos }
267 1.1 christos @deps = sort { &locals_first($a,$b) } keys %scanned;
268 1.1 christos $obj = $f;
269 1.1 christos $obj =~ s/\.c$/.\$(objext)/;
270 1.1 christos $obj = "./$obj:";
271 1.1 christos if ($#deps >= 0) {
272 1.1 christos print OUT "$obj \$(srcdir)/$f";
273 1.1 christos $len = length("$obj $f");
274 1.1 christos for $dt (@deps) {
275 1.1 christos $d = $mine{$dt};
276 1.1 christos if ($len + length($d) > 70) {
277 1.1 christos printf OUT " \\\n\t$d";
278 1.1 christos $len = 8 + length($d);
279 1.1 christos } else {
280 1.1 christos print OUT " $d";
281 1.1 christos $len += length($d) + 1;
282 1.1 christos }
283 1.1 christos }
284 1.1 christos print OUT "\n";
285 1.1 christos } else {
286 1.1 christos print OUT "$obj \$(srcdir)/$f\n";
287 1.1 christos }
288 1.1 christos $c = $crule;
289 1.1 christos $c =~ s@\$\<@\$\(srcdir\)\/$f@g;
290 1.1 christos print OUT $c;
291 1.1 christos }
292 1.1 christos }
293 1.1 christos closedir(S);
294 1.1 christos close(IN);
295 1.1 christos close(OUT);
296 1.1 christos
297 1.1 christos rename("$srcdir/Makefile.tmp", "$srcdir/Makefile.in");
298 1.1 christos }
299