dpkg-shlibdeps.pl 28 KB

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