More bytecompiler.
[p5sagit/p5-mst-13.2.git] / utils / perlbc.PL
1 #!/usr/local/bin/perl
2
3 use Config;
4 use File::Basename qw(&basename &dirname);
5 use Cwd;
6
7 # List explicitly here the variables you want Configure to
8 # generate.  Metaconfig only looks for shell variables, so you
9 # have to mention them as if they were shell variables, not
10 # %Config entries.  Thus you write
11 #  $startperl
12 # to ensure Configure will look for $Config{startperl}.
13 # Wanted:  $archlibexp
14
15 # This forces PL files to create target in same directory as PL file.
16 # This is so that make depend always knows where to find PL derivatives.
17 $origdir = cwd;
18 chdir dirname($0);
19 $file = basename($0, '.PL');
20 $file .= '.com' if $^O eq 'VMS';
21
22 open OUT,">$file" or die "Can't create $file: $!";
23
24 print "Extracting $file (with variable substitutions)\n";
25
26 # In this section, perl variables will be expanded during extraction.
27 # You can use $Config{...} to use Configure variables.
28
29 print OUT <<"!GROK!THIS!";
30 $Config{startperl}
31     eval 'exec $Config{perlpath} -S \$0 \${1+"\$@"}'
32     if \$running_under_some_shell;
33 !GROK!THIS!
34
35 # In the following, perl variables are not expanded during extraction.
36
37 print OUT <<'!NO!SUBS!';
38
39 use strict;
40 use warnings;
41
42 our($running_under_some_shell);
43
44 use Config;
45
46 require ByteLoader;
47
48 foreach my $infile (@ARGV)
49 {
50     if ($infile =~ /\.p[ml]$/)
51     {
52         my $outfile = $infile . "c";
53
54         open(OUT,"> $outfile") || die "Can't open $outfile: $!";
55
56         if ($infile =~ /\.pl$/)
57         {
58             print OUT "$Config{startperl}\n";
59             print OUT "    eval 'exec $Config{perlpath} -S \$0 \${1+\"\$@\"}'\n";
60             print OUT "    if \$running_under_some_shell;\n\n";
61         }
62
63         print OUT "use ByteLoader $ByteLoader::VERSION;\n";
64
65         close(OUT);
66
67         print "$^X -MO=Bytecode $infile >> $outfile\n";
68
69         system("$^X -MO=Bytecode $infile >> $outfile");
70     }
71     else
72     {
73         warn "Don't know how to byte compile $infile";
74     }
75 }
76 !NO!SUBS!
77
78 close OUT or die "Can't close $file: $!";
79 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
80 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';
81 chdir $origdir;