4 use File::Basename qw(&basename &dirname);
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
12 # to ensure Configure will look for $Config{startperl}.
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.
19 $file = basename($0, '.PL');
20 $file .= '.com' if $^O eq 'VMS';
22 open OUT,">$file" or die "Can't create $file: $!";
24 print "Extracting $file (with variable substitutions)\n";
26 # In this section, perl variables will be expanded during extraction.
27 # You can use $Config{...} to use Configure variables.
29 print OUT <<"!GROK!THIS!";
31 eval 'exec $Config{perlpath} -S \$0 \${1+"\$@"}'
32 if \$running_under_some_shell;
35 # In the following, perl variables are not expanded during extraction.
37 print OUT <<'!NO!SUBS!';
42 use File::Basename qw(&basename &dirname);
47 $Getopt::Long::bundling_override = 1;
48 $Getopt::Long::passthrough = 0;
49 $Getopt::Long::ignore_case = 0;
82 _usage() if (!_checkopts());
83 push(@ARGV, _maketempfile()) if ($options->{'e'});
91 --------------------------------------------------------------------------------
93 --------------------------------------------------------------------------------
103 my ($program_ext, $module_ext) = _getRegexps();
104 my ($obj, $objfile, $so, $type);
107 (($file =~ m"@$program_ext") && ($file !~ m"@$module_ext"))
108 || (defined($options->{'prog'}) || defined($options->{'run'}))
111 $objfile = ($options->{'C'}) ? $options->{'C'} : "$file.c";
114 $obj = ($options->{'o'})? $options->{'o'} :
115 _getExecutable( $file,$program_ext);
120 elsif (($file =~ m"@$module_ext") || ($options->{'mod'}))
122 die "Shared objects are not supported on Win32 yet!!!!\n"
123 if ($Config{'osname'} eq 'MSWin32');
125 $obj = ($options->{'o'})? $options->{'o'} :
126 _getExecutable($file, $module_ext);
133 _error("noextension", $file, $program_ext, $module_ext);
137 if ($type eq 'program')
139 _print("Making C($objfile) for $file!\n", 36 );
141 my $errcode = _createCode($objfile, $file);
142 (_print( "ERROR: In generating code for $file!\n", -1), return())
145 _print("Compiling C($obj) for $file!\n", 36 ) if (!$options->{'gen'});
146 $errcode = _compileCode($file, $objfile, $obj)
147 if (!$options->{'gen'});
151 _print( "ERROR: In compiling code for $objfile !\n", -1);
152 my $ofile = File::Basename::basename($objfile);
153 $ofile =~ s"\.c$"\.o"s;
155 _removeCode("$ofile");
159 _runCode($obj) if ($options->{'run'});
161 _removeCode($objfile) if (!$options->{'sav'} ||
162 ($options->{'e'} && !$options->{'C'}));
164 _removeCode($file) if ($options->{'e'});
166 _removeCode($obj) if (($options->{'e'}
167 && !$options->{'sav'}
169 || ($options->{'run'} && !$options->{'sav'}));
173 _print( "Making C($objfile) for $file!\n", 36 );
174 my $errcode = _createCode($objfile, $file, $obj);
175 (_print( "ERROR: In generating code for $file!\n", -1), return())
178 _print( "Compiling C($obj) for $file!\n", 36 ) if (!$options->{'gen'});
181 _compileCode($file, $objfile, $obj, $so ) if (!$options->{'gen'});
183 (_print( "ERROR: In compiling code for $objfile!\n", -1), return())
190 my ($sourceprog, $ext) = @_;
193 if (defined($options->{'regex'}))
195 eval("(\$obj = \$sourceprog) =~ $options->{'regex'}");
196 return(0) if (_error('badeval', $@));
197 return(0) if (_error('equal', $obj, $sourceprog));
199 elsif (defined ($options->{'ext'}))
201 ($obj = $sourceprog) =~ s"@$ext"$options->{ext}"g;
202 return(0) if (_error('equal', $obj, $sourceprog));
204 elsif (defined ($options->{'run'}))
210 ($obj = $sourceprog) =~ s"@$ext""g;
211 return(0) if (_error('equal', $obj, $sourceprog));
218 my ( $generated_cfile, $file, $final_output ) = @_;
223 if (@_ == 2) # compiling a program
225 _print( "$^X -I@INC -MO=CC,-o$generated_cfile $file\n", 36);
226 $return = _run("$
\18 -I@INC -MO=CC,-o$generated_cfile $file", 9);
229 else # compiling a shared object
232 "$
\18 -I@INC -MO=CC,-m$final_output,-o$generated_cfile $file\n", 36);
234 _run("$
\18 -I@INC -MO=CC,-m$final_output,-o$generated_cfile $file", 9);
241 my ($sourceprog, $generated_cfile, $output_executable, $shared_object) = @_;
244 if (@_ == 3) # just compiling a program
247 _ccharness($sourceprog, "-o", $output_executable, $generated_cfile);
252 my $object_file = $generated_cfile;
253 $object_file =~ s"\.c$"\.o";
255 $return[0] = _ccharness($sourceprog, "-c", $generated_cfile);
256 $return[1] = _ccharness
258 $sourceprog, "-shared","-o",
259 $shared_object, $object_file
261 return(1) if (grep ($_, @return));
268 my ($executable) = @_;
269 _print("$executable $options->{'argv'}\n", 36);
270 _run("$executable $options->{'argv'}", -1 );
276 unlink($file) if (-e $file);
284 my $sourceprog = shift(@args);
285 my ($libdir, $incdir);
287 if (-d "$Config{installarchlib}/CORE")
289 $libdir = "-L$Config{installarchlib}/CORE";
290 $incdir = "-I$Config{installarchlib}/CORE";
298 $libdir .= " -L$options->{L}" if (defined($options->{L}));
299 $incdir .= " -I$options->{L}" if (defined($options->{L}));
303 if (!grep(/^-[cS]$/, @ARGV))
305 $linkargs = sprintf("%s $libdir -lperl %s",@Config{qw(ldflags libs)});
308 my @sharedobjects = _getSharedObjects($sourceprog);
311 "$Config{cc} $Config{ccflags} $incdir @sharedobjects @args $linkargs";
314 _print ("$cccmd\n", 36);
318 sub _getSharedObjects
320 my ($sourceprog) = @_;
321 my ($tmpfile, $incfile);
325 if ($Config{'osname'} eq 'MSWin32')
332 ($tmpprog = $sourceprog) =~ s"(.*)[\/\\](.*)"$2";
333 $tmpfile = "/tmp/$tmpprog.tst";
334 $incfile = "/tmp/$tmpprog.val";
337 my $fd = new FileHandle("> $tmpfile") || die "Couldn't open $tmpfile!\n";
339 new FileHandle("$sourceprog") || die "Couldn't open $sourceprog!\n";
341 my $perl = <$fd2>; # strip off header;
346 my \$fh3 = new FileHandle("> $incfile")
347 || die "Couldn't open $incfile\\n";
350 foreach \$key (keys(\%INC)) { print \$fh3 "\$key:\$INC{\$key}\\n"; }
355 print $fd ( <$fd2> );
358 _print("$
\18 -I@INC $tmpfile\n", 36);
359 _run("$
\18 -I@INC $tmpfile", 9 );
361 $fd = new FileHandle ("$incfile");
370 foreach $line (@lines)
373 my ($modname, $modpath) = split(':', $line);
374 my ($dir, $file) = ($modpath=~ m"(.*)[\\/]($modname)");
376 if ($autolib = _lookforAuto($dir, $file))
378 push(@return, $autolib);
389 # if ($Config{'osname'} eq 'MSWin32')
390 # { $return = "C:\\TEMP\\comp$$.p"; }
392 # { $return = "/tmp/comp$$.p"; }
394 $return = "comp$$.p";
396 my $fd = new FileHandle( "> $return") || die "Couldn't open $return!\n";
397 print $fd $options->{'e'};
406 my ($dir, $file) = @_;
411 ($relshared = $file) =~ s"(.*)\.pm"$1";
413 my ($tmp, $modname) = ($relshared =~ m"(?:(.*)[\\/]){0,1}(.*)"s);
416 ($Config{'osname'} eq 'MSWin32')? "\\$modname.dll" : "/$modname.so";
420 if (-e ($return = "$Config{'installarchlib'}/auto/$relshared") )
424 elsif (-e ($return = "$Config{'installsitearch'}/auto/$relshared"))
428 elsif (-e ($return = "$dir/arch/auto/$relshared"))
438 sub _getRegexps # make the appropriate regexps for making executables,
441 my ($program_ext, $module_ext) = ([],[]);
444 @$program_ext = ($ENV{PERL_SCRIPT_EXT})? split(':', $ENV{PERL_SCRIPT_EXT}) :
445 ('.p$', '.pl$', '.bat$');
448 @$module_ext = ($ENV{PERL_MODULE_EXT})? split(':', $ENV{PERL_MODULE_EXT}) :
452 _mungeRegexp( $program_ext );
453 _mungeRegexp( $module_ext );
455 return($program_ext, $module_ext);
462 grep(s:(^|[^\\])\.:$1\x00\\.:g, @$regexp);
463 grep(s:(^|[^\x00])\\\.:$1\.:g, @$regexp);
464 grep(s:\x00::g, @$regexp);
470 my ($type, @args) = @_;
472 if ($type eq 'equal')
475 if ($args[0] eq $args[1])
477 _print ("ERROR: The object file '$args[0]' does not generate a legitimate executable file! Skipping!\n", -1);
481 elsif ($type eq 'badeval')
485 _print ("ERROR: $args[0]\n", -1);
489 elsif ($type eq 'noextension')
491 my $progext = join(',', @{$args[1]});
492 my $modext = join(',', @{$args[2]});
503 ERROR: '$args[0]' does not have a proper extension! Proper extensions are:
506 SHARED OBJECT: $modext
508 Use the '-prog' flag to force your files to be interpreted as programs.
509 Use the '-mod' flag to force your files to be interpreted as modules.
523 if ($options->{'log'})
525 $_fh = new FileHandle(">> $options->{'log'}") || push(@errors, "ERROR: Couldn't open $options->{'log'}\n");
528 if (($options->{'c'}) && (@ARGV > 1) && ($options->{'sav'} ))
531 "ERROR: The '-sav' and '-C' options are incompatible when you have more than
532 one input file! ('-C' explicitly names resulting C code, '-sav' saves it,
533 and hence, with more than one file, the c code will be overwritten for
534 each file that you compile)\n");
536 if (($options->{'o'}) && (@ARGV > 1))
539 "ERROR: The '-o' option is incompatible when you have more than one input file!
540 (-o explicitly names the resulting executable, hence, with more than
541 one file the names clash)\n");
544 if ($options->{'e'} && $options->{'sav'} && !$options->{'o'} &&
548 "ERROR: You need to specify where you are going to save the resulting
549 executable or C code, when using '-sav' and '-e'. Use '-o' or '-C'.\n");
552 if (($options->{'regex'} || $options->{'run'} || $options->{'o'})
553 && $options->{'gen'})
556 "ERROR: The options '-regex', '-run', and '-o' are incompatible with '-gen'.
557 '-gen' says to stop at C generation, and the other three modify the
558 compilation and/or running process!\n");
561 if ($options->{'run'} && $options->{'mod'})
564 "ERROR: Can't run modules that you are compiling! '-run' and '-mod' are
568 if ($options->{'e'} && @ARGV)
571 "ERROR: The option '-e' needs to be all by itself without any other
574 if ($options->{'e'} && !($options->{'o'} || $options->{'run'}))
576 $options->{'run'} = 1;
579 if (!defined($options->{'verbose'}))
581 $options->{'verbose'} = ($options->{'log'})? 64 : 7;
586 if ($options->{'verbose'} =~ m"[^tagfcd]" &&
587 !( $options->{'verbose'} eq '0' ||
588 ($options->{'verbose'} < 64 && $options->{'verbose'} > 0)))
592 "ERROR: Illegal verbosity level. Needs to have either the letters
593 't','a','g','f','c', or 'd' in it or be between 0 and 63, inclusive.\n");
596 $options->{'verbose'} = ($options->{'verbose'} =~ m"[tagfcd]")?
597 ($options->{'verbose'} =~ m"d") * 32 +
598 ($options->{'verbose'} =~ m"c") * 16 +
599 ($options->{'verbose'} =~ m"f") * 8 +
600 ($options->{'verbose'} =~ m"t") * 4 +
601 ($options->{'verbose'} =~ m"a") * 2 +
602 ($options->{'verbose'} =~ m"g") * 1
603 : $options->{'verbose'};
605 if (!$verbose_error && ( $options->{'log'} &&
607 ($options->{'verbose'} & 8) ||
608 ($options->{'verbose'} & 16) ||
609 ($options->{'verbose'} & 32 )
615 "ERROR: The verbosity level '$options->{'verbose'}' does not output anything
616 to a logfile, and you specified '-log'!\n");
619 if (!$verbose_error && ( !$options->{'log'} &&
621 ($options->{'verbose'} & 8) ||
622 ($options->{'verbose'} & 16) ||
623 ($options->{'verbose'} & 32) ||
624 ($options->{'verbose'} & 64)
630 "ERROR: The verbosity level '$options->{'verbose'}' requires that you also
631 specify a logfile via '-log'\n");
635 (_print( "\n". join("\n", @errors), -1), return(0)) if (@errors);
641 my ($text, $flag ) = @_;
643 my $logflag = int($flag/8) * 8;
644 my $regflag = $flag % 8;
646 if ($flag == -1 || ($flag & $options->{'verbose'}))
648 my $dolog = ((($logflag & $options->{'verbose'}) || $flag == -1)
649 && $options->{'log'});
651 my $doreg = (($regflag & $options->{'verbose'}) || $flag == -1);
653 if ($doreg) { print( STDERR $text ); }
654 if ($dolog) { print $_fh $text; }
660 my ($command, $flag) = @_;
662 my $logflag = ($flag != -1)? int($flag/8) * 8 : 0;
663 my $regflag = $flag % 8;
665 if ($flag == -1 || ($flag & $options->{'verbose'}))
667 my $dolog = ($logflag & $options->{'verbose'} && $options->{'log'});
668 my $doreg = (($regflag & $options->{'verbose'}) || $flag == -1);
670 if ($doreg && !$dolog)
671 { system("$command"); }
673 elsif ($doreg && $dolog)
674 { my $text = `$command 2>&1`; print $_fh $text; print STDERR $text;}
676 { my $text = `$command 2>&1`; print $_fh $text; }
691 Usage: $0 <file_list>
694 -L < extra library dirs for installation (form of 'dir1:dir2') >
695 -I < extra include dirs for installation (form of 'dir1:dir2') >
696 -C < explicit name of resulting C code >
697 -o < explicit name of resulting executable >
698 -e < to compile 'one liners'. Need executable name (-o) or '-run'>
699 -regex < rename regex, -regex 's/\.p/\.exe/' compiles a.p to a.exe >
700 -verbose < verbose level (1-63, or following letters 'gatfcd' >
701 -argv < arguments for the executables to be run via '-run' or '-e' >
704 -gen ( to just generate the c code. Implies '-sav' )
705 -sav ( to save intermediate c code, (and executables with '-run'))
706 -run ( to run the compiled program on the fly, as were interpreted.)
707 -prog ( to indicate that the files on command line are programs )
708 -mod ( to indicate that the files on command line are modules )
722 perlcc - frontend for perl compiler
726 %prompt perlcc a.p # compiles into executable 'a'
728 %prompt perlcc A.pm # compile into 'A.so'
730 %prompt perlcc a.p -o execute # compiles 'a.p' into 'execute'.
732 %prompt perlcc a.p -o execute -run # compiles 'a.p' into execute, runs on
735 %prompt perlcc a.p -o execute -run -argv 'arg1 arg2 arg3'
736 # compiles into execute, runs with
737 # arg1 arg2 arg3 as @ARGV
739 %prompt perlcc a.p b.p c.p -regex 's/\.p/\.exe'
740 # compiles into 'a.exe','b.exe','c.exe'.
742 %prompt perlcc a.p -log compilelog # compiles into 'a', saves compilation
743 # info into compilelog, as well
744 # as mirroring to screen
746 %prompt perlcc a.p -log compilelog -verbose cdf
747 # compiles into 'a', saves compilation
748 # info into compilelog, being silent
751 %prompt perlcc a.p -C a.c -gen # generates C code (into a.c) and
752 # stops without compile.
754 %prompt perlcc a.p -L ../lib a.c
755 # Compiles with the perl libraries
756 # inside ../lib included.
760 'perlcc' is the frontend into the perl compiler. Typing 'perlcc a.p'
761 compiles the code inside a.p into a standalone executable, and
762 perlcc A.pm will compile into a shared object, A.so, suitable for inclusion
763 into a perl program via "use A".
765 There are quite a few flags to perlcc which help with such issues as compiling
766 programs in bulk, testing compiled programs for compatibility with the
767 interpreter, and controlling.
773 =item -L < library_directories >
775 Adds directories in B<library_directories> to the compilation command.
777 =item -I < include_directories >
779 Adds directories inside B<include_directories> to the compilation command.
781 =item -C < c_code_name >
783 Explicitly gives the name B<c_code_name> to the generated c code which is to
784 be compiled. Can only be used if compiling one file on the command line.
786 =item -o < executable_name >
788 Explicitly gives the name B<executable_name> to the executable which is to be
789 compiled. Can only be used if compiling one file on the command line.
791 =item -e < perl_line_to_execute>
793 Compiles 'one liners', in the same way that B<perl -e> runs text strings at
794 the command line. Default is to have the 'one liner' be compiled, and run all
795 in one go (see B<-run>); giving the B<-o> flag saves the resultant executable,
796 rather than throwing it away. Use '-argv' to pass arguments to the executable
799 =item -regex <rename_regex>
801 Gives a rule B<rename_regex> - which is a legal perl regular expression - to
802 create executable file names.
804 =item -verbose <verbose_level>
806 Show exactly what steps perlcc is taking to compile your code. You can change
807 the verbosity level B<verbose_level> much in the same way that the '-D' switch
808 changes perl's debugging level, by giving either a number which is the sum of
809 bits you want or a list of letters representing what you wish to see. Here are
810 the verbosity levels so far :
812 Bit 1(g): Code Generation Errors to STDERR
813 Bit 2(a): Compilation Errors to STDERR
814 Bit 4(t): Descriptive text to STDERR
815 Bit 8(f): Code Generation Errors to file (B<-log> flag needed)
816 Bit 16(c): Compilation Errors to file (B<-log> flag needed)
817 Bit 32(d): Descriptive text to file (B<-log> flag needed)
819 If the B<-log> tag is given, the default verbose level is 63 (ie: mirroring
820 all of perlcc's output to both the screen and to a log file). If no B<-log>
821 tag is given, then the default verbose level is 7 (ie: outputting all of
822 perlcc's output to STDERR).
824 NOTE: Because of buffering concerns, you CANNOT shadow the output of '-run' to
825 both a file, and to the screen! Suggestions are welcome on how to overcome this
826 difficulty, but for now it simply does not work properly, and hence will only go
831 Opens, for append, a logfile to save some or all of the text for a given
832 compile command. No rewrite version is available, so this needs to be done
835 =item -argv <arguments>
837 In combination with '-run' or '-e', tells perlcc to run the resulting
838 executable with the string B<arguments> as @ARGV.
842 Tells perl to save the intermediate C code. Usually, this C code is the name
843 of the perl code, plus '.c'; 'perlcode.p' gets generated in 'perlcode.p.c',
844 for example. If used with the '-e' operator, you need to tell perlcc where to
845 save resulting executables.
849 Tells perlcc to only create the intermediate C code, and not compile the
850 results. Does an implicit B<-sav>, saving the C code rather than deleting it.
854 Immediately run the perl code that has been generated. NOTE: IF YOU GIVE THE
855 B<-run> FLAG TO B<perlcc>, THEN THE REST OF @ARGV WILL BE INTERPRETED AS
856 ARGUMENTS TO THE PROGRAM THAT YOU ARE COMPILING.
860 Indicate that the programs at the command line are programs, and should be
861 compiled as such. B<perlcc> will automatically determine files to be
862 programs if they have B<.p>, B<.pl>, B<.bat> extensions.
866 Indicate that the programs at the command line are modules, and should be
867 compiled as such. B<perlcc> will automatically determine files to be
868 modules if they have the extension B<.pm>.
874 Most of the work of B<perlcc> is done at the command line. However, you can
875 change the heuristic which determines what is a module and what is a program.
876 As indicated above, B<perlcc> assumes that the extensions:
880 indicate a perl program, and:
884 indicate a library, for the purposes of creating executables. And furthermore,
885 by default, these extensions will be replaced (and dropped ) in the process of
886 creating an executable.
888 To change the extensions which are programs, and which are modules, set the
889 environmental variables:
894 These two environmental variables take colon-separated, legal perl regular
895 expressions, and are used by perlcc to decide which objects are which.
898 setenv PERL_SCRIPT_EXT '.prl$:.perl$'
899 prompt% perlcc sample.perl
901 will compile the script 'sample.perl' into the executable 'sample', and
903 setenv PERL_MODULE_EXT '.perlmod$:.perlmodule$'
905 prompt% perlcc sample.perlmod
907 will compile the module 'sample.perlmod' into the shared object
910 NOTE: the '.' in the regular expressions for PERL_SCRIPT_EXT and PERL_MODULE_EXT
911 is a literal '.', and not a wild-card. To get a true wild-card, you need to
912 backslash the '.'; as in:
914 setenv PERL_SCRIPT_EXT '\.\.\.\.\.'
916 which would have the effect of compiling ANYTHING (except what is in
917 PERL_MODULE_EXT) into an executable with 5 less characters in its name.
921 'perlcc' uses a temporary file when you use the B<-e> option to evaluate
922 text and compile it. This temporary file is 'perlc$$.p'. The temporary C code is
923 perlc$$.p.c, and the temporary executable is perlc$$.
925 When you use '-run' and don't save your executable, the temporary executable is
930 perlcc currently cannot compile shared objects on Win32. This should be fixed
937 close OUT or die "Can't close $file: $!";
938 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
939 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';