dpkg-shlibdeps.pl 20 KB

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