dpkg-shlibdeps.pl 26 KB

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