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