Shlibs.pm 2.3 KB

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