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. my %shlibs;
  136. # Statictics on soname seen in the whole run (with multiple analysis of
  137. # binaries)
  138. my %global_soname_notfound;
  139. my %global_soname_used;
  140. my %global_soname_needed;
  141. # Symfile and objdump caches
  142. my %symfile_cache;
  143. my %objdump_cache;
  144. my %symfile_has_soname_cache;
  145. # Used to count errors due to missing libraries
  146. my $error_count = 0;
  147. my $cur_field;
  148. foreach my $file (keys %exec) {
  149. $cur_field = $exec{$file};
  150. print ">> Scanning $file (for $cur_field field)\n" if $debug;
  151. my $obj = Dpkg::Shlibs::Objdump::Object->new($file);
  152. my @sonames = $obj->get_needed_libraries;
  153. # Load symbols files for all needed libraries (identified by SONAME)
  154. my %libfiles;
  155. my %altlibfiles;
  156. my %soname_notfound;
  157. my %alt_soname;
  158. foreach my $soname (@sonames) {
  159. my $lib = my_find_library($soname, $obj->{RPATH}, $obj->{format}, $file);
  160. unless (defined $lib) {
  161. $soname_notfound{$soname} = 1;
  162. $global_soname_notfound{$soname} = 1;
  163. my $msg = _g("couldn't find library %s needed by %s (ELF " .
  164. "format: '%s'; RPATH: '%s')");
  165. if (scalar(split_soname($soname))) {
  166. errormsg($msg, $soname, $file, $obj->{format}, join(":", @{$obj->{RPATH}}));
  167. $error_count++;
  168. } else {
  169. warning($msg, $soname, $file, $obj->{format}, join(":", @{$obj->{RPATH}}));
  170. }
  171. next;
  172. }
  173. $libfiles{$lib} = $soname;
  174. my $reallib = realpath($lib);
  175. if ($reallib ne $lib) {
  176. $altlibfiles{$reallib} = $soname;
  177. }
  178. print "Library $soname found in $lib\n" if $debug;
  179. }
  180. my $file2pkg = find_packages(keys %libfiles, keys %altlibfiles);
  181. my $symfile = Dpkg::Shlibs::SymbolFile->new();
  182. my $dumplibs_wo_symfile = Dpkg::Shlibs::Objdump->new();
  183. my @soname_wo_symfile;
  184. foreach my $lib (keys %libfiles) {
  185. my $soname = $libfiles{$lib};
  186. if (not scalar(grep { $_ ne '' } @{$file2pkg->{$lib}})) {
  187. # The path of the library as calculated is not the
  188. # official path of a packaged file, try to fallback on
  189. # on the realpath() first, maybe this one is part of a package
  190. my $reallib = realpath($lib);
  191. if (exists $file2pkg->{$reallib}) {
  192. $file2pkg->{$lib} = $file2pkg->{$reallib};
  193. }
  194. }
  195. if (not scalar(grep { $_ ne '' } @{$file2pkg->{$lib}})) {
  196. # If the library is really not available in an installed package,
  197. # it's because it's in the process of being built
  198. # Empty package name will lead to consideration of symbols
  199. # file from the package being built only
  200. $file2pkg->{$lib} = [""];
  201. print "No associated package found for $lib\n" if $debug;
  202. }
  203. # Load symbols/shlibs files from packages providing libraries
  204. foreach my $pkg (@{$file2pkg->{$lib}}) {
  205. my $symfile_path;
  206. my $haslocaldep = 0;
  207. if (-e $shlibslocal and
  208. defined(extract_from_shlibs($soname, $shlibslocal)))
  209. {
  210. $haslocaldep = 1;
  211. }
  212. if ($packagetype eq "deb" and not $haslocaldep) {
  213. # Use fine-grained dependencies only on real deb
  214. # and only if the dependency is not provided by shlibs.local
  215. $symfile_path = find_symbols_file($pkg, $soname, $lib);
  216. }
  217. if (defined($symfile_path)) {
  218. # Load symbol information
  219. print "Using symbols file $symfile_path for $soname\n" if $debug;
  220. unless (exists $symfile_cache{$symfile_path}) {
  221. $symfile_cache{$symfile_path} =
  222. Dpkg::Shlibs::SymbolFile->new(file => $symfile_path);
  223. }
  224. $symfile->merge_object_from_symfile($symfile_cache{$symfile_path}, $soname);
  225. }
  226. if (defined($symfile_path) && $symfile->has_object($soname)) {
  227. # Initialize dependencies with the smallest minimal version
  228. # of all symbols (unversioned dependency is not ok as the
  229. # library might not have always been available in the
  230. # package and we really need it)
  231. my $dep = $symfile->get_dependency($soname);
  232. my $minver = $symfile->get_smallest_version($soname) || '';
  233. foreach my $subdep (split /\s*,\s*/, $dep) {
  234. if (not exists $dependencies{$cur_field}{$subdep}) {
  235. $dependencies{$cur_field}{$subdep} = Dpkg::Version->new($minver);
  236. print " Initialize dependency ($subdep) with minimal " .
  237. "version ($minver)\n" if $debug > 1;
  238. }
  239. }
  240. } else {
  241. # No symbol file found, fall back to standard shlibs
  242. print "Using shlibs+objdump for $soname (file $lib)\n" if $debug;
  243. unless (exists $objdump_cache{$lib}) {
  244. $objdump_cache{$lib} = Dpkg::Shlibs::Objdump::Object->new($lib);
  245. }
  246. my $libobj = $objdump_cache{$lib};
  247. my $id = $dumplibs_wo_symfile->add_object($libobj);
  248. if (($id ne $soname) and ($id ne $lib)) {
  249. warning(_g("%s has an unexpected SONAME (%s)"), $lib, $id);
  250. $alt_soname{$id} = $soname;
  251. }
  252. push @soname_wo_symfile, $soname;
  253. # Only try to generate a dependency for libraries with a SONAME
  254. if ($libobj->is_public_library() and not
  255. add_shlibs_dep($soname, $pkg, $lib)) {
  256. # This failure is fairly new, try to be kind by
  257. # ignoring as many cases that can be safely ignored
  258. my $ignore = 0;
  259. # 1/ when the lib and the binary are in the same
  260. # package
  261. my $root_file = guess_pkg_root_dir($file);
  262. my $root_lib = guess_pkg_root_dir($lib);
  263. $ignore++ if defined $root_file and defined $root_lib
  264. and check_files_are_the_same($root_file, $root_lib);
  265. # 2/ when the lib is not versioned and can't be
  266. # handled by shlibs
  267. $ignore++ unless scalar(split_soname($soname));
  268. # 3/ when we have been asked to do so
  269. $ignore++ if $ignore_missing_info;
  270. error(_g("no dependency information found for %s " .
  271. "(used by %s)"), $lib, $file)
  272. unless $ignore;
  273. }
  274. }
  275. }
  276. }
  277. # Scan all undefined symbols of the binary and resolve to a
  278. # dependency
  279. my %soname_used;
  280. foreach (@sonames) {
  281. # Initialize statistics
  282. $soname_used{$_} = 0;
  283. $global_soname_used{$_} = 0 unless exists $global_soname_used{$_};
  284. if (exists $global_soname_needed{$_}) {
  285. push @{$global_soname_needed{$_}}, $file;
  286. } else {
  287. $global_soname_needed{$_} = [ $file ];
  288. }
  289. }
  290. my $nb_warnings = 0;
  291. my $nb_skipped_warnings = 0;
  292. # Disable warnings about missing symbols when we have not been able to
  293. # find all libs
  294. my $disable_warnings = scalar(keys(%soname_notfound));
  295. my $in_public_dir = 1;
  296. if (my $relname = relative_to_pkg_root($file)) {
  297. my $parent_dir = "/" . dirname($relname);
  298. $in_public_dir = (grep { $parent_dir eq $_ } @librarypaths) ? 1 : 0;
  299. } else {
  300. warning(_g("binaries to analyze should already be " .
  301. "installed in their package's directory"));
  302. }
  303. print "Analyzing all undefined symbols\n" if $debug > 1;
  304. foreach my $sym ($obj->get_undefined_dynamic_symbols()) {
  305. my $name = $sym->{name};
  306. if ($sym->{version}) {
  307. $name .= "\@$sym->{version}";
  308. } else {
  309. $name .= "\@Base";
  310. }
  311. print " Looking up symbol $name\n" if $debug > 1;
  312. my %symdep = $symfile->lookup_symbol($name, \@sonames);
  313. if (keys %symdep) {
  314. my $depends = $symfile->get_dependency($symdep{soname},
  315. $symdep{symbol}{dep_id});
  316. print " Found in symbols file of $symdep{soname} (minver: " .
  317. "$symdep{symbol}{minver}, dep: $depends)\n" if $debug > 1;
  318. $soname_used{$symdep{soname}}++;
  319. $global_soname_used{$symdep{soname}}++;
  320. if (exists $alt_soname{$symdep{soname}}) {
  321. # Also count usage on alternate soname
  322. $soname_used{$alt_soname{$symdep{soname}}}++;
  323. $global_soname_used{$alt_soname{$symdep{soname}}}++;
  324. }
  325. update_dependency_version($depends, $symdep{symbol}{minver});
  326. } else {
  327. my $syminfo = $dumplibs_wo_symfile->locate_symbol($name);
  328. if (not defined($syminfo)) {
  329. print " Not found\n" if $debug > 1;
  330. next unless ($warnings & WARN_SYM_NOT_FOUND);
  331. next if $disable_warnings;
  332. # Complain about missing symbols only for executables
  333. # and public libraries
  334. if ($obj->is_executable() or $obj->is_public_library()) {
  335. my $print_name = $name;
  336. # Drop the default suffix for readability
  337. $print_name =~ s/\@Base$//;
  338. unless ($sym->{weak}) {
  339. if ($debug or ($in_public_dir and $nb_warnings < 10)
  340. or (!$in_public_dir and $nb_warnings < 1))
  341. {
  342. if ($in_public_dir) {
  343. warning(_g("symbol %s used by %s found in none of the " .
  344. "libraries"), $print_name, $file);
  345. } else {
  346. warning(_g("%s contains an unresolvable reference to " .
  347. "symbol %s: it's probably a plugin"),
  348. $file, $print_name);
  349. }
  350. $nb_warnings++;
  351. } else {
  352. $nb_skipped_warnings++;
  353. }
  354. }
  355. }
  356. } else {
  357. print " Found in $syminfo->{soname} ($syminfo->{objid})\n" if $debug > 1;
  358. if (exists $alt_soname{$syminfo->{soname}}) {
  359. # Also count usage on alternate soname
  360. $soname_used{$alt_soname{$syminfo->{soname}}}++;
  361. $global_soname_used{$alt_soname{$syminfo->{soname}}}++;
  362. }
  363. $soname_used{$syminfo->{soname}}++;
  364. $global_soname_used{$syminfo->{soname}}++;
  365. }
  366. }
  367. }
  368. warning(P_("%d similar warning has been skipped (use -v to see it)",
  369. "%d other similar warnings have been skipped (use -v to see " .
  370. "them all)", $nb_skipped_warnings), $nb_skipped_warnings)
  371. if $nb_skipped_warnings;
  372. foreach my $soname (@sonames) {
  373. # Adjust minimal version of dependencies with information
  374. # extracted from build-dependencies
  375. my $dev_pkg = $symfile->get_field($soname, 'Build-Depends-Package');
  376. if (defined $dev_pkg) {
  377. print "Updating dependencies of $soname with build-dependencies\n" if $debug;
  378. my $minver = get_min_version_from_deps($build_deps, $dev_pkg);
  379. if (defined $minver) {
  380. foreach my $dep ($symfile->get_dependencies($soname)) {
  381. update_dependency_version($dep, $minver, 1);
  382. print " Minimal version of $dep updated with $minver\n" if $debug;
  383. }
  384. } else {
  385. print " No minimal version found in $dev_pkg build-dependency\n" if $debug;
  386. }
  387. }
  388. # Warn about un-NEEDED libraries
  389. unless ($soname_notfound{$soname} or $soname_used{$soname}) {
  390. # Ignore warning for libm.so.6 if also linked against libstdc++
  391. next if ($soname =~ /^libm\.so\.\d+$/ and
  392. scalar grep(/^libstdc\+\+\.so\.\d+/, @sonames));
  393. next unless ($warnings & WARN_NOT_NEEDED);
  394. warning(_g("%s should not be linked against %s (it uses none of " .
  395. "the library's symbols)"), $file, $soname);
  396. }
  397. }
  398. }
  399. # Warn of unneeded libraries at the "package" level (i.e. over all
  400. # binaries that we have inspected)
  401. foreach my $soname (keys %global_soname_needed) {
  402. unless ($global_soname_notfound{$soname} or $global_soname_used{$soname}) {
  403. next if ($soname =~ /^libm\.so\.\d+$/ and scalar(
  404. grep(/^libstdc\+\+\.so\.\d+/, keys %global_soname_needed)));
  405. next unless ($warnings & WARN_DEP_AVOIDABLE);
  406. warning(P_("package could avoid a useless dependency if %s was not " .
  407. "linked against %s (it uses none of the library's symbols)",
  408. "package could avoid a useless dependency if %s were not " .
  409. "linked against %s (they use none of the library's symbols)",
  410. scalar @{$global_soname_needed{$soname}}),
  411. join(" ", @{$global_soname_needed{$soname}}), $soname);
  412. }
  413. }
  414. # Quit now if any missing libraries
  415. if ($error_count >= 1) {
  416. my $note = _g("Note: libraries are not searched in other binary packages " .
  417. "that do not have any shlibs or symbols file.\nTo help dpkg-shlibdeps " .
  418. "find private libraries, you might need to set LD_LIBRARY_PATH.");
  419. error(P_("cannot continue due to the error above",
  420. "cannot continue due to the errors listed above",
  421. $error_count) . "\n" . $note);
  422. }
  423. # Open substvars file
  424. my $fh;
  425. if ($stdout) {
  426. $fh = \*STDOUT;
  427. } else {
  428. open(my $new_fh, ">", "$varlistfile.new") ||
  429. syserr(_g("open new substvars file \`%s'"), "$varlistfile.new");
  430. if (-e $varlistfile) {
  431. open(my $old_fh, "<", $varlistfile) ||
  432. syserr(_g("open old varlist file \`%s' for reading"), $varlistfile);
  433. foreach my $entry (grep { not m/^\Q$varnameprefix\E:/ } (<$old_fh>)) {
  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. }