Shlibs.pm 3.7 KB

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