Update copyright year
[openssl.git] / util / add-depends.pl
index 1ffebc9fd79d4e1d26c6ecbf905da8aa161f44d6..4a0f1d5a7671a46fea92d72b4c6c308884311867 100644 (file)
@@ -1,7 +1,7 @@
 #! /usr/bin/env perl
-# Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
+# Copyright 2018-2021 The OpenSSL Project Authors. All Rights Reserved.
 #
-# Licensed under the OpenSSL license (the "License").  You may not use
+# Licensed under the Apache License 2.0 (the "License").  You may not use
 # this file except in compliance with the License.  You can obtain a copy
 # in the file LICENSE in the source distribution or at
 # https://www.openssl.org/source/license.html
@@ -14,6 +14,7 @@ use configdata;
 
 use File::Spec::Functions qw(:DEFAULT rel2abs);
 use File::Compare qw(compare_text);
+use feature 'state';
 
 # When using stat() on Windows, we can get it to perform better by avoid some
 # data.  This doesn't affect the mtime field, so we're not losing anything...
@@ -35,8 +36,10 @@ my @depfiles =
         scalar @st > 0;         # Determines the grep result
     }
     map { (my $x = $_) =~ s|\.o$|$depext|; $x; }
-    grep { $unified_info{sources}->{$_}->[0] =~ /\.cc?$/ }
-    keys %{$unified_info{sources}};
+    ( ( grep { $unified_info{sources}->{$_}->[0] =~ /\.cc?$/ }
+            keys %{$unified_info{sources}} ),
+      ( grep { $unified_info{shared_sources}->{$_}->[0] =~ /\.cc?$/ }
+            keys %{$unified_info{shared_sources}} ) );
 
 exit 0 unless $rebuild;
 
@@ -45,8 +48,10 @@ exit 0 unless $rebuild;
 my $producer = shift @ARGV;
 die "Producer not given\n" unless $producer;
 
-my $abs_srcdir = rel2abs($config{sourcedir});
-my $abs_blddir = rel2abs($config{builddir});
+my $srcdir = $config{sourcedir};
+my $blddir = $config{builddir};
+my $abs_srcdir = rel2abs($srcdir);
+my $abs_blddir = rel2abs($blddir);
 
 # Convenient cache of absolute to relative map.  We start with filling it
 # with mappings for the known generated header files.  They are relative to
@@ -60,7 +65,7 @@ my $abs_blddir = rel2abs($config{builddir});
 # files we depend on in the same directory that only differ by character case,
 # we're fine.
 my %depconv_cache =
-    map { lc catfile($abs_blddir, $_) => $_ }
+    map { catfile($abs_blddir, $_) => $_ }
     keys %{$unified_info{generate}};
 
 my %procedures = (
@@ -94,6 +99,14 @@ my %procedures = (
         },
     'VMS C' =>
         sub {
+            state $abs_srcdir_shaved = undef;
+            state $srcdir_shaved = undef;
+
+            unless (defined $abs_srcdir_shaved) {
+                ($abs_srcdir_shaved = $abs_srcdir) =~ s|[>\]]$||;
+                ($srcdir_shaved = $srcdir) =~ s|[>\]]$||;
+            }
+
             # current versions of DEC / Compaq / HP / VSI C strips away all
             # directory information from the object file, so we must insert it
             # back.  To make life simpler, we simply replace it with the
@@ -125,12 +138,30 @@ my %procedures = (
             # All we got now is a dependency, just shave off surrounding spaces
             $line =~ s/^\s+//;
             $line =~ s/\s+$//;
-            return ($objfile, $line);
+
+            # VMS C gives us absolute paths, always.  Let's see if we can
+            # make them relative instead.
+            $line = canonpath($line);
+
+            unless (defined $depconv_cache{$line}) {
+                my $dep = $line;
+                # Since we have already pre-populated the cache with
+                # mappings for generated headers, we only need to deal
+                # with the source tree.
+                if ($dep =~ s|^\Q$abs_srcdir_shaved\E([\.>\]])?|$srcdir_shaved$1|i) {
+                    $depconv_cache{$line} = $dep;
+                }
+            }
+            return ($objfile, $depconv_cache{$line})
+                if defined $depconv_cache{$line};
+            print STDERR "DEBUG[VMS C]: ignoring $objfile <- $line\n"
+                if $debug;
+
+            return undef;
         },
     'VC' =>
         sub {
-            # For the moment, we only support Visual C on native Windows, or
-            # compatible compilers.  With those, the flags /Zs /showIncludes
+            # On Windows, with Microsoft Visual C the flags /Zs /showIncludes
             # give us the necessary output to be able to create dependencies
             # that nmake (or any 'make' implementation) should be able to read,
             # with a bit of help.  The output we're interested in looks like
@@ -138,6 +169,15 @@ my %procedures = (
             #
             #   Note: including file: {whatever header file}
             #
+            # With Embarcadero C++Builder's preprocessor (cpp32.exe) the -Hp
+            # flag gives us the preprocessed output annotated with the following
+            # note whenever a #include file is read:
+            #
+            #    Including ->->{whatever header file}
+            #
+            # where each "->" indicates the nesting level of the #include.  The
+            # logic here is otherwise the same as the 'VC' case.
+            #
             # Since there's no object file name at all in that information,
             # we must construct it ourselves.
 
@@ -148,13 +188,14 @@ my %procedures = (
             # warnings, so we simply discard anything that doesn't start with
             # the Note:
 
-            if (/^Note: including file: */) {
+            if (/^Note: including file: */ or /^Including (->)*/) {
                 (my $tail = $') =~ s/\s*\R$//;
 
                 # VC gives us absolute paths for all include files, so to
                 # remove system header dependencies, we need to check that
-                # they don't match $abs_srcdir or $abs_blddir.
-                $tail = lc canonpath($tail);
+                # they don't match $abs_srcdir or $abs_blddir.  C++Builder gives
+                # us relative paths when possible, so convert to absolute paths.
+                $tail = rel2abs($tail);
 
                 unless (defined $depconv_cache{$tail}) {
                     my $dep = $tail;