dpkg-shlibdeps.pl 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #! /usr/bin/perl
  2. #
  3. # dpkg-shlibdeps
  4. # $Id$
  5. $dpkglibdir="/usr/lib/dpkg";
  6. $version="1.4.1.19"; # This line modified by Makefile
  7. use POSIX;
  8. use POSIX qw(:errno_h :signal_h);
  9. $shlibsoverride= '/etc/dpkg/shlibs.override';
  10. $shlibsdefault= '/etc/dpkg/shlibs.default';
  11. $shlibslocal= 'debian/shlibs.local';
  12. $shlibsppdir= '/var/lib/dpkg/info';
  13. $shlibsppext= '.shlibs';
  14. $varnameprefix= 'shlibs';
  15. $dependencyfield= 'Depends';
  16. $varlistfile= 'debian/substvars';
  17. @depfields= qw(Suggests Recommends Depends Pre-Depends);
  18. push(@INC,$dpkglibdir);
  19. require 'controllib.pl';
  20. sub usageversion {
  21. print STDERR
  22. "Debian GNU/Linux dpkg-shlibdeps $version.
  23. Copyright (C) 1996 Ian Jackson.
  24. Copyright (C) 2000 Wichert Akkerman.
  25. This is free software; see the GNU General Public Licence version 2 or
  26. later for copying conditions. There is NO warranty.
  27. Usage:
  28. dpkg-shlibdeps [<option> ...] <executable>|-e<executable> [<option>] ...
  29. Positional arguments/options (order is significant):
  30. <executable> } include dependencies for <executable>
  31. -e<executable> } (use -e if <executable> starts with \`-')
  32. -d<dependencyfield> next executable(s) set shlibs:<dependencyfield>
  33. Overall options (have global effect no matter where placed):
  34. -p<varnameprefix> set <varnameprefix>:* instead of shlibs:*.
  35. -O print variable settings to stdout
  36. -L<localshlibsfile> shlibs override file, not debian/shlibs.local
  37. -T<varlistfile> update variables here, not debian/substvars
  38. Dependency fields recognised are ".join("/",@depfields)."
  39. ";
  40. }
  41. $i=0; grep($depstrength{$_}= ++$i, @depfields);
  42. while (@ARGV) {
  43. $_=shift(@ARGV);
  44. if (m/^-T/) {
  45. $varlistfile= $';
  46. } elsif (m/^-p(\w[-:0-9A-Za-z]*)$/) {
  47. $varnameprefix= $1;
  48. } elsif (m/^-L/) {
  49. $shlibslocal= $';
  50. } elsif (m/^-O$/) {
  51. $stdout= 1;
  52. } elsif (m/^-h$/) {
  53. usageversion; exit(0);
  54. } elsif (m/^-d/) {
  55. $dependencyfield= capit($');
  56. defined($depstrength{$dependencyfield}) ||
  57. &warn("unrecognised dependency field \`$dependencyfield'");
  58. } elsif (m/^-e/) {
  59. push(@exec,$'); push(@execf,$dependencyfield);
  60. } elsif (m/^-/) {
  61. usageerr("unknown option \`$_'");
  62. } else {
  63. push(@exec,$_); push(@execf,$dependencyfield);
  64. }
  65. }
  66. @exec || usageerr("need at least one executable");
  67. sub isbin {
  68. open (F, $_[0]) || die("unable to open '$_[0]' for test");
  69. if (read (F, $d, 4) != 4) {
  70. die ("unable to read first four bytes of '$_[0]' as magic number");
  71. }
  72. if ($d =~ /^\177ELF$/) { # ELF binary
  73. return 1;
  74. } elsif ($d =~ /^\#\!..$/) { # shell script
  75. return 0;
  76. } elsif (unpack ('N', $d) == 0xcafebabe) { # JAVA binary
  77. return 0;
  78. } else {
  79. die("unrecognized file type for '$_[0]'");
  80. }
  81. }
  82. for ($i=0;$i<=$#exec;$i++) {
  83. if (!isbin ($exec[$i])) { next; }
  84. # First we get an ldd output to see what libs + paths we have at out
  85. # disposal.
  86. my %so2path = ();
  87. defined($c= open(P,"-|")) || syserr("cannot fork for ldd");
  88. if (!$c) { exec("ldd","--",$exec[$i]); syserr("cannot exec ldd"); }
  89. while (<P>) {
  90. if (m,^\s+(\S+)\s+=>\s+(\S+)\s+\(0x.+\)?$,) {
  91. $so2path{$1} = $2;
  92. }
  93. }
  94. close(P); $? && subprocerr("ldd on \`$exec[$i]'");
  95. # Now we get the direct deps of the program. We then check back with
  96. # the ldd output from above to see what our path is.
  97. defined($c= open(P,"-|")) || syserr("cannot fork for objdump");
  98. if (!$c) { exec("objdump","-p","--",$exec[$i]); syserr("cannot exec objdump"); }
  99. while (<P>) {
  100. chomp;
  101. if (m,^\s*NEEDED\s+,) {
  102. if (m,^\s*NEEDED\s+((\S+)\.so\.(\S+))$,) {
  103. push(@libname,$2); push(@libsoname,$3);
  104. push(@libf,$execf[$i]);
  105. &warn("could not find path for $1") unless defined($so2path{$1});
  106. push(@libfiles,$so2path{$1});
  107. } elsif (m,^\s*NEEDED\s+((\S+)-(\S+)\.so)$,) {
  108. push(@libname,$2); push(@libsoname,$3);
  109. push(@libf,$execf[$i]);
  110. &warn("could not find path for $1") unless defined($so2path{$1});
  111. push(@libfiles,$so2path{$1});
  112. } else {
  113. m,^\s*NEEDED\s+(\S+)$,;
  114. &warn("format of $1 not recognized");
  115. }
  116. }
  117. }
  118. close(P); $? && subprocerr("objdump on \`$exec[$i]'");
  119. }
  120. # Now: See if it is in this package. See if it is in any other package.
  121. sub searchdir {
  122. my $dir = shift;
  123. if(opendir(DIR, $dir)) {
  124. my @dirents = readdir(DIR);
  125. closedir(DIR);
  126. for (@dirents) {
  127. if ( -f "$dir/$_/DEBIAN/shlibs" ) {
  128. push(@curshlibs, "$dir/$_/DEBIAN/shlibs");
  129. next;
  130. } elsif ( $_ !~ /^\./ && -d "$dir/$_" ) {
  131. &searchdir("$dir/$_");
  132. }
  133. }
  134. }
  135. }
  136. $searchdir = $exec[0];
  137. $curpackdir = "debian/tmp";
  138. do { $searchdir =~ s,/[^/]*$,,; } while($searchdir =~ m,/, && ! -d "$searchdir/DEBIAN");
  139. if ($searchdir =~ m,/,) {
  140. $curpackdir = $searchdir;
  141. $searchdir =~ s,/[^/]*,,;
  142. &searchdir($searchdir);
  143. }
  144. if (1 || $#curshlibs >= 0) {
  145. PRELIB: for ($i=0;$i<=$#libname;$i++) {
  146. if(scanshlibsfile($shlibslocal,$libname[$i],$libsoname[$i],$libf[$i])
  147. || scanshlibsfile($shlibsoverride,$libname[$i],$libsoname[$i],$libf[$i])) {
  148. splice(@libname, $i, 1);
  149. splice(@libsoname, $i, 1);
  150. splice(@libf, $i, 1);
  151. splice(@libfiles, $i, 1);
  152. $i--;
  153. next PRELIB;
  154. }
  155. for my $shlibsfile (@curshlibs) {
  156. if(scanshlibsfile($shlibsfile, $libname[$i], $libsoname[$i], $libf[$i])) {
  157. splice(@libname, $i, 1);
  158. splice(@libsoname, $i, 1);
  159. splice(@libf, $i, 1);
  160. splice(@libfiles, $i, 1);
  161. $i--;
  162. next PRELIB;
  163. }
  164. }
  165. }
  166. }
  167. if ($#libfiles >= 0) {
  168. grep(s/\[\?\*/\\$&/g, @libname);
  169. defined($c= open(P,"-|")) || syserr("cannot fork for dpkg --search");
  170. if (!$c) {
  171. close STDERR; # we don't need to see dpkg's errors
  172. open STDERR, "> /dev/null";
  173. exec("dpkg","--search","--",map {"$_"} @libfiles); syserr("cannot exec dpkg");
  174. }
  175. while (<P>) {
  176. chomp;
  177. if (m/^local diversion |^diversion by/) {
  178. &warn("diversions involved - output may be incorrect");
  179. print(STDERR " $_\n") || syserr("write diversion info to stderr");
  180. } elsif (m=^(\S+(, \S+)*): (\S+)$=) {
  181. push @{$pathpackages{$+}}, split(/, /, $1);
  182. } else {
  183. &warn("unknown output from dpkg --search: \`$_'");
  184. }
  185. }
  186. close(P); $? && subprocerr("dpkg --search");
  187. }
  188. LIB: for ($i=0;$i<=$#libname;$i++) {
  189. if (!defined($pathpackages{$libfiles[$i]})) {
  190. &warn("could not find any packages for $libfiles[$i]".
  191. " ($libname[$i].so.$libsoname[$i])");
  192. } else {
  193. for $p (@{$pathpackages{$libfiles[$i]}}) {
  194. scanshlibsfile("$shlibsppdir/$p$shlibsppext",
  195. $libname[$i],$libsoname[$i],$libf[$i])
  196. && next LIB;
  197. }
  198. }
  199. scanshlibsfile($shlibsdefault,$libname[$i],$libsoname[$i],$libf[$i]) && next;
  200. &warn("unable to find dependency information for ".
  201. "shared library $libname[$i] (soname $libsoname[$i], path $libfiles[$i], ".
  202. "dependency field $libf[$i])");
  203. }
  204. sub scanshlibsfile {
  205. my ($fn,$ln,$lsn,$lf) = @_;
  206. my ($da,$dv,$dk);
  207. $fn= "./$fn" if $fn =~ m/^\s/;
  208. if (!open(SLF,"< $fn")) {
  209. $! == ENOENT || syserr("unable to open shared libs info file \`$fn'");
  210. return 0;
  211. }
  212. while (<SLF>) {
  213. s/\s*\n$//; next if m/^\#/;
  214. if (!m/^\s*(\S+)\s+(\S+)/) {
  215. &warn("shared libs info file \`$fn' line $.: bad line \`$_'");
  216. next;
  217. }
  218. next if $1 ne $ln || $2 ne $lsn;
  219. return 1 if $fn eq "$curpackdir/DEBIAN/shlibs";
  220. $da= $';
  221. for $dv (split(/,/,$da)) {
  222. $dv =~ s/^\s+//; $dv =~ s/\s+$//;
  223. if (defined($depstrength{$lf})) {
  224. if (!defined($predefdepfdep{$dv}) ||
  225. $depstrength{$predefdepfdep{$dv}} < $depstrength{$lf}) {
  226. $predefdepfdep{$dv}= $lf;
  227. }
  228. } else {
  229. $dk= "$lf: $dv";
  230. if (!defined($unkdepfdone{$dk})) {
  231. $unkdepfdone{$dk}= 1;
  232. $unkdepf{$lf}.= ', ' if length($unkdepf{$lf});
  233. $unkdepf{$lf}.= $dv;
  234. }
  235. }
  236. }
  237. return 1;
  238. }
  239. close(SLF);
  240. return 0;
  241. }
  242. if (!$stdout) {
  243. $varlistfile="./$varlistfile" if $fileslistfile =~ m/^\s/;
  244. open(Y,"> $varlistfile.new") ||
  245. syserr("open new substvars file \`$varlistfile.new'");
  246. chown(@fowner, "$varlistfile.new") ||
  247. syserr("chown of \`$varlistfile.new'");
  248. if (open(X,"< $varlistfile")) {
  249. while (<X>) {
  250. s/\n$//;
  251. next if m/^(\w[-:0-9A-Za-z]*):/ && $1 eq $varnameprefix;
  252. print(Y "$_\n") ||
  253. syserr("copy old entry to new varlist file \`$varlistfile.new'");
  254. }
  255. } elsif ($! != ENOENT) {
  256. syserr("open old varlist file \`$varlistfile' for reading");
  257. }
  258. $fh= 'Y';
  259. } else {
  260. $fh= 'STDOUT';
  261. }
  262. for $dv (sort keys %predefdepfdep) {
  263. $lf= $predefdepfdep{$dv};
  264. $defdepf{$lf}.= ', ' if length($defdepf{$lf});
  265. $defdepf{$lf}.= $dv;
  266. }
  267. for $lf (reverse @depfields) {
  268. next unless defined($defdepf{$lf});
  269. print($fh "$varnameprefix:$lf=$defdepf{$lf}\n")
  270. || syserr("write output entry");
  271. }
  272. for $lf (sort keys %unkdepf) {
  273. print($fh "$varnameprefix:$lf=$unkdepf{$lf}\n")
  274. || syserr("write userdef output entry");
  275. }
  276. close($fh) || syserr("close output");
  277. if (!$stdout) {
  278. rename("$varlistfile.new",$varlistfile) ||
  279. syserr("install new varlist file \`$varlistfile'");
  280. }