maint-tool 6.5 KB

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