dpkg-shlibdeps.pl 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use warnings;
  4. use English;
  5. use POSIX qw(:errno_h :signal_h);
  6. use Cwd qw(realpath);
  7. use File::Basename qw(dirname);
  8. use Dpkg;
  9. use Dpkg::Gettext;
  10. use Dpkg::ErrorHandling;
  11. use Dpkg::Path qw(relative_to_pkg_root guess_pkg_root_dir
  12. check_files_are_the_same);
  13. use Dpkg::Version qw(compare_versions);
  14. use Dpkg::Shlibs qw(find_library @librarypaths);
  15. use Dpkg::Shlibs::Objdump;
  16. use Dpkg::Shlibs::SymbolFile;
  17. use Dpkg::Arch qw(get_host_arch);
  18. use Dpkg::Fields qw(capit);
  19. use Dpkg::Deps;
  20. use Dpkg::Control;
  21. use constant {
  22. WARN_SYM_NOT_FOUND => 1,
  23. WARN_DEP_AVOIDABLE => 2,
  24. WARN_NOT_NEEDED => 4,
  25. };
  26. # By increasing importance
  27. my @depfields = qw(Suggests Recommends Depends Pre-Depends);
  28. my $i = 0; my %depstrength = map { $_ => $i++ } @depfields;
  29. textdomain("dpkg-dev");
  30. my $shlibsoverride = '/etc/dpkg/shlibs.override';
  31. my $shlibsdefault = '/etc/dpkg/shlibs.default';
  32. my $shlibslocal = 'debian/shlibs.local';
  33. my $packagetype = 'deb';
  34. my $dependencyfield = 'Depends';
  35. my $varlistfile = 'debian/substvars';
  36. my $varnameprefix = 'shlibs';
  37. my $ignore_missing_info = 0;
  38. my $warnings = 3;
  39. my $debug = 0;
  40. my @exclude = ();
  41. my @pkg_dir_to_search = ();
  42. my $host_arch = get_host_arch();
  43. my (@pkg_shlibs, @pkg_symbols, @pkg_root_dirs);
  44. if (-d "debian") {
  45. push @pkg_symbols, <debian/*/DEBIAN/symbols>;
  46. push @pkg_shlibs, <debian/*/DEBIAN/shlibs>;
  47. my %uniq = map { guess_pkg_root_dir($_) => 1 } (@pkg_symbols, @pkg_shlibs);
  48. push @pkg_root_dirs, keys %uniq;
  49. }
  50. my ($stdout, %exec);
  51. foreach (@ARGV) {
  52. if (m/^-T(.*)$/) {
  53. $varlistfile = $1;
  54. } elsif (m/^-p(\w[-:0-9A-Za-z]*)$/) {
  55. $varnameprefix = $1;
  56. } elsif (m/^-L(.*)$/) {
  57. $shlibslocal = $1;
  58. } elsif (m/^-S(.*)$/) {
  59. push @pkg_dir_to_search, $1;
  60. } elsif (m/^-O$/) {
  61. $stdout = 1;
  62. } elsif (m/^-(h|-help)$/) {
  63. usage(); exit(0);
  64. } elsif (m/^--version$/) {
  65. version(); exit(0);
  66. } elsif (m/^--admindir=(.*)$/) {
  67. $admindir = $1;
  68. -d $admindir ||
  69. error(_g("administrative directory '%s' does not exist"), $admindir);
  70. } elsif (m/^-d(.*)$/) {
  71. $dependencyfield = capit($1);
  72. defined($depstrength{$dependencyfield}) ||
  73. warning(_g("unrecognised dependency field \`%s'"), $dependencyfield);
  74. } elsif (m/^-e(.*)$/) {
  75. if (exists $exec{$1}) {
  76. # Affect the binary to the most important field
  77. if ($depstrength{$dependencyfield} > $depstrength{$exec{$1}}) {
  78. $exec{$1} = $dependencyfield;
  79. }
  80. } else {
  81. $exec{$1} = $dependencyfield;
  82. }
  83. } elsif (m/^--ignore-missing-info$/) {
  84. $ignore_missing_info = 1;
  85. } elsif (m/^--warnings=(\d+)$/) {
  86. $warnings = $1;
  87. } elsif (m/^-t(.*)$/) {
  88. $packagetype = $1;
  89. } elsif (m/^-v$/) {
  90. $debug++;
  91. } elsif (m/^-x(.*)$/) {
  92. push @exclude, $1;
  93. } elsif (m/^-/) {
  94. usageerr(_g("unknown option \`%s'"), $_);
  95. } else {
  96. if (exists $exec{$_}) {
  97. # Affect the binary to the most important field
  98. if ($depstrength{$dependencyfield} > $depstrength{$exec{$_}}) {
  99. $exec{$_} = $dependencyfield;
  100. }
  101. } else {
  102. $exec{$_} = $dependencyfield;
  103. }
  104. }
  105. }
  106. scalar keys %exec || usageerr(_g("need at least one executable"));
  107. my $control = Dpkg::Control->new();
  108. my $fields = $control->get_source();
  109. my $build_depends = defined($fields->{"Build-Depends"}) ?
  110. $fields->{"Build-Depends"} : "";
  111. my $build_deps = Dpkg::Deps::parse($build_depends, reduce_arch => 1);
  112. my %dependencies;
  113. my %shlibs;
  114. # Statictics on soname seen in the whole run (with multiple analysis of
  115. # binaries)
  116. my %global_soname_notfound;
  117. my %global_soname_used;
  118. my %global_soname_needed;
  119. # Symfile and objdump caches
  120. my %symfile_cache;
  121. my %objdump_cache;
  122. my %symfile_has_soname_cache;
  123. my $cur_field;
  124. foreach my $file (keys %exec) {
  125. $cur_field = $exec{$file};
  126. print ">> Scanning $file (for $cur_field field)\n" if $debug;
  127. my $obj = Dpkg::Shlibs::Objdump::Object->new($file);
  128. my @sonames = $obj->get_needed_libraries;
  129. # Load symbols files for all needed libraries (identified by SONAME)
  130. my %libfiles;
  131. my %altlibfiles;
  132. my %soname_notfound;
  133. my %alt_soname;
  134. foreach my $soname (@sonames) {
  135. my $lib = my_find_library($soname, $obj->{RPATH}, $obj->{format}, $file);
  136. unless (defined $lib) {
  137. $soname_notfound{$soname} = 1;
  138. $global_soname_notfound{$soname} = 1;
  139. my $msg = _g("couldn't find library %s needed by %s (ELF format: '%s'; RPATH: '%s').\n" .
  140. "Note: libraries are not searched in other binary packages " .
  141. "that do not have any shlibs or symbols file.\nTo help dpkg-shlibdeps " .
  142. "find private libraries, you might need to set LD_LIBRARY_PATH.");
  143. if (scalar(split_soname($soname))) {
  144. error($msg, $soname, $file, $obj->{format}, join(":", @{$obj->{RPATH}}));
  145. } else {
  146. warning($msg, $soname, $file, $obj->{format}, join(":", @{$obj->{RPATH}}));
  147. }
  148. next;
  149. }
  150. $libfiles{$lib} = $soname;
  151. my $reallib = realpath($lib);
  152. if ($reallib ne $lib) {
  153. $altlibfiles{$reallib} = $soname;
  154. }
  155. print "Library $soname found in $lib\n" if $debug;
  156. }
  157. my $file2pkg = find_packages(keys %libfiles, keys %altlibfiles);
  158. my $symfile = Dpkg::Shlibs::SymbolFile->new();
  159. my $dumplibs_wo_symfile = Dpkg::Shlibs::Objdump->new();
  160. my @soname_wo_symfile;
  161. foreach my $lib (keys %libfiles) {
  162. my $soname = $libfiles{$lib};
  163. if (not scalar(grep { $_ ne '' } @{$file2pkg->{$lib}})) {
  164. # The path of the library as calculated is not the
  165. # official path of a packaged file, try to fallback on
  166. # on the realpath() first, maybe this one is part of a package
  167. my $reallib = realpath($lib);
  168. if (exists $file2pkg->{$reallib}) {
  169. $file2pkg->{$lib} = $file2pkg->{$reallib};
  170. }
  171. }
  172. if (not scalar(grep { $_ ne '' } @{$file2pkg->{$lib}})) {
  173. # If the library is really not available in an installed package,
  174. # it's because it's in the process of being built
  175. # Empty package name will lead to consideration of symbols
  176. # file from the package being built only
  177. $file2pkg->{$lib} = [""];
  178. print "No associated package found for $lib\n" if $debug;
  179. }
  180. # Load symbols/shlibs files from packages providing libraries
  181. foreach my $pkg (@{$file2pkg->{$lib}}) {
  182. my $symfile_path;
  183. my $haslocaldep = 0;
  184. if (-e $shlibslocal and
  185. defined(extract_from_shlibs($soname, $shlibslocal)))
  186. {
  187. $haslocaldep = 1;
  188. }
  189. if ($packagetype eq "deb" and not $haslocaldep) {
  190. # Use fine-grained dependencies only on real deb
  191. # and only if the dependency is not provided by shlibs.local
  192. $symfile_path = find_symbols_file($pkg, $soname, $lib);
  193. }
  194. if (defined($symfile_path)) {
  195. # Load symbol information
  196. print "Using symbols file $symfile_path for $soname\n" if $debug;
  197. unless (exists $symfile_cache{$symfile_path}) {
  198. $symfile_cache{$symfile_path} =
  199. Dpkg::Shlibs::SymbolFile->new($symfile_path);
  200. }
  201. $symfile->merge_object_from_symfile($symfile_cache{$symfile_path}, $soname);
  202. }
  203. if (defined($symfile_path) && $symfile->has_object($soname)) {
  204. # Initialize dependencies with the smallest minimal version
  205. # of all symbols (unversioned dependency is not ok as the
  206. # library might not have always been available in the
  207. # package and we really need it)
  208. my $dep = $symfile->get_dependency($soname);
  209. my $minver = $symfile->get_smallest_version($soname) || '';
  210. foreach my $subdep (split /\s*,\s*/, $dep) {
  211. if (not exists $dependencies{$cur_field}{$subdep}) {
  212. $dependencies{$cur_field}{$subdep} = $minver;
  213. 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 (compare_versions($depseen{$dep}, '>>', $dependencies{$field}{$dep})) {
  431. # The version of the dep seen is stronger...
  432. $stronger = 0;
  433. } else {
  434. $stronger = 1;
  435. }
  436. $depseen{$dep} = $dependencies{$field}{$dep} if $stronger;
  437. return $stronger;
  438. }
  439. }
  440. foreach my $field (reverse @depfields) {
  441. my $dep = "";
  442. if (exists $dependencies{$field} and scalar keys %{$dependencies{$field}}) {
  443. $dep = join ", ",
  444. map {
  445. # Translate dependency templates into real dependencies
  446. if ($dependencies{$field}{$_}) {
  447. s/#MINVER#/(>= $dependencies{$field}{$_})/g;
  448. } else {
  449. s/#MINVER#//g;
  450. }
  451. s/\s+/ /g;
  452. $_;
  453. } grep { filter_deps($_, $field) }
  454. keys %{$dependencies{$field}};
  455. }
  456. if ($dep) {
  457. my $obj = Dpkg::Deps::parse($dep);
  458. error(_g("invalid dependency got generated: %s"), $dep) unless defined $obj;
  459. $obj->sort();
  460. print $fh "$varnameprefix:$field=" . $obj->dump() . "\n";
  461. }
  462. }
  463. # Replace old file by new one
  464. if (!$stdout) {
  465. close($fh);
  466. rename("$varlistfile.new",$varlistfile) ||
  467. syserr(_g("install new varlist file \`%s'"), $varlistfile);
  468. }
  469. ##
  470. ## Functions
  471. ##
  472. sub version {
  473. printf _g("Debian %s version %s.\n"), $progname, $version;
  474. printf _g("
  475. Copyright (C) 1996 Ian Jackson.
  476. Copyright (C) 2000 Wichert Akkerman.
  477. Copyright (C) 2006 Frank Lichtenheld.
  478. Copyright (C) 2007 Raphael Hertzog.
  479. ");
  480. printf _g("
  481. This is free software; see the GNU General Public Licence version 2 or
  482. later for copying conditions. There is NO warranty.
  483. ");
  484. }
  485. sub usage {
  486. printf _g(
  487. "Usage: %s [<option> ...] <executable>|-e<executable> [<option> ...]
  488. Positional options (order is significant):
  489. <executable> include dependencies for <executable>,
  490. -e<executable> (use -e if <executable> starts with \`-')
  491. -d<dependencyfield> next executable(s) set shlibs:<dependencyfield>.
  492. Options:
  493. -p<varnameprefix> set <varnameprefix>:* instead of shlibs:*.
  494. -O print variable settings to stdout.
  495. -L<localshlibsfile> shlibs override file, not debian/shlibs.local.
  496. -T<varlistfile> update variables here, not debian/substvars.
  497. -t<type> set package type (default is deb).
  498. -x<package> exclude package from the generated dependencies.
  499. -S<pkgbuilddir> search needed libraries in the given
  500. package build directory first.
  501. -v enable verbose mode (can be used multiple times).
  502. --ignore-missing-info don't fail if dependency information can't be found.
  503. --warnings=<value> define set of active warnings (see manual page).
  504. --admindir=<directory> change the administrative directory.
  505. -h, --help show this help message.
  506. --version show the version.
  507. Dependency fields recognised are:
  508. %s
  509. "), $progname, join("/",@depfields);
  510. }
  511. sub get_min_version_from_deps {
  512. my ($dep, $pkg) = @_;
  513. if ($dep->isa('Dpkg::Deps::Simple')) {
  514. if (($dep->{package} eq $pkg) &&
  515. defined($dep->{relation}) &&
  516. (($dep->{relation} eq ">=") ||
  517. ($dep->{relation} eq ">>")))
  518. {
  519. return $dep->{version};
  520. }
  521. return undef;
  522. } else {
  523. my $res;
  524. foreach my $subdep ($dep->get_deps()) {
  525. my $minver = get_min_version_from_deps($subdep, $pkg);
  526. next if not defined $minver;
  527. if (defined $res) {
  528. if (compare_versions($minver, '>>', $res)) {
  529. $res = $minver;
  530. }
  531. } else {
  532. $res = $minver;
  533. }
  534. }
  535. return $res;
  536. }
  537. }
  538. sub update_dependency_version {
  539. my ($dep, $minver, $existing_only) = @_;
  540. return if not defined($minver);
  541. foreach my $subdep (split /\s*,\s*/, $dep) {
  542. if (exists $dependencies{$cur_field}{$subdep} and
  543. defined($dependencies{$cur_field}{$subdep}))
  544. {
  545. if ($dependencies{$cur_field}{$subdep} eq '' or
  546. compare_versions($minver, '>>',
  547. $dependencies{$cur_field}{$subdep}))
  548. {
  549. $dependencies{$cur_field}{$subdep} = $minver;
  550. }
  551. } elsif (!$existing_only) {
  552. $dependencies{$cur_field}{$subdep} = $minver;
  553. }
  554. }
  555. }
  556. sub add_shlibs_dep {
  557. my ($soname, $pkg, $libfile) = @_;
  558. my @shlibs = ($shlibslocal, $shlibsoverride);
  559. if ($pkg eq "") {
  560. # If the file is not packaged, try to find out the shlibs file in
  561. # the package being built where the lib has been found
  562. my $pkg_root = guess_pkg_root_dir($libfile);
  563. if (defined $pkg_root) {
  564. push @shlibs, "$pkg_root/DEBIAN/shlibs";
  565. }
  566. # Fallback to other shlibs files but it shouldn't be necessary
  567. push @shlibs, @pkg_shlibs;
  568. } else {
  569. push @shlibs, "$admindir/info/$pkg.shlibs";
  570. }
  571. push @shlibs, $shlibsdefault;
  572. print " Looking up shlibs dependency of $soname provided by '$pkg'\n" if $debug;
  573. foreach my $file (@shlibs) {
  574. next if not -e $file;
  575. my $dep = extract_from_shlibs($soname, $file);
  576. if (defined($dep)) {
  577. print " Found $dep in $file\n" if $debug;
  578. foreach (split(/,\s*/, $dep)) {
  579. # Note: the value is empty for shlibs based dependency
  580. # symbol based dependency will put a version as value
  581. $dependencies{$cur_field}{$_} = '';
  582. }
  583. return 1;
  584. }
  585. }
  586. print " Found nothing\n" if $debug;
  587. return 0;
  588. }
  589. sub split_soname {
  590. my $soname = shift;
  591. if ($soname =~ /^(.*)\.so\.(.*)$/) {
  592. return wantarray ? ($1, $2) : 1;
  593. } elsif ($soname =~ /^(.*)-(\d.*)\.so$/) {
  594. return wantarray ? ($1, $2) : 1;
  595. } else {
  596. return wantarray ? () : 0;
  597. }
  598. }
  599. sub extract_from_shlibs {
  600. my ($soname, $shlibfile) = @_;
  601. # Split soname in name/version
  602. my ($libname, $libversion) = split_soname($soname);
  603. unless (defined $libname) {
  604. warning(_g("Can't extract name and version from library name \`%s'"),
  605. $soname);
  606. return;
  607. }
  608. # Open shlibs file
  609. $shlibfile = "./$shlibfile" if $shlibfile =~ m/^\s/;
  610. open(SHLIBS, "<", $shlibfile) ||
  611. syserr(_g("unable to open shared libs info file \`%s'"), $shlibfile);
  612. my $dep;
  613. while (<SHLIBS>) {
  614. s/\s*\n$//;
  615. next if m/^\#/;
  616. if (!m/^\s*(?:(\S+):\s+)?(\S+)\s+(\S+)(?:\s+(\S.*\S))?\s*$/) {
  617. warning(_g("shared libs info file \`%s' line %d: bad line \`%s'"),
  618. $shlibfile, $., $_);
  619. next;
  620. }
  621. my $depread = defined($4) ? $4 : '';
  622. if (($libname eq $2) && ($libversion eq $3)) {
  623. # Define dep and end here if the package type explicitely
  624. # matches. Otherwise if the packagetype is not specified, use
  625. # the dep only as a default that can be overriden by a later
  626. # line
  627. if (defined($1)) {
  628. if ($1 eq $packagetype) {
  629. $dep = $depread;
  630. last;
  631. }
  632. } else {
  633. $dep = $depread unless defined $dep;
  634. }
  635. }
  636. }
  637. close(SHLIBS);
  638. return $dep;
  639. }
  640. sub find_symbols_file {
  641. my ($pkg, $soname, $libfile) = @_;
  642. my @files;
  643. if ($pkg eq "") {
  644. # If the file is not packaged, try to find out the symbols file in
  645. # the package being built where the lib has been found
  646. my $pkg_root = guess_pkg_root_dir($libfile);
  647. if (defined $pkg_root) {
  648. push @files, "$pkg_root/DEBIAN/symbols";
  649. }
  650. # Fallback to other symbols files but it shouldn't be necessary
  651. push @files, @pkg_symbols;
  652. } else {
  653. push @files, "/etc/dpkg/symbols/$pkg.symbols.$host_arch",
  654. "/etc/dpkg/symbols/$pkg.symbols",
  655. "$admindir/info/$pkg.symbols";
  656. }
  657. foreach my $file (@files) {
  658. if (-e $file and symfile_has_soname($file, $soname)) {
  659. return $file;
  660. }
  661. }
  662. return undef;
  663. }
  664. sub symfile_has_soname {
  665. my ($file, $soname) = @_;
  666. if (exists $symfile_has_soname_cache{$file}{$soname}) {
  667. return $symfile_has_soname_cache{$file}{$soname};
  668. }
  669. open(SYM_FILE, "<", $file) ||
  670. syserr(_g("cannot open file %s"), $file);
  671. my $result = 0;
  672. while (<SYM_FILE>) {
  673. if (/^\Q$soname\E /) {
  674. $result = 1;
  675. last;
  676. }
  677. }
  678. close(SYM_FILE);
  679. $symfile_has_soname_cache{$file}{$soname} = $result;
  680. return $result;
  681. }
  682. # find_library ($soname, \@rpath, $format)
  683. sub my_find_library {
  684. my ($lib, $rpath, $format, $execfile) = @_;
  685. my $file;
  686. # Create real RPATH in case $ORIGIN is used
  687. # Note: ld.so also supports $PLATFORM and $LIB but they are
  688. # used in real case (yet)
  689. my $libdir = relative_to_pkg_root($execfile);
  690. my $origin;
  691. if (defined $libdir) {
  692. $origin = "/$libdir";
  693. $origin =~ s{/+[^/]*$}{};
  694. }
  695. my @RPATH = ();
  696. foreach my $path (@{$rpath}) {
  697. if ($path =~ /\$ORIGIN|\$\{ORIGIN\}/) {
  698. if (defined $origin) {
  699. $path =~ s/\$ORIGIN/$origin/g;
  700. $path =~ s/\$\{ORIGIN\}/$origin/g;
  701. } else {
  702. warning(_g("\$ORIGIN is used in RPATH of %s and the corresponding " .
  703. "directory could not be identified due to lack of DEBIAN " .
  704. "sub-directory in the root of package's build tree"), $execfile);
  705. }
  706. }
  707. push @RPATH, $path;
  708. }
  709. # Look into the packages we're currently building in the following
  710. # order:
  711. # - package build tree of the binary which is analyzed
  712. # - package build tree given on the command line (option -S)
  713. # - other package build trees that contain either a shlibs or a
  714. # symbols file
  715. my @builddirs;
  716. my $pkg_root = guess_pkg_root_dir($execfile);
  717. push @builddirs, $pkg_root if defined $pkg_root;
  718. push @builddirs, @pkg_dir_to_search;
  719. push @builddirs, @pkg_root_dirs;
  720. my %dir_checked;
  721. foreach my $builddir (@builddirs) {
  722. next if defined($dir_checked{$builddir});
  723. $file = find_library($lib, \@RPATH, $format, $builddir);
  724. return $file if defined($file);
  725. $dir_checked{$builddir} = 1;
  726. }
  727. # Fallback in the root directory if we have not found what we were
  728. # looking for in the packages
  729. $file = find_library($lib, \@RPATH, $format, "");
  730. return $file if defined($file);
  731. return undef;
  732. }
  733. my %cached_pkgmatch = ();
  734. sub find_packages {
  735. my @files;
  736. my $pkgmatch = {};
  737. foreach (@_) {
  738. if (exists $cached_pkgmatch{$_}) {
  739. $pkgmatch->{$_} = $cached_pkgmatch{$_};
  740. } else {
  741. push @files, $_;
  742. $cached_pkgmatch{$_} = [""]; # placeholder to cache misses too.
  743. $pkgmatch->{$_} = [""]; # might be replaced later on
  744. }
  745. }
  746. return $pkgmatch unless scalar(@files);
  747. my $pid = open(DPKG, "-|");
  748. syserr(_g("cannot fork for dpkg --search")) unless defined($pid);
  749. if (!$pid) {
  750. # Child process running dpkg --search and discarding errors
  751. close STDERR;
  752. open STDERR, ">", "/dev/null";
  753. $ENV{LC_ALL} = "C";
  754. exec("dpkg", "--search", "--", @files)
  755. || syserr(_g("cannot exec dpkg"));
  756. }
  757. while(defined($_ = <DPKG>)) {
  758. chomp($_);
  759. if (m/^local diversion |^diversion by/) {
  760. warning(_g("diversions involved - output may be incorrect"));
  761. print(STDERR " $_\n")
  762. || syserr(_g("write diversion info to stderr"));
  763. } elsif (m/^([^:]+): (\S+)$/) {
  764. $cached_pkgmatch{$2} = $pkgmatch->{$2} = [ split(/, /, $1) ];
  765. } else {
  766. warning(_g("unknown output from dpkg --search: '%s'"), $_);
  767. }
  768. }
  769. close(DPKG);
  770. return $pkgmatch;
  771. }