dpkg-shlibdeps.pl 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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 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::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 $host_arch = get_host_arch();
  67. my (@pkg_shlibs, @pkg_symbols, @pkg_root_dirs);
  68. if (-d 'debian') {
  69. push @pkg_symbols, glob 'debian/*/DEBIAN/symbols';
  70. push @pkg_shlibs, glob 'debian/*/DEBIAN/shlibs';
  71. my %uniq = map { guess_pkg_root_dir($_) => 1 } (@pkg_symbols, @pkg_shlibs);
  72. push @pkg_root_dirs, keys %uniq;
  73. }
  74. my ($stdout, %exec);
  75. foreach (@ARGV) {
  76. if (m/^-T(.*)$/) {
  77. $varlistfile = $1;
  78. } elsif (m/^-p(\w[-:0-9A-Za-z]*)$/) {
  79. $varnameprefix = $1;
  80. } elsif (m/^-L(.*)$/) {
  81. $shlibslocal = $1;
  82. } elsif (m/^-l(.*)$/) {
  83. Dpkg::Shlibs::add_library_dir($1);
  84. } elsif (m/^-S(.*)$/) {
  85. push @pkg_dir_to_search, $1;
  86. } elsif (m/^-O$/) {
  87. $stdout = 1;
  88. } elsif (m/^-O(.+)$/) {
  89. $varlistfile = $1;
  90. } elsif (m/^-(?:\?|-help)$/) {
  91. usage(); exit(0);
  92. } elsif (m/^--version$/) {
  93. version(); exit(0);
  94. } elsif (m/^--admindir=(.*)$/) {
  95. $admindir = $1;
  96. if (not -d $admindir) {
  97. error(g_("administrative directory '%s' does not exist"), $admindir);
  98. }
  99. $ENV{DPKG_ADMINDIR} = $admindir;
  100. } elsif (m/^-d(.*)$/) {
  101. $dependencyfield = field_capitalize($1);
  102. if (not defined $depstrength{$dependencyfield}) {
  103. warning(g_("unrecognized dependency field '%s'"), $dependencyfield);
  104. }
  105. } elsif (m/^-e(.*)$/) {
  106. if (exists $exec{$1}) {
  107. # Affect the binary to the most important field
  108. if ($depstrength{$dependencyfield} > $depstrength{$exec{$1}}) {
  109. $exec{$1} = $dependencyfield;
  110. }
  111. } else {
  112. $exec{$1} = $dependencyfield;
  113. }
  114. } elsif (m/^--ignore-missing-info$/) {
  115. $ignore_missing_info = 1;
  116. } elsif (m/^--warnings=(\d+)$/) {
  117. $warnings = $1;
  118. } elsif (m/^-t(.*)$/) {
  119. $packagetype = $1;
  120. } elsif (m/^-v$/) {
  121. $debug++;
  122. } elsif (m/^-x(.*)$/) {
  123. push @exclude, $1;
  124. } elsif (m/^-/) {
  125. usageerr(g_("unknown option '%s'"), $_);
  126. } else {
  127. if (exists $exec{$_}) {
  128. # Affect the binary to the most important field
  129. if ($depstrength{$dependencyfield} > $depstrength{$exec{$_}}) {
  130. $exec{$_} = $dependencyfield;
  131. }
  132. } else {
  133. $exec{$_} = $dependencyfield;
  134. }
  135. }
  136. }
  137. usageerr(g_('need at least one executable')) unless scalar keys %exec;
  138. my $control = Dpkg::Control::Info->new();
  139. my $fields = $control->get_source();
  140. my $bd_value = deps_concat($fields->{'Build-Depends'}, $fields->{'Build-Depends-Arch'});
  141. my $build_deps = deps_parse($bd_value, build_dep => 1, reduce_restrictions => 1);
  142. error(g_('error occurred while parsing %s'), 'Build-Depends/Build-Depends-Arch')
  143. unless defined $build_deps;
  144. my %dependencies;
  145. # Statictics on soname seen in the whole run (with multiple analysis of
  146. # binaries)
  147. my %global_soname_notfound;
  148. my %global_soname_used;
  149. my %global_soname_needed;
  150. # Symfile and objdump caches
  151. my %symfile_cache;
  152. my %objdump_cache;
  153. my %symfile_has_soname_cache;
  154. # Used to count errors due to missing libraries
  155. my $error_count = 0;
  156. my $cur_field;
  157. foreach my $file (keys %exec) {
  158. $cur_field = $exec{$file};
  159. print ">> Scanning $file (for $cur_field field)\n" if $debug;
  160. my $obj = Dpkg::Shlibs::Objdump::Object->new($file);
  161. my @sonames = $obj->get_needed_libraries;
  162. # Load symbols files for all needed libraries (identified by SONAME)
  163. my %libfiles;
  164. my %altlibfiles;
  165. my %soname_notfound;
  166. my %alt_soname;
  167. foreach my $soname (@sonames) {
  168. my $lib = my_find_library($soname, $obj->{RPATH}, $obj->{format}, $file);
  169. unless (defined $lib) {
  170. $soname_notfound{$soname} = 1;
  171. $global_soname_notfound{$soname} = 1;
  172. my $msg = g_("couldn't find library %s needed by %s (ELF " .
  173. "format: '%s'; RPATH: '%s')");
  174. if (scalar(split_soname($soname))) {
  175. errormsg($msg, $soname, $file, $obj->{format}, join(':', @{$obj->{RPATH}}));
  176. $error_count++;
  177. } else {
  178. warning($msg, $soname, $file, $obj->{format}, join(':', @{$obj->{RPATH}}));
  179. }
  180. next;
  181. }
  182. $libfiles{$lib} = $soname;
  183. my $reallib = realpath($lib);
  184. if ($reallib ne $lib) {
  185. $altlibfiles{$reallib} = $soname;
  186. }
  187. print "Library $soname found in $lib\n" if $debug;
  188. }
  189. my $file2pkg = find_packages(keys %libfiles, keys %altlibfiles);
  190. my $symfile = Dpkg::Shlibs::SymbolFile->new();
  191. my $dumplibs_wo_symfile = Dpkg::Shlibs::Objdump->new();
  192. my @soname_wo_symfile;
  193. foreach my $lib (keys %libfiles) {
  194. my $soname = $libfiles{$lib};
  195. if (none { $_ ne '' } @{$file2pkg->{$lib}}) {
  196. # The path of the library as calculated is not the
  197. # official path of a packaged file, try to fallback on
  198. # on the realpath() first, maybe this one is part of a package
  199. my $reallib = realpath($lib);
  200. if (exists $file2pkg->{$reallib}) {
  201. $file2pkg->{$lib} = $file2pkg->{$reallib};
  202. }
  203. }
  204. if (none { $_ ne '' } @{$file2pkg->{$lib}}) {
  205. # If the library is really not available in an installed package,
  206. # it's because it's in the process of being built
  207. # Empty package name will lead to consideration of symbols
  208. # file from the package being built only
  209. $file2pkg->{$lib} = [''];
  210. print "No associated package found for $lib\n" if $debug;
  211. }
  212. # Load symbols/shlibs files from packages providing libraries
  213. foreach my $pkg (@{$file2pkg->{$lib}}) {
  214. my $symfile_path;
  215. my $haslocaldep = 0;
  216. if (-e $shlibslocal and
  217. defined(extract_from_shlibs($soname, $shlibslocal)))
  218. {
  219. $haslocaldep = 1;
  220. }
  221. if ($packagetype eq 'deb' and not $haslocaldep) {
  222. # Use fine-grained dependencies only on real deb
  223. # and only if the dependency is not provided by shlibs.local
  224. $symfile_path = find_symbols_file($pkg, $soname, $lib);
  225. }
  226. if (defined($symfile_path)) {
  227. # Load symbol information
  228. print "Using symbols file $symfile_path for $soname\n" if $debug;
  229. $symfile_cache{$symfile_path} //=
  230. Dpkg::Shlibs::SymbolFile->new(file => $symfile_path);
  231. $symfile->merge_object_from_symfile($symfile_cache{$symfile_path}, $soname);
  232. }
  233. if (defined($symfile_path) && $symfile->has_object($soname)) {
  234. # Initialize dependencies with the smallest minimal version
  235. # of all symbols (unversioned dependency is not ok as the
  236. # library might not have always been available in the
  237. # package and we really need it)
  238. my $dep = $symfile->get_dependency($soname);
  239. my $minver = $symfile->get_smallest_version($soname) || '';
  240. update_dependency_version($dep, $minver);
  241. print " Minimal version of ($dep) initialized with ($minver)\n"
  242. if $debug > 1;
  243. } else {
  244. # No symbol file found, fall back to standard shlibs
  245. print "Using shlibs+objdump for $soname (file $lib)\n" if $debug;
  246. $objdump_cache{$lib} //= Dpkg::Shlibs::Objdump::Object->new($lib);
  247. my $libobj = $objdump_cache{$lib};
  248. my $id = $dumplibs_wo_symfile->add_object($libobj);
  249. if (($id ne $soname) and ($id ne $lib)) {
  250. warning(g_('%s has an unexpected SONAME (%s)'), $lib, $id);
  251. $alt_soname{$id} = $soname;
  252. }
  253. push @soname_wo_symfile, $soname;
  254. # Only try to generate a dependency for libraries with a SONAME
  255. if ($libobj->is_public_library() and not
  256. add_shlibs_dep($soname, $pkg, $lib)) {
  257. # This failure is fairly new, try to be kind by
  258. # ignoring as many cases that can be safely ignored
  259. my $ignore = 0;
  260. # 1/ when the lib and the binary are in the same
  261. # package
  262. my $root_file = guess_pkg_root_dir($file);
  263. my $root_lib = guess_pkg_root_dir($lib);
  264. $ignore++ if defined $root_file and defined $root_lib
  265. and check_files_are_the_same($root_file, $root_lib);
  266. # 2/ when the lib is not versioned and can't be
  267. # handled by shlibs
  268. $ignore++ unless scalar(split_soname($soname));
  269. # 3/ when we have been asked to do so
  270. $ignore++ if $ignore_missing_info;
  271. error(g_('no dependency information found for %s ' .
  272. "(used by %s)\n" .
  273. 'Hint: check if the library actually comes ' .
  274. 'from a package.'), $lib, $file)
  275. unless $ignore;
  276. }
  277. }
  278. }
  279. }
  280. # Scan all undefined symbols of the binary and resolve to a
  281. # dependency
  282. my %soname_used;
  283. foreach my $soname (@sonames) {
  284. # Initialize statistics
  285. $soname_used{$soname} = 0;
  286. $global_soname_used{$soname} //= 0;
  287. if (exists $global_soname_needed{$soname}) {
  288. push @{$global_soname_needed{$soname}}, $file;
  289. } else {
  290. $global_soname_needed{$soname} = [ $file ];
  291. }
  292. }
  293. my $nb_warnings = 0;
  294. my $nb_skipped_warnings = 0;
  295. # Disable warnings about missing symbols when we have not been able to
  296. # find all libs
  297. my $disable_warnings = scalar(keys(%soname_notfound));
  298. my $in_public_dir = 1;
  299. if (my $relname = relative_to_pkg_root($file)) {
  300. my $parent_dir = '/' . dirname($relname);
  301. $in_public_dir = any { $parent_dir eq $_ } get_library_paths();
  302. } else {
  303. warning(g_('binaries to analyze should already be ' .
  304. "installed in their package's directory"));
  305. }
  306. print "Analyzing all undefined symbols\n" if $debug > 1;
  307. foreach my $sym ($obj->get_undefined_dynamic_symbols()) {
  308. my $name = $sym->{name};
  309. if ($sym->{version}) {
  310. $name .= '@' . "$sym->{version}";
  311. } else {
  312. $name .= '@' . 'Base';
  313. }
  314. print " Looking up symbol $name\n" if $debug > 1;
  315. my %symdep = $symfile->lookup_symbol($name, \@sonames);
  316. if (keys %symdep) {
  317. my $depends = $symfile->get_dependency($symdep{soname},
  318. $symdep{symbol}{dep_id});
  319. print " Found in symbols file of $symdep{soname} (minver: " .
  320. "$symdep{symbol}{minver}, dep: $depends)\n" if $debug > 1;
  321. $soname_used{$symdep{soname}}++;
  322. $global_soname_used{$symdep{soname}}++;
  323. if (exists $alt_soname{$symdep{soname}}) {
  324. # Also count usage on alternate soname
  325. $soname_used{$alt_soname{$symdep{soname}}}++;
  326. $global_soname_used{$alt_soname{$symdep{soname}}}++;
  327. }
  328. update_dependency_version($depends, $symdep{symbol}{minver});
  329. } else {
  330. my $syminfo = $dumplibs_wo_symfile->locate_symbol($name);
  331. if (not defined($syminfo)) {
  332. print " Not found\n" if $debug > 1;
  333. next unless ($warnings & WARN_SYM_NOT_FOUND);
  334. next if $disable_warnings;
  335. # Complain about missing symbols only for executables
  336. # and public libraries
  337. if ($obj->is_executable() or $obj->is_public_library()) {
  338. my $print_name = $name;
  339. # Drop the default suffix for readability
  340. $print_name =~ s/\@Base$//;
  341. unless ($sym->{weak}) {
  342. if ($debug or ($in_public_dir and $nb_warnings < 10)
  343. or (not $in_public_dir and $nb_warnings < 1))
  344. {
  345. if ($in_public_dir) {
  346. warning(g_('symbol %s used by %s found in none of the ' .
  347. 'libraries'), $print_name, $file);
  348. } else {
  349. warning(g_('%s contains an unresolvable reference to ' .
  350. "symbol %s: it's probably a plugin"),
  351. $file, $print_name);
  352. }
  353. $nb_warnings++;
  354. } else {
  355. $nb_skipped_warnings++;
  356. }
  357. }
  358. }
  359. } else {
  360. print " Found in $syminfo->{soname} ($syminfo->{objid})\n" if $debug > 1;
  361. if (exists $alt_soname{$syminfo->{soname}}) {
  362. # Also count usage on alternate soname
  363. $soname_used{$alt_soname{$syminfo->{soname}}}++;
  364. $global_soname_used{$alt_soname{$syminfo->{soname}}}++;
  365. }
  366. $soname_used{$syminfo->{soname}}++;
  367. $global_soname_used{$syminfo->{soname}}++;
  368. }
  369. }
  370. }
  371. warning(P_('%d similar warning has been skipped (use -v to see it)',
  372. '%d other similar warnings have been skipped (use -v to see ' .
  373. 'them all)', $nb_skipped_warnings), $nb_skipped_warnings)
  374. if $nb_skipped_warnings;
  375. foreach my $soname (@sonames) {
  376. # Adjust minimal version of dependencies with information
  377. # extracted from build-dependencies
  378. my $dev_pkg = $symfile->get_field($soname, 'Build-Depends-Package');
  379. if (defined $dev_pkg) {
  380. print "Updating dependencies of $soname with build-dependencies\n" if $debug;
  381. my $minver = get_min_version_from_deps($build_deps, $dev_pkg);
  382. if (defined $minver) {
  383. foreach my $dep ($symfile->get_dependencies($soname)) {
  384. update_dependency_version($dep, $minver, 1);
  385. print " Minimal version of $dep updated with $minver\n" if $debug;
  386. }
  387. } else {
  388. print " No minimal version found in $dev_pkg build-dependency\n" if $debug;
  389. }
  390. }
  391. # Warn about un-NEEDED libraries
  392. unless ($soname_notfound{$soname} or $soname_used{$soname}) {
  393. # Ignore warning for libm.so.6 if also linked against libstdc++
  394. next if ($soname =~ /^libm\.so\.\d+$/ and
  395. any { m/^libstdc\+\+\.so\.\d+/ } @sonames);
  396. next unless ($warnings & WARN_NOT_NEEDED);
  397. warning(g_('%s should not be linked against %s (it uses none of ' .
  398. "the library's symbols)"), $file, $soname);
  399. }
  400. }
  401. }
  402. # Warn of unneeded libraries at the "package" level (i.e. over all
  403. # binaries that we have inspected)
  404. foreach my $soname (keys %global_soname_needed) {
  405. unless ($global_soname_notfound{$soname} or $global_soname_used{$soname}) {
  406. next if ($soname =~ /^libm\.so\.\d+$/ and
  407. any { m/^libstdc\+\+\.so\.\d+/ } keys %global_soname_needed);
  408. next unless ($warnings & WARN_DEP_AVOIDABLE);
  409. warning(P_('package could avoid a useless dependency if %s was not ' .
  410. "linked against %s (it uses none of the library's symbols)",
  411. 'package could avoid a useless dependency if %s were not ' .
  412. "linked against %s (they use none of the library's symbols)",
  413. scalar @{$global_soname_needed{$soname}}),
  414. join(' ', @{$global_soname_needed{$soname}}), $soname);
  415. }
  416. }
  417. # Quit now if any missing libraries
  418. if ($error_count >= 1) {
  419. my $note = g_('Note: libraries are not searched in other binary packages ' .
  420. "that do not have any shlibs or symbols file.\nTo help dpkg-shlibdeps " .
  421. 'find private libraries, you might need to use -l.');
  422. error(P_('cannot continue due to the error above',
  423. 'cannot continue due to the errors listed above',
  424. $error_count) . "\n" . $note);
  425. }
  426. # Open substvars file
  427. my $substvars = Dpkg::Substvars->new();
  428. if ($stdout) {
  429. $varlistfilenew = '-';
  430. } else {
  431. $substvars->load($varlistfile) if -e $varlistfile;
  432. $substvars->filter(remove => sub { $_[0] =~ m/^\Q$varnameprefix\E:/ });
  433. $varlistfilenew = "$varlistfile.new";
  434. }
  435. # Write out the shlibs substvars
  436. my %depseen;
  437. sub filter_deps {
  438. my ($dep, $field) = @_;
  439. # Skip dependencies on excluded packages
  440. foreach my $exc (@exclude) {
  441. return 0 if $dep =~ /^\s*\Q$exc\E\b/;
  442. }
  443. # Don't include dependencies if they are already
  444. # mentioned in a higher priority field
  445. if (not exists($depseen{$dep})) {
  446. $depseen{$dep} = $dependencies{$field}{$dep};
  447. return 1;
  448. } else {
  449. # Since dependencies can be versioned, we have to
  450. # verify if the dependency is stronger than the
  451. # previously seen one
  452. my $stronger;
  453. if ($depseen{$dep} eq $dependencies{$field}{$dep}) {
  454. # If both versions are the same (possibly unversioned)
  455. $stronger = 0;
  456. } elsif ($dependencies{$field}{$dep} eq '') {
  457. $stronger = 0; # If the dep is unversioned
  458. } elsif ($depseen{$dep} eq '') {
  459. $stronger = 1; # If the dep seen is unversioned
  460. } elsif (version_compare_relation($depseen{$dep}, REL_GT,
  461. $dependencies{$field}{$dep})) {
  462. # The version of the dep seen is stronger...
  463. $stronger = 0;
  464. } else {
  465. $stronger = 1;
  466. }
  467. $depseen{$dep} = $dependencies{$field}{$dep} if $stronger;
  468. return $stronger;
  469. }
  470. }
  471. foreach my $field (reverse @depfields) {
  472. my $dep = '';
  473. if (exists $dependencies{$field} and scalar keys %{$dependencies{$field}}) {
  474. $dep = join ', ',
  475. map {
  476. # Translate dependency templates into real dependencies
  477. my $templ = $_;
  478. if ($dependencies{$field}{$templ}) {
  479. $templ =~ s/#MINVER#/(>= $dependencies{$field}{$templ})/g;
  480. } else {
  481. $templ =~ s/#MINVER#//g;
  482. }
  483. $templ =~ s/\s+/ /g;
  484. $templ;
  485. } grep {
  486. filter_deps($_, $field)
  487. } keys %{$dependencies{$field}};
  488. }
  489. if ($dep) {
  490. my $obj = deps_parse($dep);
  491. error(g_('invalid dependency got generated: %s'), $dep) unless defined $obj;
  492. $obj->sort();
  493. $substvars->set_as_used("$varnameprefix:$field", "$obj");
  494. }
  495. }
  496. $substvars->save($varlistfilenew);
  497. # Replace old file by new one
  498. if (!$stdout) {
  499. rename $varlistfilenew, $varlistfile
  500. or syserr(g_("install new varlist file '%s'"), $varlistfile);
  501. }
  502. ##
  503. ## Functions
  504. ##
  505. sub version {
  506. printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  507. printf g_('
  508. This is free software; see the GNU General Public License version 2 or
  509. later for copying conditions. There is NO warranty.
  510. ');
  511. }
  512. sub usage {
  513. printf g_(
  514. 'Usage: %s [<option>...] <executable>|-e<executable> [<option>...]')
  515. . "\n\n" . g_(
  516. "Positional options (order is significant):
  517. <executable> include dependencies for <executable>,
  518. -e<executable> (use -e if <executable> starts with '-')
  519. -d<dependency-field> next executable(s) set shlibs:<dependency-field>.")
  520. . "\n\n" . g_(
  521. "Options:
  522. -l<library-dir> add directory to private shared library search list.
  523. -p<varname-prefix> set <varname-prefix>:* instead of shlibs:*.
  524. -O[<file>] write variable settings to stdout (or <file>).
  525. -L<local-shlibs-file> shlibs override file, not debian/shlibs.local.
  526. -T<substvars-file> update variables here, not debian/substvars.
  527. -t<type> set package type (default is deb).
  528. -x<package> exclude package from the generated dependencies.
  529. -S<package-build-dir> search needed libraries in the given
  530. package build directory first.
  531. -v enable verbose mode (can be used multiple times).
  532. --ignore-missing-info don't fail if dependency information can't be found.
  533. --warnings=<value> define set of active warnings (see manual page).
  534. --admindir=<directory> change the administrative directory.
  535. -?, --help show this help message.
  536. --version show the version.")
  537. . "\n\n" . g_(
  538. 'Dependency fields recognized are:
  539. %s
  540. '), $Dpkg::PROGNAME, join('/', @depfields);
  541. }
  542. sub get_min_version_from_deps {
  543. my ($dep, $pkg) = @_;
  544. if ($dep->isa('Dpkg::Deps::Simple')) {
  545. if (($dep->{package} eq $pkg) &&
  546. defined($dep->{relation}) &&
  547. (($dep->{relation} eq REL_GE) ||
  548. ($dep->{relation} eq REL_GT)))
  549. {
  550. return $dep->{version};
  551. }
  552. return;
  553. } else {
  554. my $res;
  555. foreach my $subdep ($dep->get_deps()) {
  556. my $minver = get_min_version_from_deps($subdep, $pkg);
  557. next if not defined $minver;
  558. if (defined $res) {
  559. if (version_compare_relation($minver, REL_GT, $res)) {
  560. $res = $minver;
  561. }
  562. } else {
  563. $res = $minver;
  564. }
  565. }
  566. return $res;
  567. }
  568. }
  569. sub update_dependency_version {
  570. my ($dep, $minver, $existing_only) = @_;
  571. return if not defined($minver);
  572. $minver = Dpkg::Version->new($minver);
  573. foreach my $subdep (split /\s*,\s*/, $dep) {
  574. if (exists $dependencies{$cur_field}{$subdep} and
  575. defined($dependencies{$cur_field}{$subdep}))
  576. {
  577. if ($dependencies{$cur_field}{$subdep} eq '' or $minver ne '' and
  578. version_compare_relation($minver, REL_GT,
  579. $dependencies{$cur_field}{$subdep}))
  580. {
  581. $dependencies{$cur_field}{$subdep} = $minver;
  582. }
  583. } elsif (!$existing_only) {
  584. $dependencies{$cur_field}{$subdep} = $minver;
  585. }
  586. }
  587. }
  588. sub add_shlibs_dep {
  589. my ($soname, $pkg, $libfile) = @_;
  590. my @shlibs = ($shlibslocal, $shlibsoverride);
  591. if ($pkg eq '') {
  592. # If the file is not packaged, try to find out the shlibs file in
  593. # the package being built where the lib has been found
  594. my $pkg_root = guess_pkg_root_dir($libfile);
  595. if (defined $pkg_root) {
  596. push @shlibs, "$pkg_root/DEBIAN/shlibs";
  597. }
  598. # Fallback to other shlibs files but it shouldn't be necessary
  599. push @shlibs, @pkg_shlibs;
  600. } else {
  601. my $control_file = get_control_path($pkg, 'shlibs');
  602. push @shlibs, $control_file if defined $control_file;
  603. }
  604. push @shlibs, $shlibsdefault;
  605. print " Looking up shlibs dependency of $soname provided by '$pkg'\n" if $debug;
  606. foreach my $file (@shlibs) {
  607. next if not -e $file;
  608. my $dep = extract_from_shlibs($soname, $file);
  609. if (defined($dep)) {
  610. print " Found $dep in $file\n" if $debug;
  611. foreach (split(/,\s*/, $dep)) {
  612. # Note: the value is empty for shlibs based dependency
  613. # symbol based dependency will put a valid version as value
  614. $dependencies{$cur_field}{$_} = Dpkg::Version->new('');
  615. }
  616. return 1;
  617. }
  618. }
  619. print " Found nothing\n" if $debug;
  620. return 0;
  621. }
  622. sub split_soname {
  623. my $soname = shift;
  624. if ($soname =~ /^(.*)\.so\.(.*)$/) {
  625. return wantarray ? ($1, $2) : 1;
  626. } elsif ($soname =~ /^(.*)-(\d.*)\.so$/) {
  627. return wantarray ? ($1, $2) : 1;
  628. } else {
  629. return wantarray ? () : 0;
  630. }
  631. }
  632. sub extract_from_shlibs {
  633. my ($soname, $shlibfile) = @_;
  634. my $shlibs_re = qr{
  635. ^\s*
  636. (?:(\S+):\s+)? # Optional type
  637. (\S+)\s+ # Library
  638. (\S+) # Version
  639. (?:
  640. \s+
  641. (\S.*\S) # Dependencies
  642. )?
  643. \s*$
  644. }x;
  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/$shlibs_re/) {
  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 overridden 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 my $path (@_) {
  782. if (exists $cached_pkgmatch{$path}) {
  783. $pkgmatch->{$path} = $cached_pkgmatch{$path};
  784. } else {
  785. push @files, $path;
  786. $cached_pkgmatch{$path} = ['']; # placeholder to cache misses too.
  787. $pkgmatch->{$path} = ['']; # 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-query --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-query', '--search', '--', @files
  800. or syserr(g_('unable to execute %s'), 'dpkg');
  801. }
  802. while (<$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. }