Shlibs.pm 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 qw(syserr);
  21. use Dpkg::Shlibs::Objdump;
  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. unshift @librarypaths, $path;
  31. }
  32. }
  33. # Update library paths with ld.so config
  34. parse_ldso_conf("/etc/ld.so.conf") if -e "/etc/ld.so.conf";
  35. my %visited;
  36. sub parse_ldso_conf {
  37. my $file = shift;
  38. open my $fh, "<", $file
  39. or syserr(sprintf(_g("couldn't open %s"), $file));
  40. $visited{$file}++;
  41. while (<$fh>) {
  42. next if /^\s*$/;
  43. chomp;
  44. s{/+$}{};
  45. if (/^include\s+(\S.*\S)\s*$/) {
  46. foreach my $include (glob($1)) {
  47. parse_ldso_conf($include) if -e $include
  48. && !$visited{$include};
  49. }
  50. } elsif (m{^\s*/}) {
  51. s/^\s+//;
  52. my $libdir = $_;
  53. unless (scalar grep { $_ eq $libdir } @librarypaths) {
  54. push @librarypaths, $libdir;
  55. }
  56. }
  57. }
  58. close $fh or syserr(sprintf(_g("couldn't close %s"), $file));;
  59. }
  60. # find_library ($soname, \@rpath, $format, $root)
  61. sub find_library {
  62. my ($lib, $rpath, $format, $root) = @_;
  63. $root = "" if not defined($root);
  64. $root =~ s{/+$}{};
  65. my @rpath = @{$rpath};
  66. foreach my $dir (@rpath, @librarypaths) {
  67. if (-e "$root$dir/$lib") {
  68. my $libformat = Dpkg::Shlibs::Objdump::get_format("$root$dir/$lib");
  69. if ($format eq $libformat) {
  70. return File::Spec->canonpath("$root$dir/$lib");
  71. }
  72. }
  73. }
  74. return undef;
  75. }
  76. 1;