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 use feature qw(state);
23 our $VERSION = '0.03';
32 use Exporter qw(import);
36 use Dpkg::ErrorHandling;
37 use Dpkg::Shlibs::Objdump;
38 use Dpkg::Util qw(:list);
39 use Dpkg::Path qw(resolve_symlink canonpath);
40 use Dpkg::Arch qw(get_build_arch get_host_arch :mappers);
42 use constant DEFAULT_LIBRARY_PATH =>
44 # XXX: Deprecated multilib paths.
45 use constant DEFAULT_MULTILIB_PATH =>
46 qw(/lib32 /usr/lib32 /lib64 /usr/lib64);
48 # Library paths set by the user.
49 my @custom_librarypaths;
50 # Library paths from the system.
51 my @system_librarypaths;
52 my $librarypaths_init;
59 open my $fh, '<', $file or syserr(g_('cannot open %s'), $file);
65 if (/^include\s+(\S.*\S)\s*$/) {
66 foreach my $include (glob($1)) {
67 parse_ldso_conf($include) if -e $include
68 && !$visited{$include};
73 if (none { $_ eq $libdir } (@custom_librarypaths, @system_librarypaths)) {
74 push @system_librarypaths, $libdir;
81 sub blank_library_paths {
82 @custom_librarypaths = ();
83 @system_librarypaths = ();
84 $librarypaths_init = 1;
87 sub setup_library_paths {
88 @custom_librarypaths = ();
89 @system_librarypaths = ();
91 # XXX: Deprecated. Update library paths with LD_LIBRARY_PATH.
92 if ($ENV{LD_LIBRARY_PATH}) {
93 foreach my $path (split /:/, $ENV{LD_LIBRARY_PATH}) {
95 # XXX: This should be added to @custom_librarypaths, but as this
96 # is deprecated we do not care as the code will go away.
97 push @system_librarypaths, $path;
101 # Adjust set of directories to consider when we're in a situation of a
102 # cross-build or a build of a cross-compiler.
105 # Detect cross compiler builds.
106 if ($ENV{DEB_TARGET_GNU_TYPE} and
107 ($ENV{DEB_TARGET_GNU_TYPE} ne $ENV{DEB_BUILD_GNU_TYPE}))
109 $multiarch = gnutriplet_to_multiarch($ENV{DEB_TARGET_GNU_TYPE});
111 # Host for normal cross builds.
112 if (get_build_arch() ne get_host_arch()) {
113 $multiarch = debarch_to_multiarch(get_host_arch());
115 # Define list of directories containing crossbuilt libraries.
117 push @system_librarypaths, "/lib/$multiarch", "/usr/lib/$multiarch";
120 push @system_librarypaths, DEFAULT_LIBRARY_PATH;
122 # Update library paths with ld.so config.
123 parse_ldso_conf('/etc/ld.so.conf') if -e '/etc/ld.so.conf';
125 push @system_librarypaths, DEFAULT_MULTILIB_PATH;
127 $librarypaths_init = 1;
130 sub add_library_dir {
133 setup_library_paths() if not $librarypaths_init;
135 push @custom_librarypaths, $dir;
138 sub get_library_paths {
139 setup_library_paths() if not $librarypaths_init;
141 return (@custom_librarypaths, @system_librarypaths);
144 # find_library ($soname, \@rpath, $format, $root)
146 my ($lib, $rpath, $format, $root) = @_;
148 setup_library_paths() if not $librarypaths_init;
150 my @librarypaths = (@{$rpath}, @custom_librarypaths, @system_librarypaths);
155 foreach my $dir (@librarypaths) {
156 my $checkdir = "$root$dir";
157 if (-e "$checkdir/$lib") {
158 my $libformat = Dpkg::Shlibs::Objdump::get_format("$checkdir/$lib");
159 if ($format eq $libformat) {
160 push @libs, canonpath("$checkdir/$lib");
162 debug(1, "Skipping lib $checkdir/$lib, libabi=0x%s != objabi=0x%s",
163 unpack('H*', $libformat), unpack('H*', $format));