Shlibs.pm 4.4 KB

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