dpkg-shlibdeps.pl 20 KB

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