This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
37e24047d4e1ee053c3c852cd7bf931d3ba7f49b
[perl5.git] / lib / ExtUtils / Mkbootstrap.pm
1 package ExtUtils::Mkbootstrap;
2
3 # There's just too much Dynaloader incest here to turn on strict vars.
4 use strict 'refs';
5
6 our $VERSION = '6.54';
7
8 require Exporter;
9 our @ISA = ('Exporter');
10 our @EXPORT = ('&Mkbootstrap');
11
12 use Config;
13
14 our $Verbose = 0;
15
16
17 sub Mkbootstrap {
18     my($baseext, @bsloadlibs)=@_;
19     @bsloadlibs = grep($_, @bsloadlibs); # strip empty libs
20
21     print STDOUT "      bsloadlibs=@bsloadlibs\n" if $Verbose;
22
23     # We need DynaLoader here because we and/or the *_BS file may
24     # call dl_findfile(). We don't say `use' here because when
25     # first building perl extensions the DynaLoader will not have
26     # been built when MakeMaker gets first used.
27     require DynaLoader;
28
29     rename "$baseext.bs", "$baseext.bso"
30       if -s "$baseext.bs";
31
32     if (-f "${baseext}_BS"){
33         $_ = "${baseext}_BS";
34         package DynaLoader; # execute code as if in DynaLoader
35         local($osname, $dlsrc) = (); # avoid warnings
36         ($osname, $dlsrc) = @Config::Config{qw(osname dlsrc)};
37         $bscode = "";
38         unshift @INC, ".";
39         require $_;
40         shift @INC;
41     }
42
43     if ($Config{'dlsrc'} =~ /^dl_dld/){
44         package DynaLoader;
45         push(@dl_resolve_using, dl_findfile('-lc'));
46     }
47
48     my(@all) = (@bsloadlibs, @DynaLoader::dl_resolve_using);
49     my($method) = '';
50     if (@all){
51         open my $bs, ">", "$baseext.bs"
52                 or die "Unable to open $baseext.bs: $!";
53         print STDOUT "Writing $baseext.bs\n";
54         print STDOUT "  containing: @all" if $Verbose;
55         print $bs "# $baseext DynaLoader bootstrap file for $^O architecture.\n";
56         print $bs "# Do not edit this file, changes will be lost.\n";
57         print $bs "# This file was automatically generated by the\n";
58         print $bs "# Mkbootstrap routine in ExtUtils::Mkbootstrap (v$VERSION).\n";
59         print $bs "\@DynaLoader::dl_resolve_using = ";
60         # If @all contains names in the form -lxxx or -Lxxx then it's asking for
61         # runtime library location so we automatically add a call to dl_findfile()
62         if (" @all" =~ m/ -[lLR]/){
63             print $bs "  dl_findfile(qw(\n  @all\n  ));\n";
64         }else{
65             print $bs "  qw(@all);\n";
66         }
67         # write extra code if *_BS says so
68         print $bs $DynaLoader::bscode if $DynaLoader::bscode;
69         print $bs "\n1;\n";
70         close $bs;
71     }
72 }
73
74 1;
75
76 __END__
77
78 =head1 NAME
79
80 ExtUtils::Mkbootstrap - make a bootstrap file for use by DynaLoader
81
82 =head1 SYNOPSIS
83
84 C<Mkbootstrap>
85
86 =head1 DESCRIPTION
87
88 Mkbootstrap typically gets called from an extension Makefile.
89
90 There is no C<*.bs> file supplied with the extension. Instead, there may
91 be a C<*_BS> file which has code for the special cases, like posix for
92 berkeley db on the NeXT.
93
94 This file will get parsed, and produce a maybe empty
95 C<@DynaLoader::dl_resolve_using> array for the current architecture.
96 That will be extended by $BSLOADLIBS, which was computed by
97 ExtUtils::Liblist::ext(). If this array still is empty, we do nothing,
98 else we write a .bs file with an C<@DynaLoader::dl_resolve_using>
99 array.
100
101 The C<*_BS> file can put some code into the generated C<*.bs> file by
102 placing it in C<$bscode>. This is a handy 'escape' mechanism that may
103 prove useful in complex situations.
104
105 If @DynaLoader::dl_resolve_using contains C<-L*> or C<-l*> entries then
106 Mkbootstrap will automatically add a dl_findfile() call to the
107 generated C<*.bs> file.
108
109 =cut