]> sourceware.org Git - newlib-cygwin.git/blob - winsup/cygwin/mkstatic
* mkstatic: Make sure that we are not cd'ed to temporary directory on exit to
[newlib-cygwin.git] / winsup / cygwin / mkstatic
1 #!/usr/bin/perl
2 use strict;
3 use Cwd;
4 use Getopt::Long;
5 use File::Temp qw/tempdir/;
6 use File::Basename;
7
8 sub xsystem(@);
9
10 my @exclude = ();
11 my @library = ();
12 my $ar;
13 our $x;
14 GetOptions('exclude=s'=>\@exclude, 'library=s'=>\@library, 'ar=s'=>\$ar, 'x!'=>\$x);
15
16 die "$0: must specify --ar\n" unless defined $ar;
17 my $lib = shift or die "$0: missing lib argument\nusage: $0 lib [map-file]\n";
18 $lib = Cwd::abs_path($lib);
19
20 my %excludes = map {($_, 1)} @exclude;
21 my $libraries = join('|', map {quotemeta} @library);
22
23 my %sources = ();
24 while (<>) {
25 my ($source, $file, $absfile);
26 if (m%^($libraries)\(([^)]*)\)%o) {
27 $source = $1;
28 $absfile = $file = $2;
29 } elsif (/^LOAD\s+(.*\.o)$/o) {
30 $source = '.';
31 $file = $1;
32 $absfile = Cwd::abs_path($file);
33 } else {
34 next;
35 }
36 push @{$sources{$source}}, $absfile unless $excludes{$file} || $excludes{$source};
37 }
38
39 my $here = getcwd();
40 my $dir = tempdir(CLEANUP=>1);
41 chdir $dir;
42 my @files = ();
43 for (sort keys %sources) {
44 if ($_ eq '.') {
45 xsystem '/bin/cp', '-a', @{$sources{$_}}, '.';
46 } else {
47 xsystem $ar, 'x', $_, @{$sources{$_}}, '.';
48 }
49 push @files, map {basename($_)} @{$sources{$_}};
50 }
51
52 unlink $lib;
53 xsystem $ar, 'crs', $lib, sort @files;
54 exit 0;
55
56 sub xsystem(@) {
57 print join(' ', 'x', @_), "\n" if $x;
58 system(@_) == 0 or die "$0: $_[0] $_[1] $_[2]... exited with non-zero status\n";
59 }
60
61 END {
62 chdir '/tmp'; # Allow $dir directory removal on Windows
63 }
This page took 0.041676 seconds and 5 git commands to generate.