chiark / gitweb /
[PATCH] klibc: update v0.205
[elogind.git] / klibc / klcc.in
1 # -*- perl -*-
2
3 # Standard includes
4 @includes = ("-I${INSTALLDIR}/${CROSS}include/arch/${ARCH}",
5              "-I${INSTALLDIR}/${CROSS}include/bits${BITSIZE}",
6              "-I${INSTALLDIR}/${CROSS}include");
7
8 # Default optimization options (for compiles without -g)
9 @optopt =  @OPTFLAGS;
10 @goptopt = ('-O');
11
12 # Options and libraries to pass to ld; shared versus static
13 @staticopt = ("$INSTALLDIR/${CROSS}lib/crt0.o");
14 @staticlib = ("$INSTALLDIR/${CROSS}lib/libc.a");
15 @sharedopt = (@EMAIN, "$INSTALLDIR/${CROSS}lib/interp.o");
16 @sharedlib = ('-R', "$INSTALLDIR/lib/${CROSS}libc.so");
17
18 # Returns the language (-x option string) for a specific extension.
19 sub filename2lang($) {
20     my ($file) = @_;
21
22     return 'c' if ( $file =~ /\.c$/ );
23     return 'c-header' if ( $file =~ /\.h$/ );
24     return 'cpp-output' if ( $file =~ /\.i$/ );
25     return 'c++-cpp-output' if ( $file =~ /\.ii$/ );
26     return 'objective-c' if ( $file =~ /\.m$/ );
27     return 'objc-cpp-output' if ( $file =~ /\.mi$/ );
28     return 'c++' if ( $file =~/\.(cc|cp|cxx|cpp|CPP|c\+\+|C)$/ );
29     return 'c++-header' if ( $file =~ /\.(hh|H)$/ );
30     return 'f77' if ( $file =~ /\.(f|for|FOR)$/ );
31     return 'f77-cpp-input' if ( $file =~ /\.(F|fpp|FPP)$/ );
32     return 'ratfor' if ( $file =~ /\.r$/ );
33
34     # Is this correct?
35     return 'ada' if ( $file =~ /\.(ads|adb)$/ );
36
37     return 'assembler' if ( $file =~ /\.s$/ );
38     return 'assembler-with-cpp' if ( $file =~/ \.S$/ );
39
40     # Linker file; there is no option to gcc to assume something
41     # is a linker file, so we make up our own...
42     return 'obj';
43 }
44
45 # Produces a series of -x options and files
46 sub files_with_lang($$) {
47     my($files, $flang) = @_;
48     my(@as) = ();
49     my($xopt) = 'none';
50     my($need);
51
52     foreach $f ( @{$files} ) {
53         $need = ${$flang}{$f};
54         $need = 'none' if ( $need eq 'obj' );
55         unless ( $xopt eq $need ||
56                  ($xopt eq 'none' && filename2lang($f) eq $need) ) {
57             push(@as, '-x', $need);
58             $xopt = $need;
59         }
60         push(@as, $f);
61     }
62
63     return @as;
64 }
65
66 # Convert a return value from system() to an exit() code
67 sub syserr($) {
68     my($e) = @_;
69
70     return ($e & 0x7f) | 0x80 if ( $e & 0xff );
71     return $e >> 8;
72 }
73
74 # Run a program; printing out the command line if $verbose is set
75 sub mysystem(@) {
76     print STDERR join(' ', @_), "\n" if ( $verbose );
77     return system(@_);
78 }
79
80 #
81 # Begin parsing options.
82 #
83
84 @ccopt = ();
85 @ldopt = ();
86
87 @files = ();                    # List of files
88 %flang = ();                    # Languages for files
89
90 # This is 'c' for compile only, 'E' for preprocess only,
91 # 'S' for compile to assembly.
92 $operation = '';                # Compile and link
93
94 # Current -x option.  If undefined, it means autodetect.
95 undef $lang;
96
97 $save_temps = 0;                # The -save-temps option
98 $verbose = 0;                   # The -v option
99 $shared = 0;                    # Are we compiling shared?
100 $debugging = 0;                 # -g or -p option present?
101 $strip = 0;                     # -s option present?
102 undef $output;                  # -o option present?
103
104 while ( defined($a = shift(@ARGV)) ) {
105     if ( $a !~ /^\-/ ) {
106         # Not an option.  Must be a filename then.
107         push(@files, $a);
108         $flang{$a} = $lang || filename2lang($a);
109     } elsif ( $a =~ /^-Wl,(.*)$/ ) {
110         # -Wl used to pass options to the linker
111         push(@ldopt, split(/,/, $1));
112     } elsif ( $a =~ /^-([fmwWQdO]|std=|ansi|pedantic)/ ) {
113         # Options to gcc
114         push(@ccopt, $a);
115     } elsif ( $a =~ /^-([DUI])(.*)$/ ) {
116         # Options to gcc, which can take either a conjoined argument
117         # (-DFOO) or a disjoint argument (-D FOO)
118         push(@ccopt, $a);
119         push(@ccopt, shift(@ARGV)) if ( $2 eq '' );
120     } elsif ( $a eq '-include' ) {
121         # Options to gcc which always take a disjoint argument
122         push(@ccopt, $a, shift(@ARGV));
123     } elsif ( $a =~ /^-[gp]/ ) {
124         # Debugging options to gcc *and* ld
125         push(@ccopt, $a);
126         push(@ldopt, $a);
127         $debugging = 1;
128     } elsif ( $a eq '-v' ) {
129         push(@ccopt, $a);
130         $verbose = 1;
131     } elsif ( $a eq '-save-temps' ) {
132         push(@ccopt, $a);
133         $save_temps = 1;
134     } elsif ( $a =~ '^-([cSE])$' ) {
135         push(@ccopt, $a);
136         $operation = $1;
137     } elsif ( $a eq '-shared' ) {
138         $shared = 1;
139     } elsif ( $a eq '-static' ) {
140         $shared = 0;
141     } elsif ( $a eq '-s' ) {
142         $strip = 1;
143     } elsif ( $a eq '-o' ) {
144         $output = shift(@ARGV);
145     } elsif ( $a eq '-nostdinc' ) {
146         push(@ccopt, $a);
147         @includes = ();
148     } elsif ( $a =~ /^(-print|--help)/ ) {
149         # Pseudo-operations; just pass to gcc and don't do anything else
150         push(@ccopt, $a);
151         $operation = 'c' if ( $operation eq '' );
152     } else {
153         die "$0: unknown option: $a\n";
154     }
155 }
156
157 if ( $debugging ) {
158     @ccopt = (@REQFLAGS, @includes, @goptopt, @ccopt);
159 } else {
160     @ccopt = (@REQFLAGS, @includes, @optopt, @ccopt);
161 }
162
163 if ( $operation ne '' ) {
164     # Just run gcc with the appropriate options
165     @outopt = ('-o', $output) if ( defined($output) );
166     $rv = mysystem($CC, @ccopt, @outopt, files_with_lang(\@files, \%flang));
167 } else {
168     @outopt = ('-o', $output || 'a.out');
169
170     @objs = ();
171     @rmobjs = ();
172
173     foreach $f ( @files ) {
174         if ( $flang{$f} eq 'obj' ) {
175             push(@objs, $f);
176         } else {
177             $fo = $f;
178             $fo =~ s/\.[^\/.]+$/\.o/;
179
180             die if ( $f eq $fo ); # safety check
181
182             push(@objs, $fo);
183             push(@rmobjs, $fo) unless ( $save_temps );
184
185             $rv = mysystem($CC, @ccopt, '-c', '-o', $fo, '-x', $flang{$f}, $f);
186
187             if ( $rv ) {
188                 unlink(@rmobjs);
189                 exit syserr($rv);
190             }
191         }
192     }
193
194     if ( $shared ) {
195         $rv = mysystem($LD, @LDFLAGS, @sharedopt, @ldopt, @outopt, @objs, @sharedlib);
196     } else {
197         $rv = mysystem($LD, @LDFLAGS, @staticopt, @ldopt, @outopt, @objs, @staticlib);
198     }
199
200     unlink(@rmobjs);
201
202     if ( $strip && !$rv ) {
203         $rv = mysystem($STRIP, @STRIPFLAGS, $output);
204     }
205 }
206
207 exit syserr($rv);