Correct a Greengrocer's apostrophe.
[p5sagit/p5-mst-13.2.git] / ext / DynaLoader / DynaLoader_pm.PL
CommitLineData
03c9e98c 1use Config;
2
3sub to_string {
4 my ($value) = @_;
146174a9 5 $value =~ s/\\/\\\\/g;
03c9e98c 6 $value =~ s/'/\\'/g;
7 return "'$value'";
8}
9
1c7f9087 10#
11# subroutine expand_os_specific expands $^O-specific preprocessing information
12# so that it will not be re-calculated at runtime in Dynaloader.pm
13#
14# Syntax of preprocessor should be kept extremely simple:
15# - directives are in double angle brackets <<...>>
16# - <<=string>> will be just evaluated
17# - for $^O-specific there are two forms:
18# <<$^O-eq-osname>>
19# <<$^O-ne-osname>>
20# this directive should be closed with respectively
21# <</$^O-eq-osname>>
22# <</$^O-ne-osname>>
23# construct <<|$^O-ne-osname>> means #else
24# nested <<$^O...>>-constructs are allowed but nested values must be for
25# different OS-names!
26#
27# -- added by VKON, 03-10-2004 to separate $^O-specific between OSes
28# (so that Win32 never checks for $^O eq 'VMS' for example)
29sub expand_os_specific {
30 my $s = shift;
31 for ($s) {
32 s/<<=(.*?)>>/$1/gee;
33 s/<<\$\^O-(eq|ne)-(\w+)>>(.*?)<<\/\$\^O-\1-\2>>/
34 my ($op, $os, $expr) = ($1,$2,$3);
35 if ($op ne 'eq' and $op ne 'ne') {die "wrong eq-ne arg in $&"};
36 if ($expr =~ m[^(.*?)<<\|\$\^O-$op-$os>>(.*?)$]s) {
37 # #if;#else;#endif
38 my ($if,$el) = ($1,$2);
39 if (($op eq 'eq' and $^O eq $os) || ($op eq 'ne' and $^O ne $os)) {
40 $if
41 }
42 else {
43 $el
44 }
45 }
46 else {
47 # #if;#endif
48 if (($op eq 'eq' and $^O eq $os) || ($op eq 'ne' and $^O ne $os)) {
49 $expr
50 }
51 else {
52 ""
53 }
54 }
55 /ges;
56 if (/<<(=|\$\^O-)/) {die "bad <<\$^O-eq/ne-osname>> expression.".
57 " Unclosed brackets?";
58 }
59 }
60 $s;
61}
62
03c9e98c 63unlink "DynaLoader.pm" if -f "DynaLoader.pm";
64open OUT, ">DynaLoader.pm" or die $!;
65print OUT <<'EOT';
66
d0a5ed9f 67# Generated from DynaLoader.pm.PL
03c9e98c 68
a0d0e21e 69package DynaLoader;
70
8e07c86e 71# And Gandalf said: 'Many folk like to know beforehand what is to
72# be set on the table; but those who have laboured to prepare the
73# feast like to keep their secret; for wonder makes the words of
74# praise louder.'
75
6662521e 76# (Quote from Tolkien suggested by Anno Siegel.)
8e07c86e 77#
78# See pod text at end of file for documentation.
79# See also ext/DynaLoader/README in source tree for other information.
80#
81# Tim.Bunce@ig.co.uk, August 1994
82
632a9a7f 83use vars qw($VERSION *AUTOLOAD);
84
1c7f9087 85$VERSION = '1.06';
dc848c6f 86
dc848c6f 87require AutoLoader;
88*AUTOLOAD = \&AutoLoader::AUTOLOAD;
8e07c86e 89
7e9f3af8 90use Config;
91
fb73857a 92# The following require can't be removed during maintenance
93# releases, sadly, because of the risk of buggy code that does
94# require Carp; Carp::croak "..."; without brackets dying
95# if Carp hasn't been loaded in earlier compile time. :-(
96# We'll let those bugs get found on the development track.
97require Carp if $] < 5.00450;
98
8e07c86e 99# enable debug/trace messages from DynaLoader perl code
100$dl_debug = $ENV{PERL_DL_DEBUG} || 0 unless defined $dl_debug;
101
ff7f3c60 102#
103# Flags to alter dl_load_file behaviour. Assigned bits:
104# 0x01 make symbols available for linking later dl_load_file's.
105# (only known to work on Solaris 2 using dlopen(RTLD_GLOBAL))
f86702cc 106# (ignored under VMS; effect is built-in to image linking)
ff7f3c60 107#
108# This is called as a class method $module->dl_load_flags. The
109# definition here will be inherited and result on "default" loading
110# behaviour unless a sub-class of DynaLoader defines its own version.
111#
112
113sub dl_load_flags { 0x00 }
114
03c9e98c 115# ($dl_dlext, $dlsrc)
116# = @Config::Config{'dlext', 'dlsrc'};
117EOT
118
1c7f9087 119$dl_dlext = $Config::Config{'dlext'};
120$dl_so = $Config::Config{'so'};
121print OUT " (\$dl_dlext, \$dlsrc) = ('$dl_dlext', ",
122 to_string($Config::Config{'dlsrc'}), ")\n;";
ff7f3c60 123
1c7f9087 124print OUT expand_os_specific(<<'EOT');
8e07c86e 125
1c7f9087 126<<$^O-eq-VMS>>
8e07c86e 127# Some systems need special handling to expand file specifications
128# (VMS support by Charles Bailey <bailey@HMIVAX.HUMGEN.UPENN.EDU>)
129# See dl_expandspec() for more details. Should be harmless but
130# inefficient to define on systems that don't need it.
632a9a7f 131$Is_VMS = $^O eq 'VMS';
1c7f9087 132<</$^O-eq-VMS>>
133$do_expand = <<$^O-eq-VMS>>1<<|$^O-eq-VMS>>0<</$^O-eq-VMS>>;
8e07c86e 134
1c7f9087 135<<$^O-eq-MacOS>>
d5201bd2 136my $Mac_FS;
1c7f9087 137$Mac_FS = eval { require Mac::FileSpec::Unixish };
138<</$^O-eq-MacOS>>
d5201bd2 139
8e07c86e 140@dl_require_symbols = (); # names of symbols we need
141@dl_resolve_using = (); # names of files to link with
142@dl_library_path = (); # path to look for files
bff25830 143
144#XSLoader.pm may have added elements before we were required
89166d32 145#@dl_shared_objects = (); # shared objects for symbols we have
bff25830 146#@dl_librefs = (); # things we have loaded
147#@dl_modules = (); # Modules we have loaded
8e07c86e 148
149# This is a fix to support DLD's unfortunate desire to relink -lc
150@dl_resolve_using = dl_findfile('-lc') if $dlsrc eq "dl_dld.xs";
151
d0a5ed9f 152EOT
03c9e98c 153
d0a5ed9f 154my $cfg_dl_library_path = <<'EOT';
632a9a7f 155push(@dl_library_path, split(' ', $Config::Config{libpth}));
03c9e98c 156EOT
157
d0a5ed9f 158sub dquoted_comma_list {
1c7f9087 159 join(", ", map {'"'.quotemeta($_).'"'} @_);
d0a5ed9f 160}
03c9e98c 161
d0a5ed9f 162if ($ENV{PERL_BUILD_EXPAND_CONFIG_VARS}) {
163 eval $cfg_dl_library_path;
164 if (!$ENV{PERL_BUILD_EXPAND_ENV_VARS}) {
165 my $dl_library_path = dquoted_comma_list(@dl_library_path);
166 print OUT <<EOT;
7e9f3af8 167# The below \@dl_library_path has been expanded (%Config) in Perl build time.
d0a5ed9f 168
169\@dl_library_path = ($dl_library_path);
170
171EOT
172 }
173}
174else {
175 print OUT <<EOT;
176# Initialise \@dl_library_path with the 'standard' library path
177# for this platform as determined by Configure.
178
179$cfg_dl_library_path
180
181EOT
182}
8e07c86e 183
8225e35f 184my $ldlibpthname;
7e9f3af8 185my $ldlibpthname_defined;
8225e35f 186my $pthsep;
187
188if ($ENV{PERL_BUILD_EXPAND_CONFIG_VARS}) {
1c7f9087 189 $ldlibpthname = to_string($Config::Config{ldlibpthname});
190 $ldlibpthname_defined = to_string(defined $Config::Config{ldlibpthname} ? 1 : 0);
191 $pthsep = to_string($Config::Config{path_sep});
8225e35f 192}
193else {
7e9f3af8 194 $ldlibpthname = q($Config::Config{ldlibpthname});
195 $ldlibpthname_defined = q(defined $Config::Config{ldlibpthname});
196 $pthsep = q($Config::Config{path_sep});
1c7f9087 197}
198print OUT <<EOT;
7e9f3af8 199my \$ldlibpthname = $ldlibpthname;
200my \$ldlibpthname_defined = $ldlibpthname_defined;
201my \$pthsep = $pthsep;
8225e35f 202
203EOT
8225e35f 204
7e9f3af8 205my $env_dl_library_path = <<'EOT';
206if ($ldlibpthname_defined &&
207 exists $ENV{$ldlibpthname}) {
208 push(@dl_library_path, split(/$pthsep/, $ENV{$ldlibpthname}));
8225e35f 209}
210
5cf1d1f1 211# E.g. HP-UX supports both its native SHLIB_PATH *and* LD_LIBRARY_PATH.
8225e35f 212
7e9f3af8 213if ($ldlibpthname_defined &&
214 $ldlibpthname ne 'LD_LIBRARY_PATH' &&
215 exists $ENV{LD_LIBRARY_PATH}) {
216 push(@dl_library_path, split(/$pthsep/, $ENV{LD_LIBRARY_PATH}));
d0a5ed9f 217}
e6197cab 218EOT
219
d0a5ed9f 220if ($ENV{PERL_BUILD_EXPAND_CONFIG_VARS} && $ENV{PERL_BUILD_EXPAND_ENV_VARS}) {
221 eval $env_dl_library_path;
e6197cab 222}
d0a5ed9f 223else {
224 print OUT <<EOT;
225# Add to \@dl_library_path any extra directories we can gather from environment
226# during runtime.
e6197cab 227
d0a5ed9f 228$env_dl_library_path
229
230EOT
e6197cab 231}
232
d0a5ed9f 233if ($ENV{PERL_BUILD_EXPAND_CONFIG_VARS} && $ENV{PERL_BUILD_EXPAND_ENV_VARS}) {
234 my $dl_library_path = dquoted_comma_list(@dl_library_path);
235 print OUT <<EOT;
7e9f3af8 236# The below \@dl_library_path has been expanded (%Config, %ENV)
237# in Perl build time.
d0a5ed9f 238
239\@dl_library_path = ($dl_library_path);
240
241EOT
146174a9 242}
8e07c86e 243
1c7f9087 244
245# following long string contains $^O-specific stuff, which is factored out
246print OUT expand_os_specific(<<'EOT');
8e07c86e 247# No prizes for guessing why we don't say 'bootstrap DynaLoader;' here.
b4cea227 248# NOTE: All dl_*.xs (including dl_none.xs) define a dl_error() XSUB
bb3913c7 249boot_DynaLoader('DynaLoader') if defined(&boot_DynaLoader) &&
b4cea227 250 !defined(&dl_error);
8e07c86e 251
252if ($dl_debug) {
253 print STDERR "DynaLoader.pm loaded (@INC, @dl_library_path)\n";
254 print STDERR "DynaLoader not linked into this perl\n"
255 unless defined(&boot_DynaLoader);
256}
257
2581; # End of main code
259
260
fb73857a 261sub croak { require Carp; Carp::croak(@_) }
262
146174a9 263sub bootstrap_inherit {
264 my $module = $_[0];
265 local *isa = *{"$module\::ISA"};
266 local @isa = (@isa, 'DynaLoader');
267 # Cannot goto due to delocalization. Will report errors on a wrong line?
268 bootstrap(@_);
269}
270
8e07c86e 271# The bootstrap function cannot be autoloaded (without complications)
272# so we define it here:
273
274sub bootstrap {
275 # use local vars to enable $module.bs script to edit values
276 local(@args) = @_;
277 local($module) = $args[0];
278 local(@dirs, $file);
279
fb73857a 280 unless ($module) {
281 require Carp;
282 Carp::confess("Usage: DynaLoader::bootstrap(module)");
283 }
8e07c86e 284
285 # A common error on platforms which don't support dynamic loading.
286 # Since it's fatal and potentially confusing we give a detailed message.
fb73857a 287 croak("Can't load module $module, dynamic loading not available in this perl.\n".
8e07c86e 288 " (You may need to build a new perl executable which either supports\n".
289 " dynamic loading or has the $module module statically linked into it.)\n")
290 unless defined(&dl_load_file);
291
59ad941d 292
1c7f9087 293 <<$^O-eq-os2>>
59ad941d 294 # Can dynaload, but cannot dynaload Perl modules...
295 die 'Dynaloaded Perl modules are not available in this build of Perl' if $OS2::is_static;
296
1c7f9087 297 <</$^O-eq-os2>>
8e07c86e 298 my @modparts = split(/::/,$module);
299 my $modfname = $modparts[-1];
300
301 # Some systems have restrictions on files names for DLL's etc.
302 # mod2fname returns appropriate file base name (typically truncated)
303 # It may also edit @modparts if required.
304 $modfname = &mod2fname(\@modparts) if defined &mod2fname;
305
1c7f9087 306 <<$^O-eq-NetWare>>
1a95e36d 307 # Truncate the module name to 8.3 format for NetWare
1c7f9087 308 if ((length($modfname) > 8)) {
f355267c 309 $modfname = substr($modfname, 0, 8);
310 }
1c7f9087 311 <</$^O-eq-NetWare>>
f355267c 312
1c7f9087 313 my $modpname = join(<<$^O-eq-MacOS>>':'<<|$^O-eq-MacOS>>'/'<</$^O-eq-MacOS>>,@modparts);
8e07c86e 314
315 print STDERR "DynaLoader::bootstrap for $module ",
1c7f9087 316 <<$^O-eq-MacOS>> "(:auto:$modpname:$modfname.<<=$dl_dlext>>)\n"
317 <<|$^O-eq-MacOS>>"(auto/$modpname/$modfname.<<=$dl_dlext>>)\n"<</$^O-eq-MacOS>>
146174a9 318 if $dl_debug;
8e07c86e 319
320 foreach (@INC) {
1c7f9087 321 <<$^O-eq-VMS>>chop($_ = VMS::Filespec::unixpath($_));<</$^O-eq-VMS>>
322 <<$^O-eq-MacOS>>
e69a2255 323 my $path = $_;
5b865721 324 if ($Mac_FS && ! -d $path) {
d5201bd2 325 $path = Mac::FileSpec::Unixish::nativize($path);
326 }
e69a2255 327 $path .= ":" unless /:$/;
1c7f9087 328 my $dir = "${path}auto:$modpname";
329 <<|$^O-eq-MacOS>>
330 my $dir = "$_/auto/$modpname";
331 <</$^O-eq-MacOS>>
1a95e36d 332
333 next unless -d $dir; # skip over uninteresting directories
334
8e07c86e 335 # check for common cases to avoid autoload of dl_findfile
1c7f9087 336 my $try = <<$^O-eq-MacOS>> "$dir:$modfname.<<=$dl_dlext>>" <<|$^O-eq-MacOS>> "$dir/$modfname.<<=$dl_dlext>>"<</$^O-eq-MacOS>>;
337 last if $file = <<$^O-eq-VMS>>($do_expand) ? dl_expandspec($try) : ((-f $try) && $try);
338 <<|$^O-eq-VMS>>(-f $try) && $try;
339 <</$^O-eq-VMS>>
1a95e36d 340
8e07c86e 341 # no luck here, save dir for possible later dl_findfile search
fb73857a 342 push @dirs, $dir;
8e07c86e 343 }
344 # last resort, let dl_findfile have a go in all known locations
fb73857a 345 $file = dl_findfile(map("-L$_",@dirs,@INC), $modfname) unless $file;
8e07c86e 346
fb73857a 347 croak("Can't locate loadable object for module $module in \@INC (\@INC contains: @INC)")
348 unless $file; # wording similar to error from 'require'
8e07c86e 349
1c7f9087 350 <<$^O-eq-VMS>>$file = uc($file) if $Config::Config{d_vms_case_sensitive_symbols};<</$^O-eq-VMS>>
8e07c86e 351 my $bootname = "boot_$module";
352 $bootname =~ s/\W/_/g;
353 @dl_require_symbols = ($bootname);
354
355 # Execute optional '.bootstrap' perl script for this module.
356 # The .bs file can be used to configure @dl_resolve_using etc to
357 # match the needs of the individual module on this architecture.
358 my $bs = $file;
3f45a6dd 359 $bs =~ s/(\.\w+)?(;\d*)?$/\.bs/; # look for .bs 'beside' the library
8e07c86e 360 if (-s $bs) { # only read file if it's not empty
d404d5bf 361 print STDERR "BS: $bs ($^O, $dlsrc)\n" if $dl_debug;
8e07c86e 362 eval { do $bs; };
363 warn "$bs: $@\n" if $@;
364 }
365
588cafc8 366 my $boot_symbol_ref;
367
1c7f9087 368 <<$^O-eq-darwin>>
369 if ($boot_symbol_ref = dl_find_symbol(0, $bootname)) {
370 goto boot; #extension library has already been loaded, e.g. darwin
588cafc8 371 }
1c7f9087 372 <</$^O-eq-darwin>>
588cafc8 373
8e07c86e 374 # Many dynamic extension loading problems will appear to come from
375 # this section of code: XYZ failed at line 123 of DynaLoader.pm.
376 # Often these errors are actually occurring in the initialisation
377 # C code of the extension XS file. Perl reports the error as being
378 # in this perl code simply because this was the last perl code
379 # it executed.
380
ff7f3c60 381 my $libref = dl_load_file($file, $module->dl_load_flags) or
549a6b10 382 croak("Can't load '$file' for module $module: ".dl_error());
8e07c86e 383
ff7f3c60 384 push(@dl_librefs,$libref); # record loaded object
385
8e07c86e 386 my @unresolved = dl_undef_symbols();
fb73857a 387 if (@unresolved) {
388 require Carp;
389 Carp::carp("Undefined symbols present after loading $file: @unresolved\n");
390 }
8e07c86e 391
588cafc8 392 $boot_symbol_ref = dl_find_symbol($libref, $bootname) or
fb73857a 393 croak("Can't find '$bootname' symbol in $file\n");
8e07c86e 394
ff7f3c60 395 push(@dl_modules, $module); # record loaded module
396
588cafc8 397 boot:
398 my $xs = dl_install_xsub("${module}::bootstrap", $boot_symbol_ref, $file);
399
8e07c86e 400 # See comment block above
8cf57410 401
402 push(@dl_shared_objects, $file); # record files loaded
403
8e07c86e 404 &$xs(@args);
405}
406
407
fb73857a 408#sub _check_file { # private utility to handle dl_expandspec vs -f tests
409# my($file) = @_;
410# return $file if (!$do_expand && -f $file); # the common case
411# return $file if ( $do_expand && ($file=dl_expandspec($file)));
412# return undef;
413#}
8e07c86e 414
415
416# Let autosplit and the autoloader deal with these functions:
417__END__
418
419
420sub dl_findfile {
421 # Read ext/DynaLoader/DynaLoader.doc for detailed information.
422 # This function does not automatically consider the architecture
423 # or the perl library auto directories.
424 my (@args) = @_;
425 my (@dirs, $dir); # which directories to search
426 my (@found); # full paths to real files we have found
1c7f9087 427 #my $dl_ext= <<=to_string($Config::Config{'dlext'})>>; # $Config::Config{'dlext'} suffix for perl extensions
428 #my $dl_so = <<=to_string($Config::Config{'so'})>>; # $Config::Config{'so'} suffix for shared libraries
8e07c86e 429
430 print STDERR "dl_findfile(@args)\n" if $dl_debug;
431
432 # accumulate directories but process files as they appear
433 arg: foreach(@args) {
434 # Special fast case: full filepath requires no search
1c7f9087 435 <<$^O-eq-VMS>>
436 if (m%[:>/\]]% && -f $_) {
54d6a3b3 437 push(@found,dl_expandspec(VMS::Filespec::vmsify($_)));
438 last arg unless wantarray;
439 next;
440 }
1c7f9087 441 <</$^O-eq-VMS>>
442 <<$^O-eq-MacOS>>
146174a9 443 if (m/:/ && -f $_) {
444 push(@found,$_);
445 last arg unless wantarray;
446 }
1c7f9087 447 <</$^O-eq-MacOS>>
448 <<$^O-ne-VMS>>
449 if (m:/: && -f $_) {
8e07c86e 450 push(@found,$_);
451 last arg unless wantarray;
452 next;
453 }
1c7f9087 454 <</$^O-ne-VMS>>
8e07c86e 455
456 # Deal with directories first:
457 # Using a -L prefix is the preferred option (faster and more robust)
458 if (m:^-L:) { s/^-L//; push(@dirs, $_); next; }
459
1c7f9087 460 <<$^O-eq-MacOS>>
146174a9 461 # Otherwise we try to try to spot directories by a heuristic
462 # (this is a more complicated issue than it first appears)
463 if (m/:/ && -d $_) { push(@dirs, $_); next; }
464 # Only files should get this far...
465 my(@names, $name); # what filenames to look for
466 s/^-l//;
467 push(@names, $_);
468 foreach $dir (@dirs, @dl_library_path) {
469 next unless -d $dir;
470 $dir =~ s/^([^:]+)$/:$1/;
471 $dir =~ s/:$//;
472 foreach $name (@names) {
473 my($file) = "$dir:$name";
474 print STDERR " checking in $dir for $name\n" if $dl_debug;
475 if (-f $file) {
476 push(@found, $file);
477 next arg; # no need to look any further
478 }
479 }
480 }
481 next;
1c7f9087 482 <</$^O-eq-MacOS>>
146174a9 483
8e07c86e 484 # Otherwise we try to try to spot directories by a heuristic
485 # (this is a more complicated issue than it first appears)
486 if (m:/: && -d $_) { push(@dirs, $_); next; }
487
1c7f9087 488 <<$^O-eq-VMS>>
7e9f3af8 489 # VMS: we may be using native VMS directory syntax instead of
8e07c86e 490 # Unix emulation, so check this as well
1c7f9087 491 if (/[:>\]]/ && -d $_) { push(@dirs, $_); next; }
492 <</$^O-eq-VMS>>
8e07c86e 493
494 # Only files should get this far...
495 my(@names, $name); # what filenames to look for
496 if (m:-l: ) { # convert -lname to appropriate library name
497 s/-l//;
1c7f9087 498 push(@names,"lib$_.<<=to_string($Config::Config{'so'})>>");
8e07c86e 499 push(@names,"lib$_.a");
500 } else { # Umm, a bare name. Try various alternatives:
501 # these should be ordered with the most likely first
1c7f9087 502 push(@names,"$_.<<=$dl_dlext>>") unless m/\.<<=$dl_dlext>>$/o;
503 push(@names,"$_.<<=$dl_so>>") unless m/\.<<=$dl_so>>$/o;
504 push(@names,"lib$_.<<=$dl_so>>") unless m:/:;
8e07c86e 505 push(@names,"$_.a") if !m/\.a$/ and $dlsrc eq "dl_dld.xs";
506 push(@names, $_);
507 }
508 foreach $dir (@dirs, @dl_library_path) {
509 next unless -d $dir;
1c7f9087 510 <<$^O-eq-VMS>>
511 chop($dir = VMS::Filespec::unixpath($dir));
512 <</$^O-eq-VMS>>
8e07c86e 513 foreach $name (@names) {
514 my($file) = "$dir/$name";
515 print STDERR " checking in $dir for $name\n" if $dl_debug;
fb73857a 516 $file = ($do_expand) ? dl_expandspec($file) : (-f $file && $file);
517 #$file = _check_file($file);
8e07c86e 518 if ($file) {
519 push(@found, $file);
520 next arg; # no need to look any further
521 }
522 }
523 }
524 }
525 if ($dl_debug) {
526 foreach(@dirs) {
527 print STDERR " dl_findfile ignored non-existent directory: $_\n" unless -d $_;
528 }
529 print STDERR "dl_findfile found: @found\n";
530 }
531 return $found[0] unless wantarray;
532 @found;
533}
534
535
536sub dl_expandspec {
537 my($spec) = @_;
538 # Optional function invoked if DynaLoader.pm sets $do_expand.
539 # Most systems do not require or use this function.
540 # Some systems may implement it in the dl_*.xs file in which case
541 # this autoload version will not be called but is harmless.
542
543 # This function is designed to deal with systems which treat some
544 # 'filenames' in a special way. For example VMS 'Logical Names'
545 # (something like unix environment variables - but different).
546 # This function should recognise such names and expand them into
547 # full file paths.
548 # Must return undef if $spec is invalid or file does not exist.
549
550 my $file = $spec; # default output to input
551
1c7f9087 552 <<$^O-eq-VMS>>
553 # dl_expandspec should be defined in dl_vms.xs
fb73857a 554 require Carp;
4633a7c4 555 Carp::croak("dl_expandspec: should be defined in XS file!\n");
1c7f9087 556 <<|$^O-eq-VMS>>
8e07c86e 557 return undef unless -f $file;
1c7f9087 558 <</$^O-eq-VMS>>
8e07c86e 559 print STDERR "dl_expandspec($spec) => $file\n" if $dl_debug;
560 $file;
561}
562
ff7f3c60 563sub dl_find_symbol_anywhere
564{
565 my $sym = shift;
566 my $libref;
567 foreach $libref (@dl_librefs) {
568 my $symref = dl_find_symbol($libref,$sym);
569 return $symref if $symref;
570 }
571 return undef;
572}
8e07c86e 573
3b35bae3 574=head1 NAME
575
576DynaLoader - Dynamically load C libraries into Perl code
577
3b35bae3 578=head1 SYNOPSIS
579
8e07c86e 580 package YourPackage;
3b35bae3 581 require DynaLoader;
c2960299 582 @ISA = qw(... DynaLoader ...);
8e07c86e 583 bootstrap YourPackage;
3b35bae3 584
ff7f3c60 585 # optional method for 'global' loading
586 sub dl_load_flags { 0x01 }
587
3b35bae3 588
589=head1 DESCRIPTION
590
c2960299 591This document defines a standard generic interface to the dynamic
3b35bae3 592linking mechanisms available on many platforms. Its primary purpose is
593to implement automatic dynamic loading of Perl modules.
594
c2960299 595This document serves as both a specification for anyone wishing to
596implement the DynaLoader for a new platform and as a guide for
597anyone wishing to use the DynaLoader directly in an application.
598
3b35bae3 599The DynaLoader is designed to be a very simple high-level
600interface that is sufficiently general to cover the requirements
601of SunOS, HP-UX, NeXT, Linux, VMS and other platforms.
602
c2960299 603It is also hoped that the interface will cover the needs of OS/2, NT
604etc and also allow pseudo-dynamic linking (using C<ld -A> at runtime).
3b35bae3 605
606It must be stressed that the DynaLoader, by itself, is practically
607useless for accessing non-Perl libraries because it provides almost no
608Perl-to-C 'glue'. There is, for example, no mechanism for calling a C
03c9e98c 609library function or supplying arguments. A C::DynaLib module
90248788 610is available from CPAN sites which performs that function for some
1f98619c 611common system types. And since the year 2000, there's also Inline::C,
612a module that allows you to write Perl subroutines in C. Also available
613from your local CPAN site.
3b35bae3 614
615DynaLoader Interface Summary
616
617 @dl_library_path
618 @dl_resolve_using
619 @dl_require_symbols
620 $dl_debug
ff7f3c60 621 @dl_librefs
622 @dl_modules
8cf57410 623 @dl_shared_objects
3b35bae3 624 Implemented in:
625 bootstrap($modulename) Perl
626 @filepaths = dl_findfile(@names) Perl
ff7f3c60 627 $flags = $modulename->dl_load_flags Perl
628 $symref = dl_find_symbol_anywhere($symbol) Perl
3b35bae3 629
ff7f3c60 630 $libref = dl_load_file($filename, $flags) C
abb9e9dc 631 $status = dl_unload_file($libref) C
3b35bae3 632 $symref = dl_find_symbol($libref, $symbol) C
633 @symbols = dl_undef_symbols() C
634 dl_install_xsub($name, $symref [, $filename]) C
635 $message = dl_error C
636
637=over 4
638
639=item @dl_library_path
640
641The standard/default list of directories in which dl_findfile() will
642search for libraries etc. Directories are searched in order:
643$dl_library_path[0], [1], ... etc
644
645@dl_library_path is initialised to hold the list of 'normal' directories
646(F</usr/lib>, etc) determined by B<Configure> (C<$Config{'libpth'}>). This should
647ensure portability across a wide range of platforms.
648
649@dl_library_path should also be initialised with any other directories
650that can be determined from the environment at runtime (such as
651LD_LIBRARY_PATH for SunOS).
652
653After initialisation @dl_library_path can be manipulated by an
654application using push and unshift before calling dl_findfile().
655Unshift can be used to add directories to the front of the search order
656either to save search time or to override libraries with the same name
657in the 'normal' directories.
658
659The load function that dl_load_file() calls may require an absolute
660pathname. The dl_findfile() function and @dl_library_path can be
661used to search for and return the absolute pathname for the
662library/object that you wish to load.
663
664=item @dl_resolve_using
665
666A list of additional libraries or other shared objects which can be
667used to resolve any undefined symbols that might be generated by a
668later call to load_file().
669
670This is only required on some platforms which do not handle dependent
7a2e2cd6 671libraries automatically. For example the Socket Perl extension
672library (F<auto/Socket/Socket.so>) contains references to many socket
673functions which need to be resolved when it's loaded. Most platforms
674will automatically know where to find the 'dependent' library (e.g.,
675F</usr/lib/libsocket.so>). A few platforms need to be told the
676location of the dependent library explicitly. Use @dl_resolve_using
677for this.
3b35bae3 678
679Example usage:
680
681 @dl_resolve_using = dl_findfile('-lsocket');
682
683=item @dl_require_symbols
684
685A list of one or more symbol names that are in the library/object file
686to be dynamically loaded. This is only required on some platforms.
687
ff7f3c60 688=item @dl_librefs
689
690An array of the handles returned by successful calls to dl_load_file(),
691made by bootstrap, in the order in which they were loaded.
692Can be used with dl_find_symbol() to look for a symbol in any of
693the loaded files.
694
695=item @dl_modules
696
697An array of module (package) names that have been bootstrap'ed.
698
8cf57410 699=item @dl_shared_objects
700
701An array of file names for the shared objects that were loaded.
702
3b35bae3 703=item dl_error()
704
705Syntax:
706
707 $message = dl_error();
708
709Error message text from the last failed DynaLoader function. Note
710that, similar to errno in unix, a successful function call does not
711reset this message.
712
713Implementations should detect the error as soon as it occurs in any of
714the other functions and save the corresponding message for later
715retrieval. This will avoid problems on some platforms (such as SunOS)
716where the error message is very temporary (e.g., dlerror()).
717
718=item $dl_debug
719
720Internal debugging messages are enabled when $dl_debug is set true.
721Currently setting $dl_debug only affects the Perl side of the
722DynaLoader. These messages should help an application developer to
723resolve any DynaLoader usage problems.
724
725$dl_debug is set to C<$ENV{'PERL_DL_DEBUG'}> if defined.
726
727For the DynaLoader developer/porter there is a similar debugging
728variable added to the C code (see dlutils.c) and enabled if Perl was
729built with the B<-DDEBUGGING> flag. This can also be set via the
730PERL_DL_DEBUG environment variable. Set to 1 for minimal information or
731higher for more.
732
733=item dl_findfile()
734
735Syntax:
736
737 @filepaths = dl_findfile(@names)
738
739Determine the full paths (including file suffix) of one or more
740loadable files given their generic names and optionally one or more
741directories. Searches directories in @dl_library_path by default and
742returns an empty list if no files were found.
743
744Names can be specified in a variety of platform independent forms. Any
745names in the form B<-lname> are converted into F<libname.*>, where F<.*> is
746an appropriate suffix for the platform.
747
748If a name does not already have a suitable prefix and/or suffix then
749the corresponding file will be searched for by trying combinations of
750prefix and suffix appropriate to the platform: "$name.o", "lib$name.*"
751and "$name".
752
753If any directories are included in @names they are searched before
c2960299 754@dl_library_path. Directories may be specified as B<-Ldir>. Any other
755names are treated as filenames to be searched for.
3b35bae3 756
757Using arguments of the form C<-Ldir> and C<-lname> is recommended.
758
759Example:
760
761 @dl_resolve_using = dl_findfile(qw(-L/usr/5lib -lposix));
762
763
764=item dl_expandspec()
765
766Syntax:
767
768 $filepath = dl_expandspec($spec)
769
770Some unusual systems, such as VMS, require special filename handling in
771order to deal with symbolic names for files (i.e., VMS's Logical Names).
772
773To support these systems a dl_expandspec() function can be implemented
774either in the F<dl_*.xs> file or code can be added to the autoloadable
c2960299 775dl_expandspec() function in F<DynaLoader.pm>. See F<DynaLoader.pm> for
776more information.
3b35bae3 777
778=item dl_load_file()
779
780Syntax:
781
ff7f3c60 782 $libref = dl_load_file($filename, $flags)
3b35bae3 783
784Dynamically load $filename, which must be the path to a shared object
785or library. An opaque 'library reference' is returned as a handle for
786the loaded object. Returns undef on error.
787
ff7f3c60 788The $flags argument to alters dl_load_file behaviour.
789Assigned bits:
790
791 0x01 make symbols available for linking later dl_load_file's.
792 (only known to work on Solaris 2 using dlopen(RTLD_GLOBAL))
f86702cc 793 (ignored under VMS; this is a normal part of image linking)
ff7f3c60 794
3b35bae3 795(On systems that provide a handle for the loaded object such as SunOS
796and HPUX, $libref will be that handle. On other systems $libref will
797typically be $filename or a pointer to a buffer containing $filename.
798The application should not examine or alter $libref in any way.)
799
ff7f3c60 800This is the function that does the real work. It should use the
801current values of @dl_require_symbols and @dl_resolve_using if required.
3b35bae3 802
803 SunOS: dlopen($filename)
804 HP-UX: shl_load($filename)
805 Linux: dld_create_reference(@dl_require_symbols); dld_link($filename)
806 NeXT: rld_load($filename, @dl_resolve_using)
807 VMS: lib$find_image_symbol($filename,$dl_require_symbols[0])
808
ff7f3c60 809(The dlopen() function is also used by Solaris and some versions of
810Linux, and is a common choice when providing a "wrapper" on other
811mechanisms as is done in the OS/2 port.)
812
abb9e9dc 813=item dl_unload_file()
814
815Syntax:
816
817 $status = dl_unload_file($libref)
818
819Dynamically unload $libref, which must be an opaque 'library reference' as
820returned from dl_load_file. Returns one on success and zero on failure.
821
822This function is optional and may not necessarily be provided on all platforms.
823If it is defined, it is called automatically when the interpreter exits for
824every shared object or library loaded by DynaLoader::bootstrap. All such
825library references are stored in @dl_librefs by DynaLoader::Bootstrap as it
22851543 826loads the libraries. The files are unloaded in last-in, first-out order.
abb9e9dc 827
828This unloading is usually necessary when embedding a shared-object perl (e.g.
829one configured with -Duseshrplib) within a larger application, and the perl
830interpreter is created and destroyed several times within the lifetime of the
831application. In this case it is possible that the system dynamic linker will
832unload and then subsequently reload the shared libperl without relocating any
833references to it from any files DynaLoaded by the previous incarnation of the
834interpreter. As a result, any shared objects opened by DynaLoader may point to
835a now invalid 'ghost' of the libperl shared object, causing apparently random
836memory corruption and crashes. This behaviour is most commonly seen when using
837Apache and mod_perl built with the APXS mechanism.
838
839 SunOS: dlclose($libref)
840 HP-UX: ???
841 Linux: ???
842 NeXT: ???
843 VMS: ???
844
845(The dlclose() function is also used by Solaris and some versions of
846Linux, and is a common choice when providing a "wrapper" on other
847mechanisms as is done in the OS/2 port.)
848
2307c6d0 849=item dl_load_flags()
ff7f3c60 850
851Syntax:
852
2307c6d0 853 $flags = dl_load_flags $modulename;
ff7f3c60 854
855Designed to be a method call, and to be overridden by a derived class
856(i.e. a class which has DynaLoader in its @ISA). The definition in
857DynaLoader itself returns 0, which produces standard behavior from
858dl_load_file().
3b35bae3 859
860=item dl_find_symbol()
861
862Syntax:
863
864 $symref = dl_find_symbol($libref, $symbol)
865
866Return the address of the symbol $symbol or C<undef> if not found. If the
867target system has separate functions to search for symbols of different
868types then dl_find_symbol() should search for function symbols first and
869then other types.
870
871The exact manner in which the address is returned in $symref is not
872currently defined. The only initial requirement is that $symref can
873be passed to, and understood by, dl_install_xsub().
874
875 SunOS: dlsym($libref, $symbol)
876 HP-UX: shl_findsym($libref, $symbol)
877 Linux: dld_get_func($symbol) and/or dld_get_symbol($symbol)
878 NeXT: rld_lookup("_$symbol")
879 VMS: lib$find_image_symbol($libref,$symbol)
880
881
ff7f3c60 882=item dl_find_symbol_anywhere()
883
884Syntax:
885
886 $symref = dl_find_symbol_anywhere($symbol)
887
888Applies dl_find_symbol() to the members of @dl_librefs and returns
889the first match found.
890
3b35bae3 891=item dl_undef_symbols()
892
893Example
894
895 @symbols = dl_undef_symbols()
896
897Return a list of symbol names which remain undefined after load_file().
898Returns C<()> if not known. Don't worry if your platform does not provide
c2960299 899a mechanism for this. Most do not need it and hence do not provide it,
900they just return an empty list.
3b35bae3 901
902
903=item dl_install_xsub()
904
905Syntax:
906
907 dl_install_xsub($perl_name, $symref [, $filename])
908
909Create a new Perl external subroutine named $perl_name using $symref as
910a pointer to the function which implements the routine. This is simply
911a direct call to newXSUB(). Returns a reference to the installed
912function.
913
914The $filename parameter is used by Perl to identify the source file for
915the function if required by die(), caller() or the debugger. If
916$filename is not defined then "DynaLoader" will be used.
917
918
1fef88e7 919=item bootstrap()
3b35bae3 920
921Syntax:
922
923bootstrap($module)
924
925This is the normal entry point for automatic dynamic loading in Perl.
926
927It performs the following actions:
928
929=over 8
930
931=item *
932
933locates an auto/$module directory by searching @INC
934
935=item *
936
937uses dl_findfile() to determine the filename to load
938
939=item *
940
941sets @dl_require_symbols to C<("boot_$module")>
942
943=item *
944
945executes an F<auto/$module/$module.bs> file if it exists
946(typically used to add to @dl_resolve_using any files which
947are required to load the module on the current platform)
948
949=item *
950
ff7f3c60 951calls dl_load_flags() to determine how to load the file.
952
953=item *
954
3b35bae3 955calls dl_load_file() to load the file
956
957=item *
958
959calls dl_undef_symbols() and warns if any symbols are undefined
960
961=item *
962
963calls dl_find_symbol() for "boot_$module"
964
965=item *
966
967calls dl_install_xsub() to install it as "${module}::bootstrap"
968
969=item *
970
8e07c86e 971calls &{"${module}::bootstrap"} to bootstrap the module (actually
972it uses the function reference returned by dl_install_xsub for speed)
3b35bae3 973
974=back
975
976=back
977
978
979=head1 AUTHOR
980
c2960299 981Tim Bunce, 11 August 1994.
982
3b35bae3 983This interface is based on the work and comments of (in no particular
984order): Larry Wall, Robert Sanders, Dean Roehrich, Jeff Okamoto, Anno
c2960299 985Siegel, Thomas Neumann, Paul Marquess, Charles Bailey, myself and others.
3b35bae3 986
987Larry Wall designed the elegant inherited bootstrap mechanism and
988implemented the first Perl 5 dynamic loader using it.
989
ff7f3c60 990Solaris global loading added by Nick Ing-Simmons with design/coding
991assistance from Tim Bunce, January 1996.
992
3b35bae3 993=cut
03c9e98c 994EOT
995
996close OUT or die $!;
997