Shlibs.pm 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 Dpkg::Gettext;
  19. use Dpkg::ErrorHandling qw(syserr);
  20. use Dpkg::Shlibs::Objdump;
  21. textdomain("dpkg-dev");
  22. use constant DEFAULT_LIBRARY_PATH =>
  23. qw(/lib /usr/lib /lib32 /usr/lib32 /lib64 /usr/lib64
  24. /emul/ia32-linux/lib /emul/ia32-linux/usr/lib);
  25. our @librarypaths = (DEFAULT_LIBRARY_PATH);
  26. # Update library paths with LD_LIBRARY_PATH
  27. if ($ENV{LD_LIBRARY_PATH}) {
  28. foreach my $path (reverse split( /:/, $ENV{LD_LIBRARY_PATH} )) {
  29. $path =~ s{/+$}{};
  30. unless (scalar grep { $_ eq $path } @librarypaths) {
  31. unshift @librarypaths, $path;
  32. }
  33. }
  34. }
  35. # Update library paths with ld.so config
  36. parse_ldso_conf("/etc/ld.so.conf") if -e "/etc/ld.so.conf";
  37. my %visited;
  38. sub parse_ldso_conf {
  39. my $file = shift;
  40. open my $fh, "<", $file
  41. or syserr(sprintf(_g("couldn't open %s"), $file));
  42. $visited{$file}++;
  43. while (<$fh>) {
  44. next if /^\s*$/;
  45. chomp;
  46. s{/+$}{};
  47. if (/^include\s+(\S.*\S)\s*$/) {
  48. foreach my $include (glob($1)) {
  49. parse_ldso_conf($include) if -e $include
  50. && !$visited{$include};
  51. }
  52. } elsif (m{^\s*/}) {
  53. s/^\s+//;
  54. my $libdir = $_;
  55. unless (scalar grep { $_ eq $libdir } @librarypaths) {
  56. push @librarypaths, $libdir;
  57. }
  58. }
  59. }
  60. close $fh or syserr(sprintf(_g("couldn't close %s"), $file));;
  61. }
  62. # find_library ($soname, \@rpath, $format, $root)
  63. sub find_library {
  64. my ($lib, $rpath, $format, $root) = @_;
  65. $root = "" if not defined($root);
  66. $root =~ s{/+$}{};
  67. my @rpath = @{$rpath};
  68. foreach my $dir (@rpath, @librarypaths) {
  69. if (-e "$root$dir/$lib") {
  70. my $libformat = Dpkg::Shlibs::Objdump::get_format("$root$dir/$lib");
  71. if ($format eq $libformat) {
  72. return "$root$dir/$lib";
  73. }
  74. }
  75. }
  76. return undef;
  77. }
  78. 1;