4 use File::Basename qw(&basename &dirname);
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
13 # to ensure Configure will look for $Config{startperl}.
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.
20 $file = basename($0, '.PL');
21 $file .= '.com' if $^O eq 'VMS';
23 open OUT,">$file" or die "Can't create $file: $!";
25 print "Extracting $file (with variable substitutions)\n";
27 # In this section, perl variables will be expanded during extraction.
28 # You can use $Config{...} to use Configure variables.
30 print OUT <<"!GROK!THIS!";
32 eval 'exec $Config{perlpath} -S \$0 \${1+"\$@"}'
33 if \$running_under_some_shell;
34 --\$running_under_some_shell;
37 # In the following, perl variables are not expanded during extraction.
39 print OUT <<'!NO!SUBS!';
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
44 # Version 2.03, Edward Peschko, Mon Feb 26 12:04:17 PST 2001
52 use Fcntl qw(:DEFAULT :flock);
53 use File::Temp qw(tempfile);
58 $SIG{INT} = sub { exit(); }; # exit gracefully and clean up after ourselves.
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
65 sub opt(*); # imal quoting
67 our ($Options, $BinPerl, $Backend);
68 our ($Input => $Output);
72 # eval { main(); 1 } or die;
82 _die("XXX: Not reached?");
85 #######################################################################
92 $Backend = 'Bytecode';
94 if (opt(S) && opt(c)) {
95 # die "$0: Do you want me to compile this or not?\n";
98 $Backend = 'CC' if opt(O);
104 vprint 0, "Compiling $Input";
106 $BinPerl = yclept(); # Calling convention for perl.
111 if ($Backend eq 'Bytecode') {
117 exit(0) if (!opt('r'));
121 vprint 0, "Running code";
122 run("$Output @ARGV");
126 # usage: vprint [level] msg args
131 } elsif ($_[0] =~ /^\d$/) {
134 # well, they forgot to use a number; means >0
138 $msg .= "\n" unless substr($msg, -1) eq "\n";
141 print "$0: $msg" if !opt('log');
142 print $logfh "$0: $msg" if opt('log');
149 # Getopt::Long::Configure("bundling"); turned off. this is silly because
150 # it doesn't allow for long switches.
151 Getopt::Long::Configure("no_ignore_case");
153 # no difference in exists and defined for %ENV; also, a "0"
154 # argument or a "" would not help cc, so skip
155 unshift @ARGV, split ' ', $ENV{PERLCC_OPTS} if $ENV{PERLCC_OPTS};
158 Getopt::Long::GetOptions( $Options,
159 'L:s', # lib directory
160 'I:s', # include directories (FOR C, NOT FOR PERL)
161 'o:s', # Output executable
162 'v:i', # Verbosity level
164 'r', # run resulting executable
165 'B', # Byte compiler backend
166 'O', # Optimised C backend
170 'r', # run the resulting executable
171 'static', # Dirty hack to enable -shared/-static
172 'shared', # Create a shared library (--shared for compat.)
173 'log:s' # where to log compilation process information
176 # This is an attempt to make perlcc's arg. handling look like cc.
177 # if ( opt('s') ) { # must quote: looks like s)foo)bar)!
178 # if (opt('s') eq 'hared') {
179 # $Options->{shared}++;
180 # } elsif (opt('s') eq 'tatic') {
181 # $Options->{static}++;
183 # warn "$0: Unknown option -s", opt('s');
189 helpme() if opt(h); # And exit
191 $Output = opt(o) || 'a.out';
192 $Output = relativize($Output);
193 $logfh = new FileHandle(">> " . opt('log')) if (opt('log'));
196 warn "$0: using -e 'code' as input file, ignoring @ARGV\n" if @ARGV;
197 # We don't use a temporary file here; why bother?
198 # XXX: this is not bullet proof -- spaces or quotes in name!
199 $Input = "-e '".opt(e)."'"; # Quotes eaten by shell
201 $Input = shift @ARGV; # XXX: more files?
202 _usage_and_die("$0: No input file specified\n") unless $Input;
203 # DWIM modules. This is bad but necessary.
204 $Options->{shared}++ if $Input =~ /\.pm\z/;
205 warn "$0: using $Input as input file, ignoring @ARGV\n" if @ARGV;
215 return exists($Options->{$opt}) && ($Options->{$opt} || 0);
219 die "$0: Compiling to shared libraries is currently disabled\n";
224 my $stash = grab_stash();
225 my $command = "$BinPerl -MO=Bytecode,$stash $Input";
226 # The -a option means we'd have to close the file and lose the
227 # lock, which would create the tiniest of races. Instead, append
228 # the output ourselves.
229 vprint 1, "Writing on $Output";
231 my $openflags = O_WRONLY | O_CREAT;
232 $openflags |= O_BINARY if eval { O_BINARY; 1 };
233 $openflags |= O_EXLOCK if eval { O_EXLOCK; 1 };
235 # these dies are not "$0: .... \n" because they "can't happen"
237 sysopen(OUT, $Output, $openflags)
238 or die "can't write to $Output: $!";
240 # this is blocking; hold on; why are we doing this??
241 # flock OUT, LOCK_EX or die "can't lock $Output: $!"
242 # unless eval { O_EXLOCK; 1 };
245 or die "couldn't trunc $Output: $!";
249 use ByteLoader $ByteLoader::VERSION;
253 vprint 1, "Compiling...";
254 vprint 3, "Calling $command";
256 my ($output_r, $error_r) = spawnit($command);
258 if (@$error_r && $? != 0) {
259 _die("$0: $Input did not compile, which can't happen:\n@$error_r\n");
261 my @error = grep { !/^$Input syntax OK$/o } @$error_r;
262 warn "$0: Unexpected compiler output:\n@error" if @error;
265 # Write it and leave.
266 print OUT @$output_r or _die("can't write $Output: $!");
267 close OUT or _die("can't close $Output: $!");
269 # wait, how could it be anything but what you see next?
270 chmod 0777 & ~umask, $Output or _die("can't chmod $Output: $!");
275 my $stash = grab_stash();
277 # What are we going to call our output C file?
281 if (opt(S) || opt(c)) {
282 # We need to keep it.
287 # File off extension if present
288 # hold on: plx is executable; also, careful of ordering!
289 $cfile =~ s/\.(?:p(?:lx|l|h)|m)\z//i;
291 $cfile = $Output if opt(c) && $Output =~ /\.c\z/i;
295 # Don't need to keep it, be safe with a tempfile.
297 ($cfh, $cfile) = tempfile("pccXXXXX", SUFFIX => ".c");
298 close $cfh; # See comment just below
300 vprint 1, "Writing C on $cfile";
302 my $max_line_len = '';
303 if ($^O eq 'MSWin32' && $Config{cc} =~ /^cl/i) {
304 $max_line_len = '-l2000,';
307 # This has to do the write itself, so we can't keep a lock. Life
309 my $command = "$BinPerl -MO=$Backend,$max_line_len$stash,-o$cfile $Input";
310 vprint 1, "Compiling...";
311 vprint 1, "Calling $command";
313 my ($output_r, $error_r) = spawnit($command);
314 my @output = @$output_r;
315 my @error = @$error_r;
317 if (@error && $? != 0) {
318 _die("$0: $Input did not compile, which can't happen:\n@error\n");
321 cc_harness($cfile,$stash) unless opt(c);
324 vprint 2, "unlinking $cfile";
325 unlink $cfile or _die("can't unlink $cfile: $!");
330 my ($cfile,$stash)=@_;
331 use ExtUtils::Embed ();
332 my $command = ExtUtils::Embed::ccopts." -o $Output $cfile ";
333 $command .= " -I".$_ for split /\s+/, opt(I);
334 $command .= " -L".$_ for split /\s+/, opt(L);
335 my @mods = split /-?u /, $stash;
336 $command .= " ".ExtUtils::Embed::ldopts("-std", \@mods);
337 vprint 3, "running $Config{cc} $command";
338 system("$Config{cc} $command");
341 # Where Perl is, and which include path to give it.
343 my $command = "$^X ";
345 # DWIM the -I to be Perl, not C, include directories.
346 if (opt(I) && $Backend eq "Bytecode") {
347 for (split /\s+/, opt(I)) {
351 warn "$0: Include directory $_ not found, skipping\n";
356 $command .= "-I$_ " for @INC;
360 # Use B::Stash to find additional modules and stuff.
365 warn "already called get_stash once" if $_stash;
367 my $command = "$BinPerl -MB::Stash -c $Input";
368 # Filename here is perfectly sanitised.
369 vprint 3, "Calling $command\n";
371 my ($stash_r, $error_r) = spawnit($command);
372 my @stash = @$stash_r;
373 my @error = @$error_r;
375 if (@error && $? != 0) {
376 _die("$0: $Input did not compile:\n@error\n");
379 $stash[0] =~ s/,-u\<none\>//;
380 vprint 2, "Stash: ", join " ", split /,?-u/, $stash[0];
382 return $_stash = $stash[0];
387 # Check the consistency of options if -B is selected.
388 # To wit, (-B|-O) ==> no -shared, no -S, no -c
391 _die("$0: Please choose one of either -B and -O.\n") if opt(O);
394 warn "$0: Will not create a shared library for bytecode\n";
395 delete $Options->{shared};
398 for my $o ( qw[c S] ) {
400 warn "$0: Compiling to bytecode is a one-pass process--",
402 delete $Options->{$o};
408 # Check the input and output files make sense, are read/writeable.
410 if ($Input eq $Output) {
411 if ($Input eq 'a.out') {
412 _die("$0: Compiling a.out is probably not what you want to do.\n");
413 # You fully deserve what you get now. No you *don't*. typos happen.
415 warn "$0: Will not write output on top of input file, ",
416 "compiling to a.out instead\n";
425 _die("$0: Input file $file is a directory, not a file\n") if -d _;
427 _die("$0: Input file $file was not found\n");
429 _die("$0: Cannot read input file $file: $!\n");
433 # XXX: die? don't try this on /dev/tty
434 warn "$0: WARNING: input $file is not a plain file\n";
441 _die("$0: Cannot write on $file, is a directory\n");
444 _die("$0: Cannot write on $file: $!\n") unless -w _;
447 _die("$0: Cannot write in this directory: $!\n");
454 warn "$0: Binary `$file' sure doesn't smell like perl source!\n";
455 print "Checking file type... ";
456 system("file", $file);
457 _die("Please try a perlier file!\n");
460 open(my $handle, "<", $file) or _die("XXX: can't open $file: $!");
461 local $_ = <$handle>;
462 if (/^#!/ && !/perl/) {
463 _die("$0: $file is a ", /^#!\s*(\S+)/, " script, not perl\n");
468 # File spawning and error collecting
470 my ($command) = shift;
473 (undef, $errname) = tempfile("pccXXXXX");
475 open (S_OUT, "$command 2>$errname |")
476 or _die("$0: Couldn't spawn the compiler.\n");
479 open (S_ERROR, $errname) or _die("$0: Couldn't read the error file.\n");
483 unlink $errname or _die("$0: Can't unlink error file $errname");
484 return (\@output, \@error);
488 print "perlcc compiler frontend, version $VERSION\n\n";
499 return() if ($args =~ m"^[/\\]");
504 $logfh->print(@_) if opt('log');
506 exit(); # should die eventually. However, needed so that a 'make compile'
507 # can compile all the way through to the end for standard dist.
513 $0 [-o executable] [-r] [-O|-B|-c|-S] [-log log] [source[.pl] | -e oneliner]
520 print interruptrun(@commands) if (!opt('log'));
521 $logfh->print(interruptrun(@commands)) if (opt('log'));
528 my $command = join('', @commands);
530 my $pid = open(FD, "$command |");
533 local($SIG{HUP}) = sub { kill 9, $pid; exit };
534 local($SIG{INT}) = sub { kill 9, $pid; exit };
537 ($ENV{PERLCC_TIMEOUT} &&
538 $Config{'osname'} ne 'MSWin32' &&
539 $command =~ m"(^|\s)perlcc\s");
543 local($SIG{ALRM}) = sub { die "INFINITE LOOP"; };
544 alarm($ENV{PERLCC_TIMEOUT}) if ($needalarm);
545 $text = join('', <FD>);
546 alarm(0) if ($needalarm);
551 eval { kill 'HUP', $pid };
552 vprint 0, "SYSTEM TIMEOUT (infinite loop?)\n";
560 unlink $cfile if ($cfile && !opt(S) && !opt(c));
567 perlcc - generate executables from Perl programs
571 $ perlcc hello # Compiles into executable 'a.out'
572 $ perlcc -o hello hello.pl # Compiles into executable 'hello'
574 $ perlcc -O file # Compiles using the optimised C backend
575 $ perlcc -B file # Compiles using the bytecode backend
577 $ perlcc -c file # Creates a C file, 'file.c'
578 $ perlcc -S -o hello file # Creates a C file, 'file.c',
579 # then compiles it to executable 'hello'
580 $ perlcc -c out.c file # Creates a C file, 'out.c' from 'file'
582 $ perlcc -e 'print q//' # Compiles a one-liner into 'a.out'
583 $ perlcc -c -e 'print q//' # Creates a C file 'a.out.c'
585 $ perlcc -r hello # compiles 'hello' into 'a.out', runs 'a.out'.
587 $ perlcc -r hello a b c # compiles 'hello' into 'a.out', runs 'a.out'.
588 # with arguments 'a b c'
590 $ perlcc hello -log c # compiles 'hello' into 'a.out' logs compile
595 F<perlcc> creates standalone executables from Perl programs, using the
596 code generators provided by the L<B> module. At present, you may
597 either create executable Perl bytecode, using the C<-B> option, or
598 generate and compile C files using the standard and 'optimised' C
601 The code generated in this way is not guaranteed to work. The whole
602 codegen suite (C<perlcc> included) should be considered B<very>
603 experimental. Use for production purposes is strongly discouraged.
609 =item -LI<library directories>
611 Adds the given directories to the library search path when C code is
612 passed to your C compiler.
614 =item -II<include directories>
616 Adds the given directories to the include file search path when C code is
617 passed to your C compiler; when using the Perl bytecode option, adds the
618 given directories to Perl's include path.
620 =item -o I<output file name>
622 Specifies the file name for the final compiled executable.
624 =item -c I<C file name>
626 Create C code only; do not compile to a standalone binary.
628 =item -e I<perl code>
630 Compile a one-liner, much the same as C<perl -e '...'>
634 Do not delete generated C code after compilation.
638 Use the Perl bytecode code generator.
642 Use the 'optimised' C code generator. This is more experimental than
643 everything else put together, and the code created is not guaranteed to
644 compile in finite time and memory, or indeed, at all.
648 Increase verbosity of output; can be repeated for more verbose output.
652 Run the resulting compiled script after compiling it.
656 Log the output of compiling to a file rather than to stdout.
664 close OUT or die "Can't close $file: $!";
665 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
666 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';