dpkg-shlibdeps.pl 27 KB

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