1#!perl -w
2use strict;
3
4# prints libraries for static linking and exits
5
6use Config;
7
8my $out;
9if (@ARGV > 1 && $ARGV[0] eq "-o") {
10    shift;
11    $out = shift;
12}
13
14my @statics = split /\s+/, $Config{static_ext};
15
16my (@extralibs, %extralibs); # collect extralibs, preserving their order
17for (@statics) {
18    my $file = "..\\lib\\auto\\$_\\extralibs.ld";
19    open my $fh, '<', $file or die "can't open $file for reading: $!";
20    push @extralibs, grep {!$extralibs{$_}++} grep {/\S/} split /\s+/, join '', <$fh>;
21}
22
23my @libnames = join " ",
24    map {s|/|\\|g;m|([^\\]+)$|;"..\\lib\\auto\\$_\\$1$Config{_a}"} @statics,
25    @extralibs;
26my $result = join " ", @libnames;
27
28if ($out) {
29    my $do_update = 0;
30    # only write if:
31    #  - output doesn't exist
32    #  - there's a change in the content of the file
33    #  - one of the generated static libs is newer than the file
34    my $out_mtime;
35    if (open my $fh, "<", $out) {
36        $out_mtime = (stat $fh)[9];
37        my $current = do { local $/; <$fh> };
38        if ($current ne $result) {
39            ++$do_update;
40        }
41        close $fh;
42    }
43    else {
44        ++$do_update;
45    }
46    if (!$do_update && $out_mtime) {
47        for my $lib (@libnames) {
48            if ((stat $lib)[9] > $out_mtime) {
49                ++$do_update;
50                last;
51            }
52        }
53    }
54
55    unless ($do_update) {
56        print "$0: No changes, no libraries changed, nothing to do\n";
57        exit;
58    }
59
60    open my $fh, ">", $out
61        or die "Cannot create $out: $!";
62    print $fh $result;
63    close $fh or die "Failed to close $out: $!\n";
64}
65else {
66    print $result;
67}
68