home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / ExtUtils / Embed.pm < prev    next >
Encoding:
Text File  |  1999-12-28  |  11.7 KB  |  462 lines

  1. require 5.002;
  2.  
  3. package ExtUtils::Embed;
  4. require Exporter;
  5. require FileHandle;
  6. use Config;
  7. use Getopt::Std;
  8.  
  9.  
  10. use vars qw(@ISA @EXPORT $VERSION
  11.         @Extensions $Verbose $lib_ext
  12.         $opt_o $opt_s 
  13.         );
  14. use strict;
  15.  
  16. $VERSION = sprintf("%d.%02d", q$Revision: 1.2505 $ =~ /(\d+)\.(\d+)/);
  17.  
  18. @ISA = qw(Exporter);
  19. @EXPORT = qw(&xsinit &ldopts 
  20.          &ccopts &ccflags &ccdlflags &perl_inc
  21.          &xsi_header &xsi_protos &xsi_body);
  22.  
  23.  
  24. $Verbose = 0;
  25. $lib_ext = $Config{lib_ext} || '.a';
  26.  
  27. sub is_cmd { $0 eq '-e' }
  28.  
  29. sub my_return {
  30.     my $val = shift;
  31.     if(is_cmd) {
  32.     print $val;
  33.     }
  34.     else {
  35.     return $val;
  36.     }
  37. }
  38.  
  39. sub xsinit { 
  40.     my($file, $std, $mods) = @_;
  41.     my($fh,@mods,%seen);
  42.     $file ||= "perlxsi.c";
  43.  
  44.     if (@_) {
  45.        @mods = @$mods if $mods;
  46.     }
  47.     else {
  48.        getopts('o:s:');
  49.        $file = $opt_o if defined $opt_o;
  50.        $std  = $opt_s  if defined $opt_s;
  51.        @mods = @ARGV;
  52.     }
  53.     $std = 1 unless scalar @mods;
  54.  
  55.     if ($file eq "STDOUT") {
  56.     $fh = \*STDOUT;
  57.     }
  58.     else {
  59.     $fh = new FileHandle "> $file";
  60.     }
  61.  
  62.     push(@mods, static_ext()) if defined $std;
  63.     @mods = grep(!$seen{$_}++, @mods);
  64.  
  65.     print $fh &xsi_header();
  66.     print $fh "EXTERN_C void xs_init _((void));\n\n";     
  67.     print $fh &xsi_protos(@mods);
  68.  
  69.     print $fh "\nEXTERN_C void\nxs_init()\n{\n";
  70.     print $fh &xsi_body(@mods);
  71.     print $fh "}\n";
  72.  
  73. }
  74.  
  75. sub xsi_header {
  76.     return <<EOF;
  77. extern "C" {
  78.  
  79.  
  80. }
  81.  
  82. EOF
  83. }    
  84.  
  85. sub xsi_protos {
  86.     my(@exts) = @_;
  87.     my(@retval,%seen);
  88.  
  89.     foreach $_ (@exts){
  90.         my($pname) = canon('/', $_);
  91.         my($mname, $cname);
  92.         ($mname = $pname) =~ s!/!::!g;
  93.         ($cname = $pname) =~ s!/!__!g;
  94.     my($ccode) = "EXTERN_C void boot_${cname} _((CV* cv));\n";
  95.     next if $seen{$ccode}++;
  96.         push(@retval, $ccode);
  97.     }
  98.     return join '', @retval;
  99. }
  100.  
  101. sub xsi_body {
  102.     my(@exts) = @_;
  103.     my($pname,@retval,%seen);
  104.     my($dl) = canon('/','DynaLoader');
  105.     push(@retval, "\tchar *file = __FILE__;\n");
  106.     push(@retval, "\tdXSUB_SYS;\n") if $] > 5.002;
  107.     push(@retval, "\n");
  108.  
  109.     foreach $_ (@exts){
  110.         my($pname) = canon('/', $_);
  111.         my($mname, $cname, $ccode);
  112.         ($mname = $pname) =~ s!/!::!g;
  113.         ($cname = $pname) =~ s!/!__!g;
  114.         if ($pname eq $dl){
  115.             $ccode = "\t/* DynaLoader is a special case */\n\tnewXS(\"${mname}::boot_${cname}\", boot_${cname}, file);\n";
  116.             push(@retval, $ccode) unless $seen{$ccode}++;
  117.         } else {
  118.             $ccode = "\tnewXS(\"${mname}::bootstrap\", boot_${cname}, file);\n";
  119.             push(@retval, $ccode) unless $seen{$ccode}++;
  120.         }
  121.     }
  122.     return join '', @retval;
  123. }
  124.  
  125. sub static_ext {
  126.     unless (scalar @Extensions) {
  127.     @Extensions = sort split /\s+/, $Config{static_ext};
  128.     unshift @Extensions, qw(DynaLoader);
  129.     }
  130.     @Extensions;
  131. }
  132.  
  133. sub ldopts {
  134.     require ExtUtils::MakeMaker;
  135.     require ExtUtils::Liblist;
  136.     my($std,$mods,$link_args,$path) = @_;
  137.     my(@mods,@link_args,@argv);
  138.     my($dllib,$config_libs,@potential_libs,@path);
  139.     local($") = ' ' unless $" eq ' ';
  140.     my $MM = bless {} => 'MY';
  141.     if (scalar @_) {
  142.        @link_args = @$link_args if $link_args;
  143.        @mods = @$mods if $mods;
  144.     }
  145.     else {
  146.        @argv = @ARGV;
  147.        while($_ = shift @argv) {
  148.        /^-std$/  && do { $std = 1; next; };
  149.        /^--$/    && do { @link_args = @argv; last; };
  150.        /^-I(.*)/ && do { $path = $1 || shift @argv; next; };
  151.        push(@mods, $_); 
  152.        }
  153.     }
  154.     $std = 1 unless scalar @link_args;
  155.     @path = $path ? split(/:/, $path) : @INC;
  156.  
  157.     push(@potential_libs, @link_args)    if scalar @link_args;
  158.     push(@potential_libs, $Config{libs}) if defined $std;
  159.  
  160.     push(@mods, static_ext()) if $std;
  161.  
  162.     my($mod,@ns,$root,$sub,$extra,$archive,@archives);
  163.     print STDERR "Searching (@path) for archives\n" if $Verbose;
  164.     foreach $mod (@mods) {
  165.     @ns = split('::', $mod);
  166.     $sub = $ns[-1];
  167.     $root = $MM->catdir(@ns);
  168.     
  169.     print STDERR "searching for '$sub${lib_ext}'\n" if $Verbose;
  170.     foreach (@path) {
  171.         next unless -e ($archive = $MM->catdir($_,"auto",$root,"$sub$lib_ext"));
  172.         push @archives, $archive;
  173.         if(-e ($extra = $MM->catdir($_,"auto",$root,"extralibs.ld"))) {
  174.         local(*FH); 
  175.         if(open(FH, $extra)) {
  176.             my($libs) = <FH>; chomp $libs;
  177.             push @potential_libs, split /\s+/, $libs;
  178.         }
  179.         else {  
  180.             warn "Couldn't open '$extra'"; 
  181.         }
  182.         }
  183.         last;
  184.     }
  185.     }
  186.  
  187.     my $libperl = (grep(/^-l\w*perl\w*$/, @link_args))[0] || "-lperl";
  188.  
  189.     my($extralibs, $bsloadlibs, $ldloadlibs, $ld_run_path) =
  190.     $MM->ext(join ' ', 
  191.          $MM->catdir("-L$Config{archlibexp}", "CORE"), " $libperl", 
  192.          @potential_libs);
  193.  
  194.     my $ld_or_bs = $bsloadlibs || $ldloadlibs;
  195.     print STDERR "bs: $bsloadlibs ** ld: $ldloadlibs" if $Verbose;
  196.     my $linkage = "$Config{ccdlflags} $Config{ldflags} @archives $ld_or_bs";
  197.     print STDERR "ldopts: '$linkage'\n" if $Verbose;
  198.  
  199.     return $linkage if scalar @_;
  200.     my_return("$linkage\n");
  201. }
  202.  
  203. sub ccflags {
  204.     my_return(" $Config{ccflags} ");
  205. }
  206.  
  207. sub ccdlflags {
  208.     my_return(" $Config{ccdlflags} ");
  209. }
  210.  
  211. sub perl_inc {
  212.     my_return(" -I$Config{archlibexp}/CORE ");
  213. }
  214.  
  215. sub ccopts {
  216.    ccflags . perl_inc;
  217. }
  218.  
  219. sub canon {
  220.     my($as, @ext) = @_;
  221.     foreach(@ext) {
  222.        next if s!::!/!g;
  223.        s:^(lib|ext)/(auto/)?::;
  224.        s:/\w+\.\w+$::;
  225.     }
  226.     grep(s:/:$as:, @ext) if ($as ne '/');
  227.     @ext;
  228. }
  229.  
  230. __END__
  231.  
  232. =head1 NAME
  233.  
  234. ExtUtils::Embed - Utilities for embedding Perl in C/C++ applications
  235.  
  236. =head1 SYNOPSIS
  237.  
  238.  
  239.  perl -MExtUtils::Embed -e xsinit 
  240.  perl -MExtUtils::Embed -e ldopts 
  241.  
  242. =head1 DESCRIPTION
  243.  
  244. ExtUtils::Embed provides utility functions for embedding a Perl interpreter
  245. and extensions in your C/C++ applications.  
  246. Typically, an application B<Makefile> will invoke ExtUtils::Embed
  247. functions while building your application.  
  248.  
  249. =head1 @EXPORT
  250.  
  251. ExtUtils::Embed exports the following functions:
  252.  
  253. xsinit(), ldopts(), ccopts(), perl_inc(), ccflags(), 
  254. ccdlflags(), xsi_header(), xsi_protos(), xsi_body()
  255.  
  256. =head1 FUNCTIONS
  257.  
  258. =over
  259.  
  260. =item xsinit()
  261.  
  262. Generate C/C++ code for the XS initializer function.
  263.  
  264. When invoked as C<`perl -MExtUtils::Embed -e xsinit --`>
  265. the following options are recognized:
  266.  
  267. B<-o> E<lt>output filenameE<gt> (Defaults to B<perlxsi.c>)
  268.  
  269. B<-o STDOUT> will print to STDOUT.
  270.  
  271. B<-std> (Write code for extensions that are linked with the current Perl.)
  272.  
  273. Any additional arguments are expected to be names of modules
  274. to generate code for.
  275.  
  276. When invoked with parameters the following are accepted and optional:
  277.  
  278. C<xsinit($filename,$std,[@modules])>
  279.  
  280. Where,
  281.  
  282. B<$filename> is equivalent to the B<-o> option.
  283.  
  284. B<$std> is boolean, equivalent to the B<-std> option.  
  285.  
  286. B<[@modules]> is an array ref, same as additional arguments mentioned above.
  287.  
  288. =item Examples
  289.  
  290.  
  291.  perl -MExtUtils::Embed -e xsinit -- -o xsinit.c Socket
  292.  
  293.  
  294. This will generate code with an B<xs_init> function that glues the perl B<Socket::bootstrap> function 
  295. to the C B<boot_Socket> function and writes it to a file named "xsinit.c".
  296.  
  297. Note that B<DynaLoader> is a special case where it must call B<boot_DynaLoader> directly.
  298.  
  299.  perl -MExtUtils::Embed -e xsinit
  300.  
  301.  
  302. This will generate code for linking with B<DynaLoader> and 
  303. each static extension found in B<$Config{static_ext}>.
  304. The code is written to the default file name B<perlxsi.c>.
  305.  
  306.  
  307.  perl -MExtUtils::Embed -e xsinit -- -o xsinit.c -std DBI DBD::Oracle
  308.  
  309.  
  310. Here, code is written for all the currently linked extensions along with code
  311. for B<DBI> and B<DBD::Oracle>.
  312.  
  313. If you have a working B<DynaLoader> then there is rarely any need to statically link in any 
  314. other extensions.
  315.  
  316. =item ldopts()
  317.  
  318. Output arguments for linking the Perl library and extensions to your
  319. application.
  320.  
  321. When invoked as C<`perl -MExtUtils::Embed -e ldopts --`>
  322. the following options are recognized:
  323.  
  324. B<-std> 
  325.  
  326. Output arguments for linking the Perl library and any extensions linked
  327. with the current Perl.
  328.  
  329. B<-I> E<lt>path1:path2E<gt>
  330.  
  331. Search path for ModuleName.a archives.  
  332. Default path is B<@INC>.
  333. Library archives are expected to be found as 
  334. B</some/path/auto/ModuleName/ModuleName.a>
  335. For example, when looking for B<Socket.a> relative to a search path, 
  336. we should find B<auto/Socket/Socket.a>  
  337.  
  338. When looking for B<DBD::Oracle> relative to a search path,
  339. we should find B<auto/DBD/Oracle/Oracle.a>
  340.  
  341. Keep in mind, you can always supply B</my/own/path/ModuleName.a>
  342. as an additional linker argument.
  343.  
  344. B<-->  E<lt>list of linker argsE<gt>
  345.  
  346. Additional linker arguments to be considered.
  347.  
  348. Any additional arguments found before the B<--> token 
  349. are expected to be names of modules to generate code for.
  350.  
  351. When invoked with parameters the following are accepted and optional:
  352.  
  353. C<ldopts($std,[@modules],[@link_args],$path)>
  354.  
  355. Where,
  356.  
  357. B<$std> is boolean, equivalent to the B<-std> option.  
  358.  
  359. B<[@modules]> is equivalent to additional arguments found before the B<--> token.
  360.  
  361. B<[@link_args]> is equivalent to arguments found after the B<--> token.
  362.  
  363. B<$path> is equivalent to the B<-I> option.
  364.  
  365. In addition, when ldopts is called with parameters, it will return the argument string
  366. rather than print it to STDOUT.
  367.  
  368. =item Examples
  369.  
  370.  
  371.  perl -MExtUtils::Embed -e ldopts
  372.  
  373.  
  374. This will print arguments for linking with B<libperl.a>, B<DynaLoader> and 
  375. extensions found in B<$Config{static_ext}>.  This includes libraries
  376. found in B<$Config{libs}> and the first ModuleName.a library
  377. for each extension that is found by searching B<@INC> or the path 
  378. specifed by the B<-I> option.  
  379. In addition, when ModuleName.a is found, additional linker arguments
  380. are picked up from the B<extralibs.ld> file in the same directory.
  381.  
  382.  
  383.  perl -MExtUtils::Embed -e ldopts -- -std Socket
  384.  
  385.  
  386. This will do the same as the above example, along with printing additional arguments for linking with the B<Socket> extension.
  387.  
  388.  
  389.  perl -MExtUtils::Embed -e ldopts -- DynaLoader
  390.  
  391.  
  392. This will print arguments for linking with just the B<DynaLoader> extension
  393. and B<libperl.a>.
  394.  
  395.  
  396.  perl -MExtUtils::Embed -e ldopts -- -std Msql -- -L/usr/msql/lib -lmsql
  397.  
  398.  
  399. Any arguments after the second '--' token are additional linker
  400. arguments that will be examined for potential conflict.  If there is no
  401. conflict, the additional arguments will be part of the output.  
  402.  
  403.  
  404. =item perl_inc()
  405.  
  406. For including perl header files this function simply prints:
  407.  
  408.  -I$Config{archlibexp}/CORE  
  409.  
  410. So, rather than having to say:
  411.  
  412.  perl -MConfig -e 'print "-I$Config{archlibexp}/CORE"'
  413.  
  414. Just say:
  415.  
  416.  perl -MExtUtils::Embed -e perl_inc
  417.  
  418. =item ccflags(), ccdlflags()
  419.  
  420. These functions simply print $Config{ccflags} and $Config{ccdlflags}
  421.  
  422. =item ccopts()
  423.  
  424. This function combines perl_inc(), ccflags() and ccdlflags() into one.
  425.  
  426. =item xsi_header()
  427.  
  428. This function simply returns a string defining the same B<EXTERN_C> macro as
  429. B<perlmain.c> along with #including B<perl.h> and B<EXTERN.h>.  
  430.  
  431. =item xsi_protos(@modules)
  432.  
  433. This function returns a string of B<boot_$ModuleName> prototypes for each @modules.
  434.  
  435. =item xsi_body(@modules)
  436.  
  437. This function returns a string of calls to B<newXS()> that glue the module B<bootstrap>
  438. function to B<boot_ModuleName> for each @modules.
  439.  
  440. B<xsinit()> uses the xsi_* functions to generate most of it's code.
  441.  
  442. =back
  443.  
  444. =head1 EXAMPLES
  445.  
  446. For examples on how to use B<ExtUtils::Embed> for building C/C++ applications
  447. with embedded perl, see the eg/ directory and L<perlembed>.
  448.  
  449. =head1 SEE ALSO
  450.  
  451. L<perlembed>
  452.  
  453. =head1 AUTHOR
  454.  
  455. Doug MacEachern E<lt>F<dougm@osf.org>E<gt>
  456.  
  457. Based on ideas from Tim Bunce E<lt>F<Tim.Bunce@ig.co.uk>E<gt> and
  458. B<minimod.pl> by Andreas Koenig E<lt>F<k@anna.in-berlin.de>E<gt> and Tim Bunce.
  459.  
  460. =cut
  461.  
  462.