This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
(perl #133326) fix and clarify handling of recurs_sv.
[perl5.git] / dist / XSLoader / XSLoader_pm.PL
CommitLineData
11fd7d05 1use strict;
9cf41c4d 2use Config;
adedc077
BF
3# We require DynaLoader to make sure that mod2fname is loaded
4eval { require DynaLoader };
9cf41c4d 5
11fd7d05 61 while unlink "XSLoader.pm";
1ae6ead9 7open OUT, '>', 'XSLoader.pm' or die $!;
9cf41c4d 8print OUT <<'EOT';
35e4400b 9# Generated from XSLoader_pm.PL (resolved %Config::Config value)
f24427cc 10# This file is unique for every OS
9cf41c4d
GS
11
12package XSLoader;
13
9a84a9ba 14$VERSION = "0.30"; # remember to update version in POD!
11fd7d05
RGS
15
16#use strict;
9cf41c4d 17
daef35db
NC
18package DynaLoader;
19
20EOT
21
22# dlutils.c before 5.006 has this:
23#
24# #ifdef DEBUGGING
25# dl_debug = SvIV( perl_get_sv("DynaLoader::dl_debug", 0x04) );
26# #endif
27#
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:
34
35print OUT <<'EOT' if $] < 5.006;
36
9cf41c4d 37# enable debug/trace messages from DynaLoader perl code
daef35db 38$dl_debug = $ENV{PERL_DL_DEBUG} || 0 unless defined $dl_debug;
9cf41c4d 39
daef35db 40EOT
b4cea227 41
daef35db 42print OUT <<'EOT';
b4cea227
GS
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
9cf41c4d 45boot_DynaLoader('DynaLoader') if defined(&boot_DynaLoader) &&
b4cea227 46 !defined(&dl_error);
9cf41c4d
GS
47package XSLoader;
48
9cf41c4d
GS
49sub load {
50 package DynaLoader;
51
30837b2a
GK
52 my ($caller, $modlibname) = caller();
53 my $module = $caller;
9e8c31cc 54
1d2b7ec5 55 if (@_) {
e6ea8c3b 56 $module = $_[0];
1d2b7ec5 57 } else {
e6ea8c3b 58 $_[0] = $module;
1d2b7ec5 59 }
9cf41c4d
GS
60
61 # work with static linking too
73bf7552
AT
62 my $boots = "$module\::bootstrap";
63 goto &$boots if defined &$boots;
9cf41c4d 64
6e0eede9 65 goto \&XSLoader::bootstrap_inherit unless $module and defined &dl_load_file;
9cf41c4d
GS
66
67 my @modparts = split(/::/,$module);
68 my $modfname = $modparts[-1];
9a84a9ba 69 my $modfname_orig = $modfname; # For .bs file search
9cf41c4d
GS
70
71EOT
72
4821216b
BF
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.
76print OUT <<'EOT' if defined &DynaLoader::mod2fname || $Config{d_libname_unique};
9cf41c4d
GS
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.
4821216b 80 $modfname = &DynaLoader::mod2fname(\@modparts) if defined &DynaLoader::mod2fname;
9cf41c4d
GS
81
82EOT
83
9d419b5f
IZ
84print OUT <<'EOT' if $^O eq 'os2';
85
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;
88
89EOT
b0d07b4e 90
9cf41c4d
GS
91print OUT <<'EOT';
92 my $modpname = join('/',@modparts);
30837b2a 93 my $c = () = split(/::/,$caller,-1);
e6ea8c3b 94 $modlibname =~ s,[\\/][^\\/]+$,, while $c--; # Q&D basename
a651dcdf
FC
95EOT
96
97my $to_print = <<'EOT';
08e3451d 98 # Does this look like a relative path?
a651dcdf
FC
99 if ($modlibname !~ m{regexp}) {
100EOT
101
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
105 : '^/'
106~e;
107
108print OUT $to_print, <<'EOT';
08e3451d
FC
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.
113 #
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.
118 #
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
121 # not hot code.
122 FOUND: {
123 for (@INC) {
124 if ($_ eq $modlibname) {
125 last FOUND;
126 }
127 }
128 # Not found. Fall back to DynaLoader.
129 goto \&XSLoader::bootstrap_inherit;
130 }
131 }
f4b4ed7b
NC
132EOT
133
134my $dl_dlext = quotemeta($Config::Config{'dlext'});
135
136print OUT <<"EOT";
137 my \$file = "\$modlibname/auto/\$modpname/\$modfname.$dl_dlext";
138EOT
139
140print OUT <<'EOT';
9cf41c4d
GS
141
142# print STDERR "XSLoader::load for $module ($file)\n" if $dl_debug;
143
9a84a9ba
CB
144 # N.B. The .bs file does not following the naming convention used
145 # by mod2fname, so use the unedited version of the name.
146
147 my $bs = "$modlibname/auto/$modpname/$modfname_orig.bs";
9cf41c4d 148
a17768d7
DIM
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;
9cf41c4d
GS
151
152 my $bootname = "boot_$module";
153 $bootname =~ s/\W/_/g;
11fd7d05 154 @DynaLoader::dl_require_symbols = ($bootname);
9cf41c4d 155
588cafc8
DM
156 my $boot_symbol_ref;
157
68d3ba50
VK
158EOT
159
588cafc8 160 if ($^O eq 'darwin') {
c35721ed
RU
161 my $extra_arg = ', 1 ' if $DynaLoader::VERSION ge '1.37';
162print OUT <<"EOT";
163 if (\$boot_symbol_ref = dl_find_symbol( 0, \$bootname $extra_arg)) {
164 goto boot; #extension library has already been loaded, e.g. darwin
165 }
68d3ba50 166EOT
588cafc8
DM
167 }
168
68d3ba50 169print OUT <<'EOT';
9cf41c4d
GS
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
175 # it executed.
176
177 my $libref = dl_load_file($file, 0) or do {
11fd7d05
RGS
178 require Carp;
179 Carp::croak("Can't load '$file' for module $module: " . dl_error());
9cf41c4d 180 };
11fd7d05 181 push(@DynaLoader::dl_librefs,$libref); # record loaded object
9cf41c4d 182
f24427cc
DD
183EOT
184my $dlsrc = $Config{dlsrc};
185if ($dlsrc eq 'dl_freemint.xs' || $dlsrc eq 'dl_dld.xs') {
186 print OUT <<'EOT';
9cf41c4d
GS
187 my @unresolved = dl_undef_symbols();
188 if (@unresolved) {
11fd7d05
RGS
189 require Carp;
190 Carp::carp("Undefined symbols present after loading $file: @unresolved\n");
9cf41c4d
GS
191 }
192
f24427cc
DD
193EOT
194}
195
196print OUT <<'EOT';
588cafc8 197 $boot_symbol_ref = dl_find_symbol($libref, $bootname) or do {
11fd7d05
RGS
198 require Carp;
199 Carp::croak("Can't find '$bootname' symbol in $file\n");
9cf41c4d
GS
200 };
201
11fd7d05 202 push(@DynaLoader::dl_modules, $module); # record loaded module
9cf41c4d 203
588cafc8 204 boot:
73bf7552 205 my $xs = dl_install_xsub($boots, $boot_symbol_ref, $file);
588cafc8 206
9cf41c4d 207 # See comment block above
89166d32 208 push(@DynaLoader::dl_shared_objects, $file); # record files loaded
9cf41c4d 209 return &$xs(@_);
6e0eede9
NC
210}
211EOT
212
e6ea8c3b 213# Can't test with DynaLoader->can('bootstrap_inherit') when building in the
6e0eede9 214# core, as XSLoader gets built before DynaLoader.
9cf41c4d 215
6e0eede9
NC
216if ($] >= 5.006) {
217 print OUT <<'EOT';
218
219sub bootstrap_inherit {
220 require DynaLoader;
221 goto \&DynaLoader::bootstrap_inherit;
11fd7d05
RGS
222}
223
6e0eede9
NC
224EOT
225} else {
226 print OUT <<'EOT';
227
11fd7d05 228sub bootstrap_inherit {
6e0eede9 229 # Versions of DynaLoader prior to 5.6.0 don't have bootstrap_inherit.
11fd7d05
RGS
230 package DynaLoader;
231
232 my $module = $_[0];
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?
9cf41c4d 236 require DynaLoader;
11fd7d05 237 DynaLoader::bootstrap(@_);
9cf41c4d
GS
238}
239
6e0eede9
NC
240EOT
241}
242
243print OUT <<'EOT';
9e8c31cc
MS
2441;
245
11fd7d05 246
9cf41c4d
GS
247__END__
248
249=head1 NAME
250
251XSLoader - Dynamically load C libraries into Perl code
252
11fd7d05
RGS
253=head1 VERSION
254
9a84a9ba 255Version 0.30
11fd7d05 256
9cf41c4d
GS
257=head1 SYNOPSIS
258
259 package YourPackage;
1d2b7ec5 260 require XSLoader;
9cf41c4d 261
42034324 262 XSLoader::load(__PACKAGE__, $VERSION);
9cf41c4d
GS
263
264=head1 DESCRIPTION
265
266This module defines a standard I<simplified> interface to the dynamic
267linking mechanisms available on many platforms. Its primary purpose is
268to implement cheap automatic dynamic loading of Perl modules.
269
4406dda9 270For a more complicated interface, see L<DynaLoader>. Many (most)
11fd7d05
RGS
271features of C<DynaLoader> are not implemented in C<XSLoader>, like for
272example the C<dl_load_flags>, not honored by C<XSLoader>.
9cf41c4d 273
d7f44de2
IZ
274=head2 Migration from C<DynaLoader>
275
276A typical module using L<DynaLoader|DynaLoader> starts like this:
277
278 package YourPackage;
279 require DynaLoader;
280
281 our @ISA = qw( OnePackage OtherPackage DynaLoader );
282 our $VERSION = '0.01';
42034324 283 __PACKAGE__->bootstrap($VERSION);
d7f44de2
IZ
284
285Change this to
286
287 package YourPackage;
288 use XSLoader;
289
290 our @ISA = qw( OnePackage OtherPackage );
291 our $VERSION = '0.01';
42034324 292 XSLoader::load(__PACKAGE__, $VERSION);
d7f44de2
IZ
293
294In other words: replace C<require DynaLoader> by C<use XSLoader>, remove
11fd7d05 295C<DynaLoader> from C<@ISA>, change C<bootstrap> by C<XSLoader::load>. Do not
d7f44de2 296forget to quote the name of your package on the C<XSLoader::load> line,
9c6b46e2 297and add comma (C<,>) before the arguments (C<$VERSION> above).
d7f44de2 298
11fd7d05
RGS
299Of course, if C<@ISA> contained only C<DynaLoader>, there is no need to have
300the C<@ISA> assignment at all; moreover, if instead of C<our> one uses the
301more backward-compatible
d7f44de2
IZ
302
303 use vars qw($VERSION @ISA);
304
11fd7d05 305one can remove this reference to C<@ISA> together with the C<@ISA> assignment.
d7f44de2 306
11fd7d05 307If no C<$VERSION> was specified on the C<bootstrap> line, the last line becomes
d7f44de2 308
42034324 309 XSLoader::load(__PACKAGE__);
d7f44de2 310
42034324 311in which case it can be further simplified to
1d2b7ec5
NC
312
313 XSLoader::load();
314
315as C<load> will use C<caller> to determine the package.
316
d7f44de2
IZ
317=head2 Backward compatible boilerplate
318
319If you want to have your cake and eat it too, you need a more complicated
320boilerplate.
321
322 package YourPackage;
d7f44de2 323
1a58b39a
N
324 our @ISA = qw( OnePackage OtherPackage );
325 our $VERSION = '0.01';
d7f44de2
IZ
326 eval {
327 require XSLoader;
42034324 328 XSLoader::load(__PACKAGE__, $VERSION);
d7f44de2
IZ
329 1;
330 } or do {
331 require DynaLoader;
332 push @ISA, 'DynaLoader';
42034324 333 __PACKAGE__->bootstrap($VERSION);
d7f44de2
IZ
334 };
335
11fd7d05 336The parentheses about C<XSLoader::load()> arguments are needed since we replaced
d7f44de2 337C<use XSLoader> by C<require>, so the compiler does not know that a function
11fd7d05 338C<XSLoader::load()> is present.
d7f44de2
IZ
339
340This boilerplate uses the low-overhead C<XSLoader> if present; if used with
681a49bf 341an antique Perl which has no C<XSLoader>, it falls back to using C<DynaLoader>.
d7f44de2
IZ
342
343=head1 Order of initialization: early load()
344
345I<Skip this section if the XSUB functions are supposed to be called from other
346modules only; read it only if you call your XSUBs from the code in your module,
347or have a C<BOOT:> section in your XS file (see L<perlxs/"The BOOT: Keyword">).
4406dda9 348What is described here is equally applicable to the L<DynaLoader|DynaLoader>
d7f44de2
IZ
349interface.>
350
351A sufficiently complicated module using XS would have both Perl code (defined
352in F<YourPackage.pm>) and XS code (defined in F<YourPackage.xs>). If this
353Perl code makes calls into this XS code, and/or this XS code makes calls to
354the Perl code, one should be careful with the order of initialization.
355
3a1b0858
NC
356The call to C<XSLoader::load()> (or C<bootstrap()>) calls the module's
357bootstrap code. For modules build by F<xsubpp> (nearly all modules) this
358has three side effects:
d7f44de2
IZ
359
360=over
361
362=item *
363
3a1b0858
NC
364A 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
366is used for the check. If not specified, it defaults to
367C<$XS_VERSION // $VERSION> (in the module's namespace)
d7f44de2
IZ
368
369=item *
370
3a1b0858 371the XSUBs are made accessible from Perl
d7f44de2
IZ
372
373=item *
374
4406dda9 375if a C<BOOT:> section was present in the F<.xs> file, the code there is called.
d7f44de2
IZ
376
377=back
378
4406dda9 379Consequently, if the code in the F<.pm> file makes calls to these XSUBs, it is
d7f44de2
IZ
380convenient to have XSUBs installed before the Perl code is defined; for
381example, this makes prototypes for XSUBs visible to this Perl code.
382Alternatively, if the C<BOOT:> section makes calls to Perl functions (or
4406dda9 383uses Perl variables) defined in the F<.pm> file, they must be defined prior to
11fd7d05 384the call to C<XSLoader::load()> (or C<bootstrap()>).
d7f44de2
IZ
385
386The first situation being much more frequent, it makes sense to rewrite the
387boilerplate as
388
389 package YourPackage;
390 use XSLoader;
1a58b39a 391 our ($VERSION, @ISA);
d7f44de2
IZ
392
393 BEGIN {
394 @ISA = qw( OnePackage OtherPackage );
395 $VERSION = '0.01';
396
397 # Put Perl code used in the BOOT: section here
398
42034324 399 XSLoader::load(__PACKAGE__, $VERSION);
d7f44de2
IZ
400 }
401
402 # Put Perl code making calls into XSUBs here
403
404=head2 The most hairy case
405
406If the interdependence of your C<BOOT:> section and Perl code is
407more complicated than this (e.g., the C<BOOT:> section makes calls to Perl
408functions which make calls to XSUBs with prototypes), get rid of the C<BOOT:>
11fd7d05 409section altogether. Replace it with a function C<onBOOT()>, and call it like
d7f44de2
IZ
410this:
411
412 package YourPackage;
413 use XSLoader;
1a58b39a 414 our ($VERSION, @ISA);
d7f44de2
IZ
415
416 BEGIN {
417 @ISA = qw( OnePackage OtherPackage );
418 $VERSION = '0.01';
42034324 419 XSLoader::load(__PACKAGE__, $VERSION);
d7f44de2
IZ
420 }
421
422 # Put Perl code used in onBOOT() function here; calls to XSUBs are
423 # prototype-checked.
424
425 onBOOT;
426
427 # Put Perl initialization code assuming that XS is initialized here
428
11fd7d05
RGS
429
430=head1 DIAGNOSTICS
431
150e77ce 432=over
11fd7d05 433
150e77ce 434=item C<Can't find '%s' symbol in %s>
11fd7d05
RGS
435
436B<(F)> The bootstrap symbol could not be found in the extension module.
437
150e77ce 438=item C<Can't load '%s' for module %s: %s>
11fd7d05
RGS
439
440B<(F)> The loading or initialisation of the extension module failed.
441The detailed error follows.
442
150e77ce 443=item C<Undefined symbols present after loading %s: %s>
11fd7d05
RGS
444
445B<(W)> As the message says, some symbols stay undefined although the
446extension module was correctly loaded and initialised. The list of undefined
447symbols follows.
448
11fd7d05
RGS
449=back
450
d7f44de2
IZ
451=head1 LIMITATIONS
452
453To reduce the overhead as much as possible, only one possible location
454is checked to find the extension DLL (this location is where C<make install>
455would put the DLL). If not found, the search for the DLL is transparently
11fd7d05 456delegated to C<DynaLoader>, which looks for the DLL along the C<@INC> list.
d7f44de2 457
11fd7d05 458In particular, this is applicable to the structure of C<@INC> used for testing
4406dda9
RGS
459not-yet-installed extensions. This means that running uninstalled extensions
460may have much more overhead than running the same extensions after
d7f44de2
IZ
461C<make install>.
462
11fd7d05 463
e6ea8c3b
CBW
464=head1 KNOWN BUGS
465
466The new simpler way to call C<XSLoader::load()> with no arguments at all
467does not work on Perl 5.8.4 and 5.8.5.
468
469
11fd7d05
RGS
470=head1 BUGS
471
9c6b46e2 472Please report any bugs or feature requests via the perlbug(1) utility.
11fd7d05
RGS
473
474
475=head1 SEE ALSO
476
477L<DynaLoader>
478
479
9c6b46e2 480=head1 AUTHORS
9cf41c4d 481
11fd7d05
RGS
482Ilya Zakharevich originally extracted C<XSLoader> from C<DynaLoader>.
483
484CPAN version is currently maintained by SE<eacute>bastien Aperghis-Tramoni
150e77ce 485E<lt>sebastien@aperghis.netE<gt>.
11fd7d05 486
150e77ce 487Previous maintainer was Michael G Schwern <schwern@pobox.com>.
11fd7d05
RGS
488
489
73bf7552
AT
490=head1 COPYRIGHT & LICENSE
491
e6ea8c3b 492Copyright (C) 1990-2011 by Larry Wall and others.
11fd7d05
RGS
493
494This program is free software; you can redistribute it and/or modify
495it under the same terms as Perl itself.
9cf41c4d
GS
496
497=cut
498EOT
499
500close OUT or die $!;