dpkg-shlibdeps.pl 29 KB

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