dpkg-shlibdeps.pl 29 KB

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