blob: 5f21adecbf7164b1e09fb5d81ee043641da426b0 [file] [log] [blame]
Richard Levitte249b4e22018-03-13 17:56:20 +01001#! /usr/bin/env perl
Matt Caswellfecb3aa2022-05-03 11:52:38 +01002# Copyright 2018-2022 The OpenSSL Project Authors. All Rights Reserved.
Richard Levitte249b4e22018-03-13 17:56:20 +01003#
Richard Levitte9059ab42018-12-06 13:03:50 +01004# Licensed under the Apache License 2.0 (the "License"). You may not use
Richard Levitte249b4e22018-03-13 17:56:20 +01005# 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
Richard Levittec39785d2018-03-15 18:06:18 +01009use strict;
10use warnings;
11
Richard Levitte249b4e22018-03-13 17:56:20 +010012use lib '.';
13use configdata;
14
Richard Levitte433e8572018-03-15 20:38:23 +010015use File::Spec::Functions qw(:DEFAULT rel2abs);
Richard Levitte249b4e22018-03-13 17:56:20 +010016use File::Compare qw(compare_text);
Richard Levitte8ed5f092018-03-15 22:05:00 +010017use feature 'state';
Richard Levitte249b4e22018-03-13 17:56:20 +010018
Richard Levittec39785d2018-03-15 18:06:18 +010019# 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;
22
Richard Levitte17928cf2018-03-15 20:37:39 +010023my $debug = $ENV{ADD_DEPENDS_DEBUG};
Richard Levitte249b4e22018-03-13 17:56:20 +010024my $buildfile = $config{build_file};
Richard Levittec39785d2018-03-15 18:06:18 +010025my $build_mtime = (stat($buildfile))[9];
Richard Levitte023cb592021-08-19 20:45:00 +020026my $configdata_mtime = (stat('configdata.pm'))[9];
Richard Levittec39785d2018-03-15 18:06:18 +010027my $rebuild = 0;
Richard Levitte249b4e22018-03-13 17:56:20 +010028my $depext = $target{dep_extension} || ".d";
Richard Levittec39785d2018-03-15 18:06:18 +010029my @depfiles =
Richard Levitted35b2c72018-03-14 12:39:45 +010030 sort
Richard Levittec39785d2018-03-15 18:06:18 +010031 grep {
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
Richard Levitte023cb592021-08-19 20:45:00 +020034 # newer than the build file or older than configdata.pm, and if it
35 # is, sets $rebuild.
Richard Levittec39785d2018-03-15 18:06:18 +010036 my @st = stat($_);
Richard Levitte023cb592021-08-19 20:45:00 +020037 $rebuild = 1
38 if @st && ($st[9] > $build_mtime || $st[9] < $configdata_mtime);
Richard Levittec39785d2018-03-15 18:06:18 +010039 scalar @st > 0; # Determines the grep result
40 }
Richard Levitte249b4e22018-03-13 17:56:20 +010041 map { (my $x = $_) =~ s|\.o$|$depext|; $x; }
Richard Levitte3866b222018-11-01 14:02:21 +010042 ( ( 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}} ) );
Richard Levitte249b4e22018-03-13 17:56:20 +010046
Richard Levittec39785d2018-03-15 18:06:18 +010047exit 0 unless $rebuild;
48
49# Ok, primary checks are done, time to do some real work
50
Richard Levitte433e8572018-03-15 20:38:23 +010051my $producer = shift @ARGV;
52die "Producer not given\n" unless $producer;
53
Richard Levitte8ed5f092018-03-15 22:05:00 +010054my $srcdir = $config{sourcedir};
55my $blddir = $config{builddir};
56my $abs_srcdir = rel2abs($srcdir);
57my $abs_blddir = rel2abs($blddir);
Richard Levittec39785d2018-03-15 18:06:18 +010058
Richard Levitte433e8572018-03-15 20:38:23 +010059# 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,
69# we're fine.
70my %depconv_cache =
Richard Levitted3c72e32018-09-12 02:38:22 +020071 map { catfile($abs_blddir, $_) => $_ }
Richard Levitte433e8572018-03-15 20:38:23 +010072 keys %{$unified_info{generate}};
Richard Levittec39785d2018-03-15 18:06:18 +010073
74my %procedures = (
Richard Levitte3bb20462021-08-19 13:07:30 +020075 'gcc' =>
76 sub {
77 (my $objfile = shift) =~ s|\.d$|.o|i;
78 my $line = shift;
79
80 # Remove the original object file
81 $line =~ s|^.*\.o: | |;
82 # All we got now is a dependency, shave off surrounding spaces
83 $line =~ s/^\s+//;
84 $line =~ s/\s+$//;
85 # Also, shave off any continuation
86 $line =~ s/\s*\\$//;
87
88 # Split the line into individual header files, and keep those
89 # that exist in some form
90 my @headers;
91 for (split(/\s+/, $line)) {
92 my $x = rel2abs($_);
93
94 if (!$depconv_cache{$x}) {
95 if (-f $x) {
96 $depconv_cache{$x} = $_;
97 }
98 }
99
100 if ($depconv_cache{$x}) {
101 push @headers, $_;
102 } else {
103 print STDERR "DEBUG[$producer]: ignoring $objfile <- $line\n"
104 if $debug;
105 }
106 }
107 return ($objfile, join(' ', @headers)) if @headers;
108 return undef;
109 },
Richard Levittec39785d2018-03-15 18:06:18 +0100110 'makedepend' =>
111 sub {
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;
119 my $line = shift;
120
121 # Discard comments
122 return undef if $line =~ /^(#.*|\s*)$/;
123
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*$/;
131
132 # All we got now is a dependency, just shave off surrounding spaces
133 $line =~ s/^\s+//;
134 $line =~ s/\s+$//;
135 return ($objfile, $line);
136 },
137 'VMS C' =>
138 sub {
Richard Levitte8ed5f092018-03-15 22:05:00 +0100139 state $abs_srcdir_shaved = undef;
140 state $srcdir_shaved = undef;
141
142 unless (defined $abs_srcdir_shaved) {
143 ($abs_srcdir_shaved = $abs_srcdir) =~ s|[>\]]$||;
144 ($srcdir_shaved = $srcdir) =~ s|[>\]]$||;
145 }
146
Richard Levittec39785d2018-03-15 18:06:18 +0100147 # 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;
154 my $line = shift;
155
156 # Shave off the target.
157 #
158 # The pattern for target and dependencies will always take this
159 # form:
160 #
161 # target SPACE : SPACE deps
162 #
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 = $';
167
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
172 # .TLB.
173 return undef if /\.TLB\s*$/;
174
175 # All we got now is a dependency, just shave off surrounding spaces
176 $line =~ s/^\s+//;
177 $line =~ s/\s+$//;
Richard Levitte8ed5f092018-03-15 22:05:00 +0100178
179 # VMS C gives us absolute paths, always. Let's see if we can
180 # make them relative instead.
Richard Levitted3c72e32018-09-12 02:38:22 +0200181 $line = canonpath($line);
Richard Levitte8ed5f092018-03-15 22:05:00 +0100182
183 unless (defined $depconv_cache{$line}) {
184 my $dep = $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) {
Richard Levitte3bb20462021-08-19 13:07:30 +0200189 # Also check that the header actually exists
190 if (-f $line) {
191 $depconv_cache{$line} = $dep;
192 }
Richard Levitte8ed5f092018-03-15 22:05:00 +0100193 }
194 }
195 return ($objfile, $depconv_cache{$line})
196 if defined $depconv_cache{$line};
Richard Levitte2e535eb2021-04-26 09:17:05 +0200197 print STDERR "DEBUG[$producer]: ignoring $objfile <- $line\n"
Richard Levitte8ed5f092018-03-15 22:05:00 +0100198 if $debug;
199
200 return undef;
Richard Levittec39785d2018-03-15 18:06:18 +0100201 },
202 'VC' =>
203 sub {
Richard Levitte2e535eb2021-04-26 09:17:05 +0200204 # 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)
Richard Levittec39785d2018-03-15 18:06:18 +0100209 #
210 # Note: including file: {whatever header file}
211 #
Richard Levitte3babc1e2021-04-26 09:28:12 +0200212 # This output is localized, so for example, the German pack gives
213 # us this:
214 #
215 # Hinweis: Einlesen der Datei: {whatever header file}
216 #
Dimitris Apostoloue304aa82022-01-03 01:00:27 +0200217 # To accommodate, we need to use a very general regular expression
Richard Levitte3babc1e2021-04-26 09:28:12 +0200218 # to parse those lines.
219 #
Richard Levittec39785d2018-03-15 18:06:18 +0100220 # Since there's no object file name at all in that information,
221 # we must construct it ourselves.
222
223 (my $objfile = shift) =~ s|\.d$|.obj|i;
224 my $line = shift;
225
226 # There are also other lines mixed in, for example compiler
227 # warnings, so we simply discard anything that doesn't start with
228 # the Note:
229
Richard Levitte3babc1e2021-04-26 09:28:12 +0200230 if (/^[^:]*: [^:]*: */) {
Richard Levittec39785d2018-03-15 18:06:18 +0100231 (my $tail = $') =~ s/\s*\R$//;
232
233 # VC gives us absolute paths for all include files, so to
234 # remove system header dependencies, we need to check that
Richard Levitte2e535eb2021-04-26 09:17:05 +0200235 # they don't match $abs_srcdir or $abs_blddir.
236 $tail = canonpath($tail);
237
238 unless (defined $depconv_cache{$tail}) {
239 my $dep = $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) {
Richard Levitte3bb20462021-08-19 13:07:30 +0200244 # Also check that the header actually exists
245 if (-f $line) {
246 $depconv_cache{$tail} = $dep;
247 }
Richard Levitte2e535eb2021-04-26 09:17:05 +0200248 }
249 }
250 return ($objfile, '"'.$depconv_cache{$tail}.'"')
251 if defined $depconv_cache{$tail};
252 print STDERR "DEBUG[$producer]: ignoring $objfile <- $tail\n"
253 if $debug;
254 }
255
256 return undef;
257 },
258 'embarcadero' =>
259 sub {
Richard Levitte3babc1e2021-04-26 09:28:12 +0200260 # With Embarcadero C++Builder's preprocessor (cpp32.exe) the -Sx -Hp
261 # flags give us the list of #include files read, like the following:
Richard Levitte2e535eb2021-04-26 09:17:05 +0200262 #
Richard Levitte3babc1e2021-04-26 09:28:12 +0200263 # Including ->->{whatever header file}
Richard Levitte2e535eb2021-04-26 09:17:05 +0200264 #
265 # where each "->" indicates the nesting level of the #include. The
266 # logic here is otherwise the same as the 'VC' scheme.
267 #
268 # Since there's no object file name at all in that information,
269 # we must construct it ourselves.
270
271 (my $objfile = shift) =~ s|\.d$|.obj|i;
272 my $line = shift;
273
274 # There are also other lines mixed in, for example compiler
275 # warnings, so we simply discard anything that doesn't start with
276 # the Note:
277
278 if (/^Including (->)*/) {
279 (my $tail = $') =~ s/\s*\R$//;
280
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.
Tanzinul Islam16f2a442020-12-10 14:53:07 +0000285 $tail = rel2abs($tail);
Richard Levitte433e8572018-03-15 20:38:23 +0100286
287 unless (defined $depconv_cache{$tail}) {
288 my $dep = $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) {
Richard Levitte3bb20462021-08-19 13:07:30 +0200293 # Also check that the header actually exists
294 if (-f $line) {
295 $depconv_cache{$tail} = $dep;
296 }
Richard Levitte433e8572018-03-15 20:38:23 +0100297 }
Richard Levittec39785d2018-03-15 18:06:18 +0100298 }
Richard Levitte433e8572018-03-15 20:38:23 +0100299 return ($objfile, '"'.$depconv_cache{$tail}.'"')
300 if defined $depconv_cache{$tail};
Richard Levitte2e535eb2021-04-26 09:17:05 +0200301 print STDERR "DEBUG[$producer]: ignoring $objfile <- $tail\n"
Richard Levitte433e8572018-03-15 20:38:23 +0100302 if $debug;
Richard Levittec39785d2018-03-15 18:06:18 +0100303 }
304
305 return undef;
306 },
307);
308my %continuations = (
Richard Levitte3bb20462021-08-19 13:07:30 +0200309 'gcc' => "\\",
Richard Levittec39785d2018-03-15 18:06:18 +0100310 'makedepend' => "\\",
311 'VMS C' => "-",
312 'VC' => "\\",
Richard Levitte2e535eb2021-04-26 09:17:05 +0200313 'embarcadero' => "\\",
Richard Levittec39785d2018-03-15 18:06:18 +0100314);
315
316die "Producer unrecognised: $producer\n"
317 unless exists $procedures{$producer} && exists $continuations{$producer};
318
319my $procedure = $procedures{$producer};
320my $continuation = $continuations{$producer};
321
322my $buildfile_new = "$buildfile-$$";
323
324my %collect = ();
Richard Levitte3bb20462021-08-19 13:07:30 +0200325foreach my $depfile (@depfiles) {
326 open IDEP,$depfile or die "Trying to read $depfile: $!\n";
327 while (<IDEP>) {
328 s|\R$||; # The better chomp
329 my ($target, $deps) = $procedure->($depfile, $_);
330 $collect{$target}->{$deps} = 1 if defined $target;
Richard Levittec39785d2018-03-15 18:06:18 +0100331 }
Richard Levitte3bb20462021-08-19 13:07:30 +0200332 close IDEP;
Richard Levittec39785d2018-03-15 18:06:18 +0100333}
334
Richard Levitte249b4e22018-03-13 17:56:20 +0100335open IBF, $buildfile or die "Trying to read $buildfile: $!\n";
336open OBF, '>', $buildfile_new or die "Trying to write $buildfile_new: $!\n";
337while (<IBF>) {
Richard Levitte249b4e22018-03-13 17:56:20 +0100338 last if /^# DO NOT DELETE THIS LINE/;
339 print OBF or die "$!\n";
Richard Levitte249b4e22018-03-13 17:56:20 +0100340}
341close IBF;
342
343print OBF "# DO NOT DELETE THIS LINE -- make depend depends on it.\n";
344
Richard Levitte3bb20462021-08-19 13:07:30 +0200345foreach my $target (sort keys %collect) {
346 my $prefix = $target . ' :';
347 my @deps = sort keys %{$collect{$target}};
Richard Levittec39785d2018-03-15 18:06:18 +0100348
Richard Levitte3bb20462021-08-19 13:07:30 +0200349 while (@deps) {
350 my $buf = $prefix;
351 $prefix = '';
Richard Levittec39785d2018-03-15 18:06:18 +0100352
Richard Levitte3bb20462021-08-19 13:07:30 +0200353 while (@deps && ($buf eq ''
354 || length($buf) + length($deps[0]) <= 77)) {
355 $buf .= ' ' . shift @deps;
Richard Levittec39785d2018-03-15 18:06:18 +0100356 }
Richard Levitte3bb20462021-08-19 13:07:30 +0200357 $buf .= ' '.$continuation if @deps;
358
359 print OBF $buf,"\n" or die "Trying to print: $!\n"
Richard Levittec39785d2018-03-15 18:06:18 +0100360 }
Richard Levitte249b4e22018-03-13 17:56:20 +0100361}
Richard Levittec39785d2018-03-15 18:06:18 +0100362
Richard Levitte249b4e22018-03-13 17:56:20 +0100363close OBF;
364
365if (compare_text($buildfile_new, $buildfile) != 0) {
366 rename $buildfile_new, $buildfile
367 or die "Trying to rename $buildfile_new -> $buildfile: $!\n";
368}
Richard Levittec39785d2018-03-15 18:06:18 +0100369
370END {
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) {}
375 }
376}