add-depends.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #! /usr/bin/env perl
  2. # Copyright 2018-2022 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. use strict;
  9. use warnings;
  10. use lib '.';
  11. use configdata;
  12. use File::Spec::Functions qw(:DEFAULT rel2abs);
  13. use File::Compare qw(compare_text);
  14. use feature 'state';
  15. # When using stat() on Windows, we can get it to perform better by avoid some
  16. # data. This doesn't affect the mtime field, so we're not losing anything...
  17. ${^WIN32_SLOPPY_STAT} = 1;
  18. my $debug = $ENV{ADD_DEPENDS_DEBUG};
  19. my $buildfile = $config{build_file};
  20. my $build_mtime = (stat($buildfile))[9];
  21. my $configdata_mtime = (stat('configdata.pm'))[9];
  22. my $rebuild = 0;
  23. my $depext = $target{dep_extension} || ".d";
  24. my @depfiles =
  25. sort
  26. grep {
  27. # This grep has side effects. Not only does if check the existence
  28. # of the dependency file given in $_, but it also checks if it's
  29. # newer than the build file or older than configdata.pm, and if it
  30. # is, sets $rebuild.
  31. my @st = stat($_);
  32. $rebuild = 1
  33. if @st && ($st[9] > $build_mtime || $st[9] < $configdata_mtime);
  34. scalar @st > 0; # Determines the grep result
  35. }
  36. map { (my $x = $_) =~ s|\.o$|$depext|; $x; }
  37. ( ( grep { $unified_info{sources}->{$_}->[0] =~ /\.cc?$/ }
  38. keys %{$unified_info{sources}} ),
  39. ( grep { $unified_info{shared_sources}->{$_}->[0] =~ /\.cc?$/ }
  40. keys %{$unified_info{shared_sources}} ) );
  41. exit 0 unless $rebuild;
  42. # Ok, primary checks are done, time to do some real work
  43. my $producer = shift @ARGV;
  44. die "Producer not given\n" unless $producer;
  45. my $srcdir = $config{sourcedir};
  46. my $blddir = $config{builddir};
  47. my $abs_srcdir = rel2abs($srcdir);
  48. my $abs_blddir = rel2abs($blddir);
  49. # Convenient cache of absolute to relative map. We start with filling it
  50. # with mappings for the known generated header files. They are relative to
  51. # the current working directory, so that's an easy task.
  52. # NOTE: there's more than C header files that are generated. They will also
  53. # generate entries in this map. We could of course deal with C header files
  54. # only, but in case we decide to handle more than just C files in the future,
  55. # we already have the mechanism in place here.
  56. # NOTE2: we lower case the index to make it searchable without regard for
  57. # character case. That could seem dangerous, but as long as we don't have
  58. # files we depend on in the same directory that only differ by character case,
  59. # we're fine.
  60. my %depconv_cache =
  61. map { catfile($abs_blddir, $_) => $_ }
  62. keys %{$unified_info{generate}};
  63. my %procedures = (
  64. 'gcc' =>
  65. sub {
  66. (my $objfile = shift) =~ s|\.d$|.o|i;
  67. my $line = shift;
  68. # Remove the original object file
  69. $line =~ s|^.*\.o: | |;
  70. # All we got now is a dependency, shave off surrounding spaces
  71. $line =~ s/^\s+//;
  72. $line =~ s/\s+$//;
  73. # Also, shave off any continuation
  74. $line =~ s/\s*\\$//;
  75. # Split the line into individual header files, and keep those
  76. # that exist in some form
  77. my @headers;
  78. for (split(/\s+/, $line)) {
  79. my $x = rel2abs($_);
  80. if (!$depconv_cache{$x}) {
  81. if (-f $x) {
  82. $depconv_cache{$x} = $_;
  83. }
  84. }
  85. if ($depconv_cache{$x}) {
  86. push @headers, $_;
  87. } else {
  88. print STDERR "DEBUG[$producer]: ignoring $objfile <- $line\n"
  89. if $debug;
  90. }
  91. }
  92. return ($objfile, join(' ', @headers)) if @headers;
  93. return undef;
  94. },
  95. 'makedepend' =>
  96. sub {
  97. # makedepend, in its infinite wisdom, wants to have the object file
  98. # in the same directory as the source file. This doesn't work too
  99. # well with out-of-source-tree builds, so we must resort to tricks
  100. # to get things right. Fortunately, the .d files are always placed
  101. # parallel with the object files, so all we need to do is construct
  102. # the object file name from the dep file name.
  103. (my $objfile = shift) =~ s|\.d$|.o|i;
  104. my $line = shift;
  105. # Discard comments
  106. return undef if $line =~ /^(#.*|\s*)$/;
  107. # Remove the original object file
  108. $line =~ s|^.*\.o: | |;
  109. # Also, remove any dependency that starts with a /, because those
  110. # are typically system headers
  111. $line =~ s/\s+\/(\\.|\S)*//g;
  112. # Finally, discard all empty lines
  113. return undef if $line =~ /^\s*$/;
  114. # All we got now is a dependency, just shave off surrounding spaces
  115. $line =~ s/^\s+//;
  116. $line =~ s/\s+$//;
  117. return ($objfile, $line);
  118. },
  119. 'VMS C' =>
  120. sub {
  121. state $abs_srcdir_shaved = undef;
  122. state $srcdir_shaved = undef;
  123. unless (defined $abs_srcdir_shaved) {
  124. ($abs_srcdir_shaved = $abs_srcdir) =~ s|[>\]]$||;
  125. ($srcdir_shaved = $srcdir) =~ s|[>\]]$||;
  126. }
  127. # current versions of DEC / Compaq / HP / VSI C strips away all
  128. # directory information from the object file, so we must insert it
  129. # back. To make life simpler, we simply replace it with the
  130. # corresponding .D file that's had its extension changed. Since
  131. # .D files are always written parallel to the object files, we
  132. # thereby get the directory information for free.
  133. (my $objfile = shift) =~ s|\.D$|.OBJ|i;
  134. my $line = shift;
  135. # Shave off the target.
  136. #
  137. # The pattern for target and dependencies will always take this
  138. # form:
  139. #
  140. # target SPACE : SPACE deps
  141. #
  142. # This is so a volume delimiter (a : without any spaces around it)
  143. # won't get mixed up with the target / deps delimiter. We use this
  144. # to easily identify what needs to be removed.
  145. m|\s:\s|; $line = $';
  146. # We know that VMS has system header files in text libraries,
  147. # extension .TLB. We also know that our header files aren't stored
  148. # in text libraries. Finally, we know that VMS C produces exactly
  149. # one dependency per line, so we simply discard any line ending with
  150. # .TLB.
  151. return undef if /\.TLB\s*$/;
  152. # All we got now is a dependency, just shave off surrounding spaces
  153. $line =~ s/^\s+//;
  154. $line =~ s/\s+$//;
  155. # VMS C gives us absolute paths, always. Let's see if we can
  156. # make them relative instead.
  157. $line = canonpath($line);
  158. unless (defined $depconv_cache{$line}) {
  159. my $dep = $line;
  160. # Since we have already pre-populated the cache with
  161. # mappings for generated headers, we only need to deal
  162. # with the source tree.
  163. if ($dep =~ s|^\Q$abs_srcdir_shaved\E([\.>\]])?|$srcdir_shaved$1|i) {
  164. # Also check that the header actually exists
  165. if (-f $line) {
  166. $depconv_cache{$line} = $dep;
  167. }
  168. }
  169. }
  170. return ($objfile, $depconv_cache{$line})
  171. if defined $depconv_cache{$line};
  172. print STDERR "DEBUG[$producer]: ignoring $objfile <- $line\n"
  173. if $debug;
  174. return undef;
  175. },
  176. 'VC' =>
  177. sub {
  178. # With Microsoft Visual C the flags /Zs /showIncludes give us the
  179. # necessary output to be able to create dependencies that nmake
  180. # (or any 'make' implementation) should be able to read, with a
  181. # bit of help. The output we're interested in looks something
  182. # like this (it always starts the same)
  183. #
  184. # Note: including file: {whatever header file}
  185. #
  186. # This output is localized, so for example, the German pack gives
  187. # us this:
  188. #
  189. # Hinweis: Einlesen der Datei: {whatever header file}
  190. #
  191. # To accommodate, we need to use a very general regular expression
  192. # to parse those lines.
  193. #
  194. # Since there's no object file name at all in that information,
  195. # we must construct it ourselves.
  196. (my $objfile = shift) =~ s|\.d$|.obj|i;
  197. my $line = shift;
  198. # There are also other lines mixed in, for example compiler
  199. # warnings, so we simply discard anything that doesn't start with
  200. # the Note:
  201. if (/^[^:]*: [^:]*: */) {
  202. (my $tail = $') =~ s/\s*\R$//;
  203. # VC gives us absolute paths for all include files, so to
  204. # remove system header dependencies, we need to check that
  205. # they don't match $abs_srcdir or $abs_blddir.
  206. $tail = canonpath($tail);
  207. unless (defined $depconv_cache{$tail}) {
  208. my $dep = $tail;
  209. # Since we have already pre-populated the cache with
  210. # mappings for generated headers, we only need to deal
  211. # with the source tree.
  212. if ($dep =~ s|^\Q$abs_srcdir\E\\|\$(SRCDIR)\\|i) {
  213. # Also check that the header actually exists
  214. if (-f $line) {
  215. $depconv_cache{$tail} = $dep;
  216. }
  217. }
  218. }
  219. return ($objfile, '"'.$depconv_cache{$tail}.'"')
  220. if defined $depconv_cache{$tail};
  221. print STDERR "DEBUG[$producer]: ignoring $objfile <- $tail\n"
  222. if $debug;
  223. }
  224. return undef;
  225. },
  226. 'embarcadero' =>
  227. sub {
  228. # With Embarcadero C++Builder's preprocessor (cpp32.exe) the -Sx -Hp
  229. # flags give us the list of #include files read, like the following:
  230. #
  231. # Including ->->{whatever header file}
  232. #
  233. # where each "->" indicates the nesting level of the #include. The
  234. # logic here is otherwise the same as the 'VC' scheme.
  235. #
  236. # Since there's no object file name at all in that information,
  237. # we must construct it ourselves.
  238. (my $objfile = shift) =~ s|\.d$|.obj|i;
  239. my $line = shift;
  240. # There are also other lines mixed in, for example compiler
  241. # warnings, so we simply discard anything that doesn't start with
  242. # the Note:
  243. if (/^Including (->)*/) {
  244. (my $tail = $') =~ s/\s*\R$//;
  245. # C++Builder gives us relative paths when possible, so to
  246. # remove system header dependencies, we convert them to
  247. # absolute paths and check that they don't match $abs_srcdir
  248. # or $abs_blddir, just as the 'VC' scheme.
  249. $tail = rel2abs($tail);
  250. unless (defined $depconv_cache{$tail}) {
  251. my $dep = $tail;
  252. # Since we have already pre-populated the cache with
  253. # mappings for generated headers, we only need to deal
  254. # with the source tree.
  255. if ($dep =~ s|^\Q$abs_srcdir\E\\|\$(SRCDIR)\\|i) {
  256. # Also check that the header actually exists
  257. if (-f $line) {
  258. $depconv_cache{$tail} = $dep;
  259. }
  260. }
  261. }
  262. return ($objfile, '"'.$depconv_cache{$tail}.'"')
  263. if defined $depconv_cache{$tail};
  264. print STDERR "DEBUG[$producer]: ignoring $objfile <- $tail\n"
  265. if $debug;
  266. }
  267. return undef;
  268. },
  269. );
  270. my %continuations = (
  271. 'gcc' => "\\",
  272. 'makedepend' => "\\",
  273. 'VMS C' => "-",
  274. 'VC' => "\\",
  275. 'embarcadero' => "\\",
  276. );
  277. die "Producer unrecognised: $producer\n"
  278. unless exists $procedures{$producer} && exists $continuations{$producer};
  279. my $procedure = $procedures{$producer};
  280. my $continuation = $continuations{$producer};
  281. my $buildfile_new = "$buildfile-$$";
  282. my %collect = ();
  283. foreach my $depfile (@depfiles) {
  284. open IDEP,$depfile or die "Trying to read $depfile: $!\n";
  285. while (<IDEP>) {
  286. s|\R$||; # The better chomp
  287. my ($target, $deps) = $procedure->($depfile, $_);
  288. $collect{$target}->{$deps} = 1 if defined $target;
  289. }
  290. close IDEP;
  291. }
  292. open IBF, $buildfile or die "Trying to read $buildfile: $!\n";
  293. open OBF, '>', $buildfile_new or die "Trying to write $buildfile_new: $!\n";
  294. while (<IBF>) {
  295. last if /^# DO NOT DELETE THIS LINE/;
  296. print OBF or die "$!\n";
  297. }
  298. close IBF;
  299. print OBF "# DO NOT DELETE THIS LINE -- make depend depends on it.\n";
  300. foreach my $target (sort keys %collect) {
  301. my $prefix = $target . ' :';
  302. my @deps = sort keys %{$collect{$target}};
  303. while (@deps) {
  304. my $buf = $prefix;
  305. $prefix = '';
  306. while (@deps && ($buf eq ''
  307. || length($buf) + length($deps[0]) <= 77)) {
  308. $buf .= ' ' . shift @deps;
  309. }
  310. $buf .= ' '.$continuation if @deps;
  311. print OBF $buf,"\n" or die "Trying to print: $!\n"
  312. }
  313. }
  314. close OBF;
  315. if (compare_text($buildfile_new, $buildfile) != 0) {
  316. rename $buildfile_new, $buildfile
  317. or die "Trying to rename $buildfile_new -> $buildfile: $!\n";
  318. }
  319. END {
  320. # On VMS, we want to remove all generations of this file, in case there
  321. # are more than one, so we loop.
  322. if (defined $buildfile_new) {
  323. while (unlink $buildfile_new) {}
  324. }
  325. }