dpkg-shlibdeps.pl 30 KB

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