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