dpkg-shlibdeps.pl 28 KB

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