/extlib/lib/perl5/ExtUtils/Mksymlists.pm

http://github.com/tokuhirom/mobirc · Perl · 312 lines · 239 code · 57 blank · 16 comment · 41 complexity · bdd10e085dba32aff6887144319896b7 MD5 · raw file

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