Shlibs.pm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # Copyright © 2007 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. our $VERSION = '0.02';
  20. our @EXPORT_OK = qw(
  21. blank_library_paths
  22. setup_library_paths
  23. get_library_paths
  24. add_library_dir
  25. find_library
  26. );
  27. use Exporter qw(import);
  28. use File::Spec;
  29. use Dpkg::Gettext;
  30. use Dpkg::ErrorHandling;
  31. use Dpkg::Shlibs::Objdump;
  32. use Dpkg::Util qw(:list);
  33. use Dpkg::Path qw(resolve_symlink canonpath);
  34. use Dpkg::Arch qw(debarch_to_gnutriplet get_build_arch get_host_arch
  35. gnutriplet_to_multiarch debarch_to_multiarch);
  36. use constant DEFAULT_LIBRARY_PATH =>
  37. qw(/lib /usr/lib);
  38. # XXX: Deprecated multilib paths.
  39. use constant DEFAULT_MULTILIB_PATH =>
  40. qw(/lib32 /usr/lib32 /lib64 /usr/lib64);
  41. my @librarypaths;
  42. my $librarypaths_init;
  43. my %visited;
  44. sub parse_ldso_conf {
  45. my $file = shift;
  46. local $_;
  47. open my $fh, '<', $file or syserr(g_('cannot open %s'), $file);
  48. $visited{$file}++;
  49. while (<$fh>) {
  50. next if /^\s*$/;
  51. chomp;
  52. s{/+$}{};
  53. if (/^include\s+(\S.*\S)\s*$/) {
  54. foreach my $include (glob($1)) {
  55. parse_ldso_conf($include) if -e $include
  56. && !$visited{$include};
  57. }
  58. } elsif (m{^\s*/}) {
  59. s/^\s+//;
  60. my $libdir = $_;
  61. if (none { $_ eq $libdir } @librarypaths) {
  62. push @librarypaths, $libdir;
  63. }
  64. }
  65. }
  66. close $fh;
  67. }
  68. sub blank_library_paths {
  69. @librarypaths = ();
  70. $librarypaths_init = 1;
  71. }
  72. sub setup_library_paths {
  73. @librarypaths = ();
  74. # XXX: Deprecated. Update library paths with LD_LIBRARY_PATH.
  75. if ($ENV{LD_LIBRARY_PATH}) {
  76. foreach my $path (split /:/, $ENV{LD_LIBRARY_PATH}) {
  77. $path =~ s{/+$}{};
  78. push @librarypaths, $path;
  79. }
  80. }
  81. # Adjust set of directories to consider when we're in a situation of a
  82. # cross-build or a build of a cross-compiler.
  83. my $multiarch;
  84. # Detect cross compiler builds.
  85. if ($ENV{DEB_TARGET_GNU_TYPE} and
  86. ($ENV{DEB_TARGET_GNU_TYPE} ne $ENV{DEB_BUILD_GNU_TYPE}))
  87. {
  88. $multiarch = gnutriplet_to_multiarch($ENV{DEB_TARGET_GNU_TYPE});
  89. }
  90. # Host for normal cross builds.
  91. if (get_build_arch() ne get_host_arch()) {
  92. $multiarch = debarch_to_multiarch(get_host_arch());
  93. }
  94. # Define list of directories containing crossbuilt libraries.
  95. if ($multiarch) {
  96. push @librarypaths, "/lib/$multiarch", "/usr/lib/$multiarch";
  97. }
  98. push @librarypaths, DEFAULT_LIBRARY_PATH;
  99. # Update library paths with ld.so config.
  100. parse_ldso_conf('/etc/ld.so.conf') if -e '/etc/ld.so.conf';
  101. push @librarypaths, DEFAULT_MULTILIB_PATH;
  102. $librarypaths_init = 1;
  103. }
  104. sub add_library_dir {
  105. my $dir = shift;
  106. setup_library_paths() if not $librarypaths_init;
  107. unshift @librarypaths, $dir;
  108. }
  109. sub get_library_paths {
  110. setup_library_paths() if not $librarypaths_init;
  111. return @librarypaths;
  112. }
  113. # find_library ($soname, \@rpath, $format, $root)
  114. sub find_library {
  115. my ($lib, $rpath, $format, $root) = @_;
  116. setup_library_paths() if not $librarypaths_init;
  117. $root //= '';
  118. $root =~ s{/+$}{};
  119. my @rpath = @{$rpath};
  120. foreach my $dir (@rpath, @librarypaths) {
  121. my $checkdir = "$root$dir";
  122. # If the directory checked is a symlink, check if it doesn't
  123. # resolve to another public directory (which is then the canonical
  124. # directory to use instead of this one). Typical example
  125. # is /usr/lib64 -> /usr/lib on amd64.
  126. if (-l $checkdir) {
  127. my $newdir = resolve_symlink($checkdir);
  128. if (any { "$root$_" eq "$newdir" } (@rpath, @librarypaths)) {
  129. $checkdir = $newdir;
  130. }
  131. }
  132. if (-e "$checkdir/$lib") {
  133. my $libformat = Dpkg::Shlibs::Objdump::get_format("$checkdir/$lib");
  134. if ($format eq $libformat) {
  135. return canonpath("$checkdir/$lib");
  136. }
  137. }
  138. }
  139. return;
  140. }
  141. 1;