Move all dependency post-processing to util/add-depends.pl
[openssl.git] / util / add-depends.pl
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
9 use strict;
10 use warnings;
11
12 use lib '.';
13 use configdata;
14
15 use File::Spec::Functions qw(canonpath rel2abs);
16 use File::Compare qw(compare_text);
17
18 # When using stat() on Windows, we can get it to perform better by avoid some
19 # data.  This doesn't affect the mtime field, so we're not losing anything...
20 ${^WIN32_SLOPPY_STAT} = 1;
21
22 my $buildfile = $config{build_file};
23 my $build_mtime = (stat($buildfile))[9];
24 my $rebuild = 0;
25 my $depext = $target{dep_extension} || ".d";
26 my @depfiles =
27     sort
28     grep {
29         # This grep has side effects.  Not only does if check the existence
30         # of the dependency file given in $_, but it also checks if it's
31         # newer than the build file, and if it is, sets $rebuild.
32         my @st = stat($_);
33         $rebuild = 1 if @st && $st[9] > $build_mtime;
34         scalar @st > 0;         # Determines the grep result
35     }
36     map { (my $x = $_) =~ s|\.o$|$depext|; $x; }
37     grep { $unified_info{sources}->{$_}->[0] =~ /\.cc?$/ }
38     keys %{$unified_info{sources}};
39
40 exit 0 unless $rebuild;
41
42 # Ok, primary checks are done, time to do some real work
43
44 my $abs_srcdir = rel2abs($config{sourcedir});
45 my $abs_blddir = rel2abs($config{builddir});
46
47 my $producer = shift @ARGV;
48 die "Producer not given\n" unless $producer;
49
50 my %procedures = (
51     'gcc' => undef,             # gcc style dependency files needs no mods
52     'makedepend' =>
53         sub {
54             # makedepend, in its infinite wisdom, wants to have the object file
55             # in the same directory as the source file.  This doesn't work too
56             # well with out-of-source-tree builds, so we must resort to tricks
57             # to get things right.  Fortunately, the .d files are always placed
58             # parallel with the object files, so all we need to do is construct
59             # the object file name from the dep file name.
60             (my $objfile = shift) =~ s|\.d$|.o|i;
61             my $line = shift;
62
63             # Discard comments
64             return undef if $line =~ /^(#.*|\s*)$/;
65
66             # Remove the original object file
67             $line =~ s|^.*\.o: | |;
68             # Also, remove any dependency that starts with a /, because those
69             # are typically system headers
70             $line =~ s/\s+\/(\\.|\S)*//g;
71             # Finally, discard all empty lines
72             return undef if $line =~ /^\s*$/;
73
74             # All we got now is a dependency, just shave off surrounding spaces
75             $line =~ s/^\s+//;
76             $line =~ s/\s+$//;
77             return ($objfile, $line);
78         },
79     'VMS C' =>
80         sub {
81             # current versions of DEC / Compaq / HP / VSI C strips away all
82             # directory information from the object file, so we must insert it
83             # back.  To make life simpler, we simply replace it with the
84             # corresponding .D file that's had its extension changed.  Since
85             # .D files are always written parallel to the object files, we
86             # thereby get the directory information for free.
87             (my $objfile = shift) =~ s|\.D$|.OBJ|i;
88             my $line = shift;
89
90             # Shave off the target.
91             #
92             # The pattern for target and dependencies will always take this
93             # form:
94             #
95             #   target SPACE : SPACE deps
96             #
97             # This is so a volume delimiter (a : without any spaces around it)
98             # won't get mixed up with the target / deps delimiter.  We use this
99             # to easily identify what needs to be removed.
100             m|\s:\s|; $line = $';
101
102             # We know that VMS has system header files in text libraries,
103             # extension .TLB.  We also know that our header files aren't stored
104             # in text libraries.  Finally, we know that VMS C produces exactly
105             # one dependency per line, so we simply discard any line ending with
106             # .TLB.
107             return undef if /\.TLB\s*$/;
108
109             # All we got now is a dependency, just shave off surrounding spaces
110             $line =~ s/^\s+//;
111             $line =~ s/\s+$//;
112             return ($objfile, $line);
113         },
114     'VC' =>
115         sub {
116             # For the moment, we only support Visual C on native Windows, or
117             # compatible compilers.  With those, the flags /Zs /showIncludes
118             # give us the necessary output to be able to create dependencies
119             # that nmake (or any 'make' implementation) should be able to read,
120             # with a bit of help.  The output we're interested in looks like
121             # this (it always starts the same)
122             #
123             #   Note: including file: {whatever header file}
124             #
125             # Since there's no object file name at all in that information,
126             # we must construct it ourselves.
127
128             (my $objfile = shift) =~ s|\.d$|.obj|i;
129             my $line = shift;
130
131             # There are also other lines mixed in, for example compiler
132             # warnings, so we simply discard anything that doesn't start with
133             # the Note:
134
135             if (/^Note: including file: */) {
136                 (my $tail = $') =~ s/\s*\R$//;
137
138                 # VC gives us absolute paths for all include files, so to
139                 # remove system header dependencies, we need to check that
140                 # they don't match $abs_srcdir or $abs_blddir
141                 $tail = canonpath($tail);
142                 if ($tail =~ m|^\Q$abs_srcdir\E|i
143                         || $tail =~ m|^\Q$abs_blddir\E|i) {
144                     return ($objfile, "\"$tail\"");
145                 }
146             }
147
148             return undef;
149         },
150 );
151 my %continuations = (
152     'gcc' => undef,
153     'makedepend' => "\\",
154     'VMS C' => "-",
155     'VC' => "\\",
156 );
157
158 die "Producer unrecognised: $producer\n"
159     unless exists $procedures{$producer} && exists $continuations{$producer};
160
161 my $procedure = $procedures{$producer};
162 my $continuation = $continuations{$producer};
163
164 my $buildfile_new = "$buildfile-$$";
165
166 my %collect = ();
167 if (defined $procedure) {
168     foreach my $depfile (@depfiles) {
169         open IDEP,$depfile or die "Trying to read $depfile: $!\n";
170         while (<IDEP>) {
171             s|\R$||;                # The better chomp
172             my ($target, $deps) = $procedure->($depfile, $_);
173             $collect{$target}->{$deps} = 1 if defined $target;
174         }
175         close IDEP;
176     }
177 }
178
179 open IBF, $buildfile or die "Trying to read $buildfile: $!\n";
180 open OBF, '>', $buildfile_new or die "Trying to write $buildfile_new: $!\n";
181 while (<IBF>) {
182     last if /^# DO NOT DELETE THIS LINE/;
183     print OBF or die "$!\n";
184 }
185 close IBF;
186
187 print OBF "# DO NOT DELETE THIS LINE -- make depend depends on it.\n";
188
189 if (defined $procedure) {
190     foreach my $target (sort keys %collect) {
191         my $prefix = $target . ' :';
192         my @deps = sort keys %{$collect{$target}};
193
194         while (@deps) {
195             my $buf = $prefix;
196             $prefix = '';
197
198             while (@deps && ($buf eq ''
199                                  || length($buf) + length($deps[0]) <= 77)) {
200                 $buf .= ' ' . shift @deps;
201             }
202             $buf .= ' '.$continuation if @deps;
203
204             print OBF $buf,"\n" or die "Trying to print: $!\n"
205         }
206     }
207 } else {
208     foreach my $depfile (@depfiles) {
209         open IDEP,$depfile or die "Trying to read $depfile: $!\n";
210         while (<IDEP>) {
211             print OBF or die "Trying to print: $!\n";
212         }
213         close IDEP;
214     }
215 }
216
217 close OBF;
218
219 if (compare_text($buildfile_new, $buildfile) != 0) {
220     rename $buildfile_new, $buildfile
221         or die "Trying to rename $buildfile_new -> $buildfile: $!\n";
222 }
223
224 END {
225     # On VMS, we want to remove all generations of this file, in case there
226     # are more than one, so we loop.
227     if (defined $buildfile_new) {
228         while (unlink $buildfile_new) {}
229     }
230 }