dpkg-shlibdeps.pl 29 KB

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