3 # We require DynaLoader to make sure that mod2fname is loaded
4 eval { require DynaLoader };
6 1 while unlink "XSLoader.pm";
7 open OUT, '>', 'XSLoader.pm' or die $!;
9 # Generated from XSLoader_pm.PL (resolved %Config::Config value)
10 # This file is unique for every OS
14 $VERSION = "0.30"; # remember to update version in POD!
22 # dlutils.c before 5.006 has this:
25 # dl_debug = SvIV( perl_get_sv("DynaLoader::dl_debug", 0x04) );
28 # where 0x04 is GV_ADDWARN, which causes a warning to be issued by the call
29 # into XS below, if DynaLoader.pm hasn't been loaded.
30 # It was changed to 0 in the commit(s) that added XSLoader to the core
31 # (9cf41c4d23a47c8b and its parent 9426adcd48655815)
32 # Hence to backport XSLoader to work silently with earlier DynaLoaders we need
33 # to ensure that the variable exists:
35 print OUT <<'EOT' if $] < 5.006;
37 # enable debug/trace messages from DynaLoader perl code
38 $dl_debug = $ENV{PERL_DL_DEBUG} || 0 unless defined $dl_debug;
43 # No prizes for guessing why we don't say 'bootstrap DynaLoader;' here.
44 # NOTE: All dl_*.xs (including dl_none.xs) define a dl_error() XSUB
45 boot_DynaLoader('DynaLoader') if defined(&boot_DynaLoader) &&
52 my ($caller, $modlibname) = caller();
61 # work with static linking too
62 my $boots = "$module\::bootstrap";
63 goto &$boots if defined &$boots;
65 goto \&XSLoader::bootstrap_inherit unless $module and defined &dl_load_file;
67 my @modparts = split(/::/,$module);
68 my $modfname = $modparts[-1];
69 my $modfname_orig = $modfname; # For .bs file search
73 # defined &DynaLoader::mod2fname catches most cases, except when
74 # cross-compiling to a system that defines mod2fname. Using
75 # $Config{d_libname_unique} is a best attempt at catching those cases.
76 print OUT <<'EOT' if defined &DynaLoader::mod2fname || $Config{d_libname_unique};
77 # Some systems have restrictions on files names for DLL's etc.
78 # mod2fname returns appropriate file base name (typically truncated)
79 # It may also edit @modparts if required.
80 $modfname = &DynaLoader::mod2fname(\@modparts) if defined &DynaLoader::mod2fname;
84 print OUT <<'EOT' if $^O eq 'os2';
86 # os2 static build can dynaload, but cannot dynaload Perl modules...
87 die 'Dynaloaded Perl modules are not available in this build of Perl' if $OS2::is_static;
92 my $modpname = join('/',@modparts);
93 my $c = () = split(/::/,$caller,-1);
94 $modlibname =~ s,[\\/][^\\/]+$,, while $c--; # Q&D basename
97 my $to_print = <<'EOT';
98 # Does this look like a relative path?
99 if ($modlibname !~ m{regexp}) {
102 $to_print =~ s~regexp~
103 $^O eq 'MSWin32' || $^O eq 'os2' || $^O eq 'cygwin' || $^O eq 'amigaos'
104 ? '^(?:[A-Za-z]:)?[\\\/]' # Optional drive letter
108 print OUT $to_print, <<'EOT';
109 # Someone may have a #line directive that changes the file name, or
110 # may be calling XSLoader::load from inside a string eval. We cer-
111 # tainly do not want to go loading some code that is not in @INC,
112 # as it could be untrusted.
114 # We could just fall back to DynaLoader here, but then the rest of
115 # this function would go untested in the perl core, since all @INC
116 # paths are relative during testing. That would be a time bomb
117 # waiting to happen, since bugs could be introduced into the code.
119 # So look through @INC to see if $modlibname is in it. A rela-
120 # tive $modlibname is not a common occurrence, so this block is
124 if ($_ eq $modlibname) {
128 # Not found. Fall back to DynaLoader.
129 goto \&XSLoader::bootstrap_inherit;
134 my $dl_dlext = quotemeta($Config::Config{'dlext'});
137 my \$file = "\$modlibname/auto/\$modpname/\$modfname.$dl_dlext";
142 # print STDERR "XSLoader::load for $module ($file)\n" if $dl_debug;
144 # N.B. The .bs file does not following the naming convention used
145 # by mod2fname, so use the unedited version of the name.
147 my $bs = "$modlibname/auto/$modpname/$modfname_orig.bs";
149 # This calls DynaLoader::bootstrap, which will load the .bs file if present
150 goto \&XSLoader::bootstrap_inherit if not -f $file or -s $bs;
152 my $bootname = "boot_$module";
153 $bootname =~ s/\W/_/g;
154 @DynaLoader::dl_require_symbols = ($bootname);
160 if ($^O eq 'darwin') {
161 my $extra_arg = ', 1 ' if $DynaLoader::VERSION ge '1.37';
163 if (\$boot_symbol_ref = dl_find_symbol( 0, \$bootname $extra_arg)) {
164 goto boot; #extension library has already been loaded, e.g. darwin
170 # Many dynamic extension loading problems will appear to come from
171 # this section of code: XYZ failed at line 123 of DynaLoader.pm.
172 # Often these errors are actually occurring in the initialisation
173 # C code of the extension XS file. Perl reports the error as being
174 # in this perl code simply because this was the last perl code
177 my $libref = dl_load_file($file, 0) or do {
179 Carp::croak("Can't load '$file' for module $module: " . dl_error());
181 push(@DynaLoader::dl_librefs,$libref); # record loaded object
184 my $dlsrc = $Config{dlsrc};
185 if ($dlsrc eq 'dl_freemint.xs' || $dlsrc eq 'dl_dld.xs') {
187 my @unresolved = dl_undef_symbols();
190 Carp::carp("Undefined symbols present after loading $file: @unresolved\n");
197 $boot_symbol_ref = dl_find_symbol($libref, $bootname) or do {
199 Carp::croak("Can't find '$bootname' symbol in $file\n");
202 push(@DynaLoader::dl_modules, $module); # record loaded module
205 my $xs = dl_install_xsub($boots, $boot_symbol_ref, $file);
207 # See comment block above
208 push(@DynaLoader::dl_shared_objects, $file); # record files loaded
213 # Can't test with DynaLoader->can('bootstrap_inherit') when building in the
214 # core, as XSLoader gets built before DynaLoader.
219 sub bootstrap_inherit {
221 goto \&DynaLoader::bootstrap_inherit;
228 sub bootstrap_inherit {
229 # Versions of DynaLoader prior to 5.6.0 don't have bootstrap_inherit.
233 local *DynaLoader::isa = *{"$module\::ISA"};
234 local @DynaLoader::isa = (@DynaLoader::isa, 'DynaLoader');
235 # Cannot goto due to delocalization. Will report errors on a wrong line?
237 DynaLoader::bootstrap(@_);
251 XSLoader - Dynamically load C libraries into Perl code
262 XSLoader::load(__PACKAGE__, $VERSION);
266 This module defines a standard I<simplified> interface to the dynamic
267 linking mechanisms available on many platforms. Its primary purpose is
268 to implement cheap automatic dynamic loading of Perl modules.
270 For a more complicated interface, see L<DynaLoader>. Many (most)
271 features of C<DynaLoader> are not implemented in C<XSLoader>, like for
272 example the C<dl_load_flags>, not honored by C<XSLoader>.
274 =head2 Migration from C<DynaLoader>
276 A typical module using L<DynaLoader|DynaLoader> starts like this:
281 our @ISA = qw( OnePackage OtherPackage DynaLoader );
282 our $VERSION = '0.01';
283 __PACKAGE__->bootstrap($VERSION);
290 our @ISA = qw( OnePackage OtherPackage );
291 our $VERSION = '0.01';
292 XSLoader::load(__PACKAGE__, $VERSION);
294 In other words: replace C<require DynaLoader> by C<use XSLoader>, remove
295 C<DynaLoader> from C<@ISA>, change C<bootstrap> by C<XSLoader::load>. Do not
296 forget to quote the name of your package on the C<XSLoader::load> line,
297 and add comma (C<,>) before the arguments (C<$VERSION> above).
299 Of course, if C<@ISA> contained only C<DynaLoader>, there is no need to have
300 the C<@ISA> assignment at all; moreover, if instead of C<our> one uses the
301 more backward-compatible
303 use vars qw($VERSION @ISA);
305 one can remove this reference to C<@ISA> together with the C<@ISA> assignment.
307 If no C<$VERSION> was specified on the C<bootstrap> line, the last line becomes
309 XSLoader::load(__PACKAGE__);
311 in which case it can be further simplified to
315 as C<load> will use C<caller> to determine the package.
317 =head2 Backward compatible boilerplate
319 If you want to have your cake and eat it too, you need a more complicated
324 our @ISA = qw( OnePackage OtherPackage );
325 our $VERSION = '0.01';
328 XSLoader::load(__PACKAGE__, $VERSION);
332 push @ISA, 'DynaLoader';
333 __PACKAGE__->bootstrap($VERSION);
336 The parentheses about C<XSLoader::load()> arguments are needed since we replaced
337 C<use XSLoader> by C<require>, so the compiler does not know that a function
338 C<XSLoader::load()> is present.
340 This boilerplate uses the low-overhead C<XSLoader> if present; if used with
341 an antique Perl which has no C<XSLoader>, it falls back to using C<DynaLoader>.
343 =head1 Order of initialization: early load()
345 I<Skip this section if the XSUB functions are supposed to be called from other
346 modules only; read it only if you call your XSUBs from the code in your module,
347 or have a C<BOOT:> section in your XS file (see L<perlxs/"The BOOT: Keyword">).
348 What is described here is equally applicable to the L<DynaLoader|DynaLoader>
351 A sufficiently complicated module using XS would have both Perl code (defined
352 in F<YourPackage.pm>) and XS code (defined in F<YourPackage.xs>). If this
353 Perl code makes calls into this XS code, and/or this XS code makes calls to
354 the Perl code, one should be careful with the order of initialization.
356 The call to C<XSLoader::load()> (or C<bootstrap()>) calls the module's
357 bootstrap code. For modules build by F<xsubpp> (nearly all modules) this
358 has three side effects:
364 A sanity check is done to ensure that the versions of the F<.pm> and the
365 (compiled) F<.xs> parts are compatible. If C<$VERSION> was specified, this
366 is used for the check. If not specified, it defaults to
367 C<$XS_VERSION // $VERSION> (in the module's namespace)
371 the XSUBs are made accessible from Perl
375 if a C<BOOT:> section was present in the F<.xs> file, the code there is called.
379 Consequently, if the code in the F<.pm> file makes calls to these XSUBs, it is
380 convenient to have XSUBs installed before the Perl code is defined; for
381 example, this makes prototypes for XSUBs visible to this Perl code.
382 Alternatively, if the C<BOOT:> section makes calls to Perl functions (or
383 uses Perl variables) defined in the F<.pm> file, they must be defined prior to
384 the call to C<XSLoader::load()> (or C<bootstrap()>).
386 The first situation being much more frequent, it makes sense to rewrite the
391 our ($VERSION, @ISA);
394 @ISA = qw( OnePackage OtherPackage );
397 # Put Perl code used in the BOOT: section here
399 XSLoader::load(__PACKAGE__, $VERSION);
402 # Put Perl code making calls into XSUBs here
404 =head2 The most hairy case
406 If the interdependence of your C<BOOT:> section and Perl code is
407 more complicated than this (e.g., the C<BOOT:> section makes calls to Perl
408 functions which make calls to XSUBs with prototypes), get rid of the C<BOOT:>
409 section altogether. Replace it with a function C<onBOOT()>, and call it like
414 our ($VERSION, @ISA);
417 @ISA = qw( OnePackage OtherPackage );
419 XSLoader::load(__PACKAGE__, $VERSION);
422 # Put Perl code used in onBOOT() function here; calls to XSUBs are
427 # Put Perl initialization code assuming that XS is initialized here
434 =item C<Can't find '%s' symbol in %s>
436 B<(F)> The bootstrap symbol could not be found in the extension module.
438 =item C<Can't load '%s' for module %s: %s>
440 B<(F)> The loading or initialisation of the extension module failed.
441 The detailed error follows.
443 =item C<Undefined symbols present after loading %s: %s>
445 B<(W)> As the message says, some symbols stay undefined although the
446 extension module was correctly loaded and initialised. The list of undefined
453 To reduce the overhead as much as possible, only one possible location
454 is checked to find the extension DLL (this location is where C<make install>
455 would put the DLL). If not found, the search for the DLL is transparently
456 delegated to C<DynaLoader>, which looks for the DLL along the C<@INC> list.
458 In particular, this is applicable to the structure of C<@INC> used for testing
459 not-yet-installed extensions. This means that running uninstalled extensions
460 may have much more overhead than running the same extensions after
466 The new simpler way to call C<XSLoader::load()> with no arguments at all
467 does not work on Perl 5.8.4 and 5.8.5.
472 Please report any bugs or feature requests via the perlbug(1) utility.
482 Ilya Zakharevich originally extracted C<XSLoader> from C<DynaLoader>.
484 CPAN version is currently maintained by SE<eacute>bastien Aperghis-Tramoni
485 E<lt>sebastien@aperghis.netE<gt>.
487 Previous maintainer was Michael G Schwern <schwern@pobox.com>.
490 =head1 COPYRIGHT & LICENSE
492 Copyright (C) 1990-2011 by Larry Wall and others.
494 This program is free software; you can redistribute it and/or modify
495 it under the same terms as Perl itself.