dpkg-shlibdeps.pl 19 KB

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