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