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