Refactor the 'depend' target
[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 lib '.';
10 use configdata;
11
12 use File::Compare qw(compare_text);
13
14 my $buildfile = $config{build_file};
15 my $buildfile_new = "$buildfile.$$";
16 my $depext = $target{dep_extension} || ".d";
17 my @deps =
18     grep { print STDERR "$_ exists: ", -f $_ ? "yes" : "no", "\n"; -f $_ }
19     map { (my $x = $_) =~ s|\.o$|$depext|; $x; }
20     grep { $unified_info{sources}->{$_}->[0] =~ /\.cc?$/ }
21     keys %{$unified_info{sources}};
22
23 print STDERR "\@deps = ( ", join(", ", @deps), " )\n";
24
25 open IBF, $buildfile or die "Trying to read $buildfile: $!\n";
26 open OBF, '>', $buildfile_new or die "Trying to write $buildfile_new: $!\n";
27 while (<IBF>) {
28     $force_rewrite = 0;
29     last if /^# DO NOT DELETE THIS LINE/;
30     print OBF or die "$!\n";
31     $force_rewrite = 1;
32 }
33 close IBF;
34
35 print OBF "# DO NOT DELETE THIS LINE -- make depend depends on it.\n";
36
37 foreach (@deps) {
38     open IBF,$_ or die "Trying to read $_: $!\n";
39     while (<IBF>) {
40         print OBF or die "$!\n";
41     }
42     close IBF;
43 }
44 close OBF;
45
46 if (compare_text($buildfile_new, $buildfile) != 0) {
47     rename $buildfile_new, $buildfile
48         or die "Trying to rename $buildfile_new -> $buildfile: $!\n";
49 }
50