2 # Copyright 2018-2022 The OpenSSL Project Authors. All Rights Reserved.
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
15 use File::Spec::Functions qw(:DEFAULT rel2abs);
16 use File::Compare qw(compare_text);
19 # When using stat() on Windows, we can get it to perform better by avoid some
20 # data. This doesn't affect the mtime field, so we're not losing anything...
21 ${^WIN32_SLOPPY_STAT} = 1;
23 my $debug = $ENV{ADD_DEPENDS_DEBUG};
24 my $buildfile = $config{build_file};
25 my $build_mtime = (stat($buildfile))[9];
26 my $configdata_mtime = (stat('configdata.pm'))[9];
28 my $depext = $target{dep_extension} || ".d";
32 # This grep has side effects. Not only does if check the existence
33 # of the dependency file given in $_, but it also checks if it's
34 # newer than the build file or older than configdata.pm, and if it
38 if @st && ($st[9] > $build_mtime || $st[9] < $configdata_mtime);
39 scalar @st > 0; # Determines the grep result
41 map { (my $x = $_) =~ s|\.o$|$depext|; $x; }
42 ( ( grep { $unified_info{sources}->{$_}->[0] =~ /\.cc?$/ }
43 keys %{$unified_info{sources}} ),
44 ( grep { $unified_info{shared_sources}->{$_}->[0] =~ /\.cc?$/ }
45 keys %{$unified_info{shared_sources}} ) );
47 exit 0 unless $rebuild;
49 # Ok, primary checks are done, time to do some real work
51 my $producer = shift @ARGV;
52 die "Producer not given\n" unless $producer;
54 my $srcdir = $config{sourcedir};
55 my $blddir = $config{builddir};
56 my $abs_srcdir = rel2abs($srcdir);
57 my $abs_blddir = rel2abs($blddir);
59 # Convenient cache of absolute to relative map. We start with filling it
60 # with mappings for the known generated header files. They are relative to
61 # the current working directory, so that's an easy task.
62 # NOTE: there's more than C header files that are generated. They will also
63 # generate entries in this map. We could of course deal with C header files
64 # only, but in case we decide to handle more than just C files in the future,
65 # we already have the mechanism in place here.
66 # NOTE2: we lower case the index to make it searchable without regard for
67 # character case. That could seem dangerous, but as long as we don't have
68 # files we depend on in the same directory that only differ by character case,
71 map { catfile($abs_blddir, $_) => $_ }
72 keys %{$unified_info{generate}};
77 (my $objfile = shift) =~ s|\.d$|.o|i;
80 # Remove the original object file
81 $line =~ s|^.*\.o: | |;
82 # All we got now is a dependency, shave off surrounding spaces
85 # Also, shave off any continuation
88 # Split the line into individual header files, and keep those
89 # that exist in some form
91 for (split(/\s+/, $line)) {
94 if (!$depconv_cache{$x}) {
96 $depconv_cache{$x} = $_;
100 if ($depconv_cache{$x}) {
103 print STDERR "DEBUG[$producer]: ignoring $objfile <- $line\n"
107 return ($objfile, join(' ', @headers)) if @headers;
112 # makedepend, in its infinite wisdom, wants to have the object file
113 # in the same directory as the source file. This doesn't work too
114 # well with out-of-source-tree builds, so we must resort to tricks
115 # to get things right. Fortunately, the .d files are always placed
116 # parallel with the object files, so all we need to do is construct
117 # the object file name from the dep file name.
118 (my $objfile = shift) =~ s|\.d$|.o|i;
122 return undef if $line =~ /^(#.*|\s*)$/;
124 # Remove the original object file
125 $line =~ s|^.*\.o: | |;
126 # Also, remove any dependency that starts with a /, because those
127 # are typically system headers
128 $line =~ s/\s+\/(\\.|\S)*//g;
129 # Finally, discard all empty lines
130 return undef if $line =~ /^\s*$/;
132 # All we got now is a dependency, just shave off surrounding spaces
135 return ($objfile, $line);
139 state $abs_srcdir_shaved = undef;
140 state $srcdir_shaved = undef;
142 unless (defined $abs_srcdir_shaved) {
143 ($abs_srcdir_shaved = $abs_srcdir) =~ s|[>\]]$||;
144 ($srcdir_shaved = $srcdir) =~ s|[>\]]$||;
147 # current versions of DEC / Compaq / HP / VSI C strips away all
148 # directory information from the object file, so we must insert it
149 # back. To make life simpler, we simply replace it with the
150 # corresponding .D file that's had its extension changed. Since
151 # .D files are always written parallel to the object files, we
152 # thereby get the directory information for free.
153 (my $objfile = shift) =~ s|\.D$|.OBJ|i;
156 # Shave off the target.
158 # The pattern for target and dependencies will always take this
161 # target SPACE : SPACE deps
163 # This is so a volume delimiter (a : without any spaces around it)
164 # won't get mixed up with the target / deps delimiter. We use this
165 # to easily identify what needs to be removed.
166 m|\s:\s|; $line = $';
168 # We know that VMS has system header files in text libraries,
169 # extension .TLB. We also know that our header files aren't stored
170 # in text libraries. Finally, we know that VMS C produces exactly
171 # one dependency per line, so we simply discard any line ending with
173 return undef if /\.TLB\s*$/;
175 # All we got now is a dependency, just shave off surrounding spaces
179 # VMS C gives us absolute paths, always. Let's see if we can
180 # make them relative instead.
181 $line = canonpath($line);
183 unless (defined $depconv_cache{$line}) {
185 # Since we have already pre-populated the cache with
186 # mappings for generated headers, we only need to deal
187 # with the source tree.
188 if ($dep =~ s|^\Q$abs_srcdir_shaved\E([\.>\]])?|$srcdir_shaved$1|i) {
189 # Also check that the header actually exists
191 $depconv_cache{$line} = $dep;
195 return ($objfile, $depconv_cache{$line})
196 if defined $depconv_cache{$line};
197 print STDERR "DEBUG[$producer]: ignoring $objfile <- $line\n"
204 # With Microsoft Visual C the flags /Zs /showIncludes give us the
205 # necessary output to be able to create dependencies that nmake
206 # (or any 'make' implementation) should be able to read, with a
207 # bit of help. The output we're interested in looks something
208 # like this (it always starts the same)
210 # Note: including file: {whatever header file}
212 # This output is localized, so for example, the German pack gives
215 # Hinweis: Einlesen der Datei: {whatever header file}
217 # To accommodate, we need to use a very general regular expression
218 # to parse those lines.
220 # Since there's no object file name at all in that information,
221 # we must construct it ourselves.
223 (my $objfile = shift) =~ s|\.d$|.obj|i;
226 # There are also other lines mixed in, for example compiler
227 # warnings, so we simply discard anything that doesn't start with
230 if (/^[^:]*: [^:]*: */) {
231 (my $tail = $') =~ s/\s*\R$//;
233 # VC gives us absolute paths for all include files, so to
234 # remove system header dependencies, we need to check that
235 # they don't match $abs_srcdir or $abs_blddir.
236 $tail = canonpath($tail);
238 unless (defined $depconv_cache{$tail}) {
240 # Since we have already pre-populated the cache with
241 # mappings for generated headers, we only need to deal
242 # with the source tree.
243 if ($dep =~ s|^\Q$abs_srcdir\E\\|\$(SRCDIR)\\|i) {
244 # Also check that the header actually exists
246 $depconv_cache{$tail} = $dep;
250 return ($objfile, '"'.$depconv_cache{$tail}.'"')
251 if defined $depconv_cache{$tail};
252 print STDERR "DEBUG[$producer]: ignoring $objfile <- $tail\n"
260 # With Embarcadero C++Builder's preprocessor (cpp32.exe) the -Sx -Hp
261 # flags give us the list of #include files read, like the following:
263 # Including ->->{whatever header file}
265 # where each "->" indicates the nesting level of the #include. The
266 # logic here is otherwise the same as the 'VC' scheme.
268 # Since there's no object file name at all in that information,
269 # we must construct it ourselves.
271 (my $objfile = shift) =~ s|\.d$|.obj|i;
274 # There are also other lines mixed in, for example compiler
275 # warnings, so we simply discard anything that doesn't start with
278 if (/^Including (->)*/) {
279 (my $tail = $') =~ s/\s*\R$//;
281 # C++Builder gives us relative paths when possible, so to
282 # remove system header dependencies, we convert them to
283 # absolute paths and check that they don't match $abs_srcdir
284 # or $abs_blddir, just as the 'VC' scheme.
285 $tail = rel2abs($tail);
287 unless (defined $depconv_cache{$tail}) {
289 # Since we have already pre-populated the cache with
290 # mappings for generated headers, we only need to deal
291 # with the source tree.
292 if ($dep =~ s|^\Q$abs_srcdir\E\\|\$(SRCDIR)\\|i) {
293 # Also check that the header actually exists
295 $depconv_cache{$tail} = $dep;
299 return ($objfile, '"'.$depconv_cache{$tail}.'"')
300 if defined $depconv_cache{$tail};
301 print STDERR "DEBUG[$producer]: ignoring $objfile <- $tail\n"
308 my %continuations = (
310 'makedepend' => "\\",
313 'embarcadero' => "\\",
316 die "Producer unrecognised: $producer\n"
317 unless exists $procedures{$producer} && exists $continuations{$producer};
319 my $procedure = $procedures{$producer};
320 my $continuation = $continuations{$producer};
322 my $buildfile_new = "$buildfile-$$";
325 foreach my $depfile (@depfiles) {
326 open IDEP,$depfile or die "Trying to read $depfile: $!\n";
328 s|\R$||; # The better chomp
329 my ($target, $deps) = $procedure->($depfile, $_);
330 $collect{$target}->{$deps} = 1 if defined $target;
335 open IBF, $buildfile or die "Trying to read $buildfile: $!\n";
336 open OBF, '>', $buildfile_new or die "Trying to write $buildfile_new: $!\n";
338 last if /^# DO NOT DELETE THIS LINE/;
339 print OBF or die "$!\n";
343 print OBF "# DO NOT DELETE THIS LINE -- make depend depends on it.\n";
345 foreach my $target (sort keys %collect) {
346 my $prefix = $target . ' :';
347 my @deps = sort keys %{$collect{$target}};
353 while (@deps && ($buf eq ''
354 || length($buf) + length($deps[0]) <= 77)) {
355 $buf .= ' ' . shift @deps;
357 $buf .= ' '.$continuation if @deps;
359 print OBF $buf,"\n" or die "Trying to print: $!\n"
365 if (compare_text($buildfile_new, $buildfile) != 0) {
366 rename $buildfile_new, $buildfile
367 or die "Trying to rename $buildfile_new -> $buildfile: $!\n";
371 # On VMS, we want to remove all generations of this file, in case there
372 # are more than one, so we loop.
373 if (defined $buildfile_new) {
374 while (unlink $buildfile_new) {}