Shlibs.pm 4.5 KB

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