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