dpkg-shlibdeps.pl 29 KB

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