add-depends.pl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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[$producer]: ignoring $objfile <- $line\n"
  137. if $debug;
  138. return undef;
  139. },
  140. 'VC' =>
  141. sub {
  142. # With Microsoft Visual C the flags /Zs /showIncludes give us the
  143. # necessary output to be able to create dependencies that nmake
  144. # (or any 'make' implementation) should be able to read, with a
  145. # bit of help. The output we're interested in looks something
  146. # like this (it always starts the same)
  147. #
  148. # Note: including file: {whatever header file}
  149. #
  150. # This output is localized, so for example, the German pack gives
  151. # us this:
  152. #
  153. # Hinweis: Einlesen der Datei: {whatever header file}
  154. #
  155. # To accomodate, we need to use a very general regular expression
  156. # to parse those lines.
  157. #
  158. # Since there's no object file name at all in that information,
  159. # we must construct it ourselves.
  160. (my $objfile = shift) =~ s|\.d$|.obj|i;
  161. my $line = shift;
  162. # There are also other lines mixed in, for example compiler
  163. # warnings, so we simply discard anything that doesn't start with
  164. # the Note:
  165. if (/^[^:]*: [^:]*: */) {
  166. (my $tail = $') =~ s/\s*\R$//;
  167. # VC gives us absolute paths for all include files, so to
  168. # remove system header dependencies, we need to check that
  169. # they don't match $abs_srcdir or $abs_blddir.
  170. $tail = canonpath($tail);
  171. unless (defined $depconv_cache{$tail}) {
  172. my $dep = $tail;
  173. # Since we have already pre-populated the cache with
  174. # mappings for generated headers, we only need to deal
  175. # with the source tree.
  176. if ($dep =~ s|^\Q$abs_srcdir\E\\|\$(SRCDIR)\\|i) {
  177. $depconv_cache{$tail} = $dep;
  178. }
  179. }
  180. return ($objfile, '"'.$depconv_cache{$tail}.'"')
  181. if defined $depconv_cache{$tail};
  182. print STDERR "DEBUG[$producer]: ignoring $objfile <- $tail\n"
  183. if $debug;
  184. }
  185. return undef;
  186. },
  187. 'embarcadero' =>
  188. sub {
  189. # With Embarcadero C++Builder's preprocessor (cpp32.exe) the -Sx -Hp
  190. # flags give us the list of #include files read, like the following:
  191. #
  192. # Including ->->{whatever header file}
  193. #
  194. # where each "->" indicates the nesting level of the #include. The
  195. # logic here is otherwise the same as the 'VC' scheme.
  196. #
  197. # Since there's no object file name at all in that information,
  198. # we must construct it ourselves.
  199. (my $objfile = shift) =~ s|\.d$|.obj|i;
  200. my $line = shift;
  201. # There are also other lines mixed in, for example compiler
  202. # warnings, so we simply discard anything that doesn't start with
  203. # the Note:
  204. if (/^Including (->)*/) {
  205. (my $tail = $') =~ s/\s*\R$//;
  206. # C++Builder gives us relative paths when possible, so to
  207. # remove system header dependencies, we convert them to
  208. # absolute paths and check that they don't match $abs_srcdir
  209. # or $abs_blddir, just as the 'VC' scheme.
  210. $tail = rel2abs($tail);
  211. unless (defined $depconv_cache{$tail}) {
  212. my $dep = $tail;
  213. # Since we have already pre-populated the cache with
  214. # mappings for generated headers, we only need to deal
  215. # with the source tree.
  216. if ($dep =~ s|^\Q$abs_srcdir\E\\|\$(SRCDIR)\\|i) {
  217. $depconv_cache{$tail} = $dep;
  218. }
  219. }
  220. return ($objfile, '"'.$depconv_cache{$tail}.'"')
  221. if defined $depconv_cache{$tail};
  222. print STDERR "DEBUG[$producer]: ignoring $objfile <- $tail\n"
  223. if $debug;
  224. }
  225. return undef;
  226. },
  227. );
  228. my %continuations = (
  229. 'gcc' => undef,
  230. 'makedepend' => "\\",
  231. 'VMS C' => "-",
  232. 'VC' => "\\",
  233. 'embarcadero' => "\\",
  234. );
  235. die "Producer unrecognised: $producer\n"
  236. unless exists $procedures{$producer} && exists $continuations{$producer};
  237. my $procedure = $procedures{$producer};
  238. my $continuation = $continuations{$producer};
  239. my $buildfile_new = "$buildfile-$$";
  240. my %collect = ();
  241. if (defined $procedure) {
  242. foreach my $depfile (@depfiles) {
  243. open IDEP,$depfile or die "Trying to read $depfile: $!\n";
  244. while (<IDEP>) {
  245. s|\R$||; # The better chomp
  246. my ($target, $deps) = $procedure->($depfile, $_);
  247. $collect{$target}->{$deps} = 1 if defined $target;
  248. }
  249. close IDEP;
  250. }
  251. }
  252. open IBF, $buildfile or die "Trying to read $buildfile: $!\n";
  253. open OBF, '>', $buildfile_new or die "Trying to write $buildfile_new: $!\n";
  254. while (<IBF>) {
  255. last if /^# DO NOT DELETE THIS LINE/;
  256. print OBF or die "$!\n";
  257. }
  258. close IBF;
  259. print OBF "# DO NOT DELETE THIS LINE -- make depend depends on it.\n";
  260. if (defined $procedure) {
  261. foreach my $target (sort keys %collect) {
  262. my $prefix = $target . ' :';
  263. my @deps = sort keys %{$collect{$target}};
  264. while (@deps) {
  265. my $buf = $prefix;
  266. $prefix = '';
  267. while (@deps && ($buf eq ''
  268. || length($buf) + length($deps[0]) <= 77)) {
  269. $buf .= ' ' . shift @deps;
  270. }
  271. $buf .= ' '.$continuation if @deps;
  272. print OBF $buf,"\n" or die "Trying to print: $!\n"
  273. }
  274. }
  275. } else {
  276. foreach my $depfile (@depfiles) {
  277. open IDEP,$depfile or die "Trying to read $depfile: $!\n";
  278. while (<IDEP>) {
  279. print OBF or die "Trying to print: $!\n";
  280. }
  281. close IDEP;
  282. }
  283. }
  284. close OBF;
  285. if (compare_text($buildfile_new, $buildfile) != 0) {
  286. rename $buildfile_new, $buildfile
  287. or die "Trying to rename $buildfile_new -> $buildfile: $!\n";
  288. }
  289. END {
  290. # On VMS, we want to remove all generations of this file, in case there
  291. # are more than one, so we loop.
  292. if (defined $buildfile_new) {
  293. while (unlink $buildfile_new) {}
  294. }
  295. }