dpkg-shlibdeps.pl 24 KB

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