dpkg-shlibdeps.pl 28 KB

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