add-depends.pl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. #! /usr/bin/env perl
  2. # Copyright 2018 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. # For the moment, we only support Visual C on native Windows, or
  143. # compatible compilers. With those, the flags /Zs /showIncludes
  144. # give us the necessary output to be able to create dependencies
  145. # that nmake (or any 'make' implementation) should be able to read,
  146. # with a bit of help. The output we're interested in looks like
  147. # this (it always starts the same)
  148. #
  149. # Note: including file: {whatever header file}
  150. #
  151. # Since there's no object file name at all in that information,
  152. # we must construct it ourselves.
  153. (my $objfile = shift) =~ s|\.d$|.obj|i;
  154. my $line = shift;
  155. # There are also other lines mixed in, for example compiler
  156. # warnings, so we simply discard anything that doesn't start with
  157. # the Note:
  158. if (/^Note: including file: */) {
  159. (my $tail = $') =~ s/\s*\R$//;
  160. # VC gives us absolute paths for all include files, so to
  161. # remove system header dependencies, we need to check that
  162. # they don't match $abs_srcdir or $abs_blddir.
  163. $tail = canonpath($tail);
  164. unless (defined $depconv_cache{$tail}) {
  165. my $dep = $tail;
  166. # Since we have already pre-populated the cache with
  167. # mappings for generated headers, we only need to deal
  168. # with the source tree.
  169. if ($dep =~ s|^\Q$abs_srcdir\E\\|\$(SRCDIR)\\|i) {
  170. $depconv_cache{$tail} = $dep;
  171. }
  172. }
  173. return ($objfile, '"'.$depconv_cache{$tail}.'"')
  174. if defined $depconv_cache{$tail};
  175. print STDERR "DEBUG[VC]: ignoring $objfile <- $tail\n"
  176. if $debug;
  177. }
  178. return undef;
  179. },
  180. );
  181. my %continuations = (
  182. 'gcc' => undef,
  183. 'makedepend' => "\\",
  184. 'VMS C' => "-",
  185. 'VC' => "\\",
  186. );
  187. die "Producer unrecognised: $producer\n"
  188. unless exists $procedures{$producer} && exists $continuations{$producer};
  189. my $procedure = $procedures{$producer};
  190. my $continuation = $continuations{$producer};
  191. my $buildfile_new = "$buildfile-$$";
  192. my %collect = ();
  193. if (defined $procedure) {
  194. foreach my $depfile (@depfiles) {
  195. open IDEP,$depfile or die "Trying to read $depfile: $!\n";
  196. while (<IDEP>) {
  197. s|\R$||; # The better chomp
  198. my ($target, $deps) = $procedure->($depfile, $_);
  199. $collect{$target}->{$deps} = 1 if defined $target;
  200. }
  201. close IDEP;
  202. }
  203. }
  204. open IBF, $buildfile or die "Trying to read $buildfile: $!\n";
  205. open OBF, '>', $buildfile_new or die "Trying to write $buildfile_new: $!\n";
  206. while (<IBF>) {
  207. last if /^# DO NOT DELETE THIS LINE/;
  208. print OBF or die "$!\n";
  209. }
  210. close IBF;
  211. print OBF "# DO NOT DELETE THIS LINE -- make depend depends on it.\n";
  212. if (defined $procedure) {
  213. foreach my $target (sort keys %collect) {
  214. my $prefix = $target . ' :';
  215. my @deps = sort keys %{$collect{$target}};
  216. while (@deps) {
  217. my $buf = $prefix;
  218. $prefix = '';
  219. while (@deps && ($buf eq ''
  220. || length($buf) + length($deps[0]) <= 77)) {
  221. $buf .= ' ' . shift @deps;
  222. }
  223. $buf .= ' '.$continuation if @deps;
  224. print OBF $buf,"\n" or die "Trying to print: $!\n"
  225. }
  226. }
  227. } else {
  228. foreach my $depfile (@depfiles) {
  229. open IDEP,$depfile or die "Trying to read $depfile: $!\n";
  230. while (<IDEP>) {
  231. print OBF or die "Trying to print: $!\n";
  232. }
  233. close IDEP;
  234. }
  235. }
  236. close OBF;
  237. if (compare_text($buildfile_new, $buildfile) != 0) {
  238. rename $buildfile_new, $buildfile
  239. or die "Trying to rename $buildfile_new -> $buildfile: $!\n";
  240. }
  241. END {
  242. # On VMS, we want to remove all generations of this file, in case there
  243. # are more than one, so we loop.
  244. if (defined $buildfile_new) {
  245. while (unlink $buildfile_new) {}
  246. }
  247. }