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