add-depends.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #! /usr/bin/env perl
  2. # Copyright 2018-2021 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 $rebuild = 0;
  22. my $depext = $target{dep_extension} || ".d";
  23. my @depfiles =
  24. sort
  25. grep {
  26. # This grep has side effects. Not only does if check the existence
  27. # of the dependency file given in $_, but it also checks if it's
  28. # newer than the build file, and if it is, sets $rebuild.
  29. my @st = stat($_);
  30. $rebuild = 1 if @st && $st[9] > $build_mtime;
  31. scalar @st > 0; # Determines the grep result
  32. }
  33. map { (my $x = $_) =~ s|\.o$|$depext|; $x; }
  34. ( ( grep { $unified_info{sources}->{$_}->[0] =~ /\.cc?$/ }
  35. keys %{$unified_info{sources}} ),
  36. ( grep { $unified_info{shared_sources}->{$_}->[0] =~ /\.cc?$/ }
  37. keys %{$unified_info{shared_sources}} ) );
  38. exit 0 unless $rebuild;
  39. # Ok, primary checks are done, time to do some real work
  40. my $producer = shift @ARGV;
  41. die "Producer not given\n" unless $producer;
  42. my $srcdir = $config{sourcedir};
  43. my $blddir = $config{builddir};
  44. my $abs_srcdir = rel2abs($srcdir);
  45. my $abs_blddir = rel2abs($blddir);
  46. # Convenient cache of absolute to relative map. We start with filling it
  47. # with mappings for the known generated header files. They are relative to
  48. # the current working directory, so that's an easy task.
  49. # NOTE: there's more than C header files that are generated. They will also
  50. # generate entries in this map. We could of course deal with C header files
  51. # only, but in case we decide to handle more than just C files in the future,
  52. # we already have the mechanism in place here.
  53. # NOTE2: we lower case the index to make it searchable without regard for
  54. # character case. That could seem dangerous, but as long as we don't have
  55. # files we depend on in the same directory that only differ by character case,
  56. # we're fine.
  57. my %depconv_cache =
  58. map { catfile($abs_blddir, $_) => $_ }
  59. keys %{$unified_info{generate}};
  60. my %procedures = (
  61. 'gcc' => undef, # gcc style dependency files needs no mods
  62. 'makedepend' =>
  63. sub {
  64. # makedepend, in its infinite wisdom, wants to have the object file
  65. # in the same directory as the source file. This doesn't work too
  66. # well with out-of-source-tree builds, so we must resort to tricks
  67. # to get things right. Fortunately, the .d files are always placed
  68. # parallel with the object files, so all we need to do is construct
  69. # the object file name from the dep file name.
  70. (my $objfile = shift) =~ s|\.d$|.o|i;
  71. my $line = shift;
  72. # Discard comments
  73. return undef if $line =~ /^(#.*|\s*)$/;
  74. # Remove the original object file
  75. $line =~ s|^.*\.o: | |;
  76. # Also, remove any dependency that starts with a /, because those
  77. # are typically system headers
  78. $line =~ s/\s+\/(\\.|\S)*//g;
  79. # Finally, discard all empty lines
  80. return undef if $line =~ /^\s*$/;
  81. # All we got now is a dependency, just shave off surrounding spaces
  82. $line =~ s/^\s+//;
  83. $line =~ s/\s+$//;
  84. return ($objfile, $line);
  85. },
  86. 'VMS C' =>
  87. sub {
  88. state $abs_srcdir_shaved = undef;
  89. state $srcdir_shaved = undef;
  90. unless (defined $abs_srcdir_shaved) {
  91. ($abs_srcdir_shaved = $abs_srcdir) =~ s|[>\]]$||;
  92. ($srcdir_shaved = $srcdir) =~ s|[>\]]$||;
  93. }
  94. # current versions of DEC / Compaq / HP / VSI C strips away all
  95. # directory information from the object file, so we must insert it
  96. # back. To make life simpler, we simply replace it with the
  97. # corresponding .D file that's had its extension changed. Since
  98. # .D files are always written parallel to the object files, we
  99. # thereby get the directory information for free.
  100. (my $objfile = shift) =~ s|\.D$|.OBJ|i;
  101. my $line = shift;
  102. # Shave off the target.
  103. #
  104. # The pattern for target and dependencies will always take this
  105. # form:
  106. #
  107. # target SPACE : SPACE deps
  108. #
  109. # This is so a volume delimiter (a : without any spaces around it)
  110. # won't get mixed up with the target / deps delimiter. We use this
  111. # to easily identify what needs to be removed.
  112. m|\s:\s|; $line = $';
  113. # We know that VMS has system header files in text libraries,
  114. # extension .TLB. We also know that our header files aren't stored
  115. # in text libraries. Finally, we know that VMS C produces exactly
  116. # one dependency per line, so we simply discard any line ending with
  117. # .TLB.
  118. return undef if /\.TLB\s*$/;
  119. # All we got now is a dependency, just shave off surrounding spaces
  120. $line =~ s/^\s+//;
  121. $line =~ s/\s+$//;
  122. # VMS C gives us absolute paths, always. Let's see if we can
  123. # make them relative instead.
  124. $line = canonpath($line);
  125. unless (defined $depconv_cache{$line}) {
  126. my $dep = $line;
  127. # Since we have already pre-populated the cache with
  128. # mappings for generated headers, we only need to deal
  129. # with the source tree.
  130. if ($dep =~ s|^\Q$abs_srcdir_shaved\E([\.>\]])?|$srcdir_shaved$1|i) {
  131. $depconv_cache{$line} = $dep;
  132. }
  133. }
  134. return ($objfile, $depconv_cache{$line})
  135. if defined $depconv_cache{$line};
  136. print STDERR "DEBUG[VMS C]: ignoring $objfile <- $line\n"
  137. if $debug;
  138. return undef;
  139. },
  140. 'VC' =>
  141. sub {
  142. # On Windows, with Microsoft Visual C the flags /Zs /showIncludes
  143. # give us the necessary output to be able to create dependencies
  144. # that nmake (or any 'make' implementation) should be able to read,
  145. # with a bit of help. The output we're interested in looks like
  146. # this (it always starts the same)
  147. #
  148. # Note: including file: {whatever header file}
  149. #
  150. # With Embarcadero C++Builder's preprocessor (cpp32.exe) the -Hp
  151. # flag gives us the preprocessed output annotated with the following
  152. # note whenever a #include file is read:
  153. #
  154. # Including ->->{whatever header file}
  155. #
  156. # where each "->" indicates the nesting level of the #include. The
  157. # logic here is otherwise the same as the 'VC' case.
  158. #
  159. # Since there's no object file name at all in that information,
  160. # we must construct it ourselves.
  161. (my $objfile = shift) =~ s|\.d$|.obj|i;
  162. my $line = shift;
  163. # There are also other lines mixed in, for example compiler
  164. # warnings, so we simply discard anything that doesn't start with
  165. # the Note:
  166. if (/^Note: including file: */ or /^Including (->)*/) {
  167. (my $tail = $') =~ s/\s*\R$//;
  168. # VC gives us absolute paths for all include files, so to
  169. # remove system header dependencies, we need to check that
  170. # they don't match $abs_srcdir or $abs_blddir. C++Builder gives
  171. # us relative paths when possible, so convert to absolute paths.
  172. $tail = rel2abs($tail);
  173. unless (defined $depconv_cache{$tail}) {
  174. my $dep = $tail;
  175. # Since we have already pre-populated the cache with
  176. # mappings for generated headers, we only need to deal
  177. # with the source tree.
  178. if ($dep =~ s|^\Q$abs_srcdir\E\\|\$(SRCDIR)\\|i) {
  179. $depconv_cache{$tail} = $dep;
  180. }
  181. }
  182. return ($objfile, '"'.$depconv_cache{$tail}.'"')
  183. if defined $depconv_cache{$tail};
  184. print STDERR "DEBUG[VC]: ignoring $objfile <- $tail\n"
  185. if $debug;
  186. }
  187. return undef;
  188. },
  189. );
  190. my %continuations = (
  191. 'gcc' => undef,
  192. 'makedepend' => "\\",
  193. 'VMS C' => "-",
  194. 'VC' => "\\",
  195. );
  196. die "Producer unrecognised: $producer\n"
  197. unless exists $procedures{$producer} && exists $continuations{$producer};
  198. my $procedure = $procedures{$producer};
  199. my $continuation = $continuations{$producer};
  200. my $buildfile_new = "$buildfile-$$";
  201. my %collect = ();
  202. if (defined $procedure) {
  203. foreach my $depfile (@depfiles) {
  204. open IDEP,$depfile or die "Trying to read $depfile: $!\n";
  205. while (<IDEP>) {
  206. s|\R$||; # The better chomp
  207. my ($target, $deps) = $procedure->($depfile, $_);
  208. $collect{$target}->{$deps} = 1 if defined $target;
  209. }
  210. close IDEP;
  211. }
  212. }
  213. open IBF, $buildfile or die "Trying to read $buildfile: $!\n";
  214. open OBF, '>', $buildfile_new or die "Trying to write $buildfile_new: $!\n";
  215. while (<IBF>) {
  216. last if /^# DO NOT DELETE THIS LINE/;
  217. print OBF or die "$!\n";
  218. }
  219. close IBF;
  220. print OBF "# DO NOT DELETE THIS LINE -- make depend depends on it.\n";
  221. if (defined $procedure) {
  222. foreach my $target (sort keys %collect) {
  223. my $prefix = $target . ' :';
  224. my @deps = sort keys %{$collect{$target}};
  225. while (@deps) {
  226. my $buf = $prefix;
  227. $prefix = '';
  228. while (@deps && ($buf eq ''
  229. || length($buf) + length($deps[0]) <= 77)) {
  230. $buf .= ' ' . shift @deps;
  231. }
  232. $buf .= ' '.$continuation if @deps;
  233. print OBF $buf,"\n" or die "Trying to print: $!\n"
  234. }
  235. }
  236. } else {
  237. foreach my $depfile (@depfiles) {
  238. open IDEP,$depfile or die "Trying to read $depfile: $!\n";
  239. while (<IDEP>) {
  240. print OBF or die "Trying to print: $!\n";
  241. }
  242. close IDEP;
  243. }
  244. }
  245. close OBF;
  246. if (compare_text($buildfile_new, $buildfile) != 0) {
  247. rename $buildfile_new, $buildfile
  248. or die "Trying to rename $buildfile_new -> $buildfile: $!\n";
  249. }
  250. END {
  251. # On VMS, we want to remove all generations of this file, in case there
  252. # are more than one, so we loop.
  253. if (defined $buildfile_new) {
  254. while (unlink $buildfile_new) {}
  255. }
  256. }