dpkg-shlibdeps.pl 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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 (keys %symdep) {
  305. my $depends = $symfile->get_dependency($symdep{soname},
  306. $symdep{symbol}{dep_id});
  307. print " Found in symbols file of $symdep{soname} (minver: " .
  308. "$symdep{symbol}{minver}, dep: $depends)\n" if $debug > 1;
  309. $soname_used{$symdep{soname}}++;
  310. $global_soname_used{$symdep{soname}}++;
  311. if (exists $alt_soname{$symdep{soname}}) {
  312. # Also count usage on alternate soname
  313. $soname_used{$alt_soname{$symdep{soname}}}++;
  314. $global_soname_used{$alt_soname{$symdep{soname}}}++;
  315. }
  316. update_dependency_version($depends, $symdep{symbol}{minver});
  317. } else {
  318. my $syminfo = $dumplibs_wo_symfile->locate_symbol($name);
  319. if (not defined($syminfo)) {
  320. print " Not found\n" if $debug > 1;
  321. next unless ($warnings & WARN_SYM_NOT_FOUND);
  322. next if $disable_warnings;
  323. # Complain about missing symbols only for executables
  324. # and public libraries
  325. if ($obj->is_executable() or $obj->is_public_library()) {
  326. my $print_name = $name;
  327. # Drop the default suffix for readability
  328. $print_name =~ s/\@Base$//;
  329. unless ($sym->{weak}) {
  330. if ($debug or ($in_public_dir and $nb_warnings < 10)
  331. or (!$in_public_dir and $nb_warnings < 1))
  332. {
  333. if ($in_public_dir) {
  334. warning(_g("symbol %s used by %s found in none of the " .
  335. "libraries."), $print_name, $file);
  336. } else {
  337. warning(_g("%s contains an unresolvable reference to " .
  338. "symbol %s: it's probably a plugin."),
  339. $file, $print_name);
  340. }
  341. $nb_warnings++;
  342. } else {
  343. $nb_skipped_warnings++;
  344. }
  345. }
  346. }
  347. } else {
  348. print " Found in $syminfo->{soname} ($syminfo->{objid})\n" if $debug > 1;
  349. if (exists $alt_soname{$syminfo->{soname}}) {
  350. # Also count usage on alternate soname
  351. $soname_used{$alt_soname{$syminfo->{soname}}}++;
  352. $global_soname_used{$alt_soname{$syminfo->{soname}}}++;
  353. }
  354. $soname_used{$syminfo->{soname}}++;
  355. $global_soname_used{$syminfo->{soname}}++;
  356. }
  357. }
  358. }
  359. warning(P_("%d similar warning has been skipped (use -v to see it).",
  360. "%d other similar warnings have been skipped (use -v to see " .
  361. "them all).", $nb_skipped_warnings), $nb_skipped_warnings)
  362. if $nb_skipped_warnings;
  363. foreach my $soname (@sonames) {
  364. # Adjust minimal version of dependencies with information
  365. # extracted from build-dependencies
  366. my $dev_pkg = $symfile->get_field($soname, 'Build-Depends-Package');
  367. if (defined $dev_pkg) {
  368. print "Updating dependencies of $soname with build-dependencies\n" if $debug;
  369. my $minver = get_min_version_from_deps($build_deps, $dev_pkg);
  370. if (defined $minver) {
  371. foreach my $dep ($symfile->get_dependencies($soname)) {
  372. update_dependency_version($dep, $minver, 1);
  373. print " Minimal version of $dep updated with $minver\n" if $debug;
  374. }
  375. } else {
  376. print " No minimal version found in $dev_pkg build-dependency\n" if $debug;
  377. }
  378. }
  379. # Warn about un-NEEDED libraries
  380. unless ($soname_notfound{$soname} or $soname_used{$soname}) {
  381. # Ignore warning for libm.so.6 if also linked against libstdc++
  382. next if ($soname =~ /^libm\.so\.\d+$/ and
  383. scalar grep(/^libstdc\+\+\.so\.\d+/, @sonames));
  384. next unless ($warnings & WARN_NOT_NEEDED);
  385. warning(_g("%s shouldn't be linked with %s (it uses none of its " .
  386. "symbols)."), $file, $soname);
  387. }
  388. }
  389. }
  390. # Warn of unneeded libraries at the "package" level (i.e. over all
  391. # binaries that we have inspected)
  392. foreach my $soname (keys %global_soname_needed) {
  393. unless ($global_soname_notfound{$soname} or $global_soname_used{$soname}) {
  394. next if ($soname =~ /^libm\.so\.\d+$/ and scalar(
  395. grep(/^libstdc\+\+\.so\.\d+/, keys %global_soname_needed)));
  396. next unless ($warnings & WARN_DEP_AVOIDABLE);
  397. warning(_g("dependency on %s could be avoided if \"%s\" were not " .
  398. "uselessly linked against it (they use none of its " .
  399. "symbols)."), $soname,
  400. join(" ", @{$global_soname_needed{$soname}}));
  401. }
  402. }
  403. # Open substvars file
  404. my $fh;
  405. if ($stdout) {
  406. $fh = \*STDOUT;
  407. } else {
  408. open(NEW, ">", "$varlistfile.new") ||
  409. syserr(_g("open new substvars file \`%s'"), "$varlistfile.new");
  410. if (-e $varlistfile) {
  411. open(OLD, "<", $varlistfile) ||
  412. syserr(_g("open old varlist file \`%s' for reading"), $varlistfile);
  413. foreach my $entry (grep { not m/^\Q$varnameprefix\E:/ } (<OLD>)) {
  414. print(NEW $entry) ||
  415. syserr(_g("copy old entry to new varlist file \`%s'"),
  416. "$varlistfile.new");
  417. }
  418. close(OLD);
  419. }
  420. $fh = \*NEW;
  421. }
  422. # Write out the shlibs substvars
  423. my %depseen;
  424. sub filter_deps {
  425. my ($dep, $field) = @_;
  426. # Skip dependencies on excluded packages
  427. foreach my $exc (@exclude) {
  428. return 0 if $dep =~ /^\s*\Q$exc\E\b/;
  429. }
  430. # Don't include dependencies if they are already
  431. # mentionned in a higher priority field
  432. if (not exists($depseen{$dep})) {
  433. $depseen{$dep} = $dependencies{$field}{$dep};
  434. return 1;
  435. } else {
  436. # Since dependencies can be versionned, we have to
  437. # verify if the dependency is stronger than the
  438. # previously seen one
  439. my $stronger;
  440. if ($depseen{$dep} eq $dependencies{$field}{$dep}) {
  441. # If both versions are the same (possibly unversionned)
  442. $stronger = 0;
  443. } elsif ($dependencies{$field}{$dep} eq '') {
  444. $stronger = 0; # If the dep is unversionned
  445. } elsif ($depseen{$dep} eq '') {
  446. $stronger = 1; # If the dep seen is unversionned
  447. } elsif (version_compare_relation($depseen{$dep}, REL_GT,
  448. $dependencies{$field}{$dep})) {
  449. # The version of the dep seen is stronger...
  450. $stronger = 0;
  451. } else {
  452. $stronger = 1;
  453. }
  454. $depseen{$dep} = $dependencies{$field}{$dep} if $stronger;
  455. return $stronger;
  456. }
  457. }
  458. foreach my $field (reverse @depfields) {
  459. my $dep = "";
  460. if (exists $dependencies{$field} and scalar keys %{$dependencies{$field}}) {
  461. $dep = join ", ",
  462. map {
  463. # Translate dependency templates into real dependencies
  464. if ($dependencies{$field}{$_}) {
  465. s/#MINVER#/(>= $dependencies{$field}{$_})/g;
  466. } else {
  467. s/#MINVER#//g;
  468. }
  469. s/\s+/ /g;
  470. $_;
  471. } grep { filter_deps($_, $field) }
  472. keys %{$dependencies{$field}};
  473. }
  474. if ($dep) {
  475. my $obj = deps_parse($dep);
  476. error(_g("invalid dependency got generated: %s"), $dep) unless defined $obj;
  477. $obj->sort();
  478. print $fh "$varnameprefix:$field=$obj\n";
  479. }
  480. }
  481. # Replace old file by new one
  482. if (!$stdout) {
  483. close($fh);
  484. rename("$varlistfile.new",$varlistfile) ||
  485. syserr(_g("install new varlist file \`%s'"), $varlistfile);
  486. }
  487. ##
  488. ## Functions
  489. ##
  490. sub version {
  491. printf _g("Debian %s version %s.\n"), $progname, $version;
  492. printf _g("
  493. Copyright (C) 1996 Ian Jackson.
  494. Copyright (C) 2000 Wichert Akkerman.
  495. Copyright (C) 2006 Frank Lichtenheld.
  496. Copyright (C) 2007 Raphael Hertzog.
  497. ");
  498. printf _g("
  499. This is free software; see the GNU General Public License version 2 or
  500. later for copying conditions. There is NO warranty.
  501. ");
  502. }
  503. sub usage {
  504. printf _g(
  505. "Usage: %s [<option> ...] <executable>|-e<executable> [<option> ...]
  506. Positional options (order is significant):
  507. <executable> include dependencies for <executable>,
  508. -e<executable> (use -e if <executable> starts with \`-')
  509. -d<dependencyfield> next executable(s) set shlibs:<dependencyfield>.
  510. Options:
  511. -p<varnameprefix> set <varnameprefix>:* instead of shlibs:*.
  512. -O print variable settings to stdout.
  513. -L<localshlibsfile> shlibs override file, not debian/shlibs.local.
  514. -T<varlistfile> update variables here, not debian/substvars.
  515. -t<type> set package type (default is deb).
  516. -x<package> exclude package from the generated dependencies.
  517. -S<pkgbuilddir> search needed libraries in the given
  518. package build directory first.
  519. -v enable verbose mode (can be used multiple times).
  520. --ignore-missing-info don't fail if dependency information can't be found.
  521. --warnings=<value> define set of active warnings (see manual page).
  522. --admindir=<directory> change the administrative directory.
  523. -h, --help show this help message.
  524. --version show the version.
  525. Dependency fields recognised are:
  526. %s
  527. "), $progname, join("/",@depfields);
  528. }
  529. sub get_min_version_from_deps {
  530. my ($dep, $pkg) = @_;
  531. if ($dep->isa('Dpkg::Deps::Simple')) {
  532. if (($dep->{package} eq $pkg) &&
  533. defined($dep->{relation}) &&
  534. (($dep->{relation} eq REL_GE) ||
  535. ($dep->{relation} eq REL_GT)))
  536. {
  537. return $dep->{version};
  538. }
  539. return undef;
  540. } else {
  541. my $res;
  542. foreach my $subdep ($dep->get_deps()) {
  543. my $minver = get_min_version_from_deps($subdep, $pkg);
  544. next if not defined $minver;
  545. if (defined $res) {
  546. if (version_compare_relation($minver, REL_GT, $res)) {
  547. $res = $minver;
  548. }
  549. } else {
  550. $res = $minver;
  551. }
  552. }
  553. return $res;
  554. }
  555. }
  556. sub update_dependency_version {
  557. my ($dep, $minver, $existing_only) = @_;
  558. return if not defined($minver);
  559. $minver = Dpkg::Version->new($minver);
  560. foreach my $subdep (split /\s*,\s*/, $dep) {
  561. if (exists $dependencies{$cur_field}{$subdep} and
  562. defined($dependencies{$cur_field}{$subdep}))
  563. {
  564. if ($dependencies{$cur_field}{$subdep} eq '' or
  565. version_compare_relation($minver, REL_GT,
  566. $dependencies{$cur_field}{$subdep}))
  567. {
  568. $dependencies{$cur_field}{$subdep} = $minver;
  569. }
  570. } elsif (!$existing_only) {
  571. $dependencies{$cur_field}{$subdep} = $minver;
  572. }
  573. }
  574. }
  575. sub add_shlibs_dep {
  576. my ($soname, $pkg, $libfile) = @_;
  577. my @shlibs = ($shlibslocal, $shlibsoverride);
  578. if ($pkg eq "") {
  579. # If the file is not packaged, try to find out the shlibs file in
  580. # the package being built where the lib has been found
  581. my $pkg_root = guess_pkg_root_dir($libfile);
  582. if (defined $pkg_root) {
  583. push @shlibs, "$pkg_root/DEBIAN/shlibs";
  584. }
  585. # Fallback to other shlibs files but it shouldn't be necessary
  586. push @shlibs, @pkg_shlibs;
  587. } else {
  588. push @shlibs, "$admindir/info/$pkg.shlibs";
  589. }
  590. push @shlibs, $shlibsdefault;
  591. print " Looking up shlibs dependency of $soname provided by '$pkg'\n" if $debug;
  592. foreach my $file (@shlibs) {
  593. next if not -e $file;
  594. my $dep = extract_from_shlibs($soname, $file);
  595. if (defined($dep)) {
  596. print " Found $dep in $file\n" if $debug;
  597. foreach (split(/,\s*/, $dep)) {
  598. # Note: the value is empty for shlibs based dependency
  599. # symbol based dependency will put a valid version as value
  600. $dependencies{$cur_field}{$_} = Dpkg::Version->new('');
  601. }
  602. return 1;
  603. }
  604. }
  605. print " Found nothing\n" if $debug;
  606. return 0;
  607. }
  608. sub split_soname {
  609. my $soname = shift;
  610. if ($soname =~ /^(.*)\.so\.(.*)$/) {
  611. return wantarray ? ($1, $2) : 1;
  612. } elsif ($soname =~ /^(.*)-(\d.*)\.so$/) {
  613. return wantarray ? ($1, $2) : 1;
  614. } else {
  615. return wantarray ? () : 0;
  616. }
  617. }
  618. sub extract_from_shlibs {
  619. my ($soname, $shlibfile) = @_;
  620. # Split soname in name/version
  621. my ($libname, $libversion) = split_soname($soname);
  622. unless (defined $libname) {
  623. warning(_g("Can't extract name and version from library name \`%s'"),
  624. $soname);
  625. return;
  626. }
  627. # Open shlibs file
  628. $shlibfile = "./$shlibfile" if $shlibfile =~ m/^\s/;
  629. open(SHLIBS, "<", $shlibfile) ||
  630. syserr(_g("unable to open shared libs info file \`%s'"), $shlibfile);
  631. my $dep;
  632. while (<SHLIBS>) {
  633. s/\s*\n$//;
  634. next if m/^\#/;
  635. if (!m/^\s*(?:(\S+):\s+)?(\S+)\s+(\S+)(?:\s+(\S.*\S))?\s*$/) {
  636. warning(_g("shared libs info file \`%s' line %d: bad line \`%s'"),
  637. $shlibfile, $., $_);
  638. next;
  639. }
  640. my $depread = defined($4) ? $4 : '';
  641. if (($libname eq $2) && ($libversion eq $3)) {
  642. # Define dep and end here if the package type explicitly
  643. # matches. Otherwise if the packagetype is not specified, use
  644. # the dep only as a default that can be overriden by a later
  645. # line
  646. if (defined($1)) {
  647. if ($1 eq $packagetype) {
  648. $dep = $depread;
  649. last;
  650. }
  651. } else {
  652. $dep = $depread unless defined $dep;
  653. }
  654. }
  655. }
  656. close(SHLIBS);
  657. return $dep;
  658. }
  659. sub find_symbols_file {
  660. my ($pkg, $soname, $libfile) = @_;
  661. my @files;
  662. if ($pkg eq "") {
  663. # If the file is not packaged, try to find out the symbols file in
  664. # the package being built where the lib has been found
  665. my $pkg_root = guess_pkg_root_dir($libfile);
  666. if (defined $pkg_root) {
  667. push @files, "$pkg_root/DEBIAN/symbols";
  668. }
  669. # Fallback to other symbols files but it shouldn't be necessary
  670. push @files, @pkg_symbols;
  671. } else {
  672. push @files, "/etc/dpkg/symbols/$pkg.symbols.$host_arch",
  673. "/etc/dpkg/symbols/$pkg.symbols",
  674. "$admindir/info/$pkg.symbols";
  675. }
  676. foreach my $file (@files) {
  677. if (-e $file and symfile_has_soname($file, $soname)) {
  678. return $file;
  679. }
  680. }
  681. return undef;
  682. }
  683. sub symfile_has_soname {
  684. my ($file, $soname) = @_;
  685. if (exists $symfile_has_soname_cache{$file}{$soname}) {
  686. return $symfile_has_soname_cache{$file}{$soname};
  687. }
  688. open(SYM_FILE, "<", $file) ||
  689. syserr(_g("cannot open file %s"), $file);
  690. my $result = 0;
  691. while (<SYM_FILE>) {
  692. if (/^\Q$soname\E /) {
  693. $result = 1;
  694. last;
  695. }
  696. }
  697. close(SYM_FILE);
  698. $symfile_has_soname_cache{$file}{$soname} = $result;
  699. return $result;
  700. }
  701. # find_library ($soname, \@rpath, $format)
  702. sub my_find_library {
  703. my ($lib, $rpath, $format, $execfile) = @_;
  704. my $file;
  705. # Create real RPATH in case $ORIGIN is used
  706. # Note: ld.so also supports $PLATFORM and $LIB but they are
  707. # used in real case (yet)
  708. my $libdir = relative_to_pkg_root($execfile);
  709. my $origin;
  710. if (defined $libdir) {
  711. $origin = "/$libdir";
  712. $origin =~ s{/+[^/]*$}{};
  713. }
  714. my @RPATH = ();
  715. foreach my $path (@{$rpath}) {
  716. if ($path =~ /\$ORIGIN|\$\{ORIGIN\}/) {
  717. if (defined $origin) {
  718. $path =~ s/\$ORIGIN/$origin/g;
  719. $path =~ s/\$\{ORIGIN\}/$origin/g;
  720. } else {
  721. warning(_g("\$ORIGIN is used in RPATH of %s and the corresponding " .
  722. "directory could not be identified due to lack of DEBIAN " .
  723. "sub-directory in the root of package's build tree"), $execfile);
  724. }
  725. }
  726. push @RPATH, $path;
  727. }
  728. # Look into the packages we're currently building in the following
  729. # order:
  730. # - package build tree of the binary which is analyzed
  731. # - package build tree given on the command line (option -S)
  732. # - other package build trees that contain either a shlibs or a
  733. # symbols file
  734. my @builddirs;
  735. my $pkg_root = guess_pkg_root_dir($execfile);
  736. push @builddirs, $pkg_root if defined $pkg_root;
  737. push @builddirs, @pkg_dir_to_search;
  738. push @builddirs, @pkg_root_dirs;
  739. my %dir_checked;
  740. foreach my $builddir (@builddirs) {
  741. next if defined($dir_checked{$builddir});
  742. $file = find_library($lib, \@RPATH, $format, $builddir);
  743. return $file if defined($file);
  744. $dir_checked{$builddir} = 1;
  745. }
  746. # Fallback in the root directory if we have not found what we were
  747. # looking for in the packages
  748. $file = find_library($lib, \@RPATH, $format, "");
  749. return $file if defined($file);
  750. return undef;
  751. }
  752. my %cached_pkgmatch = ();
  753. sub find_packages {
  754. my @files;
  755. my $pkgmatch = {};
  756. foreach (@_) {
  757. if (exists $cached_pkgmatch{$_}) {
  758. $pkgmatch->{$_} = $cached_pkgmatch{$_};
  759. } else {
  760. push @files, $_;
  761. $cached_pkgmatch{$_} = [""]; # placeholder to cache misses too.
  762. $pkgmatch->{$_} = [""]; # might be replaced later on
  763. }
  764. }
  765. return $pkgmatch unless scalar(@files);
  766. my $pid = open(DPKG, "-|");
  767. syserr(_g("cannot fork for %s"), "dpkg --search") unless defined($pid);
  768. if (!$pid) {
  769. # Child process running dpkg --search and discarding errors
  770. close STDERR;
  771. open STDERR, ">", "/dev/null";
  772. $ENV{LC_ALL} = "C";
  773. exec("dpkg", "--search", "--", @files)
  774. || syserr(_g("unable to execute %s"), "dpkg");
  775. }
  776. while(defined($_ = <DPKG>)) {
  777. chomp($_);
  778. if (m/^local diversion |^diversion by/) {
  779. warning(_g("diversions involved - output may be incorrect"));
  780. print(STDERR " $_\n")
  781. || syserr(_g("write diversion info to stderr"));
  782. } elsif (m/^([^:]+): (\S+)$/) {
  783. $cached_pkgmatch{$2} = $pkgmatch->{$2} = [ split(/, /, $1) ];
  784. } else {
  785. warning(_g("unknown output from dpkg --search: '%s'"), $_);
  786. }
  787. }
  788. close(DPKG);
  789. return $pkgmatch;
  790. }