1N/A# $Id: Embed.pm,v 1.1.1.1 2002/01/16 19:27:19 schwern Exp $
1N/Arequire 5.002;
1N/A
1N/Apackage ExtUtils::Embed;
1N/Arequire Exporter;
1N/Arequire FileHandle;
1N/Ause Config;
1N/Ause Getopt::Std;
1N/Ause File::Spec;
1N/A
1N/A#Only when we need them
1N/A#require ExtUtils::MakeMaker;
1N/A#require ExtUtils::Liblist;
1N/A
1N/Ause vars qw(@ISA @EXPORT $VERSION
1N/A @Extensions $Verbose $lib_ext
1N/A $opt_o $opt_s
1N/A );
1N/Ause strict;
1N/A
1N/A$VERSION = 1.2506_01;
1N/A
1N/A@ISA = qw(Exporter);
1N/A@EXPORT = qw(&xsinit &ldopts
1N/A &ccopts &ccflags &ccdlflags &perl_inc
1N/A &xsi_header &xsi_protos &xsi_body);
1N/A
1N/A#let's have Miniperl borrow from us instead
1N/A#require ExtUtils::Miniperl;
1N/A#*canon = \&ExtUtils::Miniperl::canon;
1N/A
1N/A$Verbose = 0;
1N/A$lib_ext = $Config{lib_ext} || '.a';
1N/A
1N/Asub is_cmd { $0 eq '-e' }
1N/A
1N/Asub my_return {
1N/A my $val = shift;
1N/A if(is_cmd) {
1N/A print $val;
1N/A }
1N/A else {
1N/A return $val;
1N/A }
1N/A}
1N/A
1N/Asub xsinit {
1N/A my($file, $std, $mods) = @_;
1N/A my($fh,@mods,%seen);
1N/A $file ||= "perlxsi.c";
1N/A my $xsinit_proto = "pTHX";
1N/A
1N/A if (@_) {
1N/A @mods = @$mods if $mods;
1N/A }
1N/A else {
1N/A getopts('o:s:');
1N/A $file = $opt_o if defined $opt_o;
1N/A $std = $opt_s if defined $opt_s;
1N/A @mods = @ARGV;
1N/A }
1N/A $std = 1 unless scalar @mods;
1N/A
1N/A if ($file eq "STDOUT") {
1N/A $fh = \*STDOUT;
1N/A }
1N/A else {
1N/A $fh = new FileHandle "> $file";
1N/A }
1N/A
1N/A push(@mods, static_ext()) if defined $std;
1N/A @mods = grep(!$seen{$_}++, @mods);
1N/A
1N/A print $fh &xsi_header();
1N/A print $fh "EXTERN_C void xs_init ($xsinit_proto);\n\n";
1N/A print $fh &xsi_protos(@mods);
1N/A
1N/A print $fh "\nEXTERN_C void\nxs_init($xsinit_proto)\n{\n";
1N/A print $fh &xsi_body(@mods);
1N/A print $fh "}\n";
1N/A
1N/A}
1N/A
1N/Asub xsi_header {
1N/A return <<EOF;
1N/A#include <EXTERN.h>
1N/A#include <perl.h>
1N/A
1N/AEOF
1N/A}
1N/A
1N/Asub xsi_protos {
1N/A my(@exts) = @_;
1N/A my(@retval,%seen);
1N/A my $boot_proto = "pTHX_ CV* cv";
1N/A foreach $_ (@exts){
1N/A my($pname) = canon('/', $_);
1N/A my($mname, $cname);
1N/A ($mname = $pname) =~ s!/!::!g;
1N/A ($cname = $pname) =~ s!/!__!g;
1N/A my($ccode) = "EXTERN_C void boot_${cname} ($boot_proto);\n";
1N/A next if $seen{$ccode}++;
1N/A push(@retval, $ccode);
1N/A }
1N/A return join '', @retval;
1N/A}
1N/A
1N/Asub xsi_body {
1N/A my(@exts) = @_;
1N/A my($pname,@retval,%seen);
1N/A my($dl) = canon('/','DynaLoader');
1N/A push(@retval, "\tchar *file = __FILE__;\n");
1N/A push(@retval, "\tdXSUB_SYS;\n") if $] > 5.002;
1N/A push(@retval, "\n");
1N/A
1N/A foreach $_ (@exts){
1N/A my($pname) = canon('/', $_);
1N/A my($mname, $cname, $ccode);
1N/A ($mname = $pname) =~ s!/!::!g;
1N/A ($cname = $pname) =~ s!/!__!g;
1N/A if ($pname eq $dl){
1N/A # Must NOT install 'DynaLoader::boot_DynaLoader' as 'bootstrap'!
1N/A # boot_DynaLoader is called directly in DynaLoader.pm
1N/A $ccode = "\t/* DynaLoader is a special case */\n\tnewXS(\"${mname}::boot_${cname}\", boot_${cname}, file);\n";
1N/A push(@retval, $ccode) unless $seen{$ccode}++;
1N/A } else {
1N/A $ccode = "\tnewXS(\"${mname}::bootstrap\", boot_${cname}, file);\n";
1N/A push(@retval, $ccode) unless $seen{$ccode}++;
1N/A }
1N/A }
1N/A return join '', @retval;
1N/A}
1N/A
1N/Asub static_ext {
1N/A unless (scalar @Extensions) {
1N/A @Extensions = sort split /\s+/, $Config{static_ext};
1N/A unshift @Extensions, qw(DynaLoader);
1N/A }
1N/A @Extensions;
1N/A}
1N/A
1N/Asub _escape {
1N/A my $arg = shift;
1N/A $$arg =~ s/([\(\)])/\\$1/g;
1N/A}
1N/A
1N/Asub _ldflags {
1N/A my $ldflags = $Config{ldflags};
1N/A _escape(\$ldflags);
1N/A return $ldflags;
1N/A}
1N/A
1N/Asub _ccflags {
1N/A my $ccflags = $Config{ccflags};
1N/A _escape(\$ccflags);
1N/A return $ccflags;
1N/A}
1N/A
1N/Asub _ccdlflags {
1N/A my $ccdlflags = $Config{ccdlflags};
1N/A _escape(\$ccdlflags);
1N/A return $ccdlflags;
1N/A}
1N/A
1N/Asub ldopts {
1N/A require ExtUtils::MakeMaker;
1N/A require ExtUtils::Liblist;
1N/A my($std,$mods,$link_args,$path) = @_;
1N/A my(@mods,@link_args,@argv);
1N/A my($dllib,$config_libs,@potential_libs,@path);
1N/A local($") = ' ' unless $" eq ' ';
1N/A if (scalar @_) {
1N/A @link_args = @$link_args if $link_args;
1N/A @mods = @$mods if $mods;
1N/A }
1N/A else {
1N/A @argv = @ARGV;
1N/A #hmm
1N/A while($_ = shift @argv) {
1N/A /^-std$/ && do { $std = 1; next; };
1N/A /^--$/ && do { @link_args = @argv; last; };
1N/A /^-I(.*)/ && do { $path = $1 || shift @argv; next; };
1N/A push(@mods, $_);
1N/A }
1N/A }
1N/A $std = 1 unless scalar @link_args;
1N/A my $sep = $Config{path_sep} || ':';
1N/A @path = $path ? split(/\Q$sep/, $path) : @INC;
1N/A
1N/A push(@potential_libs, @link_args) if scalar @link_args;
1N/A # makemaker includes std libs on windows by default
1N/A if ($^O ne 'MSWin32' and defined($std)) {
1N/A push(@potential_libs, $Config{perllibs});
1N/A }
1N/A
1N/A push(@mods, static_ext()) if $std;
1N/A
1N/A my($mod,@ns,$root,$sub,$extra,$archive,@archives);
1N/A print STDERR "Searching (@path) for archives\n" if $Verbose;
1N/A foreach $mod (@mods) {
1N/A @ns = split(/::|\/|\\/, $mod);
1N/A $sub = $ns[-1];
1N/A $root = File::Spec->catdir(@ns);
1N/A
1N/A print STDERR "searching for '$sub${lib_ext}'\n" if $Verbose;
1N/A foreach (@path) {
1N/A next unless -e ($archive = File::Spec->catdir($_,"auto",$root,"$sub$lib_ext"));
1N/A push @archives, $archive;
1N/A if(-e ($extra = File::Spec->catdir($_,"auto",$root,"extralibs.ld"))) {
1N/A local(*FH);
1N/A if(open(FH, $extra)) {
1N/A my($libs) = <FH>; chomp $libs;
1N/A push @potential_libs, split /\s+/, $libs;
1N/A }
1N/A else {
1N/A warn "Couldn't open '$extra'";
1N/A }
1N/A }
1N/A last;
1N/A }
1N/A }
1N/A #print STDERR "\@potential_libs = @potential_libs\n";
1N/A
1N/A my $libperl;
1N/A if ($^O eq 'MSWin32') {
1N/A $libperl = $Config{libperl};
1N/A }
1N/A else {
1N/A $libperl = (grep(/^-l\w*perl\w*$/, @link_args))[0] || "-lperl";
1N/A }
1N/A
1N/A my $lpath = File::Spec->catdir($Config{archlibexp}, 'CORE');
1N/A $lpath = qq["$lpath"] if $^O eq 'MSWin32';
1N/A my($extralibs, $bsloadlibs, $ldloadlibs, $ld_run_path) =
1N/A MM->ext(join ' ', "-L$lpath", $libperl, @potential_libs);
1N/A
1N/A my $ld_or_bs = $bsloadlibs || $ldloadlibs;
1N/A print STDERR "bs: $bsloadlibs ** ld: $ldloadlibs" if $Verbose;
1N/A my $ccdlflags = _ccdlflags();
1N/A my $ldflags = _ldflags();
1N/A my $linkage = "$ccdlflags $ldflags @archives $ld_or_bs";
1N/A print STDERR "ldopts: '$linkage'\n" if $Verbose;
1N/A
1N/A return $linkage if scalar @_;
1N/A my_return("$linkage\n");
1N/A}
1N/A
1N/Asub ccflags {
1N/A my $ccflags = _ccflags();
1N/A my_return(" $ccflags ");
1N/A}
1N/A
1N/Asub ccdlflags {
1N/A my $ccdlflags = _ccdlflags();
1N/A my_return(" $ccdlflags ");
1N/A}
1N/A
1N/Asub perl_inc {
1N/A my $dir = File::Spec->catdir($Config{archlibexp}, 'CORE');
1N/A $dir = qq["$dir"] if $^O eq 'MSWin32';
1N/A my_return(" -I$dir ");
1N/A}
1N/A
1N/Asub ccopts {
1N/A ccflags . perl_inc;
1N/A}
1N/A
1N/Asub canon {
1N/A my($as, @ext) = @_;
1N/A foreach(@ext) {
1N/A # might be X::Y or lib/auto/X/Y/Y.a
1N/A next if s!::!/!g;
1N/A s:^(lib|ext)/(auto/)?::;
1N/A s:/\w+\.\w+$::;
1N/A }
1N/A grep(s:/:$as:, @ext) if ($as ne '/');
1N/A @ext;
1N/A}
1N/A
1N/A__END__
1N/A
1N/A=head1 NAME
1N/A
1N/AExtUtils::Embed - Utilities for embedding Perl in C/C++ applications
1N/A
1N/A=head1 SYNOPSIS
1N/A
1N/A
1N/A perl -MExtUtils::Embed -e xsinit
1N/A perl -MExtUtils::Embed -e ccopts
1N/A perl -MExtUtils::Embed -e ldopts
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/AExtUtils::Embed provides utility functions for embedding a Perl interpreter
1N/Aand extensions in your C/C++ applications.
1N/ATypically, an application B<Makefile> will invoke ExtUtils::Embed
1N/Afunctions while building your application.
1N/A
1N/A=head1 @EXPORT
1N/A
1N/AExtUtils::Embed exports the following functions:
1N/A
1N/Axsinit(), ldopts(), ccopts(), perl_inc(), ccflags(),
1N/Accdlflags(), xsi_header(), xsi_protos(), xsi_body()
1N/A
1N/A=head1 FUNCTIONS
1N/A
1N/A=over 4
1N/A
1N/A=item xsinit()
1N/A
1N/AGenerate C/C++ code for the XS initializer function.
1N/A
1N/AWhen invoked as C<`perl -MExtUtils::Embed -e xsinit --`>
1N/Athe following options are recognized:
1N/A
1N/AB<-o> E<lt>output filenameE<gt> (Defaults to B<perlxsi.c>)
1N/A
1N/AB<-o STDOUT> will print to STDOUT.
1N/A
1N/AB<-std> (Write code for extensions that are linked with the current Perl.)
1N/A
1N/AAny additional arguments are expected to be names of modules
1N/Ato generate code for.
1N/A
1N/AWhen invoked with parameters the following are accepted and optional:
1N/A
1N/AC<xsinit($filename,$std,[@modules])>
1N/A
1N/AWhere,
1N/A
1N/AB<$filename> is equivalent to the B<-o> option.
1N/A
1N/AB<$std> is boolean, equivalent to the B<-std> option.
1N/A
1N/AB<[@modules]> is an array ref, same as additional arguments mentioned above.
1N/A
1N/A=item Examples
1N/A
1N/A
1N/A perl -MExtUtils::Embed -e xsinit -- -o xsinit.c Socket
1N/A
1N/A
1N/AThis will generate code with an B<xs_init> function that glues the perl B<Socket::bootstrap> function
1N/Ato the C B<boot_Socket> function and writes it to a file named F<xsinit.c>.
1N/A
1N/ANote that B<DynaLoader> is a special case where it must call B<boot_DynaLoader> directly.
1N/A
1N/A perl -MExtUtils::Embed -e xsinit
1N/A
1N/A
1N/AThis will generate code for linking with B<DynaLoader> and
1N/Aeach static extension found in B<$Config{static_ext}>.
1N/AThe code is written to the default file name B<perlxsi.c>.
1N/A
1N/A
1N/A perl -MExtUtils::Embed -e xsinit -- -o xsinit.c -std DBI DBD::Oracle
1N/A
1N/A
1N/AHere, code is written for all the currently linked extensions along with code
1N/Afor B<DBI> and B<DBD::Oracle>.
1N/A
1N/AIf you have a working B<DynaLoader> then there is rarely any need to statically link in any
1N/Aother extensions.
1N/A
1N/A=item ldopts()
1N/A
1N/AOutput arguments for linking the Perl library and extensions to your
1N/Aapplication.
1N/A
1N/AWhen invoked as C<`perl -MExtUtils::Embed -e ldopts --`>
1N/Athe following options are recognized:
1N/A
1N/AB<-std>
1N/A
1N/AOutput arguments for linking the Perl library and any extensions linked
1N/Awith the current Perl.
1N/A
1N/AB<-I> E<lt>path1:path2E<gt>
1N/A
1N/ASearch path for ModuleName.a archives.
1N/ADefault path is B<@INC>.
1N/ALibrary archives are expected to be found as
1N/AB</some/path/auto/ModuleName/ModuleName.a>
1N/AFor example, when looking for B<Socket.a> relative to a search path,
1N/Awe should find B<auto/Socket/Socket.a>
1N/A
1N/AWhen looking for B<DBD::Oracle> relative to a search path,
1N/Awe should find B<auto/DBD/Oracle/Oracle.a>
1N/A
1N/AKeep in mind that you can always supply B</my/own/path/ModuleName.a>
1N/Aas an additional linker argument.
1N/A
1N/AB<--> E<lt>list of linker argsE<gt>
1N/A
1N/AAdditional linker arguments to be considered.
1N/A
1N/AAny additional arguments found before the B<--> token
1N/Aare expected to be names of modules to generate code for.
1N/A
1N/AWhen invoked with parameters the following are accepted and optional:
1N/A
1N/AC<ldopts($std,[@modules],[@link_args],$path)>
1N/A
1N/AWhere:
1N/A
1N/AB<$std> is boolean, equivalent to the B<-std> option.
1N/A
1N/AB<[@modules]> is equivalent to additional arguments found before the B<--> token.
1N/A
1N/AB<[@link_args]> is equivalent to arguments found after the B<--> token.
1N/A
1N/AB<$path> is equivalent to the B<-I> option.
1N/A
1N/AIn addition, when ldopts is called with parameters, it will return the argument string
1N/Arather than print it to STDOUT.
1N/A
1N/A=item Examples
1N/A
1N/A
1N/A perl -MExtUtils::Embed -e ldopts
1N/A
1N/A
1N/AThis will print arguments for linking with B<libperl.a>, B<DynaLoader> and
1N/Aextensions found in B<$Config{static_ext}>. This includes libraries
1N/Afound in B<$Config{libs}> and the first ModuleName.a library
1N/Afor each extension that is found by searching B<@INC> or the path
1N/Aspecified by the B<-I> option.
1N/AIn addition, when ModuleName.a is found, additional linker arguments
1N/Aare picked up from the B<extralibs.ld> file in the same directory.
1N/A
1N/A
1N/A perl -MExtUtils::Embed -e ldopts -- -std Socket
1N/A
1N/A
1N/AThis will do the same as the above example, along with printing additional arguments for linking with the B<Socket> extension.
1N/A
1N/A
1N/A perl -MExtUtils::Embed -e ldopts -- DynaLoader
1N/A
1N/A
1N/AThis will print arguments for linking with just the B<DynaLoader> extension
1N/Aand B<libperl.a>.
1N/A
1N/A
1N/A perl -MExtUtils::Embed -e ldopts -- -std Msql -- -L/usr/msql/lib -lmsql
1N/A
1N/A
1N/AAny arguments after the second '--' token are additional linker
1N/Aarguments that will be examined for potential conflict. If there is no
1N/Aconflict, the additional arguments will be part of the output.
1N/A
1N/A
1N/A=item perl_inc()
1N/A
1N/AFor including perl header files this function simply prints:
1N/A
1N/A -I$Config{archlibexp}/CORE
1N/A
1N/ASo, rather than having to say:
1N/A
1N/A perl -MConfig -e 'print "-I$Config{archlibexp}/CORE"'
1N/A
1N/AJust say:
1N/A
1N/A perl -MExtUtils::Embed -e perl_inc
1N/A
1N/A=item ccflags(), ccdlflags()
1N/A
1N/AThese functions simply print $Config{ccflags} and $Config{ccdlflags}
1N/A
1N/A=item ccopts()
1N/A
1N/AThis function combines perl_inc(), ccflags() and ccdlflags() into one.
1N/A
1N/A=item xsi_header()
1N/A
1N/AThis function simply returns a string defining the same B<EXTERN_C> macro as
1N/AB<perlmain.c> along with #including B<perl.h> and B<EXTERN.h>.
1N/A
1N/A=item xsi_protos(@modules)
1N/A
1N/AThis function returns a string of B<boot_$ModuleName> prototypes for each @modules.
1N/A
1N/A=item xsi_body(@modules)
1N/A
1N/AThis function returns a string of calls to B<newXS()> that glue the module B<bootstrap>
1N/Afunction to B<boot_ModuleName> for each @modules.
1N/A
1N/AB<xsinit()> uses the xsi_* functions to generate most of its code.
1N/A
1N/A=back
1N/A
1N/A=head1 EXAMPLES
1N/A
1N/AFor examples on how to use B<ExtUtils::Embed> for building C/C++ applications
1N/Awith embedded perl, see L<perlembed>.
1N/A
1N/A=head1 SEE ALSO
1N/A
1N/AL<perlembed>
1N/A
1N/A=head1 AUTHOR
1N/A
1N/ADoug MacEachern E<lt>F<dougm@osf.org>E<gt>
1N/A
1N/ABased on ideas from Tim Bunce E<lt>F<Tim.Bunce@ig.co.uk>E<gt> and
1N/AB<minimod.pl> by Andreas Koenig E<lt>F<k@anna.in-berlin.de>E<gt> and Tim Bunce.
1N/A
1N/A=cut
1N/A