Shlibs.pm 2.4 KB

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