This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #128769] Improve base.pm @INC . message
[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";
9cf41c4d
GS
7open OUT, ">XSLoader.pm" or die $!;
8print OUT <<'EOT';
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
5993d662 14$VERSION = "0.22";
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];
69
70EOT
71
4821216b
BF
72# defined &DynaLoader::mod2fname catches most cases, except when
73# cross-compiling to a system that defines mod2fname. Using
74# $Config{d_libname_unique} is a best attempt at catching those cases.
75print OUT <<'EOT' if defined &DynaLoader::mod2fname || $Config{d_libname_unique};
9cf41c4d
GS
76 # Some systems have restrictions on files names for DLL's etc.
77 # mod2fname returns appropriate file base name (typically truncated)
78 # It may also edit @modparts if required.
4821216b 79 $modfname = &DynaLoader::mod2fname(\@modparts) if defined &DynaLoader::mod2fname;
9cf41c4d
GS
80
81EOT
82
9d419b5f
IZ
83print OUT <<'EOT' if $^O eq 'os2';
84
85 # os2 static build can dynaload, but cannot dynaload Perl modules...
86 die 'Dynaloaded Perl modules are not available in this build of Perl' if $OS2::is_static;
87
88EOT
b0d07b4e 89
9cf41c4d
GS
90print OUT <<'EOT';
91 my $modpname = join('/',@modparts);
30837b2a 92 my $c = () = split(/::/,$caller,-1);
e6ea8c3b 93 $modlibname =~ s,[\\/][^\\/]+$,, while $c--; # Q&D basename
a651dcdf
FC
94EOT
95
96my $to_print = <<'EOT';
08e3451d 97 # Does this look like a relative path?
a651dcdf
FC
98 if ($modlibname !~ m{regexp}) {
99EOT
100
101$to_print =~ s~regexp~
102 $^O eq 'MSWin32' || $^O eq 'os2' || $^O eq 'cygwin' || $^O eq 'amigaos'
103 ? '^(?:[A-Za-z]:)?[\\\/]' # Optional drive letter
104 : '^/'
105~e;
106
107print OUT $to_print, <<'EOT';
08e3451d
FC
108 # Someone may have a #line directive that changes the file name, or
109 # may be calling XSLoader::load from inside a string eval. We cer-
110 # tainly do not want to go loading some code that is not in @INC,
111 # as it could be untrusted.
112 #
113 # We could just fall back to DynaLoader here, but then the rest of
114 # this function would go untested in the perl core, since all @INC
115 # paths are relative during testing. That would be a time bomb
116 # waiting to happen, since bugs could be introduced into the code.
117 #
118 # So look through @INC to see if $modlibname is in it. A rela-
119 # tive $modlibname is not a common occurrence, so this block is
120 # not hot code.
121 FOUND: {
122 for (@INC) {
123 if ($_ eq $modlibname) {
124 last FOUND;
125 }
126 }
127 # Not found. Fall back to DynaLoader.
128 goto \&XSLoader::bootstrap_inherit;
129 }
130 }
f4b4ed7b
NC
131EOT
132
133my $dl_dlext = quotemeta($Config::Config{'dlext'});
134
135print OUT <<"EOT";
136 my \$file = "\$modlibname/auto/\$modpname/\$modfname.$dl_dlext";
137EOT
138
139print OUT <<'EOT';
9cf41c4d
GS
140
141# print STDERR "XSLoader::load for $module ($file)\n" if $dl_debug;
142
143 my $bs = $file;
144 $bs =~ s/(\.\w+)?(;\d*)?$/\.bs/; # look for .bs 'beside' the library
145
73bf7552 146 if (-s $bs) { # only read file if it's not empty
80a65c70 147# print STDERR "BS: $bs ($^O, $dlsrc)\n" if $dl_debug;
73bf7552
AT
148 eval { do $bs; };
149 warn "$bs: $@\n" if $@;
4c7a0d98 150 goto \&XSLoader::bootstrap_inherit;
73bf7552
AT
151 }
152
4c7a0d98 153 goto \&XSLoader::bootstrap_inherit if not -f $file;
9cf41c4d
GS
154
155 my $bootname = "boot_$module";
156 $bootname =~ s/\W/_/g;
11fd7d05 157 @DynaLoader::dl_require_symbols = ($bootname);
9cf41c4d 158
588cafc8
DM
159 my $boot_symbol_ref;
160
68d3ba50
VK
161EOT
162
588cafc8 163 if ($^O eq 'darwin') {
c35721ed
RU
164 my $extra_arg = ', 1 ' if $DynaLoader::VERSION ge '1.37';
165print OUT <<"EOT";
166 if (\$boot_symbol_ref = dl_find_symbol( 0, \$bootname $extra_arg)) {
167 goto boot; #extension library has already been loaded, e.g. darwin
168 }
68d3ba50 169EOT
588cafc8
DM
170 }
171
68d3ba50 172print OUT <<'EOT';
9cf41c4d
GS
173 # Many dynamic extension loading problems will appear to come from
174 # this section of code: XYZ failed at line 123 of DynaLoader.pm.
175 # Often these errors are actually occurring in the initialisation
176 # C code of the extension XS file. Perl reports the error as being
177 # in this perl code simply because this was the last perl code
178 # it executed.
179
180 my $libref = dl_load_file($file, 0) or do {
11fd7d05
RGS
181 require Carp;
182 Carp::croak("Can't load '$file' for module $module: " . dl_error());
9cf41c4d 183 };
11fd7d05 184 push(@DynaLoader::dl_librefs,$libref); # record loaded object
9cf41c4d 185
f24427cc
DD
186EOT
187my $dlsrc = $Config{dlsrc};
188if ($dlsrc eq 'dl_freemint.xs' || $dlsrc eq 'dl_dld.xs') {
189 print OUT <<'EOT';
9cf41c4d
GS
190 my @unresolved = dl_undef_symbols();
191 if (@unresolved) {
11fd7d05
RGS
192 require Carp;
193 Carp::carp("Undefined symbols present after loading $file: @unresolved\n");
9cf41c4d
GS
194 }
195
f24427cc
DD
196EOT
197}
198
199print OUT <<'EOT';
588cafc8 200 $boot_symbol_ref = dl_find_symbol($libref, $bootname) or do {
11fd7d05
RGS
201 require Carp;
202 Carp::croak("Can't find '$bootname' symbol in $file\n");
9cf41c4d
GS
203 };
204
11fd7d05 205 push(@DynaLoader::dl_modules, $module); # record loaded module
9cf41c4d 206
588cafc8 207 boot:
73bf7552 208 my $xs = dl_install_xsub($boots, $boot_symbol_ref, $file);
588cafc8 209
9cf41c4d 210 # See comment block above
89166d32 211 push(@DynaLoader::dl_shared_objects, $file); # record files loaded
9cf41c4d 212 return &$xs(@_);
6e0eede9
NC
213}
214EOT
215
e6ea8c3b 216# Can't test with DynaLoader->can('bootstrap_inherit') when building in the
6e0eede9 217# core, as XSLoader gets built before DynaLoader.
9cf41c4d 218
6e0eede9
NC
219if ($] >= 5.006) {
220 print OUT <<'EOT';
221
222sub bootstrap_inherit {
223 require DynaLoader;
224 goto \&DynaLoader::bootstrap_inherit;
11fd7d05
RGS
225}
226
6e0eede9
NC
227EOT
228} else {
229 print OUT <<'EOT';
230
11fd7d05 231sub bootstrap_inherit {
6e0eede9 232 # Versions of DynaLoader prior to 5.6.0 don't have bootstrap_inherit.
11fd7d05
RGS
233 package DynaLoader;
234
235 my $module = $_[0];
236 local *DynaLoader::isa = *{"$module\::ISA"};
237 local @DynaLoader::isa = (@DynaLoader::isa, 'DynaLoader');
238 # Cannot goto due to delocalization. Will report errors on a wrong line?
9cf41c4d 239 require DynaLoader;
11fd7d05 240 DynaLoader::bootstrap(@_);
9cf41c4d
GS
241}
242
6e0eede9
NC
243EOT
244}
245
246print OUT <<'EOT';
9e8c31cc
MS
2471;
248
11fd7d05 249
9cf41c4d
GS
250__END__
251
252=head1 NAME
253
254XSLoader - Dynamically load C libraries into Perl code
255
11fd7d05
RGS
256=head1 VERSION
257
ae635bbf 258Version 0.22
11fd7d05 259
9cf41c4d
GS
260=head1 SYNOPSIS
261
262 package YourPackage;
1d2b7ec5 263 require XSLoader;
9cf41c4d 264
1d2b7ec5 265 XSLoader::load();
9cf41c4d
GS
266
267=head1 DESCRIPTION
268
269This module defines a standard I<simplified> interface to the dynamic
270linking mechanisms available on many platforms. Its primary purpose is
271to implement cheap automatic dynamic loading of Perl modules.
272
4406dda9 273For a more complicated interface, see L<DynaLoader>. Many (most)
11fd7d05
RGS
274features of C<DynaLoader> are not implemented in C<XSLoader>, like for
275example the C<dl_load_flags>, not honored by C<XSLoader>.
9cf41c4d 276
d7f44de2
IZ
277=head2 Migration from C<DynaLoader>
278
279A typical module using L<DynaLoader|DynaLoader> starts like this:
280
281 package YourPackage;
282 require DynaLoader;
283
284 our @ISA = qw( OnePackage OtherPackage DynaLoader );
285 our $VERSION = '0.01';
286 bootstrap YourPackage $VERSION;
287
288Change this to
289
290 package YourPackage;
291 use XSLoader;
292
293 our @ISA = qw( OnePackage OtherPackage );
294 our $VERSION = '0.01';
295 XSLoader::load 'YourPackage', $VERSION;
296
297In other words: replace C<require DynaLoader> by C<use XSLoader>, remove
11fd7d05 298C<DynaLoader> from C<@ISA>, change C<bootstrap> by C<XSLoader::load>. Do not
d7f44de2 299forget to quote the name of your package on the C<XSLoader::load> line,
9c6b46e2 300and add comma (C<,>) before the arguments (C<$VERSION> above).
d7f44de2 301
11fd7d05
RGS
302Of course, if C<@ISA> contained only C<DynaLoader>, there is no need to have
303the C<@ISA> assignment at all; moreover, if instead of C<our> one uses the
304more backward-compatible
d7f44de2
IZ
305
306 use vars qw($VERSION @ISA);
307
11fd7d05 308one can remove this reference to C<@ISA> together with the C<@ISA> assignment.
d7f44de2 309
11fd7d05 310If no C<$VERSION> was specified on the C<bootstrap> line, the last line becomes
d7f44de2
IZ
311
312 XSLoader::load 'YourPackage';
313
681a49bf 314If the call to C<load> is from C<YourPackage>, then that can be further
1d2b7ec5
NC
315simplified to
316
317 XSLoader::load();
318
319as C<load> will use C<caller> to determine the package.
320
d7f44de2
IZ
321=head2 Backward compatible boilerplate
322
323If you want to have your cake and eat it too, you need a more complicated
324boilerplate.
325
326 package YourPackage;
327 use vars qw($VERSION @ISA);
328
329 @ISA = qw( OnePackage OtherPackage );
330 $VERSION = '0.01';
331 eval {
332 require XSLoader;
333 XSLoader::load('YourPackage', $VERSION);
334 1;
335 } or do {
336 require DynaLoader;
337 push @ISA, 'DynaLoader';
338 bootstrap YourPackage $VERSION;
339 };
340
11fd7d05 341The parentheses about C<XSLoader::load()> arguments are needed since we replaced
d7f44de2 342C<use XSLoader> by C<require>, so the compiler does not know that a function
11fd7d05 343C<XSLoader::load()> is present.
d7f44de2
IZ
344
345This boilerplate uses the low-overhead C<XSLoader> if present; if used with
681a49bf 346an antique Perl which has no C<XSLoader>, it falls back to using C<DynaLoader>.
d7f44de2
IZ
347
348=head1 Order of initialization: early load()
349
350I<Skip this section if the XSUB functions are supposed to be called from other
351modules only; read it only if you call your XSUBs from the code in your module,
352or have a C<BOOT:> section in your XS file (see L<perlxs/"The BOOT: Keyword">).
4406dda9 353What is described here is equally applicable to the L<DynaLoader|DynaLoader>
d7f44de2
IZ
354interface.>
355
356A sufficiently complicated module using XS would have both Perl code (defined
357in F<YourPackage.pm>) and XS code (defined in F<YourPackage.xs>). If this
358Perl code makes calls into this XS code, and/or this XS code makes calls to
359the Perl code, one should be careful with the order of initialization.
360
3a1b0858
NC
361The call to C<XSLoader::load()> (or C<bootstrap()>) calls the module's
362bootstrap code. For modules build by F<xsubpp> (nearly all modules) this
363has three side effects:
d7f44de2
IZ
364
365=over
366
367=item *
368
3a1b0858
NC
369A sanity check is done to ensure that the versions of the F<.pm> and the
370(compiled) F<.xs> parts are compatible. If C<$VERSION> was specified, this
371is used for the check. If not specified, it defaults to
372C<$XS_VERSION // $VERSION> (in the module's namespace)
d7f44de2
IZ
373
374=item *
375
3a1b0858 376the XSUBs are made accessible from Perl
d7f44de2
IZ
377
378=item *
379
4406dda9 380if a C<BOOT:> section was present in the F<.xs> file, the code there is called.
d7f44de2
IZ
381
382=back
383
4406dda9 384Consequently, if the code in the F<.pm> file makes calls to these XSUBs, it is
d7f44de2
IZ
385convenient to have XSUBs installed before the Perl code is defined; for
386example, this makes prototypes for XSUBs visible to this Perl code.
387Alternatively, if the C<BOOT:> section makes calls to Perl functions (or
4406dda9 388uses Perl variables) defined in the F<.pm> file, they must be defined prior to
11fd7d05 389the call to C<XSLoader::load()> (or C<bootstrap()>).
d7f44de2
IZ
390
391The first situation being much more frequent, it makes sense to rewrite the
392boilerplate as
393
394 package YourPackage;
395 use XSLoader;
396 use vars qw($VERSION @ISA);
397
398 BEGIN {
399 @ISA = qw( OnePackage OtherPackage );
400 $VERSION = '0.01';
401
402 # Put Perl code used in the BOOT: section here
403
404 XSLoader::load 'YourPackage', $VERSION;
405 }
406
407 # Put Perl code making calls into XSUBs here
408
409=head2 The most hairy case
410
411If the interdependence of your C<BOOT:> section and Perl code is
412more complicated than this (e.g., the C<BOOT:> section makes calls to Perl
413functions which make calls to XSUBs with prototypes), get rid of the C<BOOT:>
11fd7d05 414section altogether. Replace it with a function C<onBOOT()>, and call it like
d7f44de2
IZ
415this:
416
417 package YourPackage;
418 use XSLoader;
419 use vars qw($VERSION @ISA);
420
421 BEGIN {
422 @ISA = qw( OnePackage OtherPackage );
423 $VERSION = '0.01';
424 XSLoader::load 'YourPackage', $VERSION;
425 }
426
427 # Put Perl code used in onBOOT() function here; calls to XSUBs are
428 # prototype-checked.
429
430 onBOOT;
431
432 # Put Perl initialization code assuming that XS is initialized here
433
11fd7d05
RGS
434
435=head1 DIAGNOSTICS
436
150e77ce 437=over
11fd7d05 438
150e77ce 439=item C<Can't find '%s' symbol in %s>
11fd7d05
RGS
440
441B<(F)> The bootstrap symbol could not be found in the extension module.
442
150e77ce 443=item C<Can't load '%s' for module %s: %s>
11fd7d05
RGS
444
445B<(F)> The loading or initialisation of the extension module failed.
446The detailed error follows.
447
150e77ce 448=item C<Undefined symbols present after loading %s: %s>
11fd7d05
RGS
449
450B<(W)> As the message says, some symbols stay undefined although the
451extension module was correctly loaded and initialised. The list of undefined
452symbols follows.
453
11fd7d05
RGS
454=back
455
d7f44de2
IZ
456=head1 LIMITATIONS
457
458To reduce the overhead as much as possible, only one possible location
459is checked to find the extension DLL (this location is where C<make install>
460would put the DLL). If not found, the search for the DLL is transparently
11fd7d05 461delegated to C<DynaLoader>, which looks for the DLL along the C<@INC> list.
d7f44de2 462
11fd7d05 463In particular, this is applicable to the structure of C<@INC> used for testing
4406dda9
RGS
464not-yet-installed extensions. This means that running uninstalled extensions
465may have much more overhead than running the same extensions after
d7f44de2
IZ
466C<make install>.
467
11fd7d05 468
e6ea8c3b
CBW
469=head1 KNOWN BUGS
470
471The new simpler way to call C<XSLoader::load()> with no arguments at all
472does not work on Perl 5.8.4 and 5.8.5.
473
474
11fd7d05
RGS
475=head1 BUGS
476
9c6b46e2 477Please report any bugs or feature requests via the perlbug(1) utility.
11fd7d05
RGS
478
479
480=head1 SEE ALSO
481
482L<DynaLoader>
483
484
9c6b46e2 485=head1 AUTHORS
9cf41c4d 486
11fd7d05
RGS
487Ilya Zakharevich originally extracted C<XSLoader> from C<DynaLoader>.
488
489CPAN version is currently maintained by SE<eacute>bastien Aperghis-Tramoni
150e77ce 490E<lt>sebastien@aperghis.netE<gt>.
11fd7d05 491
150e77ce 492Previous maintainer was Michael G Schwern <schwern@pobox.com>.
11fd7d05
RGS
493
494
73bf7552
AT
495=head1 COPYRIGHT & LICENSE
496
e6ea8c3b 497Copyright (C) 1990-2011 by Larry Wall and others.
11fd7d05
RGS
498
499This program is free software; you can redistribute it and/or modify
500it under the same terms as Perl itself.
9cf41c4d
GS
501
502=cut
503EOT
504
505close OUT or die $!;