XSLoader.pm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. # Copyright 2014, 2015, 2016 Free Software Foundation, Inc.
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License,
  6. # or (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package Texinfo::XSLoader;
  16. use DynaLoader;
  17. use 5.00405;
  18. use strict;
  19. use warnings;
  20. our $TEXINFO_XS;
  21. our $VERSION = '6.3.90';
  22. our $disable_XS;
  23. # For verbose information about what's being done
  24. sub _debug($) {
  25. if ($TEXINFO_XS eq 'debug') {
  26. my $msg = shift;
  27. warn $msg . "\n";
  28. }
  29. }
  30. # For messages to say that XS module couldn't be loaded
  31. sub _fatal($) {
  32. if ($TEXINFO_XS eq 'debug'
  33. or $TEXINFO_XS eq 'required'
  34. or $TEXINFO_XS eq 'warn') {
  35. my $msg = shift;
  36. warn $msg . "\n";
  37. }
  38. }
  39. # We look for the .la and .so files in @INC because this allows us to override
  40. # which modules are used using -I flags to "perl".
  41. sub _find_file($) {
  42. my $file = shift;
  43. for my $dir (@INC) {
  44. _debug "checking $dir/$file";
  45. if (-f "$dir/$file") {
  46. _debug "found $dir/$file";
  47. return ($dir, "$dir/$file");
  48. }
  49. }
  50. return undef;
  51. }
  52. # Make symbols accessible under
  53. # namespace $FULL_MODULE_NAME (e.g. Texinfo::Convert::Paragraph),
  54. # either from XS implementation in $MODULE, or non-XS implementation
  55. # $FALLBACK_MODULE. $MODULE_NAME is the name of a Libtool file used for
  56. # loading the XS subroutines.
  57. # $INTERFACE_VERSION is a module interface number, to be changed when the XS
  58. # interface changes.
  59. sub init {
  60. my ($full_module_name,
  61. $module,
  62. $fallback_module,
  63. $module_name,
  64. $interface_version,
  65. $warning_message,
  66. $fatal_message
  67. ) = @_;
  68. # Possible values for TEXINFO_XS environment variable:
  69. #
  70. # TEXINFO_XS=omit # don't try loading xs at all
  71. # TEXINFO_XS=default # try xs, libtool and then perl paths,
  72. # # silent fallback
  73. # TEXINFO_XS=libtool # try xs, libtool only, silent fallback
  74. # TEXINFO_XS=standalone # try xs, perl paths only, silent fallback
  75. # TEXINFO_XS=warn # try xs, libtool and then perl paths, warn
  76. # # on failure
  77. # TEXINFO_XS=required # abort if not loadable, no fallback
  78. # TEXINFO_XS=debug # voluminuous debugging
  79. #
  80. # Other values are treated at the moment as 'default'.
  81. $TEXINFO_XS = $ENV{'TEXINFO_XS'};
  82. if (!defined($TEXINFO_XS)) {
  83. $TEXINFO_XS = '';
  84. }
  85. if ($TEXINFO_XS eq 'omit') {
  86. # Don't try to use the XS module
  87. goto FALLBACK;
  88. }
  89. if ($disable_XS) {
  90. _fatal "use of XS modules was disabled when Texinfo was built";
  91. goto FALLBACK;
  92. }
  93. if ($warning_message) {
  94. _debug $warning_message;
  95. }
  96. if ($fatal_message) {
  97. _fatal $fatal_message;
  98. goto FALLBACK;
  99. }
  100. if (!$module) {
  101. goto FALLBACK;
  102. }
  103. my ($libtool_dir, $libtool_archive);
  104. if ($TEXINFO_XS ne 'standalone') {
  105. ($libtool_dir, $libtool_archive) = _find_file("$module_name.la");
  106. if (!$libtool_archive) {
  107. if ($TEXINFO_XS eq 'libtool') {
  108. _fatal "$module_name: couldn't find Libtool archive file";
  109. goto FALLBACK;
  110. }
  111. _debug "$module_name: couldn't find Libtool archive file";
  112. }
  113. }
  114. my $dlname = undef;
  115. my $dlpath = undef;
  116. # Try perl paths
  117. if (!$libtool_archive) {
  118. my @modparts = split(/::/,$module);
  119. my $dlname = $modparts[-1];
  120. my $modpname = join('/',@modparts);
  121. # the directories with -L prepended setup directories to
  122. # be in the search path. Then $dlname is prepended as it is
  123. # the name really searched for.
  124. $dlpath = DynaLoader::dl_findfile(map("-L$_/auto/$modpname", @INC), $dlname);
  125. if (!$dlpath) {
  126. _fatal "$module_name: couldn't find $module";
  127. goto FALLBACK;
  128. }
  129. goto LOAD;
  130. }
  131. my $fh;
  132. open $fh, $libtool_archive;
  133. if (!$fh) {
  134. _fatal "$module_name: couldn't open Libtool archive file";
  135. goto FALLBACK;
  136. }
  137. # Look for the line in XS*.la giving the name of the loadable object.
  138. while (my $line = <$fh>) {
  139. if ($line =~ /^\s*dlname\s*=\s*'([^']+)'\s$/) {
  140. $dlname = $1;
  141. last;
  142. }
  143. }
  144. if (!$dlname) {
  145. _fatal "$module_name: couldn't find name of shared object";
  146. goto FALLBACK;
  147. }
  148. # The *.so file is under .libs in the source directory.
  149. push @DynaLoader::dl_library_path, $libtool_dir;
  150. push @DynaLoader::dl_library_path, "$libtool_dir/.libs";
  151. $dlpath = DynaLoader::dl_findfile($dlname);
  152. if (!$dlpath) {
  153. _fatal "$module_name: couldn't find $dlname";
  154. goto FALLBACK;
  155. }
  156. LOAD:
  157. #my $flags = dl_load_flags $module; # This is 0 in DynaLoader
  158. my $flags = 0;
  159. my $libref = DynaLoader::dl_load_file($dlpath, $flags);
  160. if (!$libref) {
  161. _fatal "$module_name: couldn't load file $dlpath";
  162. goto FALLBACK;
  163. }
  164. _debug "$dlpath loaded";
  165. my @undefined_symbols = DynaLoader::dl_undef_symbols();
  166. if ($#undefined_symbols+1 != 0) {
  167. _fatal "$module_name: still have undefined symbols after dl_load_file";
  168. }
  169. my $bootname = "boot_$module";
  170. $bootname =~ s/:/_/g;
  171. _debug "looking for $bootname";
  172. my $symref = DynaLoader::dl_find_symbol($libref, $bootname);
  173. if (!$symref) {
  174. _fatal "$module_name: couldn't find $bootname symbol";
  175. goto FALLBACK;
  176. }
  177. _debug "trying to call $bootname...";
  178. my $boot_fn = DynaLoader::dl_install_xsub("${module}::bootstrap",
  179. $symref, $dlname);
  180. if (!$boot_fn) {
  181. _fatal "$module_name: couldn't bootstrap";
  182. goto FALLBACK;
  183. }
  184. _debug " ...succeeded";
  185. push @DynaLoader::dl_shared_objects, $dlpath; # record files loaded
  186. # This is the module bootstrap function, which causes all the other
  187. # functions (XSUB's) provided by the module to become available to
  188. # be called from Perl code.
  189. &$boot_fn($module, $interface_version);
  190. # This makes it easier to refer to packages and symbols by name.
  191. no strict 'refs';
  192. if (defined &{"${module}::init"} and !&{"${module}::init"} ()) {
  193. _fatal "$module_name: error initializing";
  194. goto FALLBACK;
  195. }
  196. *{"${full_module_name}::"} = \%{"${module}::"};
  197. return $module;
  198. FALLBACK:
  199. if ($TEXINFO_XS eq 'required') {
  200. die "unset the TEXINFO_XS environment variable to use the "
  201. ."pure Perl modules\n";
  202. } elsif ($TEXINFO_XS eq 'warn' or $TEXINFO_XS eq 'debug') {
  203. warn "falling back to pure Perl module\n";
  204. }
  205. if (!defined $fallback_module) {
  206. die "no fallback module for $full_module_name";
  207. }
  208. # Fall back to using the Perl code.
  209. # Use eval here to interpret :: properly in module name.
  210. eval "require $fallback_module";
  211. *{"${full_module_name}::"} = \%{"${fallback_module}::"};
  212. return $fallback_module;
  213. } # end init
  214. # Override subroutine $TARGET with $SOURCE.
  215. sub override {
  216. my ($target, $source) = @_;
  217. _debug "attempting to override $target with $source...";
  218. no strict 'refs'; # access modules and symbols by name.
  219. no warnings 'redefine'; # do not warn about redefining a function.
  220. if (defined &{"${source}"}) {
  221. *{"${target}"} = \&{"${source}"};
  222. _debug " ...succeeded";
  223. } else {
  224. _debug " ...failed";
  225. }
  226. }
  227. 1;
  228. __END__