dpkg-shlibdeps.pl 29 KB

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