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