Commit | Line | Data |
9cf41c4d |
1 | use Config; |
2 | |
3 | sub to_string { |
4 | my ($value) = @_; |
5 | $value =~ s/\\/\\\\/g; |
6 | $value =~ s/'/\\'/g; |
7 | return "'$value'"; |
8 | } |
9 | |
10 | unlink "XSLoader.pm" if -f "XSLoader.pm"; |
11 | open OUT, ">XSLoader.pm" or die $!; |
12 | print OUT <<'EOT'; |
13 | # Generated from XSLoader.pm.PL (resolved %Config::Config value) |
14 | |
15 | package XSLoader; |
16 | |
17 | # And Gandalf said: 'Many folk like to know beforehand what is to |
18 | # be set on the table; but those who have laboured to prepare the |
19 | # feast like to keep their secret; for wonder makes the words of |
20 | # praise louder.' |
21 | |
22 | # (Quote from Tolkien sugested by Anno Siegel.) |
23 | # |
24 | # See pod text at end of file for documentation. |
25 | # See also ext/DynaLoader/README in source tree for other information. |
26 | # |
27 | # Tim.Bunce@ig.co.uk, August 1994 |
28 | |
29 | $VERSION = "0.01"; # avoid typo warning |
30 | |
31 | # enable debug/trace messages from DynaLoader perl code |
32 | # $dl_debug = $ENV{PERL_DL_DEBUG} || 0 unless defined $dl_debug; |
33 | |
34 | EOT |
35 | |
36 | print OUT ' my $dl_dlext = ', to_string($Config::Config{'dlext'}), ";\n" ; |
37 | |
38 | print OUT <<'EOT'; |
39 | |
9cf41c4d |
40 | package DynaLoader; |
b4cea227 |
41 | |
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 |
9cf41c4d |
44 | boot_DynaLoader('DynaLoader') if defined(&boot_DynaLoader) && |
b4cea227 |
45 | !defined(&dl_error); |
9cf41c4d |
46 | package XSLoader; |
47 | |
48 | 1; # End of main code |
49 | |
50 | # The bootstrap function cannot be autoloaded (without complications) |
51 | # so we define it here: |
52 | |
53 | sub load { |
54 | package DynaLoader; |
55 | |
56 | my($module) = $_[0]; |
57 | |
58 | # work with static linking too |
59 | my $b = "$module\::bootstrap"; |
60 | goto &$b if defined &$b; |
61 | |
62 | goto retry unless $module and defined &dl_load_file; |
63 | |
64 | my @modparts = split(/::/,$module); |
65 | my $modfname = $modparts[-1]; |
66 | |
67 | EOT |
68 | |
69 | print OUT <<'EOT' if defined &DynaLoader::mod2fname; |
70 | # Some systems have restrictions on files names for DLL's etc. |
71 | # mod2fname returns appropriate file base name (typically truncated) |
72 | # It may also edit @modparts if required. |
73 | $modfname = &mod2fname(\@modparts) if defined &mod2fname; |
74 | |
75 | EOT |
76 | |
77 | print OUT <<'EOT'; |
78 | my $modpname = join('/',@modparts); |
79 | my $modlibname = (caller())[1]; |
80 | my $c = @modparts; |
81 | $modlibname =~ s,[\\/][^\\/]+$,, while $c--; # Q&D basename |
82 | my $file = "$modlibname/auto/$modpname/$modfname.$dl_dlext"; |
83 | |
84 | # print STDERR "XSLoader::load for $module ($file)\n" if $dl_debug; |
85 | |
86 | my $bs = $file; |
87 | $bs =~ s/(\.\w+)?(;\d*)?$/\.bs/; # look for .bs 'beside' the library |
88 | |
89 | goto retry if not -f $file or -s $bs; |
90 | |
91 | my $bootname = "boot_$module"; |
92 | $bootname =~ s/\W/_/g; |
93 | @dl_require_symbols = ($bootname); |
94 | |
588cafc8 |
95 | my $boot_symbol_ref; |
96 | |
97 | if ($^O eq 'darwin') { |
98 | if ($boot_symbol_ref = dl_find_symbol(0, $bootname)) { |
99 | goto boot; #extension library has already been loaded, e.g. darwin |
100 | } |
101 | } |
102 | |
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 { |
111 | require Carp; |
112 | Carp::croak("Can't load '$file' for module $module: " . dl_error()); |
113 | }; |
114 | push(@dl_librefs,$libref); # record loaded object |
115 | |
116 | my @unresolved = dl_undef_symbols(); |
117 | if (@unresolved) { |
118 | require Carp; |
119 | Carp::carp("Undefined symbols present after loading $file: @unresolved\n"); |
120 | } |
121 | |
588cafc8 |
122 | $boot_symbol_ref = dl_find_symbol($libref, $bootname) or do { |
9cf41c4d |
123 | require Carp; |
124 | Carp::croak("Can't find '$bootname' symbol in $file\n"); |
125 | }; |
126 | |
9cf41c4d |
127 | push(@dl_modules, $module); # record loaded module |
128 | |
588cafc8 |
129 | boot: |
130 | my $xs = dl_install_xsub("${module}::bootstrap", $boot_symbol_ref, $file); |
131 | |
9cf41c4d |
132 | # See comment block above |
133 | return &$xs(@_); |
134 | |
135 | retry: |
136 | require DynaLoader; |
137 | goto &DynaLoader::bootstrap_inherit; |
138 | } |
139 | |
140 | __END__ |
141 | |
142 | =head1 NAME |
143 | |
144 | XSLoader - Dynamically load C libraries into Perl code |
145 | |
146 | =head1 SYNOPSIS |
147 | |
148 | package YourPackage; |
149 | use XSLoader; |
150 | |
151 | XSLoader::load 'YourPackage', @args; |
152 | |
153 | =head1 DESCRIPTION |
154 | |
155 | This module defines a standard I<simplified> interface to the dynamic |
156 | linking mechanisms available on many platforms. Its primary purpose is |
157 | to implement cheap automatic dynamic loading of Perl modules. |
158 | |
09f1aa21 |
159 | For more complicated interface see L<DynaLoader>. Many (most) |
160 | features of DynaLoader are not implemented in XSLoader, like for |
161 | example the dl_load_flags is not honored by XSLoader. |
9cf41c4d |
162 | |
d7f44de2 |
163 | =head2 Migration from C<DynaLoader> |
164 | |
165 | A typical module using L<DynaLoader|DynaLoader> starts like this: |
166 | |
167 | package YourPackage; |
168 | require DynaLoader; |
169 | |
170 | our @ISA = qw( OnePackage OtherPackage DynaLoader ); |
171 | our $VERSION = '0.01'; |
172 | bootstrap YourPackage $VERSION; |
173 | |
174 | Change this to |
175 | |
176 | package YourPackage; |
177 | use XSLoader; |
178 | |
179 | our @ISA = qw( OnePackage OtherPackage ); |
180 | our $VERSION = '0.01'; |
181 | XSLoader::load 'YourPackage', $VERSION; |
182 | |
183 | In other words: replace C<require DynaLoader> by C<use XSLoader>, remove |
184 | C<DynaLoader> from @ISA, change C<bootstrap> by C<XSLoader::load>. Do not |
185 | forget to quote the name of your package on the C<XSLoader::load> line, |
186 | and add comma (C<,>) before the arguments ($VERSION above). |
187 | |
188 | Of course, if @ISA contained only C<DynaLoader>, there is no need to have the |
189 | @ISA assignment at all; moreover, if instead of C<our> one uses |
190 | backward-compatible |
191 | |
192 | use vars qw($VERSION @ISA); |
193 | |
194 | one can remove this reference to @ISA together with the @ISA assignment |
195 | |
196 | If no $VERSION was specified on the C<bootstrap> line, the last line becomes |
197 | |
198 | XSLoader::load 'YourPackage'; |
199 | |
200 | =head2 Backward compatible boilerplate |
201 | |
202 | If you want to have your cake and eat it too, you need a more complicated |
203 | boilerplate. |
204 | |
205 | package YourPackage; |
206 | use vars qw($VERSION @ISA); |
207 | |
208 | @ISA = qw( OnePackage OtherPackage ); |
209 | $VERSION = '0.01'; |
210 | eval { |
211 | require XSLoader; |
212 | XSLoader::load('YourPackage', $VERSION); |
213 | 1; |
214 | } or do { |
215 | require DynaLoader; |
216 | push @ISA, 'DynaLoader'; |
217 | bootstrap YourPackage $VERSION; |
218 | }; |
219 | |
220 | The parentheses about XSLoader::load() arguments are needed since we replaced |
221 | C<use XSLoader> by C<require>, so the compiler does not know that a function |
222 | XSLoader::load() is present. |
223 | |
224 | This boilerplate uses the low-overhead C<XSLoader> if present; if used with |
225 | an antic Perl which has no C<XSLoader>, it falls back to using C<DynaLoader>. |
226 | |
227 | =head1 Order of initialization: early load() |
228 | |
229 | I<Skip this section if the XSUB functions are supposed to be called from other |
230 | modules only; read it only if you call your XSUBs from the code in your module, |
231 | or have a C<BOOT:> section in your XS file (see L<perlxs/"The BOOT: Keyword">). |
232 | What is described here is equally applicable to L<DynaLoader|DynaLoader> |
233 | interface.> |
234 | |
235 | A sufficiently complicated module using XS would have both Perl code (defined |
236 | in F<YourPackage.pm>) and XS code (defined in F<YourPackage.xs>). If this |
237 | Perl code makes calls into this XS code, and/or this XS code makes calls to |
238 | the Perl code, one should be careful with the order of initialization. |
239 | |
240 | The call to XSLoader::load() (or bootstrap()) has three side effects: |
241 | |
242 | =over |
243 | |
244 | =item * |
245 | |
246 | if $VERSION was specified, a sanity check is done to insure that the versions |
247 | of the F<.pm> and the (compiled) F<.xs> parts are compatible; |
248 | |
249 | =item * |
250 | |
251 | The XSUBs are made accessible from Perl; |
252 | |
253 | =item * |
254 | |
255 | If the C<BOOT:> section was present in F<.xs> file, the code there is called. |
256 | |
257 | =back |
258 | |
259 | Consequently, if the code in F<.pm> file makes calls to these XSUBs, it is |
260 | convenient to have XSUBs installed before the Perl code is defined; for |
261 | example, this makes prototypes for XSUBs visible to this Perl code. |
262 | Alternatively, if the C<BOOT:> section makes calls to Perl functions (or |
263 | uses Perl variables) defined in F<.pm> file, they must be defined prior to |
264 | the call to XSLoader::load() (or bootstrap()). |
265 | |
266 | The first situation being much more frequent, it makes sense to rewrite the |
267 | boilerplate as |
268 | |
269 | package YourPackage; |
270 | use XSLoader; |
271 | use vars qw($VERSION @ISA); |
272 | |
273 | BEGIN { |
274 | @ISA = qw( OnePackage OtherPackage ); |
275 | $VERSION = '0.01'; |
276 | |
277 | # Put Perl code used in the BOOT: section here |
278 | |
279 | XSLoader::load 'YourPackage', $VERSION; |
280 | } |
281 | |
282 | # Put Perl code making calls into XSUBs here |
283 | |
284 | =head2 The most hairy case |
285 | |
286 | If the interdependence of your C<BOOT:> section and Perl code is |
287 | more complicated than this (e.g., the C<BOOT:> section makes calls to Perl |
288 | functions which make calls to XSUBs with prototypes), get rid of the C<BOOT:> |
289 | section altogether. Replace it with a function onBOOT(), and call it like |
290 | this: |
291 | |
292 | package YourPackage; |
293 | use XSLoader; |
294 | use vars qw($VERSION @ISA); |
295 | |
296 | BEGIN { |
297 | @ISA = qw( OnePackage OtherPackage ); |
298 | $VERSION = '0.01'; |
299 | XSLoader::load 'YourPackage', $VERSION; |
300 | } |
301 | |
302 | # Put Perl code used in onBOOT() function here; calls to XSUBs are |
303 | # prototype-checked. |
304 | |
305 | onBOOT; |
306 | |
307 | # Put Perl initialization code assuming that XS is initialized here |
308 | |
309 | =head1 LIMITATIONS |
310 | |
311 | To reduce the overhead as much as possible, only one possible location |
312 | is checked to find the extension DLL (this location is where C<make install> |
313 | would put the DLL). If not found, the search for the DLL is transparently |
314 | delegated to C<DynaLoader>, which looks for the DLL along the @INC list. |
315 | |
316 | In particular, this is applicable to the structure of @INC used for testing |
317 | not-yet-installed extensions. This means that the overhead of running |
318 | uninstalled extension may be much more than running the same extension after |
319 | C<make install>. |
320 | |
9cf41c4d |
321 | =head1 AUTHOR |
322 | |
323 | Ilya Zakharevich: extraction from DynaLoader. |
324 | |
325 | =cut |
326 | EOT |
327 | |
328 | close OUT or die $!; |
329 | |