util/add-depends.pl: sort the dependency files
[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     sort
19     grep { -f $_ }
20     map { (my $x = $_) =~ s|\.o$|$depext|; $x; }
21     grep { $unified_info{sources}->{$_}->[0] =~ /\.cc?$/ }
22     keys %{$unified_info{sources}};
23
24 open IBF, $buildfile or die "Trying to read $buildfile: $!\n";
25 open OBF, '>', $buildfile_new or die "Trying to write $buildfile_new: $!\n";
26 while (<IBF>) {
27     $force_rewrite = 0;
28     last if /^# DO NOT DELETE THIS LINE/;
29     print OBF or die "$!\n";
30     $force_rewrite = 1;
31 }
32 close IBF;
33
34 print OBF "# DO NOT DELETE THIS LINE -- make depend depends on it.\n";
35
36 foreach (@deps) {
37     open IBF,$_ or die "Trying to read $_: $!\n";
38     while (<IBF>) {
39         print OBF or die "$!\n";
40     }
41     close IBF;
42 }
43 close OBF;
44
45 if (compare_text($buildfile_new, $buildfile) != 0) {
46     rename $buildfile_new, $buildfile
47         or die "Trying to rename $buildfile_new -> $buildfile: $!\n";
48 }
49 # On VMS, we want to remove all generations of this file, in case there are
50 # more than one
51 while (unlink $buildfile_new) {}