PageRenderTime 61ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/cpan/ExtUtils-MakeMaker/lib/ExtUtils/Mksymlists.pm

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