5d0cf3d02aeb0b3df898b59aaddef3ce95fc8906
[openssl.git] / util / postprocess-makedepend.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
17 my $abs_srcdir = rel2abs($config{sourcedir});
18 my $abs_blddir = rel2abs($config{builddir});
19
20 my $producer = shift @ARGV;
21 die "Producer not given\n" unless $producer;
22
23 my $procedure = {
24     'makedepend' =>
25         sub {
26             my $line = shift;
27
28             # makedepend, in its infinite wisdom, wants to have the object file
29             # in the same directory as the source file.  This doesn't work too
30             # well with out-of-source-tree builds, so we must resort to tricks
31             # to get things right.  The trick is to call makedepend with an
32             # extra suffix that contains the desired object file path, like
33             # this:
34             #
35             #   makedepend -f- -o"|dir/foo.o" -- $(CFLAGS) -- ../some/foo.c
36             #
37             # The result will look something like this:
38             #
39             #   ../somewhere/foo|dir/foo.o: deps...
40             #
41             # Which is easy to massage by removing everything up to the first |
42
43             # Remove everything up to the first |
44             $line =~ s/^.*\|//;
45             # Also, remove any dependency that starts with a /, because those
46             # are typically system headers
47             $line =~ s/\s+\/(\\.|\S)*//g;
48             # Finally, discard all empty lines or comment lines
49             return undef if $line =~ /:\s*$/ || $line =~ /^(#.*|\s*)$/;
50
51             $line.="\n" unless $line =~ /\R$/g;
52
53             return $line;
54         },
55     'VMS C' =>
56         sub {
57             my $line = shift;
58
59             # current versions of DEC / Compaq / HP / VSI C strips away all
60             # directory information from the object file, so we must insert it
61             # back. Just to be safe against future changes, we check that there
62             # really is no directory information.
63             my $directory = shift;
64
65             # The pattern for target and dependencies will always take this
66             # form:
67             #
68             #   target SPACE : SPACE deps
69             #
70             # This is so a volume delimiter (a : without any spaces around it)
71             # won't get mixed up with the target / deps delimiter.  We use this
72             # fact in the regexp below to make sure we do look at the target.
73             $line =~ s/^/$directory/ unless /^\S+[:>\]]\S+\s+:/;
74
75             # We know that VMS has system header files in text libraries,
76             # extension .TLB.  We also know that our header files aren't stored
77             # in text libraries.  Finally, we know that VMS C produces exactly
78             # one dependency per line, so we simply discard any line ending with
79             # .TLB.
80             return undef if /\.TLB\s*$/;
81
82             return $line;
83         },
84     'VC' =>
85         sub {
86             my $line = shift;
87             my $object = shift;
88
89             # For the moment, we only support Visual C on native Windows, or
90             # compatible compilers.  With those, the flags /Zs /showIncludes
91             # give us the necessary output to be able to create dependencies
92             # that nmake (or any 'make' implementation) should be able to read,
93             # with a bit of help.  The output we're interested in looks like
94             # this (it always starts the same)
95             #
96             #   Note: including file: {whatever header file}
97             #
98             # So all we really have to do is to is to replace the start of the
99             # line with an object file specification, given to us as an extra
100             # argument (passed from $ARGV[1]);
101             #
102             # There are also other lines mixed in, for example compiler
103             # warnings, so we simply discard anything that doesn't start with
104             # the Note:
105
106             if (/^Note: including file: */) {
107                 (my $tail = $') =~ s/\s*\R$//;
108
109                 # VC gives us absolute paths for all include files, so to
110                 # remove system header dependencies, we need to check that
111                 # they don't match $abs_srcdir or $abs_blddir
112                 $tail = canonpath($tail);
113                 if ($tail =~ m|^\Q$abs_srcdir\E|i
114                         || $tail =~ m|^\Q$abs_blddir\E|i) {
115                     return "${object}: \"$tail\"\n";
116                 }
117             }
118
119             return undef;
120         },
121 } -> {$producer};
122
123 die "Producer unrecognised: $producer\n" unless defined $procedure;
124
125 while (<STDIN>) {
126     if ($_ = $procedure->($_, @ARGV)) {
127         print or die "$!\n";
128     }
129 }