doc: Move Perl version baseline as the first perl coding style subsection
[dpkg.git] / scripts / dpkg-shlibdeps.pl
blobade77de0dd06dde118b8042041c978d0872746e7
1 #!/usr/bin/perl
3 # dpkg-shlibdeps
5 # Copyright © 1996 Ian Jackson
6 # Copyright © 2000 Wichert Akkerman
7 # Copyright © 2006 Frank Lichtenheld
8 # Copyright © 2006-2010,2012-2015 Guillem Jover <guillem@debian.org>
9 # Copyright © 2007, 2016 Raphaël Hertzog <hertzog@debian.org>
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License
22 # along with this program. If not, see <https://www.gnu.org/licenses/>.
24 use strict;
25 use warnings;
26 use feature qw(state);
28 use List::Util qw(any none);
29 use Cwd qw(realpath);
30 use File::Basename qw(dirname);
32 use Dpkg ();
33 use Dpkg::Gettext;
34 use Dpkg::ErrorHandling;
35 use Dpkg::Path qw(relative_to_pkg_root guess_pkg_root_dir
36 check_files_are_the_same get_control_path);
37 use Dpkg::Version;
38 use Dpkg::Shlibs qw(find_library get_library_paths);
39 use Dpkg::Shlibs::Objdump;
40 use Dpkg::Shlibs::SymbolFile;
41 use Dpkg::Substvars;
42 use Dpkg::Arch qw(get_host_arch);
43 use Dpkg::Deps;
44 use Dpkg::Control::Info;
45 use Dpkg::Control::Fields;
48 use constant {
49 WARN_SYM_NOT_FOUND => 1,
50 WARN_DEP_AVOIDABLE => 2,
51 WARN_NOT_NEEDED => 4,
54 # By increasing importance
55 my @depfields = qw(Suggests Recommends Depends Pre-Depends);
56 my $i = 0; my %depstrength = map { $_ => $i++ } @depfields;
58 textdomain('dpkg-dev');
60 my $admindir = $Dpkg::ADMINDIR;
61 my $shlibsoverride = "$Dpkg::CONFDIR/shlibs.override";
62 my $shlibsdefault = "$Dpkg::CONFDIR/shlibs.default";
63 my $shlibslocal = 'debian/shlibs.local';
64 my $packagetype = 'deb';
65 my $dependencyfield = 'Depends';
66 my $varlistfile = 'debian/substvars';
67 my $varlistfilenew;
68 my $varnameprefix = 'shlibs';
69 my $ignore_missing_info = 0;
70 my $warnings = WARN_SYM_NOT_FOUND | WARN_DEP_AVOIDABLE;
71 my $debug = 0;
72 my @exclude = ();
73 my @pkg_dir_to_search = ();
74 my @pkg_dir_to_ignore = ();
75 my $host_arch = get_host_arch();
77 my (@pkg_shlibs, @pkg_symbols, @pkg_root_dirs);
79 my ($stdout, %exec);
80 foreach (@ARGV) {
81 if (m/^-T(.*)$/) {
82 $varlistfile = $1;
83 } elsif (m/^-p(\w[-:0-9A-Za-z]*)$/) {
84 $varnameprefix = $1;
85 } elsif (m/^-L(.*)$/) {
86 $shlibslocal = $1;
87 } elsif (m/^-l(.*)$/) {
88 Dpkg::Shlibs::add_library_dir($1);
89 } elsif (m/^-S(.*)$/) {
90 push @pkg_dir_to_search, $1;
91 } elsif (m/^-I(.*)$/) {
92 push @pkg_dir_to_ignore, $1;
93 } elsif (m/^-O$/) {
94 $stdout = 1;
95 } elsif (m/^-O(.+)$/) {
96 $varlistfile = $1;
97 } elsif (m/^-(?:\?|-help)$/) {
98 usage(); exit(0);
99 } elsif (m/^--version$/) {
100 version(); exit(0);
101 } elsif (m/^--admindir=(.*)$/) {
102 $admindir = $1;
103 if (not -d $admindir) {
104 error(g_("administrative directory '%s' does not exist"), $admindir);
106 $ENV{DPKG_ADMINDIR} = $admindir;
107 } elsif (m/^-d(.*)$/) {
108 $dependencyfield = field_capitalize($1);
109 if (not defined $depstrength{$dependencyfield}) {
110 warning(g_("unrecognized dependency field '%s'"), $dependencyfield);
112 } elsif (m/^-e(.*)$/) {
113 if (exists $exec{$1}) {
114 # Affect the binary to the most important field
115 if ($depstrength{$dependencyfield} > $depstrength{$exec{$1}}) {
116 $exec{$1} = $dependencyfield;
118 } else {
119 $exec{$1} = $dependencyfield;
121 } elsif (m/^--ignore-missing-info$/) {
122 $ignore_missing_info = 1;
123 } elsif (m/^--warnings=(\d+)$/) {
124 $warnings = $1;
125 } elsif (m/^-t(.*)$/) {
126 $packagetype = $1;
127 } elsif (m/^-v$/) {
128 $debug++;
129 } elsif (m/^-x(.*)$/) {
130 push @exclude, $1;
131 } elsif (m/^-/) {
132 usageerr(g_("unknown option '%s'"), $_);
133 } else {
134 if (exists $exec{$_}) {
135 # Affect the binary to the most important field
136 if ($depstrength{$dependencyfield} > $depstrength{$exec{$_}}) {
137 $exec{$_} = $dependencyfield;
139 } else {
140 $exec{$_} = $dependencyfield;
144 usageerr(g_('need at least one executable')) unless scalar keys %exec;
146 report_options(debug_level => $debug);
148 sub ignore_pkgdir {
149 my $path = shift;
150 return any { $path =~ /^\Q$_\E/ } @pkg_dir_to_ignore;
153 if (-d 'debian') {
154 push @pkg_symbols, grep { !ignore_pkgdir($_) } glob 'debian/*/DEBIAN/symbols';
155 push @pkg_shlibs, grep { !ignore_pkgdir($_) } glob 'debian/*/DEBIAN/shlibs';
156 my %uniq = map { guess_pkg_root_dir($_) => 1 } (@pkg_symbols, @pkg_shlibs);
157 push @pkg_root_dirs, keys %uniq;
160 my $control = Dpkg::Control::Info->new();
161 my $fields = $control->get_source();
162 my $bd_value = deps_concat($fields->{'Build-Depends'}, $fields->{'Build-Depends-Arch'});
163 my $build_deps = deps_parse($bd_value, build_dep => 1, reduce_restrictions => 1);
164 error(g_('cannot parse %s field'), 'Build-Depends/Build-Depends-Arch')
165 unless defined $build_deps;
167 my %dependencies;
169 # Statistics on soname seen in the whole run (with multiple analysis of
170 # binaries)
171 my %global_soname_notfound;
172 my %global_soname_used;
173 my %global_soname_needed;
175 # Cached data.
176 my %shlibs_cache;
177 my %symfile_cache;
178 my %objdump_cache;
179 my %symfile_has_soname_cache;
181 # Used to count errors due to missing libraries
182 my $error_count = 0;
184 my $cur_field;
185 foreach my $file (keys %exec) {
186 $cur_field = $exec{$file};
187 debug(1, ">> Scanning $file (for $cur_field field)");
189 my $obj = Dpkg::Shlibs::Objdump::Object->new($file);
190 my @sonames = $obj->get_needed_libraries;
192 # Load symbols files for all needed libraries (identified by SONAME)
193 my %libfiles;
194 my %altlibfiles;
195 my %soname_libs;
196 my %soname_notfound;
197 my %alt_soname;
198 foreach my $soname (@sonames) {
199 my @libs = my_find_library($soname, $obj->{RPATH}, $obj->{exec_abi}, $file);
200 unless (scalar @libs) {
201 $soname_notfound{$soname} = 1;
202 $global_soname_notfound{$soname} = 1;
203 my $msg = g_('cannot find library %s needed by %s (ELF ' .
204 "format: '%s' abi: '%s'; RPATH: '%s')");
205 if (scalar(split_soname($soname))) {
206 errormsg($msg, $soname, $file, $obj->{format}, $obj->{exec_abi},
207 join(':', @{$obj->{RPATH}}));
208 $error_count++;
209 } else {
210 warning($msg, $soname, $file, $obj->{format}, $obj->{exec_abi},
211 join(':', @{$obj->{RPATH}}));
213 next;
216 # Track shared libraries for a given SONAME.
217 push @{$soname_libs{$soname}}, @libs;
219 # Track shared libraries for package mapping.
220 foreach my $lib (@libs) {
221 $libfiles{$lib} = $soname;
222 my $reallib = realpath($lib);
223 if ($reallib ne $lib) {
224 $altlibfiles{$reallib} = $soname;
226 debug(1, "Library $soname found in $lib");
229 my $file2pkg = find_packages(keys %libfiles, keys %altlibfiles);
230 my $symfile = Dpkg::Shlibs::SymbolFile->new();
231 my $dumplibs_wo_symfile = Dpkg::Shlibs::Objdump->new();
232 SONAME: foreach my $soname (@sonames) {
233 # Select the first good entry from the ordered list that we got from
234 # find_library(), and skip to the next SONAME.
236 foreach my $lib (@{$soname_libs{$soname}}) {
237 if (none { $_ ne '' } @{$file2pkg->{$lib}}) {
238 # The path of the library as calculated is not the
239 # official path of a packaged file, try to fallback on
240 # the realpath() first, maybe this one is part of a package
241 my $reallib = realpath($lib);
242 if (exists $file2pkg->{$reallib}) {
243 $file2pkg->{$lib} = $file2pkg->{$reallib};
246 if (none { $_ ne '' } @{$file2pkg->{$lib}}) {
247 # If the library is really not available in an installed package,
248 # it's because it's in the process of being built
249 # Empty package name will lead to consideration of symbols
250 # file from the package being built only
251 $file2pkg->{$lib} = [''];
252 debug(1, "No associated package found for $lib");
255 # Load symbols/shlibs files from packages providing libraries
256 my $missing_wanted_shlibs_info = 0;
257 foreach my $pkg (@{$file2pkg->{$lib}}) {
258 my $symfile_path;
259 my $haslocaldep = 0;
260 if (-e $shlibslocal and
261 defined(extract_from_shlibs($soname, $shlibslocal)))
263 $haslocaldep = 1;
265 if ($packagetype eq 'deb' and not $haslocaldep) {
266 # Use fine-grained dependencies only on real deb
267 # and only if the dependency is not provided by shlibs.local
268 $symfile_path = find_symbols_file($pkg, $soname, $lib);
270 if (defined($symfile_path)) {
271 # Load symbol information
272 debug(1, "Using symbols file $symfile_path for $soname");
273 $symfile_cache{$symfile_path} //=
274 Dpkg::Shlibs::SymbolFile->new(file => $symfile_path);
275 $symfile->merge_object_from_symfile($symfile_cache{$symfile_path}, $soname);
277 if (defined($symfile_path) && $symfile->has_object($soname)) {
278 # Initialize dependencies with the smallest minimal version
279 # of all symbols (unversioned dependency is not ok as the
280 # library might not have always been available in the
281 # package and we really need it)
282 my $dep = $symfile->get_dependency($soname);
283 my $minver = $symfile->get_smallest_version($soname) || '';
284 update_dependency_version($dep, $minver);
285 debug(2, " Minimal version of ($dep) initialized with ($minver)");
287 # Found a symbols file for the SONAME.
288 next SONAME;
289 } else {
290 # No symbol file found, fall back to standard shlibs
291 debug(1, "Using shlibs+objdump for $soname (file $lib)");
292 $objdump_cache{$lib} //= Dpkg::Shlibs::Objdump::Object->new($lib);
293 my $libobj = $objdump_cache{$lib};
294 my $id = $dumplibs_wo_symfile->add_object($libobj);
295 if (($id ne $soname) and ($id ne $lib)) {
296 warning(g_('%s has an unexpected SONAME (%s)'), $lib, $id);
297 $alt_soname{$id} = $soname;
300 # Only try to generate a dependency for libraries with a SONAME
301 if (not $libobj->is_public_library()) {
302 debug(1, "Skipping shlibs+objdump info for private library $lib");
303 next;
306 # If we found a shlibs file for the SONAME, skip to the next.
307 next SONAME if add_shlibs_dep($soname, $pkg, $lib);
309 $missing_wanted_shlibs_info = 1;
311 debug(1, "No shlibs+objdump info available, trying next package for $lib");
315 next if not $missing_wanted_shlibs_info;
317 # We will only reach this point, if we have found no symbols nor
318 # shlibs files for the given SONAME.
320 # This failure is fairly new, try to be kind by
321 # ignoring as many cases that can be safely ignored
322 my $ignore = 0;
323 # 1/ when the lib and the binary are in the same
324 # package
325 my $root_file = guess_pkg_root_dir($file);
326 my $root_lib = guess_pkg_root_dir($lib);
327 $ignore++ if defined $root_file and defined $root_lib
328 and check_files_are_the_same($root_file, $root_lib);
329 # 2/ when the lib is not versioned and can't be
330 # handled by shlibs
331 $ignore++ unless scalar split_soname($soname);
332 # 3/ when we have been asked to do so
333 $ignore++ if $ignore_missing_info;
334 error(g_('no dependency information found for %s ' .
335 "(used by %s)\n" .
336 'Hint: check if the library actually comes ' .
337 'from a package.'), $lib, $file)
338 unless $ignore;
342 # Scan all undefined symbols of the binary and resolve to a
343 # dependency
344 my %soname_used;
345 foreach my $soname (@sonames) {
346 # Initialize statistics
347 $soname_used{$soname} = 0;
348 $global_soname_used{$soname} //= 0;
349 if (exists $global_soname_needed{$soname}) {
350 push @{$global_soname_needed{$soname}}, $file;
351 } else {
352 $global_soname_needed{$soname} = [ $file ];
355 my $nb_warnings = 0;
356 my $nb_skipped_warnings = 0;
357 # Disable warnings about missing symbols when we have not been able to
358 # find all libs
359 my $disable_warnings = scalar(keys(%soname_notfound));
360 my $in_public_dir = 1;
361 if (my $relname = relative_to_pkg_root($file)) {
362 my $parent_dir = '/' . dirname($relname);
363 $in_public_dir = any { $parent_dir eq $_ } get_library_paths();
364 } else {
365 warning(g_('binaries to analyze should already be ' .
366 "installed in their package's directory"));
368 debug(2, 'Analyzing all undefined symbols');
369 foreach my $sym ($obj->get_undefined_dynamic_symbols()) {
370 my $name = $sym->{name};
371 if ($sym->{version}) {
372 $name .= '@' . "$sym->{version}";
373 } else {
374 $name .= '@' . 'Base';
376 debug(2, " Looking up symbol $name");
377 my %symdep = $symfile->lookup_symbol($name, \@sonames);
378 if (keys %symdep) {
379 my $depends = $symfile->get_dependency($symdep{soname},
380 $symdep{symbol}{dep_id});
381 debug(2, " Found in symbols file of $symdep{soname} (minver: " .
382 "$symdep{symbol}{minver}, dep: $depends)");
383 $soname_used{$symdep{soname}}++;
384 $global_soname_used{$symdep{soname}}++;
385 if (exists $alt_soname{$symdep{soname}}) {
386 # Also count usage on alternate soname
387 $soname_used{$alt_soname{$symdep{soname}}}++;
388 $global_soname_used{$alt_soname{$symdep{soname}}}++;
390 update_dependency_version($depends, $symdep{symbol}{minver});
391 } else {
392 my $syminfo = $dumplibs_wo_symfile->locate_symbol($name);
393 if (not defined($syminfo)) {
394 debug(2, ' Not found');
395 next unless ($warnings & WARN_SYM_NOT_FOUND);
396 next if $disable_warnings;
397 # Complain about missing symbols only for executables
398 # and public libraries
399 if ($obj->is_executable() or $obj->is_public_library()) {
400 my $print_name = $name;
401 # Drop the default suffix for readability
402 $print_name =~ s/\@Base$//;
403 unless ($sym->{weak}) {
404 if ($debug or ($in_public_dir and $nb_warnings < 10)
405 or (not $in_public_dir and $nb_warnings < 1))
407 if ($in_public_dir) {
408 warning(g_('symbol %s used by %s found in none of the ' .
409 'libraries'), $print_name, $file);
410 } else {
411 warning(g_('%s contains an unresolvable reference to ' .
412 "symbol %s: it's probably a plugin"),
413 $file, $print_name);
415 $nb_warnings++;
416 } else {
417 $nb_skipped_warnings++;
421 } else {
422 debug(2, " Found in $syminfo->{soname} ($syminfo->{objid})");
423 if (exists $alt_soname{$syminfo->{soname}}) {
424 # Also count usage on alternate soname
425 $soname_used{$alt_soname{$syminfo->{soname}}}++;
426 $global_soname_used{$alt_soname{$syminfo->{soname}}}++;
428 $soname_used{$syminfo->{soname}}++;
429 $global_soname_used{$syminfo->{soname}}++;
433 warning(P_('%d similar warning has been skipped (use -v to see it)',
434 '%d other similar warnings have been skipped (use -v to see ' .
435 'them all)', $nb_skipped_warnings), $nb_skipped_warnings)
436 if $nb_skipped_warnings;
437 foreach my $soname (@sonames) {
438 # Adjust minimal version of dependencies with information
439 # extracted from build-dependencies
440 my $dev_pkgs = $symfile->get_field($soname, 'Build-Depends-Packages') //
441 $symfile->get_field($soname, 'Build-Depends-Package');
442 foreach my $dev_pkg (split /[,\s]+/, $dev_pkgs // '') {
443 debug(1, "Updating dependencies of $soname with build-dependencies");
444 my $minver = get_min_version_from_deps($build_deps, $dev_pkg);
445 if (defined $minver) {
446 foreach my $dep ($symfile->get_dependencies($soname)) {
447 update_dependency_version($dep, $minver, 1);
448 debug(1, " Minimal version of $dep updated with $minver");
450 } else {
451 debug(1, " No minimal version found in $dev_pkg build-dependency");
455 # Warn about un-NEEDED libraries
456 unless ($soname_notfound{$soname} or $soname_used{$soname}) {
457 # Ignore warning for libm.so.6 if also linked against libstdc++
458 next if ($soname =~ /^libm\.so\.\d+$/ and
459 any { m/^libstdc\+\+\.so\.\d+/ } @sonames);
460 next unless ($warnings & WARN_NOT_NEEDED);
461 warning(g_('%s should not be linked against %s (it uses none of ' .
462 "the library's symbols)"), $file, $soname);
467 # Warn of unneeded libraries at the "package" level (i.e. over all
468 # binaries that we have inspected)
469 foreach my $soname (keys %global_soname_needed) {
470 unless ($global_soname_notfound{$soname} or $global_soname_used{$soname}) {
471 next if ($soname =~ /^libm\.so\.\d+$/ and
472 any { m/^libstdc\+\+\.so\.\d+/ } keys %global_soname_needed);
473 next unless ($warnings & WARN_DEP_AVOIDABLE);
474 warning(P_('package could avoid a useless dependency if %s was not ' .
475 "linked against %s (it uses none of the library's symbols)",
476 'package could avoid a useless dependency if %s were not ' .
477 "linked against %s (they use none of the library's symbols)",
478 scalar @{$global_soname_needed{$soname}}),
479 join(' ', @{$global_soname_needed{$soname}}), $soname);
483 # Quit now if any missing libraries
484 if ($error_count >= 1) {
485 my $note = g_('Note: libraries are not searched in other binary packages ' .
486 "that do not have any shlibs or symbols file.\n" .
487 'To help dpkg-shlibdeps find private libraries, you might ' .
488 'need to use -l.');
489 error(P_('cannot continue due to the error above',
490 'cannot continue due to the errors listed above',
491 $error_count) . "\n" . $note);
494 # Open substvars file
496 my $substvars = Dpkg::Substvars->new();
497 if ($stdout) {
498 $varlistfilenew = '-';
499 } else {
500 $substvars->load($varlistfile) if -e $varlistfile;
501 $substvars->filter(remove => sub { $_[0] =~ m/^\Q$varnameprefix\E:/ });
503 $varlistfilenew = "$varlistfile.new";
506 # Write out the shlibs substvars
507 my %depseen;
509 sub filter_deps {
510 my ($dep, $field) = @_;
511 # Skip dependencies on excluded packages
512 foreach my $exc (@exclude) {
513 return 0 if $dep =~ /^\s*\Q$exc\E\b/;
515 # Don't include dependencies if they are already
516 # mentioned in a higher priority field
517 if (not exists($depseen{$dep})) {
518 $depseen{$dep} = $dependencies{$field}{$dep};
519 return 1;
520 } else {
521 # Since dependencies can be versioned, we have to
522 # verify if the dependency is stronger than the
523 # previously seen one
524 my $stronger;
525 if ($depseen{$dep} eq $dependencies{$field}{$dep}) {
526 # If both versions are the same (possibly unversioned)
527 $stronger = 0;
528 } elsif ($dependencies{$field}{$dep} eq '') {
529 $stronger = 0; # If the dep is unversioned
530 } elsif ($depseen{$dep} eq '') {
531 $stronger = 1; # If the dep seen is unversioned
532 } elsif (version_compare_relation($depseen{$dep}, REL_GT,
533 $dependencies{$field}{$dep})) {
534 # The version of the dep seen is stronger...
535 $stronger = 0;
536 } else {
537 $stronger = 1;
539 $depseen{$dep} = $dependencies{$field}{$dep} if $stronger;
540 return $stronger;
544 foreach my $field (reverse @depfields) {
545 my $dep = '';
546 if (exists $dependencies{$field} and scalar keys %{$dependencies{$field}}) {
547 $dep = join ', ',
548 map {
549 # Translate dependency templates into real dependencies
550 my $templ = $_;
551 if ($dependencies{$field}{$templ}->is_valid() and
552 $dependencies{$field}{$templ}->as_string()) {
553 $templ =~ s/#MINVER#/(>= $dependencies{$field}{$templ})/g;
554 } else {
555 $templ =~ s/#MINVER#//g;
557 $templ =~ s/\s+/ /g;
558 $templ;
559 } grep {
560 filter_deps($_, $field)
561 } keys %{$dependencies{$field}};
563 if ($dep) {
564 my $obj = deps_parse($dep);
565 error(g_('invalid dependency got generated: %s'), $dep) unless defined $obj;
566 $obj->sort();
567 $substvars->set_as_used("$varnameprefix:$field", "$obj");
571 $substvars->save($varlistfilenew);
573 # Replace old file by new one
574 if (!$stdout) {
575 rename $varlistfilenew, $varlistfile
576 or syserr(g_("install new varlist file '%s'"), $varlistfile);
580 ## Functions
583 sub version {
584 printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
586 printf g_('
587 This is free software; see the GNU General Public License version 2 or
588 later for copying conditions. There is NO warranty.
592 sub usage {
593 printf g_(
594 'Usage: %s [<option>...] <executable>|-e<executable> [<option>...]')
595 . "\n\n" . g_(
596 "Positional options (order is significant):
597 <executable> include dependencies for <executable>,
598 -e<executable> (use -e if <executable> starts with '-')
599 -d<dependency-field> next executable(s) set shlibs:<dependency-field>.")
600 . "\n\n" . g_(
601 "Options:
602 -l<library-dir> add directory to private shared library search list.
603 -p<varname-prefix> set <varname-prefix>:* instead of shlibs:*.
604 -O[<file>] write variable settings to stdout (or <file>).
605 -L<local-shlibs-file> shlibs override file, not debian/shlibs.local.
606 -T<substvars-file> update variables here, not debian/substvars.
607 -t<type> set package type (default is deb).
608 -x<package> exclude package from the generated dependencies.
609 -S<package-build-dir> search needed libraries in the given
610 package build directory first.
611 -I<package-build-dir> ignore needed libraries, shlibs and symbols files
612 in the given build directory.
613 -v enable verbose mode (can be used multiple times).
614 --ignore-missing-info don't fail if dependency information can't be found.
615 --warnings=<value> define set of active warnings (see manual page).
616 --admindir=<directory> change the administrative directory.
617 -?, --help show this help message.
618 --version show the version.")
619 . "\n\n" . g_(
620 'Dependency fields recognized are:
622 '), $Dpkg::PROGNAME, join('/', @depfields);
625 sub get_min_version_from_deps {
626 my ($dep, $pkg) = @_;
627 if ($dep->isa('Dpkg::Deps::Simple')) {
628 if (($dep->{package} eq $pkg) &&
629 defined($dep->{relation}) &&
630 (($dep->{relation} eq REL_GE) ||
631 ($dep->{relation} eq REL_GT)))
633 return $dep->{version};
635 return;
636 } else {
637 my $res;
638 foreach my $subdep ($dep->get_deps()) {
639 my $minver = get_min_version_from_deps($subdep, $pkg);
640 next if not defined $minver;
641 if (defined $res) {
642 if (version_compare_relation($minver, REL_GT, $res)) {
643 $res = $minver;
645 } else {
646 $res = $minver;
649 return $res;
653 sub update_dependency_version {
654 my ($dep, $minver, $existing_only) = @_;
655 return if not defined($minver);
656 $minver = Dpkg::Version->new($minver);
657 foreach my $subdep (split /\s*,\s*/, $dep) {
658 if (exists $dependencies{$cur_field}{$subdep} and
659 defined($dependencies{$cur_field}{$subdep}))
661 if ($dependencies{$cur_field}{$subdep} eq '' or $minver ne '' and
662 version_compare_relation($minver, REL_GT,
663 $dependencies{$cur_field}{$subdep}))
665 $dependencies{$cur_field}{$subdep} = $minver;
667 } elsif (!$existing_only) {
668 $dependencies{$cur_field}{$subdep} = $minver;
673 sub add_shlibs_dep {
674 my ($soname, $pkg, $libfile) = @_;
675 my @shlibs = ($shlibslocal, $shlibsoverride);
676 if ($pkg eq '') {
677 # If the file is not packaged, try to find out the shlibs file in
678 # the package being built where the lib has been found
679 my $pkg_root = guess_pkg_root_dir($libfile);
680 if (defined $pkg_root) {
681 push @shlibs, "$pkg_root/DEBIAN/shlibs";
683 # Fallback to other shlibs files but it shouldn't be necessary
684 push @shlibs, @pkg_shlibs;
685 } else {
686 my $control_file = get_control_path($pkg, 'shlibs');
687 push @shlibs, $control_file if defined $control_file;
689 push @shlibs, $shlibsdefault;
690 debug(1, " Looking up shlibs dependency of $soname provided by '$pkg'");
691 foreach my $file (@shlibs) {
692 next if not -e $file;
693 my $dep = extract_from_shlibs($soname, $file);
694 if (defined($dep)) {
695 debug(1, " Found $dep in $file");
696 foreach (split(/,\s*/, $dep)) {
697 # Note: the value is empty for shlibs based dependency
698 # symbol based dependency will put a valid version as value
699 $dependencies{$cur_field}{$_} = Dpkg::Version->new('');
701 return 1;
704 debug(1, ' Found nothing');
705 return 0;
708 sub split_soname {
709 my $soname = shift;
711 if ($soname =~ /^(.+)\.so\.(.+)$/) {
712 # Shared library with stable <name>.so.<version> format.
713 return wantarray ? ($1, $2) : 1;
714 } elsif ($soname =~ /^(.+)-(\d.*)\.so$/) {
715 # Shared library/module with unstable <name>-<version>.so format.
716 return wantarray ? ($1, $2) : 1;
717 } else {
718 # Something else.
719 return wantarray ? () : 0;
723 sub extract_from_shlibs {
724 my ($soname, $shlibfile) = @_;
726 if (exists $shlibs_cache{$shlibfile}{$soname}) {
727 return $shlibs_cache{$shlibfile}{$soname};
730 my $shlibs_re = qr{
731 ^\s*
732 (?:(\S+):\s+)? # Optional type
733 (\S+)\s+ # Library
734 (\S+) # Version
737 (\S.*\S) # Dependencies
739 \s*$
742 # Split soname in name/version
743 my ($libname, $libversion) = split_soname($soname);
744 unless (defined $libname) {
745 warning(g_("can't extract name and version from library name '%s'"),
746 $soname);
747 $shlibs_cache{$shlibfile}{$soname} = undef;
748 return;
750 # Open shlibs file
751 open(my $shlibs_fh, '<', $shlibfile)
752 or syserr(g_("unable to open shared libs info file '%s'"), $shlibfile);
753 my $dep;
754 while (<$shlibs_fh>) {
755 s/\s*\n$//;
756 next if m/^\#/;
757 ## no critic (RegularExpressions::ProhibitCaptureWithoutTest)
758 if (!m/$shlibs_re/) {
759 warning(g_("shared libs info file '%s' line %d: bad line '%s'"),
760 $shlibfile, $., $_);
761 next;
763 my $depread = $4 // '';
764 if (($libname eq $2) && ($libversion eq $3)) {
765 # Define dep and end here if the package type explicitly
766 # matches. Otherwise if the packagetype is not specified, use
767 # the dep only as a default that can be overridden by a later
768 # line
769 if (defined($1)) {
770 if ($1 eq $packagetype) {
771 $dep = $depread;
772 last;
774 } else {
775 $dep //= $depread;
779 close($shlibs_fh);
780 $shlibs_cache{$shlibfile}{$soname} = $dep;
781 return $dep;
784 sub find_symbols_file {
785 my ($pkg, $soname, $libfile) = @_;
786 my @files;
787 if ($pkg eq '') {
788 # If the file is not packaged, try to find out the symbols file in
789 # the package being built where the lib has been found
790 my $pkg_root = guess_pkg_root_dir($libfile);
791 if (defined $pkg_root) {
792 push @files, "$pkg_root/DEBIAN/symbols";
794 # Fallback to other symbols files but it shouldn't be necessary
795 push @files, @pkg_symbols;
796 } else {
797 push @files, "$Dpkg::CONFDIR/symbols/$pkg.symbols.$host_arch",
798 "$Dpkg::CONFDIR/symbols/$pkg.symbols";
800 state %control_file_cache;
801 if (not exists $control_file_cache{$pkg}) {
802 $control_file_cache{$pkg} = get_control_path($pkg, 'symbols');
804 my $control_file = $control_file_cache{$pkg};
805 push @files, $control_file if defined $control_file;
808 foreach my $file (@files) {
809 if (-e $file and symfile_has_soname($file, $soname)) {
810 return $file;
813 return;
816 sub symfile_has_soname {
817 my ($file, $soname) = @_;
819 if (exists $symfile_has_soname_cache{$file}{$soname}) {
820 return $symfile_has_soname_cache{$file}{$soname};
823 open(my $symfile_fh, '<', $file)
824 or syserr(g_('cannot open file %s'), $file);
825 my $result = 0;
826 while (<$symfile_fh>) {
827 if (/^\Q$soname\E /) {
828 $result = 1;
829 last;
832 close($symfile_fh);
833 $symfile_has_soname_cache{$file}{$soname} = $result;
834 return $result;
837 # find_library ($soname, \@rpath, $format)
838 sub my_find_library {
839 my ($lib, $rpath, $format, $execfile) = @_;
841 # Create real RPATH in case $ORIGIN is used
842 # Note: ld.so also supports $PLATFORM and $LIB but they are
843 # used in real case (yet)
844 my $libdir = relative_to_pkg_root($execfile);
845 my $origin;
846 if (defined $libdir) {
847 $origin = "/$libdir";
848 $origin =~ s{/+[^/]*$}{};
850 my @RPATH = ();
851 foreach my $path (@{$rpath}) {
852 if ($path =~ /\$ORIGIN|\$\{ORIGIN\}/) {
853 if (defined $origin) {
854 $path =~ s/\$ORIGIN/$origin/g;
855 $path =~ s/\$\{ORIGIN\}/$origin/g;
856 } else {
857 warning(g_('$ORIGIN is used in RPATH of %s and the corresponding ' .
858 'directory could not be identified due to lack of DEBIAN ' .
859 "sub-directory in the root of package's build tree"), $execfile);
862 push @RPATH, $path;
865 # Look into the packages we're currently building in the following
866 # order:
867 # - package build tree of the binary which is analyzed
868 # - package build tree given on the command line (option -S)
869 # - other package build trees that contain either a shlibs or a
870 # symbols file
871 # But ignore:
872 # - package build tree given on the command line (option -I)
874 my @builddirs;
875 my $pkg_root = guess_pkg_root_dir($execfile);
876 push @builddirs, $pkg_root if defined $pkg_root;
877 push @builddirs, @pkg_dir_to_search;
878 push @builddirs, @pkg_root_dirs;
879 my %dir_checked;
880 foreach my $builddir (@builddirs) {
881 next if defined($dir_checked{$builddir});
882 next if ignore_pkgdir($builddir);
883 my @libs = find_library($lib, \@RPATH, $format, $builddir);
884 return @libs if scalar @libs;
885 $dir_checked{$builddir} = 1;
888 # Fallback in the root directory if we have not found what we were
889 # looking for in the packages
890 return find_library($lib, \@RPATH, $format, '');
893 my %cached_pkgmatch = ();
895 sub find_packages {
896 my @paths = @_;
898 my @files;
899 my $pkgmatch = {};
901 foreach my $path (@paths) {
902 if (exists $cached_pkgmatch{$path}) {
903 $pkgmatch->{$path} = $cached_pkgmatch{$path};
904 } else {
905 push @files, $path;
906 $cached_pkgmatch{$path} = ['']; # placeholder to cache misses too.
907 $pkgmatch->{$path} = ['']; # might be replaced later on
910 return $pkgmatch unless scalar(@files);
912 my $pid = open(my $dpkg_fh, '-|');
913 syserr(g_('cannot fork for %s'), 'dpkg-query --search') unless defined $pid;
914 if (!$pid) {
915 # Child process running dpkg --search and discarding errors
916 close STDERR;
917 open STDERR, '>', '/dev/null'
918 or syserr(g_('cannot open file %s'), '/dev/null');
919 $ENV{LC_ALL} = 'C';
920 exec 'dpkg-query', '--search', '--', @files
921 or syserr(g_('unable to execute %s'), 'dpkg');
923 while (<$dpkg_fh>) {
924 chomp;
925 if (m/^local diversion |^diversion by/) {
926 warning(g_('diversions involved - output may be incorrect'));
927 print { *STDERR } " $_\n"
928 or syserr(g_('write diversion info to stderr'));
929 } elsif (m/^([-a-z0-9+.:, ]+): (\/.*)$/) {
930 my ($pkgs, $path) = ($1, $2);
931 my $realpath = realpath($path);
932 $cached_pkgmatch{$path} = $pkgmatch->{$path} = [ split /, /, $pkgs ];
933 $cached_pkgmatch{$realpath} = $pkgmatch->{$realpath} = [ split /, /, $pkgs ];
934 } else {
935 warning(g_("unknown output from dpkg --search: '%s'"), $_);
938 close($dpkg_fh);
939 return $pkgmatch;