maint-tool revision 302408
12061Sjkh#!/usr/bin/perl
250479Speter# -*- perl -*-
32061Sjkh
438666Sjb#   Copyright (C) 2001
532427Sjb#   Free Software Foundation
6111131Sru#
7111131Sru# This file is part of the libiberty library.
8217733Sbz# Libiberty is free software; you can redistribute it and/or
9217733Sbz# modify it under the terms of the GNU Library General Public
1038666Sjb# License as published by the Free Software Foundation; either
1138666Sjb# version 2 of the License, or (at your option) any later version.
1238666Sjb#
13159363Strhodes# Libiberty is distributed in the hope that it will be useful,
1464049Salex# but WITHOUT ANY WARRANTY; without even the implied warranty of
1564049Salex# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16116679Ssimokawa# Library General Public License for more details.
1766071Smarkm#
18116679Ssimokawa# You should have received a copy of the GNU Library General Public
1973504Sobrien# License along with libiberty; see the file COPYING.LIB.  If not,
20204661Simp# write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
21158962Snetchild# Boston, MA 02110-1301, USA.
2238666Sjb#
23169597Sdes# Originally written by DJ Delorie <dj@redhat.com>
24169597Sdes
25169597Sdes
26169597Sdes# This is a trivial script which checks the lists of C and O files in
27169597Sdes# the Makefile for consistency.
28169597Sdes
29169597Sdes$mode = shift;
30169597Sdes$srcdir = ".";
31217125Simp
3232427Sjbif ($mode eq "-s") {
3338666Sjb    $srcdir = shift;
34108451Sschweikh    $mode = shift;
3538666Sjb}
3638666Sjb
3738666Sjb&missing() if $mode eq "missing";
3838666Sjb&undoc() if $mode eq "undoc";
3917308Speter&deps() if $mode eq "deps";
40217273Simp
41217294Simpexit 0;
4219175Sbde
4396205Sjwdformat STDOUT =
44217297Simp^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~
45217297Simp$out
4638042Sbde        ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
4796205Sjwd$out
4896205Sjwd.
4938042Sbde
5096205Sjwd######################################################################
51159363Strhodes
52159363Strhodessub missing {
5317308Speter
5496205Sjwd    opendir(S, $srcdir);
5596205Sjwd    while ($f = readdir S) {
5617308Speter	$have{$f} = 1;
57148330Snetchild    }
58148330Snetchild    closedir(S);
59148330Snetchild    opendir(S, ".");
60148330Snetchild    while ($f = readdir S) {
61159831Sobrien	$have{$f} = 1;
62148330Snetchild    }
63148330Snetchild    closedir(S);
64148330Snetchild
65148330Snetchild    for $a (@ARGV) {
66178653Srwatson	$listed{$a} = 1;
67148330Snetchild	$have{$a} = 0;
68148330Snetchild    }
6996205Sjwd
7096205Sjwd    for $f (sort keys %have) {
7196205Sjwd	next unless $have{$f};
72162147Sru	if ($f =~ /\.c$/) {
73162147Sru	    print "S $f\n";
7498723Sdillon	}
7598723Sdillon    }
7698723Sdillon    for $f (sort keys %listed) {
7738666Sjb	if ($f =~ /(.*)\.c$/) {
7838666Sjb	    $base = $1;
7917308Speter	    if (! $listed{"$base.o"}) {
80123311Speter		print "O $f\n";
81123311Speter	    }
82123311Speter	}
83123311Speter    }
84175833Sjhb}
85175833Sjhb
86169597Sdes######################################################################
87169597Sdes
88169597Sdessub undoc {
89169597Sdes
90159349Simp    opendir(S, $srcdir);
91158962Snetchild    while ($file = readdir S) {
92158962Snetchild	if ($file =~ /\.texi$/) {
93158962Snetchild	    open(T, "$srcdir/$file");
94156840Sru	    while (<T>) {
95123311Speter		if (/^\@deftype[^\(]* ([^\s\(]+) *\(/) {
96137288Speter		    $documented{$1} = 1;
97209128Sraj		}
98209128Sraj	    }
99156740Sru	    close(T);
1002061Sjkh	}
10197769Sru	if ($file =~ /\.c$/) {
10297252Sru	    open(C, "$srcdir/$file");
103119579Sru	    while (<C>) {
10497252Sru		if (/\@undocumented (\S+)/) {
10595730Sru		    $documented{$1} = 1;
10695793Sru		}
107111617Sru		if (/^static /) {
10895730Sru		    if (! /[\(;]/) {
109116679Ssimokawa			s/[\r\n]+$/ /;
11095730Sru			$_ .= <C>;
111116679Ssimokawa		    }
11295730Sru		    while ($_ =~ /\([^\)]*$/) {
113110035Sru			s/[\r\n]+$/ /;
114107516Sru			$_ .= <C>;
115138921Sru		    }
116156145Syar		}
117138921Sru		s/ VPARAMS([ \(])/$1/;
118133942Sru		s/PREFIX\(([^\)]*)\)/byte_$1/;
119133942Sru		if (/^static [^\(]* ([^\s\(]+) *\(.*\)$/) {
120156145Syar		    $documented{$1} = 1;
121133942Sru		}
122110035Sru	    }
123117234Sru	}
124110035Sru    }
125117229Sru    closedir(D);
126117234Sru
12754324Smarcel    # $out = join(' ', sort keys %documented);
12817308Speter    # write;
129119519Smarcel    # print "\n";
130119519Smarcel
131119519Smarcel    system "etags $srcdir/*.c $srcdir/../include/*.h";
132119519Smarcel    open(TAGS, "TAGS");
133119519Smarcel    while (<TAGS>) {
134119519Smarcel	s/[\r\n]+$//;
135119579Sru	if (/^\014$/) {
136119519Smarcel	    $filename = <TAGS>;
137119519Smarcel	    $filename =~ s/[\r\n]+$//;
138119519Smarcel	    $filename =~ s/,\d+$//;
139119519Smarcel	    $filename =~ s@.*[/\\]@@;
140119519Smarcel	    next;
141126031Sgad	}
142126024Sgad	if ($filename =~ /\.c$/ ) {
143126024Sgad	    next unless /^[_a-zA-Z]/;
144126024Sgad	} else {
145126024Sgad	    next unless /^\# *define/;
146126024Sgad	    s/\# *define *//;
147126024Sgad	}
148126024Sgad
149126024Sgad	s/ VPARAMS//;
150126024Sgad	s/ *\177.*//;
151126024Sgad	s/,$//;
152126024Sgad	s/DEFUN\(//;
153126024Sgad	s/\(//;
154126024Sgad
155126031Sgad	next if /^static /;
156126024Sgad	next if /\s/;
157126024Sgad	next if /^_/;
158126024Sgad	next if $documented{$_};
159172744Sdelphij	next if /_H_?$/;
160126024Sgad
161126024Sgad	if ($seen_in{$_} ne $filename) {
162126024Sgad	    $saw{$_} ++;
163133376Sharti	}
164126024Sgad	$seen_in{$_} = $filename;
165126024Sgad    }
166172744Sdelphij
167126024Sgad    for $k (keys %saw) {
168126024Sgad	delete $saw{$k} if $saw{$k} > 1;
169125885Sgad    }
170125885Sgad
17138666Sjb    for $k (sort keys %saw) {
17217308Speter	$fromfile{$seen_in{$k}} .= " " if $fromfile{$seen_in{$k}};
173119519Smarcel	$fromfile{$seen_in{$k}} .= $k;
174119579Sru    }
175133376Sharti
176110035Sru    for $f (sort keys %fromfile) {
1772302Spaul	$out = "$f: $fromfile{$f}";
17839206Sjkh	write;
17939206Sjkh    }
18039206Sjkh}
181133945Sru
182177609Sru######################################################################
183177609Sru
184177609Srusub deps_for {
185177609Sru    my($f) = @_;
186133945Sru    my(%d);
187132358Smarkm    open(F, $f);
18817308Speter    %d = ();
18954324Smarcel    while (<F>) {
19054324Smarcel	if (/^#\s*include\s+["<](.*)[">]/) {
191132234Smarcel	    $d{$1} = 1;
192132234Smarcel	}
193132234Smarcel    }
194132234Smarcel    close(F);
19554324Smarcel    return keys %d;
19654324Smarcel}
19754324Smarcel
198118531Srusub canonicalize {
19954324Smarcel    my ($p) = @_;
20054324Smarcel    0 while $p =~ s@/\./@/@g;
20154324Smarcel    0 while $p =~ s@^\./@@g;
20254324Smarcel    0 while $p =~ s@/[^/]+/\.\./@/@g;
20354324Smarcel    return $p;
20454324Smarcel}
205133376Sharti
20654324Smarcelsub locals_first {
207133376Sharti    my ($a,$b) = @_;
208133376Sharti    return -1 if $a eq "config.h";
20954324Smarcel    return  1 if $b eq "config.h";
21054324Smarcel    return $a cmp $b;
21154324Smarcel}
21254324Smarcel
21354324Smarcelsub deps {
214133376Sharti
21554324Smarcel    $crule  = "\tif [ x\"\$(PICFLAG)\" != x ]; then \\\n";
21654324Smarcel    $crule .= "\t  \$(COMPILE.c) \$(PICFLAG) \$< -o pic/\$@; \\\n";
21754324Smarcel    $crule .= "\telse true; fi\n";
218118531Sru    $crule .= "\t\$(COMPILE.c) \$< \$(OUTPUT_OPTION)\n";
219118531Sru    $crule .= "\n";
22054324Smarcel
221132234Smarcel    $incdir = shift @ARGV;
222132234Smarcel
223132234Smarcel    opendir(INC, $incdir);
224132234Smarcel    while ($f = readdir INC) {
225132234Smarcel	next unless $f =~ /\.h$/;
226132588Skensmith	$mine{$f} = "\$(INCDIR)/$f";
227132358Smarkm	$deps{$f} = join(' ', &deps_for("$incdir/$f"));
228132234Smarcel    }
229132358Smarkm    $mine{'config.h'} = "config.h";
230132234Smarcel
231132234Smarcel    opendir(INC, $srcdir);
232132234Smarcel    while ($f = readdir INC) {
23354324Smarcel	next unless $f =~ /\.h$/;
23454324Smarcel	$mine{$f} = "\$(srcdir)/$f";
23595730Sru	$deps{$f} = join(' ', &deps_for("$srcdir/$f"));
23695730Sru    }
23795730Sru    $mine{'config.h'} = "config.h";
23895730Sru
23995730Sru    open(IN, "$srcdir/Makefile.in");
24095730Sru    open(OUT, ">$srcdir/Makefile.tmp");
24195730Sru    while (<IN>) {
24238666Sjb	last if /remainder of this file/;
243107374Sru	print OUT;
24417308Speter    }
24555678Smarcel    print OUT "# The dependencies in the remainder of this file are automatically\n";
246143032Sharti    print OUT "# generated by \"make maint-deps\".  Manual edits will be lost.\n\n";
247138515Sharti
248117793Sru    opendir(S, $srcdir);
249110035Sru    for $f (sort readdir S) {
250174564Simp	if ($f =~ /\.c$/) {
251110035Sru
2522061Sjkh	    %scanned = ();
25317308Speter	    @pending = &deps_for("$srcdir/$f");
254107516Sru	    while (@pending) {
255174539Simp		@tmp = @pending;
256174539Simp		@pending = ();
25755678Smarcel		for $p (@tmp) {
258107516Sru		    next unless $mine{$p};
259107516Sru		    if (!$scanned{$p}) {
260107516Sru			push(@pending, split(' ', $deps{$p}));
261174564Simp			$scanned{$p} = 1;
262107516Sru		    }
263139112Sru		}
264164470Sjb	    }
265107516Sru	    @deps = sort { &locals_first($a,$b) } keys %scanned;
266122204Skris	    $obj = $f;
26755678Smarcel	    $obj =~ s/\.c$/.o/;
26855678Smarcel	    $obj = "./$obj:";
269116696Sru	    if ($#deps >= 0) {
27055678Smarcel		print OUT "$obj \$(srcdir)/$f";
271133376Sharti		$len = length("$obj $f");
272107516Sru		for $dt (@deps) {
273107516Sru		    $d = $mine{$dt};
274107516Sru		    if ($len + length($d) > 70) {
275107516Sru			printf OUT " \\\n\t$d";
27655678Smarcel			$len = 8 + length($d);
277185499Salfred		    } else {
278217754Sbz			print OUT " $d";
279216487Sjhb			$len += length($d) + 1;
280185499Salfred		    }
28155678Smarcel		}
282111131Sru		print OUT "\n";
283111131Sru	    } else {
284111131Sru		print OUT "$obj \$(srcdir)/$f\n";
285133945Sru	    }
286111131Sru	    $c = $crule;
287111131Sru	    $c =~ s@\$\<@\$\(srcdir\)\/$f@g;
288217125Simp	    print OUT $c;
289216520Snwhitehorn	}
290216520Snwhitehorn    }
291217123Simp    closedir(S);
292216520Snwhitehorn    close(IN);
293216520Snwhitehorn    close(OUT);
294216520Snwhitehorn
295216520Snwhitehorn    rename("$srcdir/Makefile.tmp", "$srcdir/Makefile.in");
296216520Snwhitehorn}
297216520Snwhitehorn