Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

642 lines
18 KiB

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