Shlibs.pm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright (C) 2007 Raphael Hertzog
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License along
  11. # with this program; if not, write to the Free Software Foundation, Inc.,
  12. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  13. package Dpkg::Shlibs;
  14. use strict;
  15. use warnings;
  16. use base qw(Exporter);
  17. our @EXPORT_OK = qw(@librarypaths find_library);
  18. use File::Spec;
  19. use Dpkg::Gettext;
  20. use Dpkg::ErrorHandling;
  21. use Dpkg::Shlibs::Objdump;
  22. use Dpkg::Path qw(resolve_symlink canonpath);
  23. use Dpkg::Arch qw(debarch_to_gnutriplet get_build_arch get_host_arch);
  24. use constant DEFAULT_LIBRARY_PATH =>
  25. qw(/lib /usr/lib /lib32 /usr/lib32 /lib64 /usr/lib64
  26. /emul/ia32-linux/lib /emul/ia32-linux/usr/lib);
  27. # Adjust set of directories to consider when we're in a situation of a
  28. # cross-build or a build of a cross-compiler
  29. my @crosslibrarypaths;
  30. my $crossprefix;
  31. # Detect cross compiler builds
  32. if ($ENV{GCC_TARGET}) {
  33. $crossprefix = debarch_to_gnutriplet($ENV{GCC_TARGET});
  34. }
  35. if ($ENV{DEB_TARGET_GNU_TYPE} and
  36. ($ENV{DEB_TARGET_GNU_TYPE} ne $ENV{DEB_BUILD_GNU_TYPE}))
  37. {
  38. $crossprefix = $ENV{DEB_TARGET_GNU_TYPE};
  39. }
  40. # host for normal cross builds.
  41. if (get_build_arch() ne get_host_arch()) {
  42. $crossprefix = debarch_to_gnutriplet(get_host_arch());
  43. }
  44. # Define list of directories containing crossbuilt libraries
  45. if ($crossprefix) {
  46. push @crosslibrarypaths, "/$crossprefix/lib", "/usr/$crossprefix/lib",
  47. "/$crossprefix/lib32", "/usr/$crossprefix/lib32",
  48. "/$crossprefix/lib64", "/usr/$crossprefix/lib64";
  49. }
  50. our @librarypaths = (DEFAULT_LIBRARY_PATH, @crosslibrarypaths);
  51. # Update library paths with LD_LIBRARY_PATH
  52. if ($ENV{LD_LIBRARY_PATH}) {
  53. foreach my $path (reverse split( /:/, $ENV{LD_LIBRARY_PATH} )) {
  54. $path =~ s{/+$}{};
  55. unshift @librarypaths, $path;
  56. }
  57. }
  58. # Update library paths with ld.so config
  59. parse_ldso_conf("/etc/ld.so.conf") if -e "/etc/ld.so.conf";
  60. my %visited;
  61. sub parse_ldso_conf {
  62. my $file = shift;
  63. open my $fh, "<", $file or syserr(_g("cannot open %s"), $file);
  64. $visited{$file}++;
  65. while (<$fh>) {
  66. next if /^\s*$/;
  67. chomp;
  68. s{/+$}{};
  69. if (/^include\s+(\S.*\S)\s*$/) {
  70. foreach my $include (glob($1)) {
  71. parse_ldso_conf($include) if -e $include
  72. && !$visited{$include};
  73. }
  74. } elsif (m{^\s*/}) {
  75. s/^\s+//;
  76. my $libdir = $_;
  77. unless (scalar grep { $_ eq $libdir } @librarypaths) {
  78. push @librarypaths, $libdir;
  79. }
  80. }
  81. }
  82. close $fh;
  83. }
  84. # find_library ($soname, \@rpath, $format, $root)
  85. sub find_library {
  86. my ($lib, $rpath, $format, $root) = @_;
  87. $root = "" if not defined($root);
  88. $root =~ s{/+$}{};
  89. my @rpath = @{$rpath};
  90. foreach my $dir (@rpath, @librarypaths) {
  91. my $checkdir = "$root$dir";
  92. # If the directory checked is a symlink, check if it doesn't
  93. # resolve to another public directory (which is then the canonical
  94. # directory to use instead of this one). Typical example
  95. # is /usr/lib64 -> /usr/lib on amd64.
  96. if (-l $checkdir) {
  97. my $newdir = resolve_symlink($checkdir);
  98. if (grep { "$root$_" eq "$newdir" } (@rpath, @librarypaths)) {
  99. $checkdir = $newdir;
  100. }
  101. }
  102. if (-e "$checkdir/$lib") {
  103. my $libformat = Dpkg::Shlibs::Objdump::get_format("$checkdir/$lib");
  104. if ($format eq $libformat) {
  105. return canonpath("$checkdir/$lib");
  106. }
  107. }
  108. }
  109. return undef;
  110. }
  111. 1;