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