dpkg-shlibdeps.pl 26 KB

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