XSLoader.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. # Generated from XSLoader.pm.PL (resolved %Config::Config value)
  2. package XSLoader;
  3. $VERSION = "0.20";
  4. #use strict;
  5. package DynaLoader;
  6. # No prizes for guessing why we don't say 'bootstrap DynaLoader;' here.
  7. # NOTE: All dl_*.xs (including dl_none.xs) define a dl_error() XSUB
  8. boot_DynaLoader('DynaLoader') if defined(&boot_DynaLoader) &&
  9. !defined(&dl_error);
  10. package XSLoader;
  11. sub load {
  12. package DynaLoader;
  13. my ($caller, $modlibname) = caller();
  14. my $module = $caller;
  15. # OE: Allow env to form dynamic loader to look in a different place
  16. # This is so it finds the host .so files, not the targets
  17. if (defined $ENV{PERLHOSTLIB})
  18. {
  19. my $hostlib = $ENV{PERLHOSTLIB};
  20. print STDERR "*** Module name IN: $modlibname\n";
  21. ($p1, $p2, $p3, $p4, $p5) = $modlibname =~ m/(^(.*lib\w*\/)?)((perl\/[0-9\.]*\/)?)(.*)$/;
  22. print STDERR "*** p1: $p1 p3: $p3 p5: $p5\n";
  23. if ( $p1 ne "" ) {
  24. $modlibname = $hostlib.$p5;
  25. }
  26. print STDERR "*** Module name OUT: $modlibname\n";
  27. }
  28. if (@_) {
  29. $module = $_[0];
  30. } else {
  31. $_[0] = $module;
  32. }
  33. # work with static linking too
  34. my $boots = "$module\::bootstrap";
  35. goto &$boots if defined &$boots;
  36. goto \&XSLoader::bootstrap_inherit unless $module and defined &dl_load_file;
  37. my @modparts = split(/::/,$module);
  38. my $modfname = $modparts[-1];
  39. my $modpname = join('/',@modparts);
  40. my $c = () = split(/::/,$caller,-1);
  41. $modlibname =~ s,[\\/][^\\/]+$,, while $c--; # Q&D basename
  42. # Does this look like a relative path?
  43. if ($modlibname !~ m|^[\\/]|) {
  44. # Someone may have a #line directive that changes the file name, or
  45. # may be calling XSLoader::load from inside a string eval. We cer-
  46. # tainly do not want to go loading some code that is not in @INC,
  47. # as it could be untrusted.
  48. #
  49. # We could just fall back to DynaLoader here, but then the rest of
  50. # this function would go untested in the perl core, since all @INC
  51. # paths are relative during testing. That would be a time bomb
  52. # waiting to happen, since bugs could be introduced into the code.
  53. #
  54. # So look through @INC to see if $modlibname is in it. A rela-
  55. # tive $modlibname is not a common occurrence, so this block is
  56. # not hot code.
  57. FOUND: {
  58. for (@INC) {
  59. if ($_ eq $modlibname) {
  60. last FOUND;
  61. }
  62. }
  63. # Not found. Fall back to DynaLoader.
  64. goto \&XSLoader::bootstrap_inherit;
  65. }
  66. }
  67. my $file = "$modlibname/auto/$modpname/$modfname.so";
  68. # print STDERR "XSLoader::load for $module ($file)\n" if $dl_debug;
  69. my $bs = $file;
  70. $bs =~ s/(\.\w+)?(;\d*)?$/\.bs/; # look for .bs 'beside' the library
  71. if (-s $bs) { # only read file if it's not empty
  72. # print STDERR "BS: $bs ($^O, $dlsrc)\n" if $dl_debug;
  73. eval { do $bs; };
  74. warn "$bs: $@\n" if $@;
  75. goto \&XSLoader::bootstrap_inherit;
  76. }
  77. goto \&XSLoader::bootstrap_inherit if not -f $file;
  78. my $bootname = "boot_$module";
  79. $bootname =~ s/\W/_/g;
  80. @DynaLoader::dl_require_symbols = ($bootname);
  81. my $boot_symbol_ref;
  82. # Many dynamic extension loading problems will appear to come from
  83. # this section of code: XYZ failed at line 123 of DynaLoader.pm.
  84. # Often these errors are actually occurring in the initialisation
  85. # C code of the extension XS file. Perl reports the error as being
  86. # in this perl code simply because this was the last perl code
  87. # it executed.
  88. my $libref = dl_load_file($file, 0) or do {
  89. require Carp;
  90. Carp::croak("Can't load '$file' for module $module: " . dl_error());
  91. };
  92. push(@DynaLoader::dl_librefs,$libref); # record loaded object
  93. my @unresolved = dl_undef_symbols();
  94. if (@unresolved) {
  95. require Carp;
  96. Carp::carp("Undefined symbols present after loading $file: @unresolved\n");
  97. }
  98. $boot_symbol_ref = dl_find_symbol($libref, $bootname) or do {
  99. require Carp;
  100. Carp::croak("Can't find '$bootname' symbol in $file\n");
  101. };
  102. push(@DynaLoader::dl_modules, $module); # record loaded module
  103. boot:
  104. my $xs = dl_install_xsub($boots, $boot_symbol_ref, $file);
  105. # See comment block above
  106. push(@DynaLoader::dl_shared_objects, $file); # record files loaded
  107. return &$xs(@_);
  108. }
  109. sub bootstrap_inherit {
  110. require DynaLoader;
  111. goto \&DynaLoader::bootstrap_inherit;
  112. }
  113. 1;
  114. __END__
  115. =head1 NAME
  116. XSLoader - Dynamically load C libraries into Perl code
  117. =head1 VERSION
  118. Version 0.17
  119. =head1 SYNOPSIS
  120. package YourPackage;
  121. require XSLoader;
  122. XSLoader::load();
  123. =head1 DESCRIPTION
  124. This module defines a standard I<simplified> interface to the dynamic
  125. linking mechanisms available on many platforms. Its primary purpose is
  126. to implement cheap automatic dynamic loading of Perl modules.
  127. For a more complicated interface, see L<DynaLoader>. Many (most)
  128. features of C<DynaLoader> are not implemented in C<XSLoader>, like for
  129. example the C<dl_load_flags>, not honored by C<XSLoader>.
  130. =head2 Migration from C<DynaLoader>
  131. A typical module using L<DynaLoader|DynaLoader> starts like this:
  132. package YourPackage;
  133. require DynaLoader;
  134. our @ISA = qw( OnePackage OtherPackage DynaLoader );
  135. our $VERSION = '0.01';
  136. bootstrap YourPackage $VERSION;
  137. Change this to
  138. package YourPackage;
  139. use XSLoader;
  140. our @ISA = qw( OnePackage OtherPackage );
  141. our $VERSION = '0.01';
  142. XSLoader::load 'YourPackage', $VERSION;
  143. In other words: replace C<require DynaLoader> by C<use XSLoader>, remove
  144. C<DynaLoader> from C<@ISA>, change C<bootstrap> by C<XSLoader::load>. Do not
  145. forget to quote the name of your package on the C<XSLoader::load> line,
  146. and add comma (C<,>) before the arguments (C<$VERSION> above).
  147. Of course, if C<@ISA> contained only C<DynaLoader>, there is no need to have
  148. the C<@ISA> assignment at all; moreover, if instead of C<our> one uses the
  149. more backward-compatible
  150. use vars qw($VERSION @ISA);
  151. one can remove this reference to C<@ISA> together with the C<@ISA> assignment.
  152. If no C<$VERSION> was specified on the C<bootstrap> line, the last line becomes
  153. XSLoader::load 'YourPackage';
  154. If the call to C<load> is from C<YourPackage>, then that can be further
  155. simplified to
  156. XSLoader::load();
  157. as C<load> will use C<caller> to determine the package.
  158. =head2 Backward compatible boilerplate
  159. If you want to have your cake and eat it too, you need a more complicated
  160. boilerplate.
  161. package YourPackage;
  162. use vars qw($VERSION @ISA);
  163. @ISA = qw( OnePackage OtherPackage );
  164. $VERSION = '0.01';
  165. eval {
  166. require XSLoader;
  167. XSLoader::load('YourPackage', $VERSION);
  168. 1;
  169. } or do {
  170. require DynaLoader;
  171. push @ISA, 'DynaLoader';
  172. bootstrap YourPackage $VERSION;
  173. };
  174. The parentheses about C<XSLoader::load()> arguments are needed since we replaced
  175. C<use XSLoader> by C<require>, so the compiler does not know that a function
  176. C<XSLoader::load()> is present.
  177. This boilerplate uses the low-overhead C<XSLoader> if present; if used with
  178. an antique Perl which has no C<XSLoader>, it falls back to using C<DynaLoader>.
  179. =head1 Order of initialization: early load()
  180. I<Skip this section if the XSUB functions are supposed to be called from other
  181. modules only; read it only if you call your XSUBs from the code in your module,
  182. or have a C<BOOT:> section in your XS file (see L<perlxs/"The BOOT: Keyword">).
  183. What is described here is equally applicable to the L<DynaLoader|DynaLoader>
  184. interface.>
  185. A sufficiently complicated module using XS would have both Perl code (defined
  186. in F<YourPackage.pm>) and XS code (defined in F<YourPackage.xs>). If this
  187. Perl code makes calls into this XS code, and/or this XS code makes calls to
  188. the Perl code, one should be careful with the order of initialization.
  189. The call to C<XSLoader::load()> (or C<bootstrap()>) calls the module's
  190. bootstrap code. For modules build by F<xsubpp> (nearly all modules) this
  191. has three side effects:
  192. =over
  193. =item *
  194. A sanity check is done to ensure that the versions of the F<.pm> and the
  195. (compiled) F<.xs> parts are compatible. If C<$VERSION> was specified, this
  196. is used for the check. If not specified, it defaults to
  197. C<$XS_VERSION // $VERSION> (in the module's namespace)
  198. =item *
  199. the XSUBs are made accessible from Perl
  200. =item *
  201. if a C<BOOT:> section was present in the F<.xs> file, the code there is called.
  202. =back
  203. Consequently, if the code in the F<.pm> file makes calls to these XSUBs, it is
  204. convenient to have XSUBs installed before the Perl code is defined; for
  205. example, this makes prototypes for XSUBs visible to this Perl code.
  206. Alternatively, if the C<BOOT:> section makes calls to Perl functions (or
  207. uses Perl variables) defined in the F<.pm> file, they must be defined prior to
  208. the call to C<XSLoader::load()> (or C<bootstrap()>).
  209. The first situation being much more frequent, it makes sense to rewrite the
  210. boilerplate as
  211. package YourPackage;
  212. use XSLoader;
  213. use vars qw($VERSION @ISA);
  214. BEGIN {
  215. @ISA = qw( OnePackage OtherPackage );
  216. $VERSION = '0.01';
  217. # Put Perl code used in the BOOT: section here
  218. XSLoader::load 'YourPackage', $VERSION;
  219. }
  220. # Put Perl code making calls into XSUBs here
  221. =head2 The most hairy case
  222. If the interdependence of your C<BOOT:> section and Perl code is
  223. more complicated than this (e.g., the C<BOOT:> section makes calls to Perl
  224. functions which make calls to XSUBs with prototypes), get rid of the C<BOOT:>
  225. section altogether. Replace it with a function C<onBOOT()>, and call it like
  226. this:
  227. package YourPackage;
  228. use XSLoader;
  229. use vars qw($VERSION @ISA);
  230. BEGIN {
  231. @ISA = qw( OnePackage OtherPackage );
  232. $VERSION = '0.01';
  233. XSLoader::load 'YourPackage', $VERSION;
  234. }
  235. # Put Perl code used in onBOOT() function here; calls to XSUBs are
  236. # prototype-checked.
  237. onBOOT;
  238. # Put Perl initialization code assuming that XS is initialized here
  239. =head1 DIAGNOSTICS
  240. =over
  241. =item C<Can't find '%s' symbol in %s>
  242. B<(F)> The bootstrap symbol could not be found in the extension module.
  243. =item C<Can't load '%s' for module %s: %s>
  244. B<(F)> The loading or initialisation of the extension module failed.
  245. The detailed error follows.
  246. =item C<Undefined symbols present after loading %s: %s>
  247. B<(W)> As the message says, some symbols stay undefined although the
  248. extension module was correctly loaded and initialised. The list of undefined
  249. symbols follows.
  250. =back
  251. =head1 LIMITATIONS
  252. To reduce the overhead as much as possible, only one possible location
  253. is checked to find the extension DLL (this location is where C<make install>
  254. would put the DLL). If not found, the search for the DLL is transparently
  255. delegated to C<DynaLoader>, which looks for the DLL along the C<@INC> list.
  256. In particular, this is applicable to the structure of C<@INC> used for testing
  257. not-yet-installed extensions. This means that running uninstalled extensions
  258. may have much more overhead than running the same extensions after
  259. C<make install>.
  260. =head1 KNOWN BUGS
  261. The new simpler way to call C<XSLoader::load()> with no arguments at all
  262. does not work on Perl 5.8.4 and 5.8.5.
  263. =head1 BUGS
  264. Please report any bugs or feature requests via the perlbug(1) utility.
  265. =head1 SEE ALSO
  266. L<DynaLoader>
  267. =head1 AUTHORS
  268. Ilya Zakharevich originally extracted C<XSLoader> from C<DynaLoader>.
  269. CPAN version is currently maintained by SE<eacute>bastien Aperghis-Tramoni
  270. E<lt>sebastien@aperghis.netE<gt>.
  271. Previous maintainer was Michael G Schwern <schwern@pobox.com>.
  272. =head1 COPYRIGHT & LICENSE
  273. Copyright (C) 1990-2011 by Larry Wall and others.
  274. This program is free software; you can redistribute it and/or modify
  275. it under the same terms as Perl itself.
  276. =cut