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