dpkg-shlibdeps.pl 29 KB

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