maint-tool 6.5 KB

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