Source code of Windows XP (NT5)
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.

502 lines
12 KiB

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