dpkg-shlibdeps.pl 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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::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 @librarypaths);
  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/^-(\?|-help)$/) {
  87. usage(); exit(0);
  88. } elsif (m/^--version$/) {
  89. version(); exit(0);
  90. } elsif (m/^--admindir=(.*)$/) {
  91. $admindir = $1;
  92. -d $admindir ||
  93. error(_g("administrative directory '%s' does not exist"), $admindir);
  94. $ENV{DPKG_ADMINDIR} = $admindir;
  95. } elsif (m/^-d(.*)$/) {
  96. $dependencyfield = field_capitalize($1);
  97. defined($depstrength{$dependencyfield}) ||
  98. warning(_g("unrecognized dependency field '%s'"), $dependencyfield);
  99. } elsif (m/^-e(.*)$/) {
  100. if (exists $exec{$1}) {
  101. # Affect the binary to the most important field
  102. if ($depstrength{$dependencyfield} > $depstrength{$exec{$1}}) {
  103. $exec{$1} = $dependencyfield;
  104. }
  105. } else {
  106. $exec{$1} = $dependencyfield;
  107. }
  108. } elsif (m/^--ignore-missing-info$/) {
  109. $ignore_missing_info = 1;
  110. } elsif (m/^--warnings=(\d+)$/) {
  111. $warnings = $1;
  112. } elsif (m/^-t(.*)$/) {
  113. $packagetype = $1;
  114. } elsif (m/^-v$/) {
  115. $debug++;
  116. } elsif (m/^-x(.*)$/) {
  117. push @exclude, $1;
  118. } elsif (m/^-/) {
  119. usageerr(_g("unknown option \`%s'"), $_);
  120. } else {
  121. if (exists $exec{$_}) {
  122. # Affect the binary to the most important field
  123. if ($depstrength{$dependencyfield} > $depstrength{$exec{$_}}) {
  124. $exec{$_} = $dependencyfield;
  125. }
  126. } else {
  127. $exec{$_} = $dependencyfield;
  128. }
  129. }
  130. }
  131. scalar keys %exec || usageerr(_g('need at least one executable'));
  132. my $control = Dpkg::Control::Info->new();
  133. my $fields = $control->get_source();
  134. my $bd_value = deps_concat($fields->{'Build-Depends'}, $fields->{'Build-Depends-Arch'});
  135. my $build_deps = deps_parse($bd_value, build_dep => 1, reduce_arch => 1);
  136. error(_g('error occurred while parsing %s'), 'Build-Depends/Build-Depends-Arch')
  137. unless defined $build_deps;
  138. my %dependencies;
  139. # Statictics on soname seen in the whole run (with multiple analysis of
  140. # binaries)
  141. my %global_soname_notfound;
  142. my %global_soname_used;
  143. my %global_soname_needed;
  144. # Symfile and objdump caches
  145. my %symfile_cache;
  146. my %objdump_cache;
  147. my %symfile_has_soname_cache;
  148. # Used to count errors due to missing libraries
  149. my $error_count = 0;
  150. my $cur_field;
  151. foreach my $file (keys %exec) {
  152. $cur_field = $exec{$file};
  153. print ">> Scanning $file (for $cur_field field)\n" if $debug;
  154. my $obj = Dpkg::Shlibs::Objdump::Object->new($file);
  155. my @sonames = $obj->get_needed_libraries;
  156. # Load symbols files for all needed libraries (identified by SONAME)
  157. my %libfiles;
  158. my %altlibfiles;
  159. my %soname_notfound;
  160. my %alt_soname;
  161. foreach my $soname (@sonames) {
  162. my $lib = my_find_library($soname, $obj->{RPATH}, $obj->{format}, $file);
  163. unless (defined $lib) {
  164. $soname_notfound{$soname} = 1;
  165. $global_soname_notfound{$soname} = 1;
  166. my $msg = _g("couldn't find library %s needed by %s (ELF " .
  167. "format: '%s'; RPATH: '%s')");
  168. if (scalar(split_soname($soname))) {
  169. errormsg($msg, $soname, $file, $obj->{format}, join(':', @{$obj->{RPATH}}));
  170. $error_count++;
  171. } else {
  172. warning($msg, $soname, $file, $obj->{format}, join(':', @{$obj->{RPATH}}));
  173. }
  174. next;
  175. }
  176. $libfiles{$lib} = $soname;
  177. my $reallib = realpath($lib);
  178. if ($reallib ne $lib) {
  179. $altlibfiles{$reallib} = $soname;
  180. }
  181. print "Library $soname found in $lib\n" if $debug;
  182. }
  183. my $file2pkg = find_packages(keys %libfiles, keys %altlibfiles);
  184. my $symfile = Dpkg::Shlibs::SymbolFile->new();
  185. my $dumplibs_wo_symfile = Dpkg::Shlibs::Objdump->new();
  186. my @soname_wo_symfile;
  187. foreach my $lib (keys %libfiles) {
  188. my $soname = $libfiles{$lib};
  189. if (none { $_ ne '' } @{$file2pkg->{$lib}}) {
  190. # The path of the library as calculated is not the
  191. # official path of a packaged file, try to fallback on
  192. # on the realpath() first, maybe this one is part of a package
  193. my $reallib = realpath($lib);
  194. if (exists $file2pkg->{$reallib}) {
  195. $file2pkg->{$lib} = $file2pkg->{$reallib};
  196. }
  197. }
  198. if (none { $_ ne '' } @{$file2pkg->{$lib}}) {
  199. # If the library is really not available in an installed package,
  200. # it's because it's in the process of being built
  201. # Empty package name will lead to consideration of symbols
  202. # file from the package being built only
  203. $file2pkg->{$lib} = [''];
  204. print "No associated package found for $lib\n" if $debug;
  205. }
  206. # Load symbols/shlibs files from packages providing libraries
  207. foreach my $pkg (@{$file2pkg->{$lib}}) {
  208. my $symfile_path;
  209. my $haslocaldep = 0;
  210. if (-e $shlibslocal and
  211. defined(extract_from_shlibs($soname, $shlibslocal)))
  212. {
  213. $haslocaldep = 1;
  214. }
  215. if ($packagetype eq 'deb' and not $haslocaldep) {
  216. # Use fine-grained dependencies only on real deb
  217. # and only if the dependency is not provided by shlibs.local
  218. $symfile_path = find_symbols_file($pkg, $soname, $lib);
  219. }
  220. if (defined($symfile_path)) {
  221. # Load symbol information
  222. print "Using symbols file $symfile_path for $soname\n" if $debug;
  223. unless (exists $symfile_cache{$symfile_path}) {
  224. $symfile_cache{$symfile_path} =
  225. Dpkg::Shlibs::SymbolFile->new(file => $symfile_path);
  226. }
  227. $symfile->merge_object_from_symfile($symfile_cache{$symfile_path}, $soname);
  228. }
  229. if (defined($symfile_path) && $symfile->has_object($soname)) {
  230. # Initialize dependencies with the smallest minimal version
  231. # of all symbols (unversioned dependency is not ok as the
  232. # library might not have always been available in the
  233. # package and we really need it)
  234. my $dep = $symfile->get_dependency($soname);
  235. my $minver = $symfile->get_smallest_version($soname) || '';
  236. foreach my $subdep (split /\s*,\s*/, $dep) {
  237. if (not exists $dependencies{$cur_field}{$subdep}) {
  238. $dependencies{$cur_field}{$subdep} = Dpkg::Version->new($minver);
  239. print " Initialize dependency ($subdep) with minimal " .
  240. "version ($minver)\n" if $debug > 1;
  241. }
  242. }
  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. unless (exists $objdump_cache{$lib}) {
  247. $objdump_cache{$lib} = Dpkg::Shlibs::Objdump::Object->new($lib);
  248. }
  249. my $libobj = $objdump_cache{$lib};
  250. my $id = $dumplibs_wo_symfile->add_object($libobj);
  251. if (($id ne $soname) and ($id ne $lib)) {
  252. warning(_g('%s has an unexpected SONAME (%s)'), $lib, $id);
  253. $alt_soname{$id} = $soname;
  254. }
  255. push @soname_wo_symfile, $soname;
  256. # Only try to generate a dependency for libraries with a SONAME
  257. if ($libobj->is_public_library() and not
  258. add_shlibs_dep($soname, $pkg, $lib)) {
  259. # This failure is fairly new, try to be kind by
  260. # ignoring as many cases that can be safely ignored
  261. my $ignore = 0;
  262. # 1/ when the lib and the binary are in the same
  263. # package
  264. my $root_file = guess_pkg_root_dir($file);
  265. my $root_lib = guess_pkg_root_dir($lib);
  266. $ignore++ if defined $root_file and defined $root_lib
  267. and check_files_are_the_same($root_file, $root_lib);
  268. # 2/ when the lib is not versioned and can't be
  269. # handled by shlibs
  270. $ignore++ unless scalar(split_soname($soname));
  271. # 3/ when we have been asked to do so
  272. $ignore++ if $ignore_missing_info;
  273. error(_g('no dependency information found for %s ' .
  274. '(used by %s)'), $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 (@sonames) {
  284. # Initialize statistics
  285. $soname_used{$_} = 0;
  286. $global_soname_used{$_} = 0 unless exists $global_soname_used{$_};
  287. if (exists $global_soname_needed{$_}) {
  288. push @{$global_soname_needed{$_}}, $file;
  289. } else {
  290. $global_soname_needed{$_} = [ $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 $_ } @librarypaths;
  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 $fh;
  428. if ($stdout) {
  429. $fh = \*STDOUT;
  430. } else {
  431. open(my $new_fh, '>', "$varlistfile.new") ||
  432. syserr(_g("open new substvars file \`%s'"), "$varlistfile.new");
  433. if (-e $varlistfile) {
  434. open(my $old_fh, '<', $varlistfile) ||
  435. syserr(_g("open old varlist file \`%s' for reading"), $varlistfile);
  436. while (my $entry = <$old_fh>) {
  437. next if $entry =~ m/^\Q$varnameprefix\E:/;
  438. print($new_fh $entry) ||
  439. syserr(_g("copy old entry to new varlist file \`%s'"),
  440. "$varlistfile.new");
  441. }
  442. close($old_fh);
  443. }
  444. $fh = $new_fh;
  445. }
  446. # Write out the shlibs substvars
  447. my %depseen;
  448. sub filter_deps {
  449. my ($dep, $field) = @_;
  450. # Skip dependencies on excluded packages
  451. foreach my $exc (@exclude) {
  452. return 0 if $dep =~ /^\s*\Q$exc\E\b/;
  453. }
  454. # Don't include dependencies if they are already
  455. # mentionned in a higher priority field
  456. if (not exists($depseen{$dep})) {
  457. $depseen{$dep} = $dependencies{$field}{$dep};
  458. return 1;
  459. } else {
  460. # Since dependencies can be versionned, we have to
  461. # verify if the dependency is stronger than the
  462. # previously seen one
  463. my $stronger;
  464. if ($depseen{$dep} eq $dependencies{$field}{$dep}) {
  465. # If both versions are the same (possibly unversionned)
  466. $stronger = 0;
  467. } elsif ($dependencies{$field}{$dep} eq '') {
  468. $stronger = 0; # If the dep is unversionned
  469. } elsif ($depseen{$dep} eq '') {
  470. $stronger = 1; # If the dep seen is unversionned
  471. } elsif (version_compare_relation($depseen{$dep}, REL_GT,
  472. $dependencies{$field}{$dep})) {
  473. # The version of the dep seen is stronger...
  474. $stronger = 0;
  475. } else {
  476. $stronger = 1;
  477. }
  478. $depseen{$dep} = $dependencies{$field}{$dep} if $stronger;
  479. return $stronger;
  480. }
  481. }
  482. foreach my $field (reverse @depfields) {
  483. my $dep = '';
  484. if (exists $dependencies{$field} and scalar keys %{$dependencies{$field}}) {
  485. $dep = join ', ',
  486. map {
  487. # Translate dependency templates into real dependencies
  488. if ($dependencies{$field}{$_}) {
  489. s/#MINVER#/(>= $dependencies{$field}{$_})/g;
  490. } else {
  491. s/#MINVER#//g;
  492. }
  493. s/\s+/ /g;
  494. $_;
  495. } grep { filter_deps($_, $field) }
  496. keys %{$dependencies{$field}};
  497. }
  498. if ($dep) {
  499. my $obj = deps_parse($dep);
  500. error(_g('invalid dependency got generated: %s'), $dep) unless defined $obj;
  501. $obj->sort();
  502. print $fh "$varnameprefix:$field=$obj\n";
  503. }
  504. }
  505. # Replace old file by new one
  506. if (!$stdout) {
  507. close($fh) || syserr(_g('cannot close %s'), "$varlistfile.new");
  508. rename("$varlistfile.new",$varlistfile) ||
  509. syserr(_g("install new varlist file \`%s'"), $varlistfile);
  510. }
  511. ##
  512. ## Functions
  513. ##
  514. sub version {
  515. printf _g("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  516. printf _g('
  517. This is free software; see the GNU General Public License version 2 or
  518. later for copying conditions. There is NO warranty.
  519. ');
  520. }
  521. sub usage {
  522. printf _g(
  523. 'Usage: %s [<option>...] <executable>|-e<executable> [<option>...]')
  524. . "\n\n" . _g(
  525. "Positional options (order is significant):
  526. <executable> include dependencies for <executable>,
  527. -e<executable> (use -e if <executable> starts with '-')
  528. -d<dependency-field> next executable(s) set shlibs:<dependency-field>.")
  529. . "\n\n" . _g(
  530. "Options:
  531. -l<library-dir> add directory to private shared library search list.
  532. -p<varname-prefix> set <varname-prefix>:* instead of shlibs:*.
  533. -O print variable settings to stdout.
  534. -L<local-shlibs-file> shlibs override file, not debian/shlibs.local.
  535. -T<substvars-file> update variables here, not debian/substvars.
  536. -t<type> set package type (default is deb).
  537. -x<package> exclude package from the generated dependencies.
  538. -S<package-build-dir> search needed libraries in the given
  539. package build directory first.
  540. -v enable verbose mode (can be used multiple times).
  541. --ignore-missing-info don't fail if dependency information can't be found.
  542. --warnings=<value> define set of active warnings (see manual page).
  543. --admindir=<directory> change the administrative directory.
  544. -?, --help show this help message.
  545. --version show the version.")
  546. . "\n\n" . _g(
  547. 'Dependency fields recognized are:
  548. %s
  549. '), $Dpkg::PROGNAME, join('/', @depfields);
  550. }
  551. sub get_min_version_from_deps {
  552. my ($dep, $pkg) = @_;
  553. if ($dep->isa('Dpkg::Deps::Simple')) {
  554. if (($dep->{package} eq $pkg) &&
  555. defined($dep->{relation}) &&
  556. (($dep->{relation} eq REL_GE) ||
  557. ($dep->{relation} eq REL_GT)))
  558. {
  559. return $dep->{version};
  560. }
  561. return;
  562. } else {
  563. my $res;
  564. foreach my $subdep ($dep->get_deps()) {
  565. my $minver = get_min_version_from_deps($subdep, $pkg);
  566. next if not defined $minver;
  567. if (defined $res) {
  568. if (version_compare_relation($minver, REL_GT, $res)) {
  569. $res = $minver;
  570. }
  571. } else {
  572. $res = $minver;
  573. }
  574. }
  575. return $res;
  576. }
  577. }
  578. sub update_dependency_version {
  579. my ($dep, $minver, $existing_only) = @_;
  580. return if not defined($minver);
  581. $minver = Dpkg::Version->new($minver);
  582. foreach my $subdep (split /\s*,\s*/, $dep) {
  583. if (exists $dependencies{$cur_field}{$subdep} and
  584. defined($dependencies{$cur_field}{$subdep}))
  585. {
  586. if ($dependencies{$cur_field}{$subdep} eq '' or
  587. version_compare_relation($minver, REL_GT,
  588. $dependencies{$cur_field}{$subdep}))
  589. {
  590. $dependencies{$cur_field}{$subdep} = $minver;
  591. }
  592. } elsif (!$existing_only) {
  593. $dependencies{$cur_field}{$subdep} = $minver;
  594. }
  595. }
  596. }
  597. sub add_shlibs_dep {
  598. my ($soname, $pkg, $libfile) = @_;
  599. my @shlibs = ($shlibslocal, $shlibsoverride);
  600. if ($pkg eq '') {
  601. # If the file is not packaged, try to find out the shlibs file in
  602. # the package being built where the lib has been found
  603. my $pkg_root = guess_pkg_root_dir($libfile);
  604. if (defined $pkg_root) {
  605. push @shlibs, "$pkg_root/DEBIAN/shlibs";
  606. }
  607. # Fallback to other shlibs files but it shouldn't be necessary
  608. push @shlibs, @pkg_shlibs;
  609. } else {
  610. my $control_file = get_control_path($pkg, 'shlibs');
  611. push @shlibs, $control_file if defined $control_file;
  612. }
  613. push @shlibs, $shlibsdefault;
  614. print " Looking up shlibs dependency of $soname provided by '$pkg'\n" if $debug;
  615. foreach my $file (@shlibs) {
  616. next if not -e $file;
  617. my $dep = extract_from_shlibs($soname, $file);
  618. if (defined($dep)) {
  619. print " Found $dep in $file\n" if $debug;
  620. foreach (split(/,\s*/, $dep)) {
  621. # Note: the value is empty for shlibs based dependency
  622. # symbol based dependency will put a valid version as value
  623. $dependencies{$cur_field}{$_} = Dpkg::Version->new('');
  624. }
  625. return 1;
  626. }
  627. }
  628. print " Found nothing\n" if $debug;
  629. return 0;
  630. }
  631. sub split_soname {
  632. my $soname = shift;
  633. if ($soname =~ /^(.*)\.so\.(.*)$/) {
  634. return wantarray ? ($1, $2) : 1;
  635. } elsif ($soname =~ /^(.*)-(\d.*)\.so$/) {
  636. return wantarray ? ($1, $2) : 1;
  637. } else {
  638. return wantarray ? () : 0;
  639. }
  640. }
  641. sub extract_from_shlibs {
  642. my ($soname, $shlibfile) = @_;
  643. # Split soname in name/version
  644. my ($libname, $libversion) = split_soname($soname);
  645. unless (defined $libname) {
  646. warning(_g("Can't extract name and version from library name \`%s'"),
  647. $soname);
  648. return;
  649. }
  650. # Open shlibs file
  651. $shlibfile = "./$shlibfile" if $shlibfile =~ m/^\s/;
  652. open(my $shlibs_fh, '<', $shlibfile) ||
  653. syserr(_g("unable to open shared libs info file \`%s'"), $shlibfile);
  654. my $dep;
  655. while (<$shlibs_fh>) {
  656. s/\s*\n$//;
  657. next if m/^\#/;
  658. if (!m/^\s*(?:(\S+):\s+)?(\S+)\s+(\S+)(?:\s+(\S.*\S))?\s*$/) {
  659. warning(_g("shared libs info file \`%s' line %d: bad line \`%s'"),
  660. $shlibfile, $., $_);
  661. next;
  662. }
  663. my $depread = defined($4) ? $4 : '';
  664. if (($libname eq $2) && ($libversion eq $3)) {
  665. # Define dep and end here if the package type explicitly
  666. # matches. Otherwise if the packagetype is not specified, use
  667. # the dep only as a default that can be overriden by a later
  668. # line
  669. if (defined($1)) {
  670. if ($1 eq $packagetype) {
  671. $dep = $depread;
  672. last;
  673. }
  674. } else {
  675. $dep //= $depread;
  676. }
  677. }
  678. }
  679. close($shlibs_fh);
  680. return $dep;
  681. }
  682. sub find_symbols_file {
  683. my ($pkg, $soname, $libfile) = @_;
  684. my @files;
  685. if ($pkg eq '') {
  686. # If the file is not packaged, try to find out the symbols file in
  687. # the package being built where the lib has been found
  688. my $pkg_root = guess_pkg_root_dir($libfile);
  689. if (defined $pkg_root) {
  690. push @files, "$pkg_root/DEBIAN/symbols";
  691. }
  692. # Fallback to other symbols files but it shouldn't be necessary
  693. push @files, @pkg_symbols;
  694. } else {
  695. push @files, "$Dpkg::CONFDIR/symbols/$pkg.symbols.$host_arch",
  696. "$Dpkg::CONFDIR/symbols/$pkg.symbols";
  697. my $control_file = get_control_path($pkg, 'symbols');
  698. push @files, $control_file if defined $control_file;
  699. }
  700. foreach my $file (@files) {
  701. if (-e $file and symfile_has_soname($file, $soname)) {
  702. return $file;
  703. }
  704. }
  705. return;
  706. }
  707. sub symfile_has_soname {
  708. my ($file, $soname) = @_;
  709. if (exists $symfile_has_soname_cache{$file}{$soname}) {
  710. return $symfile_has_soname_cache{$file}{$soname};
  711. }
  712. open(my $symfile_fh, '<', $file) ||
  713. syserr(_g('cannot open file %s'), $file);
  714. my $result = 0;
  715. while (<$symfile_fh>) {
  716. if (/^\Q$soname\E /) {
  717. $result = 1;
  718. last;
  719. }
  720. }
  721. close($symfile_fh);
  722. $symfile_has_soname_cache{$file}{$soname} = $result;
  723. return $result;
  724. }
  725. # find_library ($soname, \@rpath, $format)
  726. sub my_find_library {
  727. my ($lib, $rpath, $format, $execfile) = @_;
  728. my $file;
  729. # Create real RPATH in case $ORIGIN is used
  730. # Note: ld.so also supports $PLATFORM and $LIB but they are
  731. # used in real case (yet)
  732. my $libdir = relative_to_pkg_root($execfile);
  733. my $origin;
  734. if (defined $libdir) {
  735. $origin = "/$libdir";
  736. $origin =~ s{/+[^/]*$}{};
  737. }
  738. my @RPATH = ();
  739. foreach my $path (@{$rpath}) {
  740. if ($path =~ /\$ORIGIN|\$\{ORIGIN\}/) {
  741. if (defined $origin) {
  742. $path =~ s/\$ORIGIN/$origin/g;
  743. $path =~ s/\$\{ORIGIN\}/$origin/g;
  744. } else {
  745. warning(_g('$ORIGIN is used in RPATH of %s and the corresponding ' .
  746. 'directory could not be identified due to lack of DEBIAN ' .
  747. "sub-directory in the root of package's build tree"), $execfile);
  748. }
  749. }
  750. push @RPATH, $path;
  751. }
  752. # Look into the packages we're currently building in the following
  753. # order:
  754. # - package build tree of the binary which is analyzed
  755. # - package build tree given on the command line (option -S)
  756. # - other package build trees that contain either a shlibs or a
  757. # symbols file
  758. my @builddirs;
  759. my $pkg_root = guess_pkg_root_dir($execfile);
  760. push @builddirs, $pkg_root if defined $pkg_root;
  761. push @builddirs, @pkg_dir_to_search;
  762. push @builddirs, @pkg_root_dirs;
  763. my %dir_checked;
  764. foreach my $builddir (@builddirs) {
  765. next if defined($dir_checked{$builddir});
  766. $file = find_library($lib, \@RPATH, $format, $builddir);
  767. return $file if defined($file);
  768. $dir_checked{$builddir} = 1;
  769. }
  770. # Fallback in the root directory if we have not found what we were
  771. # looking for in the packages
  772. $file = find_library($lib, \@RPATH, $format, '');
  773. return $file if defined($file);
  774. return;
  775. }
  776. my %cached_pkgmatch = ();
  777. sub find_packages {
  778. my @files;
  779. my $pkgmatch = {};
  780. foreach (@_) {
  781. if (exists $cached_pkgmatch{$_}) {
  782. $pkgmatch->{$_} = $cached_pkgmatch{$_};
  783. } else {
  784. push @files, $_;
  785. $cached_pkgmatch{$_} = ['']; # placeholder to cache misses too.
  786. $pkgmatch->{$_} = ['']; # might be replaced later on
  787. }
  788. }
  789. return $pkgmatch unless scalar(@files);
  790. my $pid = open(my $dpkg_fh, '-|');
  791. syserr(_g('cannot fork for %s'), 'dpkg --search') unless defined($pid);
  792. if (!$pid) {
  793. # Child process running dpkg --search and discarding errors
  794. close STDERR;
  795. open STDERR, '>', '/dev/null';
  796. $ENV{LC_ALL} = 'C';
  797. exec('dpkg', '--search', '--', @files)
  798. || syserr(_g('unable to execute %s'), 'dpkg');
  799. }
  800. while (defined($_ = <$dpkg_fh>)) {
  801. chomp($_);
  802. if (m/^local diversion |^diversion by/) {
  803. warning(_g('diversions involved - output may be incorrect'));
  804. print(STDERR " $_\n")
  805. || syserr(_g('write diversion info to stderr'));
  806. } elsif (m/^([-a-z0-9+.:, ]+): (\/.*)$/) {
  807. $cached_pkgmatch{$2} = $pkgmatch->{$2} = [ split(/, /, $1) ];
  808. } else {
  809. warning(_g("unknown output from dpkg --search: '%s'"), $_);
  810. }
  811. }
  812. close($dpkg_fh);
  813. return $pkgmatch;
  814. }