DB_File 1.817
[p5sagit/p5-mst-13.2.git] / ext / DynaLoader / XSLoader_pm.PL
CommitLineData
11fd7d05 1use strict;
9cf41c4d 2use Config;
3
4sub to_string {
5 my ($value) = @_;
6 $value =~ s/\\/\\\\/g;
7 $value =~ s/'/\\'/g;
8 return "'$value'";
9}
10
11fd7d05 111 while unlink "XSLoader.pm";
9cf41c4d 12open OUT, ">XSLoader.pm" or die $!;
13print OUT <<'EOT';
14# Generated from XSLoader.pm.PL (resolved %Config::Config value)
15
16package XSLoader;
17
b0d07b4e 18$VERSION = "0.08";
11fd7d05 19
20#use strict;
9cf41c4d 21
22# enable debug/trace messages from DynaLoader perl code
23# $dl_debug = $ENV{PERL_DL_DEBUG} || 0 unless defined $dl_debug;
24
25EOT
26
27print OUT ' my $dl_dlext = ', to_string($Config::Config{'dlext'}), ";\n" ;
28
29print OUT <<'EOT';
30
9cf41c4d 31package DynaLoader;
b4cea227 32
33# No prizes for guessing why we don't say 'bootstrap DynaLoader;' here.
34# NOTE: All dl_*.xs (including dl_none.xs) define a dl_error() XSUB
9cf41c4d 35boot_DynaLoader('DynaLoader') if defined(&boot_DynaLoader) &&
b4cea227 36 !defined(&dl_error);
9cf41c4d 37package XSLoader;
38
9cf41c4d 39sub load {
40 package DynaLoader;
41
9e8c31cc 42 die q{XSLoader::load('Your::Module', $Your::Module::VERSION)} unless @_;
43
9cf41c4d 44 my($module) = $_[0];
45
46 # work with static linking too
47 my $b = "$module\::bootstrap";
48 goto &$b if defined &$b;
49
50 goto retry unless $module and defined &dl_load_file;
51
52 my @modparts = split(/::/,$module);
53 my $modfname = $modparts[-1];
54
55EOT
56
57print OUT <<'EOT' if defined &DynaLoader::mod2fname;
58 # Some systems have restrictions on files names for DLL's etc.
59 # mod2fname returns appropriate file base name (typically truncated)
60 # It may also edit @modparts if required.
61 $modfname = &mod2fname(\@modparts) if defined &mod2fname;
62
63EOT
64
9d419b5f 65print OUT <<'EOT' if $^O eq 'os2';
66
67 # os2 static build can dynaload, but cannot dynaload Perl modules...
68 die 'Dynaloaded Perl modules are not available in this build of Perl' if $OS2::is_static;
69
70EOT
b0d07b4e 71
9cf41c4d 72print OUT <<'EOT';
73 my $modpname = join('/',@modparts);
74 my $modlibname = (caller())[1];
75 my $c = @modparts;
76 $modlibname =~ s,[\\/][^\\/]+$,, while $c--; # Q&D basename
77 my $file = "$modlibname/auto/$modpname/$modfname.$dl_dlext";
78
79# print STDERR "XSLoader::load for $module ($file)\n" if $dl_debug;
80
81 my $bs = $file;
82 $bs =~ s/(\.\w+)?(;\d*)?$/\.bs/; # look for .bs 'beside' the library
83
84 goto retry if not -f $file or -s $bs;
85
86 my $bootname = "boot_$module";
87 $bootname =~ s/\W/_/g;
11fd7d05 88 @DynaLoader::dl_require_symbols = ($bootname);
9cf41c4d 89
588cafc8 90 my $boot_symbol_ref;
91
68d3ba50 92EOT
93
588cafc8 94 if ($^O eq 'darwin') {
68d3ba50 95print OUT <<'EOT';
588cafc8 96 if ($boot_symbol_ref = dl_find_symbol(0, $bootname)) {
97 goto boot; #extension library has already been loaded, e.g. darwin
98 }
68d3ba50 99EOT
588cafc8 100 }
101
68d3ba50 102print OUT <<'EOT';
9cf41c4d 103 # Many dynamic extension loading problems will appear to come from
104 # this section of code: XYZ failed at line 123 of DynaLoader.pm.
105 # Often these errors are actually occurring in the initialisation
106 # C code of the extension XS file. Perl reports the error as being
107 # in this perl code simply because this was the last perl code
108 # it executed.
109
110 my $libref = dl_load_file($file, 0) or do {
11fd7d05 111 require Carp;
112 Carp::croak("Can't load '$file' for module $module: " . dl_error());
9cf41c4d 113 };
11fd7d05 114 push(@DynaLoader::dl_librefs,$libref); # record loaded object
9cf41c4d 115
116 my @unresolved = dl_undef_symbols();
117 if (@unresolved) {
11fd7d05 118 require Carp;
119 Carp::carp("Undefined symbols present after loading $file: @unresolved\n");
9cf41c4d 120 }
121
588cafc8 122 $boot_symbol_ref = dl_find_symbol($libref, $bootname) or do {
11fd7d05 123 require Carp;
124 Carp::croak("Can't find '$bootname' symbol in $file\n");
9cf41c4d 125 };
126
11fd7d05 127 push(@DynaLoader::dl_modules, $module); # record loaded module
9cf41c4d 128
588cafc8 129 boot:
130 my $xs = dl_install_xsub("${module}::bootstrap", $boot_symbol_ref, $file);
131
9cf41c4d 132 # See comment block above
89166d32 133 push(@DynaLoader::dl_shared_objects, $file); # record files loaded
9cf41c4d 134 return &$xs(@_);
135
136 retry:
11fd7d05 137 my $bootstrap_inherit = DynaLoader->can('bootstrap_inherit') ||
138 XSLoader->can('bootstrap_inherit');
139 goto &$bootstrap_inherit;
140}
141
142# Versions of DynaLoader prior to 5.6.0 don't have this function.
143sub bootstrap_inherit {
144 package DynaLoader;
145
146 my $module = $_[0];
147 local *DynaLoader::isa = *{"$module\::ISA"};
148 local @DynaLoader::isa = (@DynaLoader::isa, 'DynaLoader');
149 # Cannot goto due to delocalization. Will report errors on a wrong line?
9cf41c4d 150 require DynaLoader;
11fd7d05 151 DynaLoader::bootstrap(@_);
9cf41c4d 152}
153
9e8c31cc 1541;
155
11fd7d05 156
9cf41c4d 157__END__
158
159=head1 NAME
160
161XSLoader - Dynamically load C libraries into Perl code
162
11fd7d05 163=head1 VERSION
164
b0d07b4e 165Version 0.08
11fd7d05 166
9cf41c4d 167=head1 SYNOPSIS
168
169 package YourPackage;
170 use XSLoader;
171
9e8c31cc 172 XSLoader::load 'YourPackage', $YourPackage::VERSION;
9cf41c4d 173
174=head1 DESCRIPTION
175
176This module defines a standard I<simplified> interface to the dynamic
177linking mechanisms available on many platforms. Its primary purpose is
178to implement cheap automatic dynamic loading of Perl modules.
179
4406dda9 180For a more complicated interface, see L<DynaLoader>. Many (most)
11fd7d05 181features of C<DynaLoader> are not implemented in C<XSLoader>, like for
182example the C<dl_load_flags>, not honored by C<XSLoader>.
9cf41c4d 183
d7f44de2 184=head2 Migration from C<DynaLoader>
185
186A typical module using L<DynaLoader|DynaLoader> starts like this:
187
188 package YourPackage;
189 require DynaLoader;
190
191 our @ISA = qw( OnePackage OtherPackage DynaLoader );
192 our $VERSION = '0.01';
193 bootstrap YourPackage $VERSION;
194
195Change this to
196
197 package YourPackage;
198 use XSLoader;
199
200 our @ISA = qw( OnePackage OtherPackage );
201 our $VERSION = '0.01';
202 XSLoader::load 'YourPackage', $VERSION;
203
204In other words: replace C<require DynaLoader> by C<use XSLoader>, remove
11fd7d05 205C<DynaLoader> from C<@ISA>, change C<bootstrap> by C<XSLoader::load>. Do not
d7f44de2 206forget to quote the name of your package on the C<XSLoader::load> line,
9c6b46e2 207and add comma (C<,>) before the arguments (C<$VERSION> above).
d7f44de2 208
11fd7d05 209Of course, if C<@ISA> contained only C<DynaLoader>, there is no need to have
210the C<@ISA> assignment at all; moreover, if instead of C<our> one uses the
211more backward-compatible
d7f44de2 212
213 use vars qw($VERSION @ISA);
214
11fd7d05 215one can remove this reference to C<@ISA> together with the C<@ISA> assignment.
d7f44de2 216
11fd7d05 217If no C<$VERSION> was specified on the C<bootstrap> line, the last line becomes
d7f44de2 218
219 XSLoader::load 'YourPackage';
220
221=head2 Backward compatible boilerplate
222
223If you want to have your cake and eat it too, you need a more complicated
224boilerplate.
225
226 package YourPackage;
227 use vars qw($VERSION @ISA);
228
229 @ISA = qw( OnePackage OtherPackage );
230 $VERSION = '0.01';
231 eval {
232 require XSLoader;
233 XSLoader::load('YourPackage', $VERSION);
234 1;
235 } or do {
236 require DynaLoader;
237 push @ISA, 'DynaLoader';
238 bootstrap YourPackage $VERSION;
239 };
240
11fd7d05 241The parentheses about C<XSLoader::load()> arguments are needed since we replaced
d7f44de2 242C<use XSLoader> by C<require>, so the compiler does not know that a function
11fd7d05 243C<XSLoader::load()> is present.
d7f44de2 244
245This boilerplate uses the low-overhead C<XSLoader> if present; if used with
246an antic Perl which has no C<XSLoader>, it falls back to using C<DynaLoader>.
247
248=head1 Order of initialization: early load()
249
250I<Skip this section if the XSUB functions are supposed to be called from other
251modules only; read it only if you call your XSUBs from the code in your module,
252or have a C<BOOT:> section in your XS file (see L<perlxs/"The BOOT: Keyword">).
4406dda9 253What is described here is equally applicable to the L<DynaLoader|DynaLoader>
d7f44de2 254interface.>
255
256A sufficiently complicated module using XS would have both Perl code (defined
257in F<YourPackage.pm>) and XS code (defined in F<YourPackage.xs>). If this
258Perl code makes calls into this XS code, and/or this XS code makes calls to
259the Perl code, one should be careful with the order of initialization.
260
11fd7d05 261The call to C<XSLoader::load()> (or C<bootstrap()>) has three side effects:
d7f44de2 262
263=over
264
265=item *
266
11fd7d05 267if C<$VERSION> was specified, a sanity check is done to ensure that the
268versions of the F<.pm> and the (compiled) F<.xs> parts are compatible;
d7f44de2 269
270=item *
271
4406dda9 272the XSUBs are made accessible from Perl;
d7f44de2 273
274=item *
275
4406dda9 276if a C<BOOT:> section was present in the F<.xs> file, the code there is called.
d7f44de2 277
278=back
279
4406dda9 280Consequently, if the code in the F<.pm> file makes calls to these XSUBs, it is
d7f44de2 281convenient to have XSUBs installed before the Perl code is defined; for
282example, this makes prototypes for XSUBs visible to this Perl code.
283Alternatively, if the C<BOOT:> section makes calls to Perl functions (or
4406dda9 284uses Perl variables) defined in the F<.pm> file, they must be defined prior to
11fd7d05 285the call to C<XSLoader::load()> (or C<bootstrap()>).
d7f44de2 286
287The first situation being much more frequent, it makes sense to rewrite the
288boilerplate as
289
290 package YourPackage;
291 use XSLoader;
292 use vars qw($VERSION @ISA);
293
294 BEGIN {
295 @ISA = qw( OnePackage OtherPackage );
296 $VERSION = '0.01';
297
298 # Put Perl code used in the BOOT: section here
299
300 XSLoader::load 'YourPackage', $VERSION;
301 }
302
303 # Put Perl code making calls into XSUBs here
304
305=head2 The most hairy case
306
307If the interdependence of your C<BOOT:> section and Perl code is
308more complicated than this (e.g., the C<BOOT:> section makes calls to Perl
309functions which make calls to XSUBs with prototypes), get rid of the C<BOOT:>
11fd7d05 310section altogether. Replace it with a function C<onBOOT()>, and call it like
d7f44de2 311this:
312
313 package YourPackage;
314 use XSLoader;
315 use vars qw($VERSION @ISA);
316
317 BEGIN {
318 @ISA = qw( OnePackage OtherPackage );
319 $VERSION = '0.01';
320 XSLoader::load 'YourPackage', $VERSION;
321 }
322
323 # Put Perl code used in onBOOT() function here; calls to XSUBs are
324 # prototype-checked.
325
326 onBOOT;
327
328 # Put Perl initialization code assuming that XS is initialized here
329
11fd7d05 330
331=head1 DIAGNOSTICS
332
150e77ce 333=over
11fd7d05 334
150e77ce 335=item C<Can't find '%s' symbol in %s>
11fd7d05 336
337B<(F)> The bootstrap symbol could not be found in the extension module.
338
150e77ce 339=item C<Can't load '%s' for module %s: %s>
11fd7d05 340
341B<(F)> The loading or initialisation of the extension module failed.
342The detailed error follows.
343
150e77ce 344=item C<Undefined symbols present after loading %s: %s>
11fd7d05 345
346B<(W)> As the message says, some symbols stay undefined although the
347extension module was correctly loaded and initialised. The list of undefined
348symbols follows.
349
150e77ce 350=item C<XSLoader::load('Your::Module', $Your::Module::VERSION)>
11fd7d05 351
352B<(F)> You tried to invoke C<load()> without any argument. You must supply
353a module name, and optionally its version.
354
355=back
356
357
d7f44de2 358=head1 LIMITATIONS
359
360To reduce the overhead as much as possible, only one possible location
361is checked to find the extension DLL (this location is where C<make install>
362would put the DLL). If not found, the search for the DLL is transparently
11fd7d05 363delegated to C<DynaLoader>, which looks for the DLL along the C<@INC> list.
d7f44de2 364
11fd7d05 365In particular, this is applicable to the structure of C<@INC> used for testing
4406dda9 366not-yet-installed extensions. This means that running uninstalled extensions
367may have much more overhead than running the same extensions after
d7f44de2 368C<make install>.
369
11fd7d05 370
371=head1 BUGS
372
9c6b46e2 373Please report any bugs or feature requests via the perlbug(1) utility.
11fd7d05 374
375
376=head1 SEE ALSO
377
378L<DynaLoader>
379
380
9c6b46e2 381=head1 AUTHORS
9cf41c4d 382
11fd7d05 383Ilya Zakharevich originally extracted C<XSLoader> from C<DynaLoader>.
384
385CPAN version is currently maintained by SE<eacute>bastien Aperghis-Tramoni
150e77ce 386E<lt>sebastien@aperghis.netE<gt>.
11fd7d05 387
150e77ce 388Previous maintainer was Michael G Schwern <schwern@pobox.com>.
11fd7d05 389
390
391=head1 COPYRIGHT
392
393This program is free software; you can redistribute it and/or modify
394it under the same terms as Perl itself.
9cf41c4d 395
396=cut
397EOT
398
399close OUT or die $!;