dpkg-shlibdeps.pl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use warnings;
  4. use English;
  5. use POSIX qw(:errno_h :signal_h);
  6. use Cwd qw(realpath);
  7. use Dpkg;
  8. use Dpkg::Gettext;
  9. use Dpkg::ErrorHandling qw(warning error failure syserr usageerr);
  10. use Dpkg::Path qw(relative_to_pkg_root guess_pkg_root_dir
  11. check_files_are_the_same);
  12. use Dpkg::Version qw(compare_versions);
  13. use Dpkg::Shlibs qw(find_library);
  14. use Dpkg::Shlibs::Objdump;
  15. use Dpkg::Shlibs::SymbolFile;
  16. use Dpkg::Arch qw(get_host_arch);
  17. use Dpkg::Fields qw(capit);
  18. use Dpkg::Deps;
  19. push(@INC,$dpkglibdir);
  20. require 'controllib.pl';
  21. # By increasing importance
  22. my @depfields = qw(Suggests Recommends Depends Pre-Depends);
  23. my $i = 0; my %depstrength = map { $_ => $i++ } @depfields;
  24. textdomain("dpkg-dev");
  25. my $shlibsoverride = '/etc/dpkg/shlibs.override';
  26. my $shlibsdefault = '/etc/dpkg/shlibs.default';
  27. my $shlibslocal = 'debian/shlibs.local';
  28. my $packagetype = 'deb';
  29. my $dependencyfield = 'Depends';
  30. my $varlistfile = 'debian/substvars';
  31. my $varnameprefix = 'shlibs';
  32. my $ignore_missing_info = 0;
  33. my $debug = 0;
  34. my @exclude = ();
  35. my $host_arch = get_host_arch();
  36. my (@pkg_shlibs, @pkg_symbols);
  37. if (-d "debian") {
  38. push @pkg_symbols, <debian/*/DEBIAN/symbols>;
  39. push @pkg_shlibs, <debian/*/DEBIAN/shlibs>;
  40. }
  41. my ($stdout, %exec);
  42. foreach (@ARGV) {
  43. if (m/^-T(.*)$/) {
  44. $varlistfile = $1;
  45. } elsif (m/^-p(\w[-:0-9A-Za-z]*)$/) {
  46. $varnameprefix = $1;
  47. } elsif (m/^-L(.*)$/) {
  48. $shlibslocal = $1;
  49. } elsif (m/^-O$/) {
  50. $stdout = 1;
  51. } elsif (m/^-(h|-help)$/) {
  52. usage(); exit(0);
  53. } elsif (m/^--version$/) {
  54. version(); exit(0);
  55. } elsif (m/^--admindir=(.*)$/) {
  56. $admindir = $1;
  57. -d $admindir ||
  58. error(_g("administrative directory '%s' does not exist"), $admindir);
  59. } elsif (m/^-d(.*)$/) {
  60. $dependencyfield = capit($1);
  61. defined($depstrength{$dependencyfield}) ||
  62. warning(_g("unrecognised dependency field \`%s'"), $dependencyfield);
  63. } elsif (m/^-e(.*)$/) {
  64. $exec{$1} = $dependencyfield;
  65. } elsif (m/^--ignore-missing-info$/) {
  66. $ignore_missing_info = 1;
  67. } elsif (m/^-t(.*)$/) {
  68. $packagetype = $1;
  69. } elsif (m/^-v$/) {
  70. $debug = 1;
  71. } elsif (m/^-x(.*)$/) {
  72. push @exclude, $1;
  73. } elsif (m/^-/) {
  74. usageerr(_g("unknown option \`%s'"), $_);
  75. } else {
  76. $exec{$_} = $dependencyfield;
  77. }
  78. }
  79. scalar keys %exec || usageerr(_g("need at least one executable"));
  80. our %fi;
  81. parsecontrolfile("debian/control");
  82. my $build_depends = defined($fi{"C Build-Depends"}) ?
  83. $fi{"C Build-Depends"} : "";
  84. my $build_deps = Dpkg::Deps::parse($build_depends, reduce_arch => 1);
  85. my %dependencies;
  86. my %shlibs;
  87. my $cur_field;
  88. foreach my $file (keys %exec) {
  89. my $pkg_root = guess_pkg_root_dir($file);
  90. $cur_field = $exec{$file};
  91. print "Scanning $file (for $cur_field field)\n" if $debug;
  92. my $obj = Dpkg::Shlibs::Objdump::Object->new($file);
  93. my @sonames = $obj->get_needed_libraries;
  94. # Load symbols files for all needed libraries (identified by SONAME)
  95. my %libfiles;
  96. my %altlibfiles;
  97. my %soname_notfound;
  98. foreach my $soname (@sonames) {
  99. my $lib = my_find_library($soname, $obj->{RPATH}, $obj->{format}, $file);
  100. unless (defined $lib) {
  101. $soname_notfound{$soname} = 1;
  102. my $msg = _g("couldn't find library %s needed by %s (its RPATH is '%s').\n" .
  103. "Note: libraries are not searched in other binary packages " .
  104. "that do not have any shlibs file.\nTo help dpkg-shlibdeps " .
  105. "find private libraries, you might need to set LD_LIBRARY_PATH.");
  106. if (scalar(split_soname($soname))) {
  107. failure($msg, $soname, $file, join(":", @{$obj->{RPATH}}));
  108. } else {
  109. warning($msg, $soname, $file, join(":", @{$obj->{RPATH}}));
  110. }
  111. next;
  112. }
  113. $libfiles{$lib} = $soname;
  114. my $reallib = realpath($lib);
  115. if ($reallib ne $lib) {
  116. $altlibfiles{$reallib} = $soname;
  117. }
  118. print "Library $soname found in $lib\n" if $debug;
  119. }
  120. my $file2pkg = find_packages(keys %libfiles, keys %altlibfiles);
  121. my $symfile = Dpkg::Shlibs::SymbolFile->new();
  122. my $dumplibs_wo_symfile = Dpkg::Shlibs::Objdump->new();
  123. my @soname_wo_symfile;
  124. foreach my $lib (keys %libfiles) {
  125. my $soname = $libfiles{$lib};
  126. if (not scalar(grep { $_ ne '' } @{$file2pkg->{$lib}})) {
  127. # The path of the library as calculated is not the
  128. # official path of a packaged file, try to fallback on
  129. # on the realpath() first, maybe this one is part of a package
  130. my $reallib = realpath($lib);
  131. if (exists $file2pkg->{$reallib}) {
  132. $file2pkg->{$lib} = $file2pkg->{$reallib};
  133. }
  134. }
  135. if (not scalar(grep { $_ ne '' } @{$file2pkg->{$lib}})) {
  136. # If the library is really not available in an installed package,
  137. # it's because it's in the process of being built
  138. # Empty package name will lead to consideration of symbols
  139. # file from the package being built only
  140. $file2pkg->{$lib} = [""];
  141. print "No associated package found for $lib\n" if $debug;
  142. }
  143. # Load symbols/shlibs files from packages providing libraries
  144. foreach my $pkg (@{$file2pkg->{$lib}}) {
  145. my $dpkg_symfile;
  146. if ($packagetype eq "deb") {
  147. # Use fine-grained dependencies only on real deb
  148. $dpkg_symfile = find_symbols_file($pkg, $soname);
  149. if (defined $dpkg_symfile) {
  150. # Load symbol information
  151. print "Using symbols file $dpkg_symfile for $soname\n" if $debug;
  152. $symfile->load($dpkg_symfile);
  153. }
  154. }
  155. if (defined($dpkg_symfile) && $symfile->has_object($soname)) {
  156. # Initialize dependencies as an unversioned dependency
  157. my $dep = $symfile->get_dependency($soname);
  158. foreach my $subdep (split /\s*,\s*/, $dep) {
  159. if (not exists $dependencies{$cur_field}{$subdep}) {
  160. $dependencies{$cur_field}{$subdep} = '';
  161. }
  162. }
  163. } else {
  164. # No symbol file found, fall back to standard shlibs
  165. my $id = $dumplibs_wo_symfile->parse($lib);
  166. push @soname_wo_symfile, $soname;
  167. my $libobj = $dumplibs_wo_symfile->get_object($id);
  168. # Only try to generate a dependency for libraries with a SONAME
  169. if ($libobj->is_public_library() and not
  170. add_shlibs_dep($soname, $pkg, $pkg_root)) {
  171. # This failure is fairly new, try to be kind by
  172. # ignoring as many cases that can be safely ignored
  173. my $ignore = 0;
  174. # 1/ when the lib and the binary are in the same
  175. # package
  176. my $root_file = guess_pkg_root_dir($file);
  177. my $root_lib = guess_pkg_root_dir($lib);
  178. $ignore++ if defined $root_file and defined $root_lib
  179. and check_files_are_the_same($root_file, $root_lib);
  180. # 2/ when the lib is not versioned and can't be
  181. # handled by shlibs
  182. $ignore++ unless scalar(split_soname($soname));
  183. # 3/ when we have been asked to do so
  184. $ignore++ if $ignore_missing_info;
  185. failure(_g("no dependency information found for %s " .
  186. "(used by %s)."), $lib, $file)
  187. unless $ignore;
  188. }
  189. }
  190. }
  191. }
  192. # Scan all undefined symbols of the binary and resolve to a
  193. # dependency
  194. my %used_sonames = map { $_ => 0 } @sonames;
  195. my $nb_warnings = 0;
  196. my $nb_skipped_warnings = 0;
  197. # Disable warnings about missing symbols when we have not been able to
  198. # find all libs
  199. my $disable_warnings = scalar(keys(%soname_notfound));
  200. foreach my $sym ($obj->get_undefined_dynamic_symbols()) {
  201. my $name = $sym->{name};
  202. if ($sym->{version}) {
  203. $name .= "\@$sym->{version}";
  204. } else {
  205. $name .= "\@Base";
  206. }
  207. my $symdep = $symfile->lookup_symbol($name, \@sonames);
  208. if (defined($symdep)) {
  209. $used_sonames{$symdep->{soname}}++;
  210. update_dependency_version($symdep->{depends},
  211. $symdep->{minver});
  212. } else {
  213. my $syminfo = $dumplibs_wo_symfile->locate_symbol($name);
  214. if (not defined($syminfo)) {
  215. next if $disable_warnings;
  216. # Complain about missing symbols only for executables
  217. # and public libraries
  218. if ($obj->is_executable() or $obj->is_public_library()) {
  219. my $print_name = $name;
  220. # Drop the default suffix for readability
  221. $print_name =~ s/\@Base$//;
  222. unless ($sym->{weak}) {
  223. if ($debug or $nb_warnings < 10) {
  224. warning(_g("symbol %s used by %s found in none of the " .
  225. "libraries."), $print_name, $file);
  226. $nb_warnings++;
  227. } else {
  228. $nb_skipped_warnings++;
  229. }
  230. }
  231. }
  232. } else {
  233. $used_sonames{$syminfo->{soname}}++;
  234. }
  235. }
  236. }
  237. warning(_g("%d other similar warnings have been skipped (use -v to see " .
  238. "them all)."), $nb_skipped_warnings) if $nb_skipped_warnings;
  239. foreach my $soname (@sonames) {
  240. # Adjust minimal version of dependencies with information
  241. # extracted from build-dependencies
  242. my $dev_pkg = $symfile->get_field($soname, 'Build-Depends-Package');
  243. if (defined $dev_pkg) {
  244. my $minver = get_min_version_from_deps($build_deps, $dev_pkg);
  245. if (defined $minver) {
  246. foreach my $dep ($symfile->get_dependencies($soname)) {
  247. update_dependency_version($dep, $minver);
  248. }
  249. }
  250. }
  251. # Warn about un-NEEDED libraries
  252. unless ($soname_notfound{$soname} or $used_sonames{$soname}) {
  253. # Ignore warning for libm.so.6 if also linked against libstdc++
  254. next if ($soname =~ /^libm\.so\.\d+$/ and
  255. scalar grep(/^libstdc\+\+\.so\.\d+/, @sonames));
  256. warning(_g("%s shouldn't be linked with %s (it uses none of its " .
  257. "symbols)."), $file, $soname);
  258. }
  259. }
  260. }
  261. # Open substvars file
  262. my $fh;
  263. if ($stdout) {
  264. $fh = \*STDOUT;
  265. } else {
  266. open(NEW, ">", "$varlistfile.new") ||
  267. syserr(_g("open new substvars file \`%s'"), "$varlistfile.new");
  268. if (-e $varlistfile) {
  269. open(OLD, "<", $varlistfile) ||
  270. syserr(_g("open old varlist file \`%s' for reading"), $varlistfile);
  271. foreach my $entry (grep { not m/^\Q$varnameprefix\E:/ } (<OLD>)) {
  272. print(NEW $entry) ||
  273. syserr(_g("copy old entry to new varlist file \`%s'"),
  274. "$varlistfile.new");
  275. }
  276. close(OLD);
  277. }
  278. $fh = \*NEW;
  279. }
  280. # Write out the shlibs substvars
  281. my %depseen;
  282. sub filter_deps {
  283. my ($dep, $field) = @_;
  284. # Skip dependencies on excluded packages
  285. foreach my $exc (@exclude) {
  286. return 0 if $dep =~ /^\s*\Q$exc\E\b/;
  287. }
  288. # Don't include dependencies if they are already
  289. # mentionned in a higher priority field
  290. if (not defined($depseen{$dep})) {
  291. $depseen{$dep} = $dependencies{$field}{$dep};
  292. return 1;
  293. } else {
  294. # Since dependencies can be versionned, we have to
  295. # verify if the dependency is stronger than the
  296. # previously seen one
  297. if (compare_versions($depseen{$dep}, '>>', $dependencies{$field}{$dep})) {
  298. return 0;
  299. } else {
  300. $depseen{$dep} = $dependencies{$field}{$dep};
  301. return 1;
  302. }
  303. }
  304. }
  305. foreach my $field (reverse @depfields) {
  306. my $dep = "";
  307. if (exists $dependencies{$field} and scalar keys %{$dependencies{$field}}) {
  308. $dep = join ", ",
  309. map {
  310. # Translate dependency templates into real dependencies
  311. if ($dependencies{$field}{$_}) {
  312. s/#MINVER#/(>= $dependencies{$field}{$_})/g;
  313. } else {
  314. s/#MINVER#//g;
  315. }
  316. s/\s+/ /g;
  317. $_;
  318. } grep { filter_deps($_, $field) }
  319. keys %{$dependencies{$field}};
  320. }
  321. if ($dep) {
  322. print $fh "$varnameprefix:$field=$dep\n";
  323. }
  324. }
  325. # Replace old file by new one
  326. if (!$stdout) {
  327. close($fh);
  328. rename("$varlistfile.new",$varlistfile) ||
  329. syserr(_g("install new varlist file \`%s'"), $varlistfile);
  330. }
  331. ##
  332. ## Functions
  333. ##
  334. sub version {
  335. printf _g("Debian %s version %s.\n"), $progname, $version;
  336. printf _g("
  337. Copyright (C) 1996 Ian Jackson.
  338. Copyright (C) 2000 Wichert Akkerman.
  339. Copyright (C) 2006 Frank Lichtenheld.
  340. Copyright (C) 2007 Raphael Hertzog.
  341. ");
  342. printf _g("
  343. This is free software; see the GNU General Public Licence version 2 or
  344. later for copying conditions. There is NO warranty.
  345. ");
  346. }
  347. sub usage {
  348. printf _g(
  349. "Usage: %s [<option> ...] <executable>|-e<executable> [<option> ...]
  350. Positional options (order is significant):
  351. <executable> include dependencies for <executable>,
  352. -e<executable> (use -e if <executable> starts with \`-')
  353. -d<dependencyfield> next executable(s) set shlibs:<dependencyfield>.
  354. Options:
  355. -p<varnameprefix> set <varnameprefix>:* instead of shlibs:*.
  356. -O print variable settings to stdout.
  357. -L<localshlibsfile> shlibs override file, not debian/shlibs.local.
  358. -T<varlistfile> update variables here, not debian/substvars.
  359. -t<type> set package type (default is deb).
  360. -x<package> exclude package from the generated dependencies.
  361. --admindir=<directory> change the administrative directory.
  362. -h, --help show this help message.
  363. --version show the version.
  364. Dependency fields recognised are:
  365. %s
  366. "), $progname, join("/",@depfields);
  367. }
  368. sub get_min_version_from_deps {
  369. my ($dep, $pkg) = @_;
  370. if ($dep->isa('Dpkg::Deps::Simple')) {
  371. if (($dep->{package} eq $pkg) &&
  372. defined($dep->{relation}) &&
  373. (($dep->{relation} eq ">=") ||
  374. ($dep->{relation} eq ">>")))
  375. {
  376. return $dep->{version};
  377. }
  378. return undef;
  379. } else {
  380. my $res;
  381. foreach my $subdep ($dep->get_deps()) {
  382. my $minver = get_min_version_from_deps($subdep, $pkg);
  383. next if not defined $minver;
  384. if (defined $res) {
  385. if (compare_versions($minver, '>>', $res)) {
  386. $res = $minver;
  387. }
  388. } else {
  389. $res = $minver;
  390. }
  391. }
  392. return $res;
  393. }
  394. }
  395. sub update_dependency_version {
  396. my ($dep, $minver) = @_;
  397. return if not defined($minver);
  398. foreach my $subdep (split /\s*,\s*/, $dep) {
  399. if (exists $dependencies{$cur_field}{$subdep} and
  400. defined($dependencies{$cur_field}{$subdep}))
  401. {
  402. if ($dependencies{$cur_field}{$subdep} eq '' or
  403. compare_versions($minver, '>>',
  404. $dependencies{$cur_field}{$subdep}))
  405. {
  406. $dependencies{$cur_field}{$subdep} = $minver;
  407. }
  408. } else {
  409. $dependencies{$cur_field}{$subdep} = $minver;
  410. }
  411. }
  412. }
  413. sub add_shlibs_dep {
  414. my ($soname, $pkg, $pkg_root) = @_;
  415. my @shlibs = ($shlibslocal, $shlibsoverride);
  416. # Make sure the shlibs of the current package is analyzed before the
  417. # shlibs of other binary package from the same source
  418. push @shlibs, "$pkg_root/DEBIAN/shlibs" if defined($pkg_root);
  419. push @shlibs, @pkg_shlibs, "$admindir/info/$pkg.shlibs", $shlibsdefault;
  420. print "Looking up shlibs dependency of $soname provided by '$pkg'\n" if $debug;
  421. foreach my $file (@shlibs) {
  422. next if not -e $file;
  423. my $dep = extract_from_shlibs($soname, $file);
  424. if (defined($dep)) {
  425. print "Found $dep in $file\n" if $debug;
  426. foreach (split(/,\s*/, $dep)) {
  427. $dependencies{$cur_field}{$_} = 1;
  428. }
  429. return 1;
  430. }
  431. }
  432. print "Found nothing\n" if $debug;
  433. return 0;
  434. }
  435. sub split_soname {
  436. my $soname = shift;
  437. if ($soname =~ /^(.*)\.so\.(.*)$/) {
  438. return wantarray ? ($1, $2) : 1;
  439. } elsif ($soname =~ /^(.*)-(\d.*)\.so$/) {
  440. return wantarray ? ($1, $2) : 1;
  441. } else {
  442. return wantarray ? () : 0;
  443. }
  444. }
  445. sub extract_from_shlibs {
  446. my ($soname, $shlibfile) = @_;
  447. # Split soname in name/version
  448. my ($libname, $libversion) = split_soname($soname);
  449. unless (defined $libname) {
  450. warning(_g("Can't extract name and version from library name \`%s'"),
  451. $soname);
  452. return;
  453. }
  454. # Open shlibs file
  455. $shlibfile = "./$shlibfile" if $shlibfile =~ m/^\s/;
  456. open(SHLIBS, "<", $shlibfile) ||
  457. syserr(_g("unable to open shared libs info file \`%s'"), $shlibfile);
  458. my $dep;
  459. while (<SHLIBS>) {
  460. s/\s*\n$//;
  461. next if m/^\#/;
  462. if (!m/^\s*(?:(\S+):\s+)?(\S+)\s+(\S+)(?:\s+(\S.*\S))?\s*$/) {
  463. warning(_g("shared libs info file \`%s' line %d: bad line \`%s'"),
  464. $shlibfile, $., $_);
  465. next;
  466. }
  467. my $depread = defined($4) ? $4 : '';
  468. if (($libname eq $2) && ($libversion eq $3)) {
  469. # Define dep and end here if the package type explicitely
  470. # matches. Otherwise if the packagetype is not specified, use
  471. # the dep only as a default that can be overriden by a later
  472. # line
  473. if (defined($1)) {
  474. if ($1 eq $packagetype) {
  475. $dep = $depread;
  476. last;
  477. }
  478. } else {
  479. $dep = $depread unless defined $dep;
  480. }
  481. }
  482. }
  483. close(SHLIBS);
  484. return $dep;
  485. }
  486. sub find_symbols_file {
  487. my ($pkg, $soname) = @_;
  488. foreach my $file (@pkg_symbols,
  489. "/etc/dpkg/symbols/$pkg.symbols.$host_arch",
  490. "/etc/dpkg/symbols/$pkg.symbols",
  491. "$admindir/info/$pkg.symbols")
  492. {
  493. if (-e $file and symfile_has_soname($file, $soname)) {
  494. return $file;
  495. }
  496. }
  497. return undef;
  498. }
  499. sub symfile_has_soname {
  500. my ($file, $soname) = @_;
  501. open(SYM_FILE, "<", $file) ||
  502. syserr(_g("cannot open file %s"), $file);
  503. my $result = 0;
  504. while (<SYM_FILE>) {
  505. if (/^\Q$soname\E /) {
  506. $result = 1;
  507. last;
  508. }
  509. }
  510. close(SYM_FILE);
  511. return $result;
  512. }
  513. # find_library ($soname, \@rpath, $format)
  514. sub my_find_library {
  515. my ($lib, $rpath, $format, $execfile) = @_;
  516. my $file;
  517. # Create real RPATH in case $ORIGIN is used
  518. # Note: ld.so also supports $PLATFORM and $LIB but they are
  519. # used in real case (yet)
  520. my $libdir = relative_to_pkg_root($execfile);
  521. my $origin;
  522. if (defined $libdir) {
  523. $origin = "/$libdir";
  524. $origin =~ s{/+[^/]*$}{};
  525. }
  526. my @RPATH = ();
  527. foreach my $path (@{$rpath}) {
  528. if ($path =~ /\$ORIGIN|\$\{ORIGIN\}/) {
  529. if (defined $origin) {
  530. $path =~ s/\$ORIGIN/$origin/g;
  531. $path =~ s/\$\{ORIGIN\}/$origin/g;
  532. } else {
  533. warning(_g("\$ORIGIN is used in RPATH of %s and the corresponding " .
  534. "directory could not be identified due to lack of DEBIAN " .
  535. "sub-directory in the root of package's build tree"), $execfile);
  536. }
  537. }
  538. push @RPATH, $path;
  539. }
  540. # Look into the packages we're currently building (but only those
  541. # that provides shlibs file and the one that contains the binary being
  542. # anlyzed...)
  543. # TODO: we should probably replace that by a cleaner way to look into
  544. # the various temporary build directories...
  545. my @copy = (@pkg_shlibs);
  546. my $pkg_root = guess_pkg_root_dir($execfile);
  547. unshift @copy, $pkg_root if defined $pkg_root;
  548. foreach my $builddir (map { s{/DEBIAN/shlibs$}{}; $_ } @copy) {
  549. $file = find_library($lib, \@RPATH, $format, $builddir);
  550. return $file if defined($file);
  551. }
  552. # Fallback in the root directory if we have not found what we were
  553. # looking for in the packages
  554. $file = find_library($lib, \@RPATH, $format, "");
  555. return $file if defined($file);
  556. return undef;
  557. }
  558. my %cached_pkgmatch = ();
  559. sub find_packages {
  560. my @files;
  561. my $pkgmatch = {};
  562. foreach (@_) {
  563. if (exists $cached_pkgmatch{$_}) {
  564. $pkgmatch->{$_} = $cached_pkgmatch{$_};
  565. } else {
  566. push @files, $_;
  567. $cached_pkgmatch{$_} = [""]; # placeholder to cache misses too.
  568. $pkgmatch->{$_} = [""]; # might be replaced later on
  569. }
  570. }
  571. return $pkgmatch unless scalar(@files);
  572. my $pid = open(DPKG, "-|");
  573. syserr(_g("cannot fork for dpkg --search")) unless defined($pid);
  574. if (!$pid) {
  575. # Child process running dpkg --search and discarding errors
  576. close STDERR;
  577. open STDERR, ">", "/dev/null";
  578. $ENV{LC_ALL} = "C";
  579. exec("dpkg", "--search", "--", @files)
  580. || syserr(_g("cannot exec dpkg"));
  581. }
  582. while(defined($_ = <DPKG>)) {
  583. chomp($_);
  584. if (m/^local diversion |^diversion by/) {
  585. warning(_g("diversions involved - output may be incorrect"));
  586. print(STDERR " $_\n")
  587. || syserr(_g("write diversion info to stderr"));
  588. } elsif (m/^([^:]+): (\S+)$/) {
  589. $cached_pkgmatch{$2} = $pkgmatch->{$2} = [ split(/, /, $1) ];
  590. } else {
  591. warning(_g("unknown output from dpkg --search: '%s'"), $_);
  592. }
  593. }
  594. close(DPKG);
  595. return $pkgmatch;
  596. }