Shlibs.pm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # Copyright © 2007, 2016 Raphaël Hertzog <hertzog@debian.org>
  2. # Copyright © 2007-2008, 2012-2015 Guillem Jover <guillem@debian.org>
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package Dpkg::Shlibs;
  17. use strict;
  18. use warnings;
  19. use feature qw(state);
  20. our $VERSION = '0.03';
  21. our @EXPORT_OK = qw(
  22. blank_library_paths
  23. setup_library_paths
  24. get_library_paths
  25. add_library_dir
  26. find_library
  27. );
  28. use Exporter qw(import);
  29. use File::Spec;
  30. use Dpkg::Gettext;
  31. use Dpkg::ErrorHandling;
  32. use Dpkg::Shlibs::Objdump;
  33. use Dpkg::Util qw(:list);
  34. use Dpkg::Path qw(resolve_symlink canonpath);
  35. use Dpkg::Arch qw(debarch_to_gnutriplet get_build_arch get_host_arch
  36. gnutriplet_to_multiarch debarch_to_multiarch);
  37. use constant DEFAULT_LIBRARY_PATH =>
  38. qw(/lib /usr/lib);
  39. # XXX: Deprecated multilib paths.
  40. use constant DEFAULT_MULTILIB_PATH =>
  41. qw(/lib32 /usr/lib32 /lib64 /usr/lib64);
  42. # Library paths set by the user.
  43. my @custom_librarypaths;
  44. # Library paths from the system.
  45. my @system_librarypaths;
  46. my $librarypaths_init;
  47. sub parse_ldso_conf {
  48. my $file = shift;
  49. state %visited;
  50. local $_;
  51. open my $fh, '<', $file or syserr(g_('cannot open %s'), $file);
  52. $visited{$file}++;
  53. while (<$fh>) {
  54. next if /^\s*$/;
  55. chomp;
  56. s{/+$}{};
  57. if (/^include\s+(\S.*\S)\s*$/) {
  58. foreach my $include (glob($1)) {
  59. parse_ldso_conf($include) if -e $include
  60. && !$visited{$include};
  61. }
  62. } elsif (m{^\s*/}) {
  63. s/^\s+//;
  64. my $libdir = $_;
  65. if (none { $_ eq $libdir } (@custom_librarypaths, @system_librarypaths)) {
  66. push @system_librarypaths, $libdir;
  67. }
  68. }
  69. }
  70. close $fh;
  71. }
  72. sub blank_library_paths {
  73. @custom_librarypaths = ();
  74. @system_librarypaths = ();
  75. $librarypaths_init = 1;
  76. }
  77. sub setup_library_paths {
  78. @custom_librarypaths = ();
  79. @system_librarypaths = ();
  80. # XXX: Deprecated. Update library paths with LD_LIBRARY_PATH.
  81. if ($ENV{LD_LIBRARY_PATH}) {
  82. foreach my $path (split /:/, $ENV{LD_LIBRARY_PATH}) {
  83. $path =~ s{/+$}{};
  84. # XXX: This should be added to @custom_librarypaths, but as this
  85. # is deprecated we do not care as the code will go away.
  86. push @system_librarypaths, $path;
  87. }
  88. }
  89. # Adjust set of directories to consider when we're in a situation of a
  90. # cross-build or a build of a cross-compiler.
  91. my $multiarch;
  92. # Detect cross compiler builds.
  93. if ($ENV{DEB_TARGET_GNU_TYPE} and
  94. ($ENV{DEB_TARGET_GNU_TYPE} ne $ENV{DEB_BUILD_GNU_TYPE}))
  95. {
  96. $multiarch = gnutriplet_to_multiarch($ENV{DEB_TARGET_GNU_TYPE});
  97. }
  98. # Host for normal cross builds.
  99. if (get_build_arch() ne get_host_arch()) {
  100. $multiarch = debarch_to_multiarch(get_host_arch());
  101. }
  102. # Define list of directories containing crossbuilt libraries.
  103. if ($multiarch) {
  104. push @system_librarypaths, "/lib/$multiarch", "/usr/lib/$multiarch";
  105. }
  106. push @system_librarypaths, DEFAULT_LIBRARY_PATH;
  107. # Update library paths with ld.so config.
  108. parse_ldso_conf('/etc/ld.so.conf') if -e '/etc/ld.so.conf';
  109. push @system_librarypaths, DEFAULT_MULTILIB_PATH;
  110. $librarypaths_init = 1;
  111. }
  112. sub add_library_dir {
  113. my $dir = shift;
  114. setup_library_paths() if not $librarypaths_init;
  115. push @custom_librarypaths, $dir;
  116. }
  117. sub get_library_paths {
  118. setup_library_paths() if not $librarypaths_init;
  119. return (@custom_librarypaths, @system_librarypaths);
  120. }
  121. # find_library ($soname, \@rpath, $format, $root)
  122. sub find_library {
  123. my ($lib, $rpath, $format, $root) = @_;
  124. setup_library_paths() if not $librarypaths_init;
  125. my @librarypaths = (@{$rpath}, @custom_librarypaths, @system_librarypaths);
  126. my @libs;
  127. $root //= '';
  128. $root =~ s{/+$}{};
  129. foreach my $dir (@librarypaths) {
  130. my $checkdir = "$root$dir";
  131. if (-e "$checkdir/$lib") {
  132. my $libformat = Dpkg::Shlibs::Objdump::get_format("$checkdir/$lib");
  133. if ($format eq $libformat) {
  134. push @libs, canonpath("$checkdir/$lib");
  135. }
  136. }
  137. }
  138. return @libs;
  139. }
  140. 1;