dpkg-shlibdeps.pl 29 KB

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