add-depends.pl 10 KB

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