Commit | Line | Data |
---|---|---|
52cebf5e EP |
1 | #!/usr/local/bin/perl |
2 | ||
3 | use Config; | |
4 | use File::Basename qw(&basename &dirname); | |
de0d1968 | 5 | use File::Spec; |
8a5546a1 | 6 | use Cwd; |
52cebf5e EP |
7 | |
8 | # List explicitly here the variables you want Configure to | |
9 | # generate. Metaconfig only looks for shell variables, so you | |
10 | # have to mention them as if they were shell variables, not | |
11 | # %Config entries. Thus you write | |
12 | # $startperl | |
13 | # to ensure Configure will look for $Config{startperl}. | |
14 | # Wanted: $archlibexp | |
15 | ||
16 | # This forces PL files to create target in same directory as PL file. | |
17 | # This is so that make depend always knows where to find PL derivatives. | |
8a5546a1 | 18 | $origdir = cwd; |
52cebf5e EP |
19 | chdir dirname($0); |
20 | $file = basename($0, '.PL'); | |
21 | $file .= '.com' if $^O eq 'VMS'; | |
22 | ||
23 | open OUT,">$file" or die "Can't create $file: $!"; | |
24 | ||
25 | print "Extracting $file (with variable substitutions)\n"; | |
26 | ||
27 | # In this section, perl variables will be expanded during extraction. | |
28 | # You can use $Config{...} to use Configure variables. | |
29 | ||
30 | print OUT <<"!GROK!THIS!"; | |
31 | $Config{startperl} | |
32 | eval 'exec $Config{perlpath} -S \$0 \${1+"\$@"}' | |
33 | if \$running_under_some_shell; | |
ecde9bf0 | 34 | --\$running_under_some_shell; |
52cebf5e EP |
35 | !GROK!THIS! |
36 | ||
37 | # In the following, perl variables are not expanded during extraction. | |
38 | ||
39 | print OUT <<'!NO!SUBS!'; | |
40 | ||
ecde9bf0 SC |
41 | # Version 2.0, Simon Cozens, Thu Mar 30 17:52:45 JST 2000 |
42 | # Version 2.01, Tom Christiansen, Thu Mar 30 08:25:14 MST 2000 | |
43 | # Version 2.02, Simon Cozens, Sun Apr 16 01:53:36 JST 2000 | |
e4f0d88d | 44 | # Version 2.03, Edward Peschko, Mon Feb 26 12:04:17 PST 2001 |
ecde9bf0 | 45 | |
52cebf5e | 46 | use strict; |
ecde9bf0 | 47 | use warnings; |
33024943 | 48 | use 5.006_000; |
52cebf5e | 49 | |
e4f0d88d | 50 | use FileHandle; |
ecde9bf0 SC |
51 | use Config; |
52 | use Fcntl qw(:DEFAULT :flock); | |
53 | use File::Temp qw(tempfile); | |
54 | use Cwd; | |
e4f0d88d | 55 | our $VERSION = 2.03; |
ecde9bf0 | 56 | $| = 1; |
52cebf5e | 57 | |
e4f0d88d EP |
58 | $SIG{INT} = sub { exit(); }; # exit gracefully and clean up after ourselves. |
59 | ||
ecde9bf0 SC |
60 | use subs qw{ |
61 | cc_harness check_read check_write checkopts_byte choose_backend | |
62 | compile_byte compile_cstyle compile_module generate_code | |
63 | grab_stash parse_argv sanity_check vprint yclept spawnit | |
64 | }; | |
65 | sub opt(*); # imal quoting | |
52cebf5e | 66 | |
ecde9bf0 SC |
67 | our ($Options, $BinPerl, $Backend); |
68 | our ($Input => $Output); | |
e4f0d88d EP |
69 | our ($logfh); |
70 | our ($cfile); | |
ef712cf7 | 71 | |
ecde9bf0 | 72 | # eval { main(); 1 } or die; |
52cebf5e EP |
73 | |
74 | main(); | |
75 | ||
e4f0d88d | 76 | sub main { |
ecde9bf0 SC |
77 | parse_argv(); |
78 | check_write($Output); | |
79 | choose_backend(); | |
80 | generate_code(); | |
e4f0d88d EP |
81 | run_code(); |
82 | _die("XXX: Not reached?"); | |
52cebf5e | 83 | } |
9636a016 | 84 | |
ecde9bf0 | 85 | ####################################################################### |
52cebf5e | 86 | |
ecde9bf0 SC |
87 | sub choose_backend { |
88 | # Choose the backend. | |
89 | $Backend = 'C'; | |
90 | if (opt(B)) { | |
91 | checkopts_byte(); | |
92 | $Backend = 'Bytecode'; | |
52cebf5e | 93 | } |
ecde9bf0 SC |
94 | if (opt(S) && opt(c)) { |
95 | # die "$0: Do you want me to compile this or not?\n"; | |
96 | delete $Options->{S}; | |
52cebf5e | 97 | } |
ecde9bf0 | 98 | $Backend = 'CC' if opt(O); |
52cebf5e EP |
99 | } |
100 | ||
52cebf5e | 101 | |
ecde9bf0 | 102 | sub generate_code { |
a07043ec | 103 | |
ecde9bf0 | 104 | vprint 0, "Compiling $Input"; |
9636a016 | 105 | |
ecde9bf0 | 106 | $BinPerl = yclept(); # Calling convention for perl. |
52cebf5e | 107 | |
ecde9bf0 SC |
108 | if (opt(shared)) { |
109 | compile_module(); | |
110 | } else { | |
111 | if ($Backend eq 'Bytecode') { | |
112 | compile_byte(); | |
113 | } else { | |
114 | compile_cstyle(); | |
115 | } | |
52cebf5e | 116 | } |
e4f0d88d EP |
117 | exit(0) if (!opt('r')); |
118 | } | |
52cebf5e | 119 | |
e4f0d88d EP |
120 | sub run_code { |
121 | vprint 0, "Running code"; | |
122 | run("$Output @ARGV"); | |
123 | exit(0); | |
52cebf5e EP |
124 | } |
125 | ||
ecde9bf0 SC |
126 | # usage: vprint [level] msg args |
127 | sub vprint { | |
128 | my $level; | |
129 | if (@_ == 1) { | |
130 | $level = 1; | |
131 | } elsif ($_[0] =~ /^\d$/) { | |
132 | $level = shift; | |
133 | } else { | |
134 | # well, they forgot to use a number; means >0 | |
135 | $level = 0; | |
136 | } | |
137 | my $msg = "@_"; | |
138 | $msg .= "\n" unless substr($msg, -1) eq "\n"; | |
e4f0d88d EP |
139 | if (opt(v) > $level) |
140 | { | |
141 | print "$0: $msg" if !opt('log'); | |
142 | print $logfh "$0: $msg" if opt('log'); | |
143 | } | |
144 | } | |
ecde9bf0 SC |
145 | |
146 | sub parse_argv { | |
147 | ||
148 | use Getopt::Long; | |
f5eac215 JH |
149 | |
150 | # disallows using long arguments | |
151 | # Getopt::Long::Configure("bundling"); | |
152 | ||
ecde9bf0 SC |
153 | Getopt::Long::Configure("no_ignore_case"); |
154 | ||
155 | # no difference in exists and defined for %ENV; also, a "0" | |
156 | # argument or a "" would not help cc, so skip | |
157 | unshift @ARGV, split ' ', $ENV{PERLCC_OPTS} if $ENV{PERLCC_OPTS}; | |
158 | ||
159 | $Options = {}; | |
160 | Getopt::Long::GetOptions( $Options, | |
161 | 'L:s', # lib directory | |
162 | 'I:s', # include directories (FOR C, NOT FOR PERL) | |
163 | 'o:s', # Output executable | |
e4f0d88d | 164 | 'v:i', # Verbosity level |
ecde9bf0 | 165 | 'e:s', # One-liner |
e4f0d88d | 166 | 'r', # run resulting executable |
ecde9bf0 SC |
167 | 'B', # Byte compiler backend |
168 | 'O', # Optimised C backend | |
169 | 'c', # Compile only | |
170 | 'h', # Help me | |
171 | 'S', # Dump C files | |
e4f0d88d EP |
172 | 'r', # run the resulting executable |
173 | 'static', # Dirty hack to enable -shared/-static | |
ecde9bf0 | 174 | 'shared', # Create a shared library (--shared for compat.) |
e4f0d88d | 175 | 'log:s' # where to log compilation process information |
ecde9bf0 SC |
176 | ); |
177 | ||
ecde9bf0 | 178 | $Options->{v} += 0; |
52cebf5e | 179 | |
ecde9bf0 | 180 | helpme() if opt(h); # And exit |
ef712cf7 | 181 | |
ecde9bf0 | 182 | $Output = opt(o) || 'a.out'; |
e4f0d88d EP |
183 | $Output = relativize($Output); |
184 | $logfh = new FileHandle(">> " . opt('log')) if (opt('log')); | |
ef712cf7 | 185 | |
ecde9bf0 SC |
186 | if (opt(e)) { |
187 | warn "$0: using -e 'code' as input file, ignoring @ARGV\n" if @ARGV; | |
188 | # We don't use a temporary file here; why bother? | |
189 | # XXX: this is not bullet proof -- spaces or quotes in name! | |
190 | $Input = "-e '".opt(e)."'"; # Quotes eaten by shell | |
191 | } else { | |
192 | $Input = shift @ARGV; # XXX: more files? | |
e4f0d88d | 193 | _usage_and_die("$0: No input file specified\n") unless $Input; |
ecde9bf0 SC |
194 | # DWIM modules. This is bad but necessary. |
195 | $Options->{shared}++ if $Input =~ /\.pm\z/; | |
196 | warn "$0: using $Input as input file, ignoring @ARGV\n" if @ARGV; | |
197 | check_read($Input); | |
198 | check_perl($Input); | |
199 | sanity_check(); | |
52cebf5e EP |
200 | } |
201 | ||
ecde9bf0 | 202 | } |
5268c7a4 | 203 | |
ecde9bf0 SC |
204 | sub opt(*) { |
205 | my $opt = shift; | |
206 | return exists($Options->{$opt}) && ($Options->{$opt} || 0); | |
207 | } | |
52cebf5e | 208 | |
ecde9bf0 SC |
209 | sub compile_module { |
210 | die "$0: Compiling to shared libraries is currently disabled\n"; | |
52cebf5e EP |
211 | } |
212 | ||
ecde9bf0 SC |
213 | sub compile_byte { |
214 | require ByteLoader; | |
215 | my $stash = grab_stash(); | |
216 | my $command = "$BinPerl -MO=Bytecode,$stash $Input"; | |
217 | # The -a option means we'd have to close the file and lose the | |
218 | # lock, which would create the tiniest of races. Instead, append | |
219 | # the output ourselves. | |
220 | vprint 1, "Writing on $Output"; | |
52cebf5e | 221 | |
ecde9bf0 SC |
222 | my $openflags = O_WRONLY | O_CREAT; |
223 | $openflags |= O_BINARY if eval { O_BINARY; 1 }; | |
224 | $openflags |= O_EXLOCK if eval { O_EXLOCK; 1 }; | |
ef712cf7 | 225 | |
ecde9bf0 | 226 | # these dies are not "$0: .... \n" because they "can't happen" |
ef712cf7 | 227 | |
ecde9bf0 SC |
228 | sysopen(OUT, $Output, $openflags) |
229 | or die "can't write to $Output: $!"; | |
52cebf5e | 230 | |
ecde9bf0 SC |
231 | # this is blocking; hold on; why are we doing this?? |
232 | # flock OUT, LOCK_EX or die "can't lock $Output: $!" | |
233 | # unless eval { O_EXLOCK; 1 }; | |
52cebf5e | 234 | |
ecde9bf0 SC |
235 | truncate(OUT, 0) |
236 | or die "couldn't trunc $Output: $!"; | |
52cebf5e | 237 | |
ecde9bf0 SC |
238 | print OUT <<EOF; |
239 | #!$^X | |
240 | use ByteLoader $ByteLoader::VERSION; | |
52cebf5e EP |
241 | EOF |
242 | ||
ecde9bf0 SC |
243 | # Now the compile: |
244 | vprint 1, "Compiling..."; | |
245 | vprint 3, "Calling $command"; | |
52cebf5e | 246 | |
d873810b | 247 | my ($output_r, $error_r) = spawnit($command); |
52cebf5e | 248 | |
d873810b | 249 | if (@$error_r && $? != 0) { |
e4f0d88d | 250 | _die("$0: $Input did not compile, which can't happen:\n@$error_r\n"); |
d873810b NC |
251 | } else { |
252 | my @error = grep { !/^$Input syntax OK$/o } @$error_r; | |
253 | warn "$0: Unexpected compiler output:\n@error" if @error; | |
ef712cf7 | 254 | } |
d873810b | 255 | |
ecde9bf0 | 256 | # Write it and leave. |
e4f0d88d EP |
257 | print OUT @$output_r or _die("can't write $Output: $!"); |
258 | close OUT or _die("can't close $Output: $!"); | |
52cebf5e | 259 | |
ecde9bf0 | 260 | # wait, how could it be anything but what you see next? |
e4f0d88d | 261 | chmod 0777 & ~umask, $Output or _die("can't chmod $Output: $!"); |
ecde9bf0 | 262 | exit 0; |
52cebf5e | 263 | } |
52cebf5e | 264 | |
ecde9bf0 SC |
265 | sub compile_cstyle { |
266 | my $stash = grab_stash(); | |
ef712cf7 | 267 | |
ecde9bf0 | 268 | # What are we going to call our output C file? |
ecde9bf0 | 269 | my $lose = 0; |
e4f0d88d EP |
270 | my ($cfh); |
271 | ||
ecde9bf0 SC |
272 | if (opt(S) || opt(c)) { |
273 | # We need to keep it. | |
274 | if (opt(e)) { | |
275 | $cfile = "a.out.c"; | |
276 | } else { | |
277 | $cfile = $Input; | |
278 | # File off extension if present | |
279 | # hold on: plx is executable; also, careful of ordering! | |
280 | $cfile =~ s/\.(?:p(?:lx|l|h)|m)\z//i; | |
281 | $cfile .= ".c"; | |
282 | $cfile = $Output if opt(c) && $Output =~ /\.c\z/i; | |
283 | } | |
284 | check_write($cfile); | |
285 | } else { | |
286 | # Don't need to keep it, be safe with a tempfile. | |
287 | $lose = 1; | |
288 | ($cfh, $cfile) = tempfile("pccXXXXX", SUFFIX => ".c"); | |
289 | close $cfh; # See comment just below | |
52cebf5e | 290 | } |
ecde9bf0 | 291 | vprint 1, "Writing C on $cfile"; |
52cebf5e | 292 | |
ecde9bf0 SC |
293 | my $max_line_len = ''; |
294 | if ($^O eq 'MSWin32' && $Config{cc} =~ /^cl/i) { | |
295 | $max_line_len = '-l2000,'; | |
296 | } | |
52cebf5e | 297 | |
ecde9bf0 SC |
298 | # This has to do the write itself, so we can't keep a lock. Life |
299 | # sucks. | |
300 | my $command = "$BinPerl -MO=$Backend,$max_line_len$stash,-o$cfile $Input"; | |
301 | vprint 1, "Compiling..."; | |
302 | vprint 1, "Calling $command"; | |
52cebf5e | 303 | |
ecde9bf0 SC |
304 | my ($output_r, $error_r) = spawnit($command); |
305 | my @output = @$output_r; | |
306 | my @error = @$error_r; | |
52cebf5e | 307 | |
ecde9bf0 | 308 | if (@error && $? != 0) { |
e4f0d88d | 309 | _die("$0: $Input did not compile, which can't happen:\n@error\n"); |
ecde9bf0 | 310 | } |
52cebf5e | 311 | |
ecde9bf0 | 312 | cc_harness($cfile,$stash) unless opt(c); |
52cebf5e | 313 | |
ecde9bf0 SC |
314 | if ($lose) { |
315 | vprint 2, "unlinking $cfile"; | |
e4f0d88d | 316 | unlink $cfile or _die("can't unlink $cfile: $!"); |
ecde9bf0 | 317 | } |
52cebf5e EP |
318 | } |
319 | ||
ecde9bf0 SC |
320 | sub cc_harness { |
321 | my ($cfile,$stash)=@_; | |
322 | use ExtUtils::Embed (); | |
323 | my $command = ExtUtils::Embed::ccopts." -o $Output $cfile "; | |
3af308c7 SC |
324 | $command .= " -I".$_ for split /\s+/, opt(I); |
325 | $command .= " -L".$_ for split /\s+/, opt(L); | |
ecde9bf0 | 326 | my @mods = split /-?u /, $stash; |
3af308c7 | 327 | $command .= " ".ExtUtils::Embed::ldopts("-std", \@mods); |
f5eac215 | 328 | $command .= " -lperl"; |
e4f0d88d EP |
329 | vprint 3, "running $Config{cc} $command"; |
330 | system("$Config{cc} $command"); | |
52cebf5e EP |
331 | } |
332 | ||
ecde9bf0 SC |
333 | # Where Perl is, and which include path to give it. |
334 | sub yclept { | |
335 | my $command = "$^X "; | |
336 | ||
337 | # DWIM the -I to be Perl, not C, include directories. | |
338 | if (opt(I) && $Backend eq "Bytecode") { | |
339 | for (split /\s+/, opt(I)) { | |
340 | if (-d $_) { | |
341 | push @INC, $_; | |
342 | } else { | |
343 | warn "$0: Include directory $_ not found, skipping\n"; | |
344 | } | |
52cebf5e EP |
345 | } |
346 | } | |
ecde9bf0 SC |
347 | |
348 | $command .= "-I$_ " for @INC; | |
349 | return $command; | |
52cebf5e EP |
350 | } |
351 | ||
ecde9bf0 | 352 | # Use B::Stash to find additional modules and stuff. |
52cebf5e | 353 | { |
ecde9bf0 SC |
354 | my $_stash; |
355 | sub grab_stash { | |
52cebf5e | 356 | |
ecde9bf0 | 357 | warn "already called get_stash once" if $_stash; |
52cebf5e | 358 | |
ecde9bf0 SC |
359 | my $command = "$BinPerl -MB::Stash -c $Input"; |
360 | # Filename here is perfectly sanitised. | |
361 | vprint 3, "Calling $command\n"; | |
9636a016 | 362 | |
ecde9bf0 SC |
363 | my ($stash_r, $error_r) = spawnit($command); |
364 | my @stash = @$stash_r; | |
365 | my @error = @$error_r; | |
52cebf5e | 366 | |
ecde9bf0 | 367 | if (@error && $? != 0) { |
e4f0d88d | 368 | _die("$0: $Input did not compile:\n@error\n"); |
ecde9bf0 | 369 | } |
52cebf5e | 370 | |
ecde9bf0 SC |
371 | $stash[0] =~ s/,-u\<none\>//; |
372 | vprint 2, "Stash: ", join " ", split /,?-u/, $stash[0]; | |
373 | chomp $stash[0]; | |
374 | return $_stash = $stash[0]; | |
52cebf5e EP |
375 | } |
376 | ||
ecde9bf0 | 377 | } |
52cebf5e | 378 | |
ecde9bf0 SC |
379 | # Check the consistency of options if -B is selected. |
380 | # To wit, (-B|-O) ==> no -shared, no -S, no -c | |
381 | sub checkopts_byte { | |
52cebf5e | 382 | |
e4f0d88d | 383 | _die("$0: Please choose one of either -B and -O.\n") if opt(O); |
52cebf5e | 384 | |
ecde9bf0 SC |
385 | if (opt(shared)) { |
386 | warn "$0: Will not create a shared library for bytecode\n"; | |
387 | delete $Options->{shared}; | |
388 | } | |
52cebf5e | 389 | |
ecde9bf0 SC |
390 | for my $o ( qw[c S] ) { |
391 | if (opt($o)) { | |
392 | warn "$0: Compiling to bytecode is a one-pass process--", | |
393 | "-$o ignored\n"; | |
394 | delete $Options->{$o}; | |
395 | } | |
52cebf5e EP |
396 | } |
397 | ||
52cebf5e EP |
398 | } |
399 | ||
ecde9bf0 SC |
400 | # Check the input and output files make sense, are read/writeable. |
401 | sub sanity_check { | |
402 | if ($Input eq $Output) { | |
403 | if ($Input eq 'a.out') { | |
e4f0d88d EP |
404 | _die("$0: Compiling a.out is probably not what you want to do.\n"); |
405 | # You fully deserve what you get now. No you *don't*. typos happen. | |
ecde9bf0 SC |
406 | } else { |
407 | warn "$0: Will not write output on top of input file, ", | |
408 | "compiling to a.out instead\n"; | |
409 | $Output = "a.out"; | |
410 | } | |
52cebf5e EP |
411 | } |
412 | } | |
413 | ||
ecde9bf0 SC |
414 | sub check_read { |
415 | my $file = shift; | |
416 | unless (-r $file) { | |
e4f0d88d | 417 | _die("$0: Input file $file is a directory, not a file\n") if -d _; |
ecde9bf0 | 418 | unless (-e _) { |
e4f0d88d | 419 | _die("$0: Input file $file was not found\n"); |
ecde9bf0 | 420 | } else { |
e4f0d88d | 421 | _die("$0: Cannot read input file $file: $!\n"); |
ecde9bf0 | 422 | } |
52cebf5e | 423 | } |
ecde9bf0 SC |
424 | unless (-f _) { |
425 | # XXX: die? don't try this on /dev/tty | |
426 | warn "$0: WARNING: input $file is not a plain file\n"; | |
427 | } | |
52cebf5e EP |
428 | } |
429 | ||
ecde9bf0 SC |
430 | sub check_write { |
431 | my $file = shift; | |
432 | if (-d $file) { | |
e4f0d88d | 433 | _die("$0: Cannot write on $file, is a directory\n"); |
ecde9bf0 SC |
434 | } |
435 | if (-e _) { | |
e4f0d88d | 436 | _die("$0: Cannot write on $file: $!\n") unless -w _; |
ecde9bf0 SC |
437 | } |
438 | unless (-w cwd()) { | |
e4f0d88d | 439 | _die("$0: Cannot write in this directory: $!\n"); |
ef712cf7 | 440 | } |
ef712cf7 EP |
441 | } |
442 | ||
ecde9bf0 SC |
443 | sub check_perl { |
444 | my $file = shift; | |
445 | unless (-T $file) { | |
446 | warn "$0: Binary `$file' sure doesn't smell like perl source!\n"; | |
447 | print "Checking file type... "; | |
448 | system("file", $file); | |
e4f0d88d | 449 | _die("Please try a perlier file!\n"); |
ecde9bf0 SC |
450 | } |
451 | ||
e4f0d88d | 452 | open(my $handle, "<", $file) or _die("XXX: can't open $file: $!"); |
ecde9bf0 SC |
453 | local $_ = <$handle>; |
454 | if (/^#!/ && !/perl/) { | |
e4f0d88d | 455 | _die("$0: $file is a ", /^#!\s*(\S+)/, " script, not perl\n"); |
ecde9bf0 SC |
456 | } |
457 | ||
458 | } | |
459 | ||
460 | # File spawning and error collecting | |
461 | sub spawnit { | |
462 | my ($command) = shift; | |
463 | my (@error,@output); | |
464 | my $errname; | |
465 | (undef, $errname) = tempfile("pccXXXXX"); | |
466 | { | |
467 | open (S_OUT, "$command 2>$errname |") | |
e4f0d88d | 468 | or _die("$0: Couldn't spawn the compiler.\n"); |
ecde9bf0 SC |
469 | @output = <S_OUT>; |
470 | } | |
e4f0d88d | 471 | open (S_ERROR, $errname) or _die("$0: Couldn't read the error file.\n"); |
ecde9bf0 SC |
472 | @error = <S_ERROR>; |
473 | close S_ERROR; | |
474 | close S_OUT; | |
e4f0d88d | 475 | unlink $errname or _die("$0: Can't unlink error file $errname"); |
ecde9bf0 SC |
476 | return (\@output, \@error); |
477 | } | |
52cebf5e | 478 | |
ecde9bf0 SC |
479 | sub helpme { |
480 | print "perlcc compiler frontend, version $VERSION\n\n"; | |
481 | { no warnings; | |
482 | exec "pod2usage $0"; | |
483 | exec "perldoc $0"; | |
484 | exec "pod2text $0"; | |
485 | } | |
52cebf5e EP |
486 | } |
487 | ||
e4f0d88d EP |
488 | sub relativize { |
489 | my ($args) = @_; | |
490 | ||
491 | return() if ($args =~ m"^[/\\]"); | |
492 | return("./$args"); | |
493 | } | |
494 | ||
495 | sub _die { | |
496 | $logfh->print(@_) if opt('log'); | |
497 | print STDERR @_; | |
498 | exit(); # should die eventually. However, needed so that a 'make compile' | |
499 | # can compile all the way through to the end for standard dist. | |
500 | } | |
501 | ||
502 | sub _usage_and_die { | |
503 | _die(<<EOU); | |
504 | $0: Usage: | |
505 | $0 [-o executable] [-r] [-O|-B|-c|-S] [-log log] [source[.pl] | -e oneliner] | |
506 | EOU | |
507 | } | |
508 | ||
509 | sub run { | |
510 | my (@commands) = @_; | |
511 | ||
512 | print interruptrun(@commands) if (!opt('log')); | |
513 | $logfh->print(interruptrun(@commands)) if (opt('log')); | |
514 | } | |
515 | ||
516 | sub interruptrun | |
517 | { | |
518 | my (@commands) = @_; | |
519 | ||
520 | my $command = join('', @commands); | |
521 | local(*FD); | |
522 | my $pid = open(FD, "$command |"); | |
523 | my $text; | |
524 | ||
525 | local($SIG{HUP}) = sub { kill 9, $pid; exit }; | |
526 | local($SIG{INT}) = sub { kill 9, $pid; exit }; | |
527 | ||
528 | my $needalarm = | |
529 | ($ENV{PERLCC_TIMEOUT} && | |
530 | $Config{'osname'} ne 'MSWin32' && | |
531 | $command =~ m"(^|\s)perlcc\s"); | |
532 | ||
533 | eval | |
534 | { | |
535 | local($SIG{ALRM}) = sub { die "INFINITE LOOP"; }; | |
536 | alarm($ENV{PERLCC_TIMEOUT}) if ($needalarm); | |
537 | $text = join('', <FD>); | |
538 | alarm(0) if ($needalarm); | |
539 | }; | |
540 | ||
541 | if ($@) | |
542 | { | |
543 | eval { kill 'HUP', $pid }; | |
544 | vprint 0, "SYSTEM TIMEOUT (infinite loop?)\n"; | |
545 | } | |
546 | ||
547 | close(FD); | |
548 | return($text); | |
549 | } | |
550 | ||
551 | END { | |
552 | unlink $cfile if ($cfile && !opt(S) && !opt(c)); | |
553 | } | |
52cebf5e EP |
554 | |
555 | __END__ | |
556 | ||
557 | =head1 NAME | |
558 | ||
ecde9bf0 | 559 | perlcc - generate executables from Perl programs |
52cebf5e EP |
560 | |
561 | =head1 SYNOPSIS | |
562 | ||
ecde9bf0 SC |
563 | $ perlcc hello # Compiles into executable 'a.out' |
564 | $ perlcc -o hello hello.pl # Compiles into executable 'hello' | |
52cebf5e | 565 | |
ecde9bf0 SC |
566 | $ perlcc -O file # Compiles using the optimised C backend |
567 | $ perlcc -B file # Compiles using the bytecode backend | |
52cebf5e | 568 | |
ecde9bf0 SC |
569 | $ perlcc -c file # Creates a C file, 'file.c' |
570 | $ perlcc -S -o hello file # Creates a C file, 'file.c', | |
571 | # then compiles it to executable 'hello' | |
572 | $ perlcc -c out.c file # Creates a C file, 'out.c' from 'file' | |
52cebf5e | 573 | |
ecde9bf0 SC |
574 | $ perlcc -e 'print q//' # Compiles a one-liner into 'a.out' |
575 | $ perlcc -c -e 'print q//' # Creates a C file 'a.out.c' | |
e4f0d88d | 576 | |
f5eac215 JH |
577 | $ perlcc -I /foo hello # extra headers (notice the space after -I) |
578 | $ perlcc -L /foo hello # extra libraries (notice the space after -L) | |
e4f0d88d | 579 | |
f5eac215 | 580 | $ perlcc -r hello # compiles 'hello' into 'a.out', runs 'a.out'. |
e4f0d88d EP |
581 | $ perlcc -r hello a b c # compiles 'hello' into 'a.out', runs 'a.out'. |
582 | # with arguments 'a b c' | |
583 | ||
584 | $ perlcc hello -log c # compiles 'hello' into 'a.out' logs compile | |
585 | # log into 'c'. | |
586 | ||
52cebf5e EP |
587 | =head1 DESCRIPTION |
588 | ||
ecde9bf0 SC |
589 | F<perlcc> creates standalone executables from Perl programs, using the |
590 | code generators provided by the L<B> module. At present, you may | |
591 | either create executable Perl bytecode, using the C<-B> option, or | |
592 | generate and compile C files using the standard and 'optimised' C | |
593 | backends. | |
52cebf5e | 594 | |
ecde9bf0 SC |
595 | The code generated in this way is not guaranteed to work. The whole |
596 | codegen suite (C<perlcc> included) should be considered B<very> | |
597 | experimental. Use for production purposes is strongly discouraged. | |
52cebf5e | 598 | |
ecde9bf0 | 599 | =head1 OPTIONS |
52cebf5e EP |
600 | |
601 | =over 4 | |
602 | ||
ecde9bf0 | 603 | =item -LI<library directories> |
52cebf5e | 604 | |
ecde9bf0 SC |
605 | Adds the given directories to the library search path when C code is |
606 | passed to your C compiler. | |
52cebf5e | 607 | |
ecde9bf0 | 608 | =item -II<include directories> |
52cebf5e | 609 | |
ecde9bf0 SC |
610 | Adds the given directories to the include file search path when C code is |
611 | passed to your C compiler; when using the Perl bytecode option, adds the | |
612 | given directories to Perl's include path. | |
9636a016 | 613 | |
ecde9bf0 | 614 | =item -o I<output file name> |
9636a016 | 615 | |
ecde9bf0 | 616 | Specifies the file name for the final compiled executable. |
9636a016 | 617 | |
ecde9bf0 | 618 | =item -c I<C file name> |
9636a016 | 619 | |
ecde9bf0 | 620 | Create C code only; do not compile to a standalone binary. |
52cebf5e | 621 | |
ecde9bf0 | 622 | =item -e I<perl code> |
52cebf5e | 623 | |
ecde9bf0 | 624 | Compile a one-liner, much the same as C<perl -e '...'> |
52cebf5e | 625 | |
ecde9bf0 | 626 | =item -S |
52cebf5e | 627 | |
ecde9bf0 | 628 | Do not delete generated C code after compilation. |
52cebf5e | 629 | |
ecde9bf0 | 630 | =item -B |
52cebf5e | 631 | |
ecde9bf0 | 632 | Use the Perl bytecode code generator. |
52cebf5e | 633 | |
ecde9bf0 | 634 | =item -O |
52cebf5e | 635 | |
ecde9bf0 SC |
636 | Use the 'optimised' C code generator. This is more experimental than |
637 | everything else put together, and the code created is not guaranteed to | |
638 | compile in finite time and memory, or indeed, at all. | |
52cebf5e | 639 | |
ecde9bf0 | 640 | =item -v |
52cebf5e | 641 | |
ecde9bf0 | 642 | Increase verbosity of output; can be repeated for more verbose output. |
52cebf5e | 643 | |
e4f0d88d EP |
644 | =item -r |
645 | ||
646 | Run the resulting compiled script after compiling it. | |
647 | ||
648 | =item -log | |
649 | ||
650 | Log the output of compiling to a file rather than to stdout. | |
651 | ||
52cebf5e EP |
652 | =back |
653 | ||
52cebf5e EP |
654 | =cut |
655 | ||
656 | !NO!SUBS! | |
657 | ||
658 | close OUT or die "Can't close $file: $!"; | |
659 | chmod 0755, $file or die "Can't reset permissions for $file: $!\n"; | |
660 | exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':'; | |
8a5546a1 | 661 | chdir $origdir; |