Shlibs.pm 4.3 KB

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