Shlibs.pm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 <http://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. open my $fh, '<', $file or syserr(_g('cannot open %s'), $file);
  74. $visited{$file}++;
  75. while (<$fh>) {
  76. next if /^\s*$/;
  77. chomp;
  78. s{/+$}{};
  79. if (/^include\s+(\S.*\S)\s*$/) {
  80. foreach my $include (glob($1)) {
  81. parse_ldso_conf($include) if -e $include
  82. && !$visited{$include};
  83. }
  84. } elsif (m{^\s*/}) {
  85. s/^\s+//;
  86. my $libdir = $_;
  87. if (none { $_ eq $libdir } @librarypaths) {
  88. push @librarypaths, $libdir;
  89. }
  90. }
  91. }
  92. close $fh;
  93. }
  94. sub add_library_dir {
  95. my ($dir) = @_;
  96. unshift @librarypaths, $dir;
  97. }
  98. sub get_library_paths {
  99. return @librarypaths;
  100. }
  101. sub reset_library_paths {
  102. @librarypaths = ();
  103. }
  104. # find_library ($soname, \@rpath, $format, $root)
  105. sub find_library {
  106. my ($lib, $rpath, $format, $root) = @_;
  107. $root //= '';
  108. $root =~ s{/+$}{};
  109. my @rpath = @{$rpath};
  110. foreach my $dir (@rpath, @librarypaths) {
  111. my $checkdir = "$root$dir";
  112. # If the directory checked is a symlink, check if it doesn't
  113. # resolve to another public directory (which is then the canonical
  114. # directory to use instead of this one). Typical example
  115. # is /usr/lib64 -> /usr/lib on amd64.
  116. if (-l $checkdir) {
  117. my $newdir = resolve_symlink($checkdir);
  118. if (any { "$root$_" eq "$newdir" } (@rpath, @librarypaths)) {
  119. $checkdir = $newdir;
  120. }
  121. }
  122. if (-e "$checkdir/$lib") {
  123. my $libformat = Dpkg::Shlibs::Objdump::get_format("$checkdir/$lib");
  124. if ($format eq $libformat) {
  125. return canonpath("$checkdir/$lib");
  126. }
  127. }
  128. }
  129. return;
  130. }
  131. 1;