1 # Copyright © 2007, 2016 Raphaël Hertzog <hertzog@debian.org>
2 # Copyright © 2007-2008, 2012-2015 Guillem Jover <guillem@debian.org>
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
21 Dpkg::Shlibs - shared library location handling
25 This module provides functions to locate shared libraries.
27 B<Note>: This is a private module, its API can change at any time.
31 package Dpkg
::Shlibs
0.03;
35 use feature
qw(state);
45 use Exporter
qw(import);
46 use List
::Util
qw(none);
50 use Dpkg
::ErrorHandling
;
51 use Dpkg
::Shlibs
::Objdump
;
52 use Dpkg
::Path
qw(resolve_symlink canonpath);
53 use Dpkg
::Arch
qw(get_build_arch get_host_arch :mappers);
55 use constant DEFAULT_LIBRARY_PATH
=>
57 # XXX: Deprecated multilib paths.
58 use constant DEFAULT_MULTILIB_PATH
=>
59 qw(/lib32 /usr/lib32 /lib64 /usr/lib64);
61 # Library paths set by the user.
62 my @custom_librarypaths;
63 # Library paths from the system.
64 my @system_librarypaths;
65 my $librarypaths_init;
72 open my $fh, '<', $file or syserr
(g_
('cannot open %s'), $file);
78 if (/^include\s+(\S.*\S)\s*$/) {
79 foreach my $include (glob($1)) {
80 parse_ldso_conf
($include) if -e
$include
81 && !$visited{$include};
86 if (none
{ $_ eq $libdir } (@custom_librarypaths, @system_librarypaths)) {
87 push @system_librarypaths, $libdir;
94 sub blank_library_paths
{
95 @custom_librarypaths = ();
96 @system_librarypaths = ();
97 $librarypaths_init = 1;
100 sub setup_library_paths
{
101 @custom_librarypaths = ();
102 @system_librarypaths = ();
104 # XXX: Deprecated. Update library paths with LD_LIBRARY_PATH.
105 if ($ENV{LD_LIBRARY_PATH
}) {
107 my $cwd = Cwd
::getcwd
;
109 foreach my $path (split /:/, $ENV{LD_LIBRARY_PATH
}) {
112 my $realpath = Cwd
::realpath
($path);
113 next unless defined $realpath;
114 if ($realpath =~ m/^\Q$cwd\E/) {
115 warning
(g_
('deprecated use of LD_LIBRARY_PATH with private ' .
116 'library directory which interferes with ' .
117 'cross-building, please use -l option instead'));
120 # XXX: This should be added to @custom_librarypaths, but as this
121 # is deprecated we do not care as the code will go away.
122 push @system_librarypaths, $path;
126 # Adjust set of directories to consider when we're in a situation of a
127 # cross-build or a build of a cross-compiler.
130 # Detect cross compiler builds.
131 if ($ENV{DEB_TARGET_GNU_TYPE
} and
132 ($ENV{DEB_TARGET_GNU_TYPE
} ne $ENV{DEB_BUILD_GNU_TYPE
}))
134 $multiarch = gnutriplet_to_multiarch
($ENV{DEB_TARGET_GNU_TYPE
});
136 # Host for normal cross builds.
137 if (get_build_arch
() ne get_host_arch
()) {
138 $multiarch = debarch_to_multiarch
(get_host_arch
());
140 # Define list of directories containing crossbuilt libraries.
142 push @system_librarypaths, "/lib/$multiarch", "/usr/lib/$multiarch";
145 push @system_librarypaths, DEFAULT_LIBRARY_PATH
;
147 # Update library paths with ld.so config.
148 parse_ldso_conf
('/etc/ld.so.conf') if -e
'/etc/ld.so.conf';
150 push @system_librarypaths, DEFAULT_MULTILIB_PATH
;
152 $librarypaths_init = 1;
155 sub add_library_dir
{
158 setup_library_paths
() if not $librarypaths_init;
160 push @custom_librarypaths, $dir;
163 sub get_library_paths
{
164 setup_library_paths
() if not $librarypaths_init;
166 return (@custom_librarypaths, @system_librarypaths);
169 # find_library ($soname, \@rpath, $format, $root)
171 my ($lib, $rpath, $format, $root) = @_;
173 setup_library_paths
() if not $librarypaths_init;
175 my @librarypaths = (@
{$rpath}, @custom_librarypaths, @system_librarypaths);
180 foreach my $dir (@librarypaths) {
181 my $checkdir = "$root$dir";
182 if (-e
"$checkdir/$lib") {
183 my $libformat = Dpkg
::Shlibs
::Objdump
::get_format
("$checkdir/$lib");
184 if (not defined $libformat) {
185 warning
(g_
("unknown executable format in file '%s'"), "$checkdir/$lib");
186 } elsif ($format eq $libformat) {
187 push @libs, canonpath
("$checkdir/$lib");
189 debug
(1, "Skipping lib $checkdir/$lib, libabi=<%s> != objabi=<%s>",
190 $libformat, $format);
201 This is a private module.