Shlibs.pm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. use base qw(Exporter);
  19. our @EXPORT_OK = qw(@librarypaths find_library);
  20. use File::Spec;
  21. use Dpkg::Gettext;
  22. use Dpkg::ErrorHandling;
  23. use Dpkg::Shlibs::Objdump;
  24. use Dpkg::Path qw(resolve_symlink canonpath);
  25. use Dpkg::Arch qw(debarch_to_gnutriplet get_build_arch get_host_arch);
  26. use constant DEFAULT_LIBRARY_PATH =>
  27. qw(/lib /usr/lib /lib32 /usr/lib32 /lib64 /usr/lib64
  28. /emul/ia32-linux/lib /emul/ia32-linux/usr/lib);
  29. # Adjust set of directories to consider when we're in a situation of a
  30. # cross-build or a build of a cross-compiler
  31. my @crosslibrarypaths;
  32. my $crossprefix;
  33. # Detect cross compiler builds
  34. if ($ENV{GCC_TARGET}) {
  35. $crossprefix = debarch_to_gnutriplet($ENV{GCC_TARGET});
  36. }
  37. if ($ENV{DEB_TARGET_GNU_TYPE} and
  38. ($ENV{DEB_TARGET_GNU_TYPE} ne $ENV{DEB_BUILD_GNU_TYPE}))
  39. {
  40. $crossprefix = $ENV{DEB_TARGET_GNU_TYPE};
  41. }
  42. # host for normal cross builds.
  43. if (get_build_arch() ne get_host_arch()) {
  44. $crossprefix = debarch_to_gnutriplet(get_host_arch());
  45. }
  46. # Define list of directories containing crossbuilt libraries
  47. if ($crossprefix) {
  48. push @crosslibrarypaths, "/$crossprefix/lib", "/usr/$crossprefix/lib",
  49. "/$crossprefix/lib32", "/usr/$crossprefix/lib32",
  50. "/$crossprefix/lib64", "/usr/$crossprefix/lib64";
  51. }
  52. our @librarypaths = (DEFAULT_LIBRARY_PATH, @crosslibrarypaths);
  53. # Update library paths with LD_LIBRARY_PATH
  54. if ($ENV{LD_LIBRARY_PATH}) {
  55. foreach my $path (reverse split( /:/, $ENV{LD_LIBRARY_PATH} )) {
  56. $path =~ s{/+$}{};
  57. unshift @librarypaths, $path;
  58. }
  59. }
  60. # Update library paths with ld.so config
  61. parse_ldso_conf("/etc/ld.so.conf") if -e "/etc/ld.so.conf";
  62. my %visited;
  63. sub parse_ldso_conf {
  64. my $file = shift;
  65. open my $fh, "<", $file or syserr(_g("cannot open %s"), $file);
  66. $visited{$file}++;
  67. while (<$fh>) {
  68. next if /^\s*$/;
  69. chomp;
  70. s{/+$}{};
  71. if (/^include\s+(\S.*\S)\s*$/) {
  72. foreach my $include (glob($1)) {
  73. parse_ldso_conf($include) if -e $include
  74. && !$visited{$include};
  75. }
  76. } elsif (m{^\s*/}) {
  77. s/^\s+//;
  78. my $libdir = $_;
  79. unless (scalar grep { $_ eq $libdir } @librarypaths) {
  80. push @librarypaths, $libdir;
  81. }
  82. }
  83. }
  84. close $fh;
  85. }
  86. # find_library ($soname, \@rpath, $format, $root)
  87. sub find_library {
  88. my ($lib, $rpath, $format, $root) = @_;
  89. $root = "" if not defined($root);
  90. $root =~ s{/+$}{};
  91. my @rpath = @{$rpath};
  92. foreach my $dir (@rpath, @librarypaths) {
  93. my $checkdir = "$root$dir";
  94. # If the directory checked is a symlink, check if it doesn't
  95. # resolve to another public directory (which is then the canonical
  96. # directory to use instead of this one). Typical example
  97. # is /usr/lib64 -> /usr/lib on amd64.
  98. if (-l $checkdir) {
  99. my $newdir = resolve_symlink($checkdir);
  100. if (grep { "$root$_" eq "$newdir" } (@rpath, @librarypaths)) {
  101. $checkdir = $newdir;
  102. }
  103. }
  104. if (-e "$checkdir/$lib") {
  105. my $libformat = Dpkg::Shlibs::Objdump::get_format("$checkdir/$lib");
  106. if ($format eq $libformat) {
  107. return canonpath("$checkdir/$lib");
  108. }
  109. }
  110. }
  111. return undef;
  112. }
  113. 1;