Use $^O; don't use Config unless we need it
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / Mksymlists.pm
1 package ExtUtils::Mksymlists;
2 use strict qw[ subs refs ];
3 # no strict 'vars';  # until filehandles are exempted
4
5 use Carp;
6 use Exporter;
7 # mention vars twice to prevent single-use warnings
8 @ExtUtils::Mksymlists::ISA = @ExtUtils::Mksymlists::ISA = 'Exporter';
9 @ExtUtils::Mksymlists::EXPORT = @ExtUtils::Mksymlists::EXPORT = '&Mksymlists';
10 $ExtUtils::Mksymlists::VERSION = $ExtUtils::Mksymlists::VERSION = '1.00';
11
12 sub Mksymlists {
13     my(%spec) = @_;
14
15     croak("Insufficient information specified to Mksymlists")
16         unless ( $spec{NAME} or
17                  ($spec{FILE} and ($spec{DL_FUNCS} or $spec{FUNCLIST})) );
18
19     $spec{DL_VARS} = [] unless $spec{DL_VARS};
20     ($spec{FILE} = $spec{NAME}) =~ s/.*::// unless $spec{FILE};
21     $spec{DL_FUNCS} = { $spec{NAME} => [] }
22         unless ( ($spec{DL_FUNCS} and keys %{$spec{DL_FUNCS}}) or
23                  $spec{FUNCLIST});
24     $spec{FUNCLIST} = [] unless $spec{FUNCLIST};
25     if (defined $spec{DL_FUNCS}) {
26         my($package);
27         foreach $package (keys %{$spec{DL_FUNCS}}) {
28             my($packprefix,$sym,$bootseen);
29             ($packprefix = $package) =~ s/\W/_/g;
30             foreach $sym (@{$spec{DL_FUNCS}->{$package}}) {
31                 if ($sym =~ /^boot_/) {
32                     push(@{$spec{FUNCLIST}},$sym);
33                     $bootseen++;
34                 }
35                 else { push(@{$spec{FUNCLIST}},"XS_${packprefix}_$sym"); }
36             }
37             push(@{$spec{FUNCLIST}},"boot_$packprefix") unless $bootseen;
38         }
39     }
40
41 #    We'll need this if we ever add any OS which uses mod2fname
42 #    require DynaLoader;
43     if (defined &DynaLoader::mod2fname and not $spec{DLBASE}) {
44         $spec{DLBASE} = DynaLoader::mod2fname([ split(/::/,$spec{NAME}) ]);
45     }
46
47     if    ($^O eq 'aix') { _write_aix(\%spec); }
48     elsif ($^O eq 'VMS') { _write_vms(\%spec) }
49     elsif ($^O eq 'os2') { _write_os2(\%spec) }
50     else { croak("Don't know how to create linker option file for $^O\n"); }
51 }
52
53
54 sub _write_aix {
55     my($data) = @_;
56
57     rename "$data->{FILE}.exp", "$data->{FILE}.exp_old";
58
59     open(EXP,">$data->{FILE}.exp")
60         or croak("Can't create $data->{FILE}.exp: $!\n");
61     print EXP join("\n",@{$data->{DL_VARS}}, "\n") if @{$data->{DL_VARS}};
62     print EXP join("\n",@{$data->{FUNCLIST}}, "\n") if @{$data->{FUNCLIST}};
63     close EXP;
64 }
65
66
67 sub _write_os2 {
68     my($data) = @_;
69
70     if (not $data->{DLBASE}) {
71         ($data->{DLBASE} = $data->{NAME}) =~ s/.*:://;
72         $data->{DLBASE} = substr($data->{DLBASE},0,7) . '_';
73     }
74     rename "$data->{FILE}.def", "$data->{FILE}_def.old";
75
76     open(DEF,">$data->{FILE}.def")
77         or croak("Can't create $data->{FILE}.def: $!\n");
78     print DEF "LIBRARY '$data->{DLBASE}' INITINSTANCE TERMINSTANCE\n";
79     print DEF "CODE LOADONCALL\n";
80     print DEF "DATA LOADONCALL NONSHARED MULTIPLE\n";
81     print DEF "EXPORTS\n  ";
82     print DEF join("\n  ",@{$data->{DL_VARS}}, "\n") if @{$data->{DL_VARS}};
83     print DEF join("\n  ",@{$data->{FUNCLIST}}, "\n") if @{$data->{FUNCLIST}};
84     if (%{$data->{IMPORTS}}) {
85         print DEF "IMPORTS\n";
86 my ($name, $exp);
87 while (($name, $exp)= each %{$data->{IMPORTS}}) {
88   print DEF "  $name=$exp\n";
89 }
90     }
91     close DEF;
92 }
93
94
95 sub _write_vms {
96     my($data) = @_;
97
98     require Config;
99
100     my($isvax) = $Config::Config{'arch'} =~ /VAX/i;
101     my($sym);
102
103     rename "$data->{FILE}.opt", "$data->{FILE}.opt_old";
104
105     open(OPT,">$data->{FILE}.opt")
106         or croak("Can't create $data->{FILE}.opt: $!\n");
107
108     # Options file declaring universal symbols
109     # Used when linking shareable image for dynamic extension,
110     # or when linking PerlShr into which we've added this package
111     # as a static extension
112     # We don't do anything to preserve order, so we won't relax
113     # the GSMATCH criteria for a dynamic extension
114
115     foreach $sym (@{$data->{FUNCLIST}}) {
116         if ($isvax) { print OPT "UNIVERSAL=$sym\n" }
117         else        { print OPT "SYMBOL_VECTOR=($sym=PROCEDURE)\n"; }
118     }
119     foreach $sym (@{$data->{DL_VARS}}) {
120         print OPT "PSECT_ATTR=${sym},PIC,OVR,RD,NOEXE,WRT,NOSHR\n";
121         if ($isvax) { print OPT "UNIVERSAL=$sym\n" }
122         else        { print OPT "SYMBOL_VECTOR=($sym=DATA)\n"; }
123     }
124     close OPT;
125
126     # Options file specifying RTLs to which this extension must be linked.
127     # Eventually, the list of libraries will be supplied by a working
128     # extliblist routine.
129     open OPT,'>rtls.opt';
130     print OPT "PerlShr/Share\n";
131     foreach $rtl (split(/\s+/,$Config::Config{'libs'})) { print OPT "$rtl\n"; }
132     close OPT;
133 }
134
135 1;
136
137 __END__
138
139 =head1 NAME
140
141 ExtUtils::Mksymlists - write linker options files for dynamic extension
142
143 =head1 SYNOPSIS
144
145     use ExtUtils::Mksymlists;
146     Mksymlists({ NAME     => $name ,
147                  DL_VARS  => [ $var1, $var2, $var3 ],
148                  DL_FUNCS => { $pkg1 => [ $func1, $func2 ],
149                                $pkg2 => [ $func3 ] });
150
151 =head1 DESCRIPTION
152
153 C<ExtUtils::Mksymlists> produces files used by the linker under some OSs
154 during the creation of shared libraries for synamic extensions.  It is
155 normally called from a MakeMaker-generated Makefile when the extension
156 is built.  The linker option file is generated by calling the function
157 C<Mksymlists>, which is exported by default from C<ExtUtils::Mksymlists>.
158 It takes one argument, a list of key-value pairs, in which the following
159 keys are recognized:
160
161 =item NAME
162
163 This gives the name of the extension (I<e.g.> Tk::Canvas) for which
164 the linker option file will be produced.
165
166 =item DL_FUNCS
167
168 This is identical to the DL_FUNCS attribute available via MakeMaker,
169 from which it is usually taken.  Its value is a reference to an
170 associative array, in which each key is the name of a package, and
171 each value is an a reference to an array of function names which
172 should be exported by the extension.  For instance, one might say
173 C<DL_FUNCS =E<gt> { Homer::Iliad   =E<gt> [ qw(trojans greeks) ],
174 Homer::Odyssey =E<gt> [ qw(travellers family suitors) ] }>.  The
175 function names should be identical to those in the XSUB code;
176 C<Mksymlists> will alter the names written to the linker option
177 file to match the changes made by F<xsubpp>.  In addition, if
178 none of the functions in a list begin with the string B<boot_>,
179 C<Mksymlists> will add a bootstrap function for that package,
180 just as xsubpp does.  (If a B<boot_E<lt>pkgE<gt>> function is
181 present in the list, it is passed through unchanged.)  If
182 DL_FUNCS is not specified, it defaults to the bootstrap
183 function for the extension specified in NAME.
184
185 =item DL_VARS
186
187 This is identical to the DL_VARS attribute available via MakeMaker,
188 and, like DL_FUNCS, it is usually specified via MakeMaker.  Its
189 value is a reference to an array of variable names which should
190 be exported by the extension.
191
192 =item FILE
193
194 This key can be used to specify the name of the linker option file
195 (minus the OS-specific extension), if for some reason you do not
196 want to use the default value, which is the last word of the NAME
197 attribute (I<e.g.> for Tk::Canvas, FILE defaults to 'Canvas').
198
199 =item FUNCLIST
200
201 This provides an alternate means to specify function names to be
202 exported from the extension.  Its value is a reference to an
203 array of function names to be exported by the extension.  These
204 names are passed through unaltered to the linker options file.
205
206 =item DLBASE
207
208 This item specifies the name by which the linker knows the
209 extension, which may be different from the name of the
210 extension itself (for instance, some linkers add an '_' to the
211 name of the extension).  If it is not specified, it is derived
212 from the NAME attribute.  It is presently used only by OS2.
213
214 When calling C<Mksymlists>, one should always specify the NAME
215 attribute.  In most cases, this is all that's necessary.  In
216 the case of unusual extensions, however, the other attributes
217 can be used to provide additional information to the linker.
218
219 =head1 AUTHOR
220
221 Charles Bailey I<E<lt>bailey@genetics.upenn.eduE<gt>>
222
223 =head1 REVISION
224
225 Last revised 14-Feb-1996, for Perl 5.002.