dpkg-shlibdeps.pl 28 KB

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