138032Speter#!/usr/bin/perl
298121Sgshapiro# -*- perl -*-
364562Sgshapiro
438032Speter#   Copyright (C) 2001
538032Speter#   Free Software Foundation
638032Speter#
738032Speter# This file is part of the libiberty library.
838032Speter# Libiberty is free software; you can redistribute it and/or
938032Speter# modify it under the terms of the GNU Library General Public
1038032Speter# License as published by the Free Software Foundation; either
1138032Speter# version 2 of the License, or (at your option) any later version.
1238032Speter#
1338032Speter# Libiberty is distributed in the hope that it will be useful,
1464562Sgshapiro# but WITHOUT ANY WARRANTY; without even the implied warranty of
1538032Speter# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16110560Sgshapiro# Library General Public License for more details.
1764562Sgshapiro#
1864562Sgshapiro# You should have received a copy of the GNU Library General Public
1964562Sgshapiro# License along with libiberty; see the file COPYING.LIB.  If not,
2090792Sgshapiro# write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
2190792Sgshapiro# Boston, MA 02110-1301, USA.
2290792Sgshapiro#
2364562Sgshapiro# Originally written by DJ Delorie <dj@redhat.com>
2490792Sgshapiro
2590792Sgshapiro
2638032Speter# This is a trivial script which checks the lists of C and O files in
2790792Sgshapiro# the Makefile for consistency.
2890792Sgshapiro
2990792Sgshapiro$mode = shift;
3090792Sgshapiro$srcdir = ".";
3190792Sgshapiro
3290792Sgshapiroif ($mode eq "-s") {
3390792Sgshapiro    $srcdir = shift;
3490792Sgshapiro    $mode = shift;
3590792Sgshapiro}
3690792Sgshapiro
3790792Sgshapiro&missing() if $mode eq "missing";
3890792Sgshapiro&undoc() if $mode eq "undoc";
3990792Sgshapiro&deps() if $mode eq "deps";
4090792Sgshapiro
4190792Sgshapiroexit 0;
4290792Sgshapiro
4390792Sgshapiroformat STDOUT =
4490792Sgshapiro^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~
4590792Sgshapiro$out
4690792Sgshapiro        ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
4790792Sgshapiro$out
4890792Sgshapiro.
4990792Sgshapiro
5090792Sgshapiro######################################################################
5190792Sgshapiro
5290792Sgshapirosub missing {
5390792Sgshapiro
5490792Sgshapiro    opendir(S, $srcdir);
5590792Sgshapiro    while ($f = readdir S) {
5690792Sgshapiro	$have{$f} = 1;
5790792Sgshapiro    }
5890792Sgshapiro    closedir(S);
5990792Sgshapiro    opendir(S, ".");
6090792Sgshapiro    while ($f = readdir S) {
6190792Sgshapiro	$have{$f} = 1;
6290792Sgshapiro    }
6390792Sgshapiro    closedir(S);
6490792Sgshapiro
6590792Sgshapiro    for $a (@ARGV) {
6690792Sgshapiro	$listed{$a} = 1;
6790792Sgshapiro	$have{$a} = 0;
6890792Sgshapiro    }
6990792Sgshapiro
7090792Sgshapiro    for $f (sort keys %have) {
7190792Sgshapiro	next unless $have{$f};
7290792Sgshapiro	if ($f =~ /\.c$/) {
7390792Sgshapiro	    print "S $f\n";
7490792Sgshapiro	}
7590792Sgshapiro    }
7690792Sgshapiro    for $f (sort keys %listed) {
7790792Sgshapiro	if ($f =~ /(.*)\.c$/) {
7890792Sgshapiro	    $base = $1;
7990792Sgshapiro	    if (! $listed{"$base.o"}) {
8090792Sgshapiro		print "O $f\n";
8190792Sgshapiro	    }
8290792Sgshapiro	}
8390792Sgshapiro    }
8490792Sgshapiro}
8590792Sgshapiro
8690792Sgshapiro######################################################################
8790792Sgshapiro
8890792Sgshapirosub undoc {
8990792Sgshapiro
9090792Sgshapiro    opendir(S, $srcdir);
9190792Sgshapiro    while ($file = readdir S) {
9290792Sgshapiro	if ($file =~ /\.texi$/) {
9390792Sgshapiro	    open(T, "$srcdir/$file");
9490792Sgshapiro	    while (<T>) {
9590792Sgshapiro		if (/^\@deftype[^\(]* ([^\s\(]+) *\(/) {
9690792Sgshapiro		    $documented{$1} = 1;
9790792Sgshapiro		}
9890792Sgshapiro	    }
9990792Sgshapiro	    close(T);
10090792Sgshapiro	}
10190792Sgshapiro	if ($file =~ /\.c$/) {
10290792Sgshapiro	    open(C, "$srcdir/$file");
10390792Sgshapiro	    while (<C>) {
10490792Sgshapiro		if (/\@undocumented (\S+)/) {
10590792Sgshapiro		    $documented{$1} = 1;
10690792Sgshapiro		}
10790792Sgshapiro		if (/^static /) {
10890792Sgshapiro		    if (! /[\(;]/) {
10990792Sgshapiro			s/[\r\n]+$/ /;
11090792Sgshapiro			$_ .= <C>;
11190792Sgshapiro		    }
11290792Sgshapiro		    while ($_ =~ /\([^\)]*$/) {
11390792Sgshapiro			s/[\r\n]+$/ /;
11490792Sgshapiro			$_ .= <C>;
11590792Sgshapiro		    }
11690792Sgshapiro		}
11790792Sgshapiro		s/ VPARAMS([ \(])/$1/;
11890792Sgshapiro		s/PREFIX\(([^\)]*)\)/byte_$1/;
11990792Sgshapiro		if (/^static [^\(]* ([^\s\(]+) *\(.*\)$/) {
12090792Sgshapiro		    $documented{$1} = 1;
12190792Sgshapiro		}
12290792Sgshapiro	    }
12390792Sgshapiro	}
12490792Sgshapiro    }
12590792Sgshapiro    closedir(D);
12690792Sgshapiro
12790792Sgshapiro    # $out = join(' ', sort keys %documented);
12890792Sgshapiro    # write;
12990792Sgshapiro    # print "\n";
13090792Sgshapiro
13190792Sgshapiro    system "etags $srcdir/*.c $srcdir/../include/*.h";
13290792Sgshapiro    open(TAGS, "TAGS");
13390792Sgshapiro    while (<TAGS>) {
13490792Sgshapiro	s/[\r\n]+$//;
13538032Speter	if (/^\014$/) {
13638032Speter	    $filename = <TAGS>;
13738032Speter	    $filename =~ s/[\r\n]+$//;
13838032Speter	    $filename =~ s/,\d+$//;
13938032Speter	    $filename =~ s@.*[/\\]@@;
14038032Speter	    next;
14138032Speter	}
14238032Speter	if ($filename =~ /\.c$/ ) {
14338032Speter	    next unless /^[_a-zA-Z]/;
14438032Speter	} else {
14538032Speter	    next unless /^\# *define/;
14638032Speter	    s/\# *define *//;
14738032Speter	}
14838032Speter
14938032Speter	s/ VPARAMS//;
15038032Speter	s/ *\177.*//;
15138032Speter	s/,$//;
15238032Speter	s/DEFUN\(//;
15338032Speter	s/\(//;
15438032Speter
15538032Speter	next if /^static /;
15638032Speter	next if /\s/;
15738032Speter	next if /^_/;
15838032Speter	next if $documented{$_};
15938032Speter	next if /_H_?$/;
16038032Speter
16138032Speter	if ($seen_in{$_} ne $filename) {
16238032Speter	    $saw{$_} ++;
16338032Speter	}
16438032Speter	$seen_in{$_} = $filename;
16538032Speter    }
16638032Speter
16738032Speter    for $k (keys %saw) {
16890792Sgshapiro	delete $saw{$k} if $saw{$k} > 1;
16990792Sgshapiro    }
17090792Sgshapiro
17190792Sgshapiro    for $k (sort keys %saw) {
17238032Speter	$fromfile{$seen_in{$k}} .= " " if $fromfile{$seen_in{$k}};
17390792Sgshapiro	$fromfile{$seen_in{$k}} .= $k;
17438032Speter    }
17538032Speter
17638032Speter    for $f (sort keys %fromfile) {
17738032Speter	$out = "$f: $fromfile{$f}";
17838032Speter	write;
17938032Speter    }
18038032Speter}
18138032Speter
18238032Speter######################################################################
18338032Speter
18490792Sgshapirosub deps_for {
18590792Sgshapiro    my($f) = @_;
18638032Speter    my(%d);
18738032Speter    open(F, $f);
18838032Speter    %d = ();
18938032Speter    while (<F>) {
19038032Speter	if (/^#\s*include\s+["<](.*)[">]/) {
19138032Speter	    $d{$1} = 1;
19238032Speter	}
19338032Speter    }
19438032Speter    close(F);
19538032Speter    return keys %d;
19638032Speter}
19738032Speter
19838032Spetersub canonicalize {
19938032Speter    my ($p) = @_;
20038032Speter    0 while $p =~ s@/\./@/@g;
20138032Speter    0 while $p =~ s@^\./@@g;
20238032Speter    0 while $p =~ s@/[^/]+/\.\./@/@g;
20364562Sgshapiro    return $p;
20438032Speter}
20564562Sgshapiro
20664562Sgshapirosub locals_first {
20738032Speter    my ($a,$b) = @_;
20890792Sgshapiro    return -1 if $a eq "config.h";
20938032Speter    return  1 if $b eq "config.h";
21090792Sgshapiro    return $a cmp $b;
21138032Speter}
21290792Sgshapiro
21338032Spetersub deps {
21490792Sgshapiro
21590792Sgshapiro    $crule  = "\tif [ x\"\$(PICFLAG)\" != x ]; then \\\n";
21638032Speter    $crule .= "\t  \$(COMPILE.c) \$(PICFLAG) \$< -o pic/\$@; \\\n";
21790792Sgshapiro    $crule .= "\telse true; fi\n";
21890792Sgshapiro    $crule .= "\t\$(COMPILE.c) \$< \$(OUTPUT_OPTION)\n";
21938032Speter    $crule .= "\n";
22090792Sgshapiro
22190792Sgshapiro    $incdir = shift @ARGV;
22290792Sgshapiro
22390792Sgshapiro    opendir(INC, $incdir);
22490792Sgshapiro    while ($f = readdir INC) {
22590792Sgshapiro	next unless $f =~ /\.h$/;
22690792Sgshapiro	$mine{$f} = "\$(INCDIR)/$f";
22790792Sgshapiro	$deps{$f} = join(' ', &deps_for("$incdir/$f"));
22890792Sgshapiro    }
22990792Sgshapiro    $mine{'config.h'} = "config.h";
23090792Sgshapiro
23190792Sgshapiro    opendir(INC, $srcdir);
23290792Sgshapiro    while ($f = readdir INC) {
23338032Speter	next unless $f =~ /\.h$/;
23490792Sgshapiro	$mine{$f} = "\$(srcdir)/$f";
23590792Sgshapiro	$deps{$f} = join(' ', &deps_for("$srcdir/$f"));
23690792Sgshapiro    }
23790792Sgshapiro    $mine{'config.h'} = "config.h";
23838032Speter
23990792Sgshapiro    open(IN, "$srcdir/Makefile.in");
24090792Sgshapiro    open(OUT, ">$srcdir/Makefile.tmp");
24190792Sgshapiro    while (<IN>) {
24290792Sgshapiro	last if /remainder of this file/;
24390792Sgshapiro	print OUT;
24490792Sgshapiro    }
24538032Speter    print OUT "# The dependencies in the remainder of this file are automatically\n";
24638032Speter    print OUT "# generated by \"make maint-deps\".  Manual edits will be lost.\n\n";
24790792Sgshapiro
24890792Sgshapiro    opendir(S, $srcdir);
24990792Sgshapiro    for $f (sort readdir S) {
25038032Speter	if ($f =~ /\.c$/) {
25190792Sgshapiro
25238032Speter	    %scanned = ();
25338032Speter	    @pending = &deps_for("$srcdir/$f");
25490792Sgshapiro	    while (@pending) {
25590792Sgshapiro		@tmp = @pending;
25638032Speter		@pending = ();
25790792Sgshapiro		for $p (@tmp) {
25890792Sgshapiro		    next unless $mine{$p};
25990792Sgshapiro		    if (!$scanned{$p}) {
26090792Sgshapiro			push(@pending, split(' ', $deps{$p}));
26190792Sgshapiro			$scanned{$p} = 1;
26290792Sgshapiro		    }
26390792Sgshapiro		}
26490792Sgshapiro	    }
26590792Sgshapiro	    @deps = sort { &locals_first($a,$b) } keys %scanned;
26690792Sgshapiro	    $obj = $f;
26790792Sgshapiro	    $obj =~ s/\.c$/.o/;
26838032Speter	    $obj = "./$obj:";
26990792Sgshapiro	    if ($#deps >= 0) {
27090792Sgshapiro		print OUT "$obj \$(srcdir)/$f";
27190792Sgshapiro		$len = length("$obj $f");
27290792Sgshapiro		for $dt (@deps) {
27390792Sgshapiro		    $d = $mine{$dt};
27490792Sgshapiro		    if ($len + length($d) > 70) {
27590792Sgshapiro			printf OUT " \\\n\t$d";
27690792Sgshapiro			$len = 8 + length($d);
27790792Sgshapiro		    } else {
27890792Sgshapiro			print OUT " $d";
27990792Sgshapiro			$len += length($d) + 1;
28090792Sgshapiro		    }
28138032Speter		}
28238032Speter		print OUT "\n";
28390792Sgshapiro	    } else {
28490792Sgshapiro		print OUT "$obj \$(srcdir)/$f\n";
28538032Speter	    }
28690792Sgshapiro	    $c = $crule;
28790792Sgshapiro	    $c =~ s@\$\<@\$\(srcdir\)\/$f@g;
28890792Sgshapiro	    print OUT $c;
28990792Sgshapiro	}
29038032Speter    }
29190792Sgshapiro    closedir(S);
29290792Sgshapiro    close(IN);
29390792Sgshapiro    close(OUT);
29438032Speter
29538032Speter    rename("$srcdir/Makefile.tmp", "$srcdir/Makefile.in");
29690792Sgshapiro}
29738032Speter