dpkg-shlibdeps.pl 25 KB

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