Leaked source code of windows server 2003
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.

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