dpkg-shlibdeps.pl 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. defined($c= open(P,"-|")) || syserr("cannot fork for objdump");
  85. if (!$c) { exec("objdump","-p","--",$exec[$i]); syserr("cannot exec objdump"); }
  86. while (<P>) {
  87. chomp;
  88. if (m,^\s*NEEDED\s+(\S+)\.so\.(\S+)$,) {
  89. push(@libname,$1); push(@libsoname,$2);
  90. push(@libf,$execf[$i]);
  91. push(@libfiles,"$1.so.$2");
  92. }
  93. }
  94. close(P); $? && subprocerr("objdump on \`$exec[$i]'");
  95. }
  96. # Now: See if it is in this package. See if it is in any other package.
  97. sub searchdir {
  98. my $dir = shift;
  99. if(opendir(DIR, $dir)) {
  100. my @dirents = readdir(DIR);
  101. closedir(DIR);
  102. for (@dirents) {
  103. if ( -f "$dir/$_/DEBIAN/shlibs" ) {
  104. push(@curshlibs, "$dir/$_/DEBIAN/shlibs");
  105. next;
  106. } elsif ( $_ !~ /^\./ && -d "$dir/$_" ) {
  107. &searchdir("$dir/$_");
  108. }
  109. }
  110. }
  111. }
  112. $searchdir = $exec[0];
  113. $curpackdir = "debian/tmp";
  114. do { $searchdir =~ s,/[^/]*$,,; } while($searchdir =~ m,/, && ! -d "$searchdir/DEBIAN");
  115. if ($searchdir =~ m,/,) {
  116. $curpackdir = $searchdir;
  117. $searchdir =~ s,/[^/]*,,;
  118. &searchdir($searchdir);
  119. }
  120. if (1 || $#curshlibs >= 0) {
  121. PRELIB: for ($i=0;$i<=$#libname;$i++) {
  122. if(scanshlibsfile($shlibslocal,$libname[$i],$libsoname[$i],$libf[$i])
  123. || scanshlibsfile($shlibsoverride,$libname[$i],$libsoname[$i],$libf[$i])) {
  124. splice(@libname, $i, 1);
  125. splice(@libsoname, $i, 1);
  126. splice(@libf, $i, 1);
  127. splice(@libfiles, $i, 1);
  128. $i--;
  129. next PRELIB;
  130. }
  131. for my $shlibsfile (@curshlibs) {
  132. if(scanshlibsfile($shlibsfile, $libname[$i], $libsoname[$i], $libf[$i])) {
  133. splice(@libname, $i, 1);
  134. splice(@libsoname, $i, 1);
  135. splice(@libf, $i, 1);
  136. splice(@libfiles, $i, 1);
  137. $i--;
  138. next PRELIB;
  139. }
  140. }
  141. }
  142. }
  143. if ($#libfiles >= 0) {
  144. grep(s/\[\?\*/\\$&/g, @libname);
  145. defined($c= open(P,"-|")) || syserr("cannot fork for dpkg --search");
  146. if (!$c) {
  147. close STDERR; # we don't need to see dpkg's errors
  148. open STDERR, "> /dev/null";
  149. exec("dpkg","--search","--",map {"*/$_"} @libfiles); syserr("cannot exec dpkg");
  150. }
  151. while (<P>) {
  152. chomp;
  153. if (m/^local diversion |^diversion by/) {
  154. &warn("diversions involved - output may be incorrect");
  155. print(STDERR " $_\n") || syserr("write diversion info to stderr");
  156. } elsif (m=^(\S+(, \S+)*): /.+/([^/]+)$=) {
  157. push @{$pathpackages{$+}}, split(/, /, $1);
  158. } else {
  159. &warn("unknown output from dpkg --search: \`$_'");
  160. }
  161. }
  162. close(P); $? && subprocerr("dpkg --search");
  163. }
  164. LIB: for ($i=0;$i<=$#libname;$i++) {
  165. if (!defined($pathpackages{$libfiles[$i]})) {
  166. &warn("could not find any packages for $libfiles[$i]".
  167. " ($libname[$i].so.$libsoname[$i])");
  168. } else {
  169. for $p (@{$pathpackages{$libfiles[$i]}}) {
  170. scanshlibsfile("$shlibsppdir/$p$shlibsppext",
  171. $libname[$i],$libsoname[$i],$libf[$i])
  172. && next LIB;
  173. }
  174. }
  175. scanshlibsfile($shlibsdefault,$libname[$i],$libsoname[$i],$libf[$i]) && next;
  176. &warn("unable to find dependency information for ".
  177. "shared library $libname[$i] (soname $libsoname[$i], path $libfiles[$i], ".
  178. "dependency field $libf[$i])");
  179. }
  180. sub scanshlibsfile {
  181. my ($fn,$ln,$lsn,$lf) = @_;
  182. my ($da,$dv,$dk);
  183. $fn= "./$fn" if $fn =~ m/^\s/;
  184. if (!open(SLF,"< $fn")) {
  185. $! == ENOENT || syserr("unable to open shared libs info file \`$fn'");
  186. return 0;
  187. }
  188. while (<SLF>) {
  189. s/\s*\n$//; next if m/^\#/;
  190. if (!m/^\s*(\S+)\s+(\S+)/) {
  191. &warn("shared libs info file \`$fn' line $.: bad line \`$_'");
  192. next;
  193. }
  194. next if $1 ne $ln || $2 ne $lsn;
  195. return 1 if $fn eq "$curpackdir/DEBIAN/shlibs";
  196. $da= $';
  197. for $dv (split(/,/,$da)) {
  198. $dv =~ s/^\s+//; $dv =~ s/\s+$//;
  199. if (defined($depstrength{$lf})) {
  200. if (!defined($predefdepfdep{$dv}) ||
  201. $depstrength{$predefdepfdep{$dv}} < $depstrength{$lf}) {
  202. $predefdepfdep{$dv}= $lf;
  203. }
  204. } else {
  205. $dk= "$lf: $dv";
  206. if (!defined($unkdepfdone{$dk})) {
  207. $unkdepfdone{$dk}= 1;
  208. $unkdepf{$lf}.= ', ' if length($unkdepf{$lf});
  209. $unkdepf{$lf}.= $dv;
  210. }
  211. }
  212. }
  213. return 1;
  214. }
  215. close(SLF);
  216. return 0;
  217. }
  218. if (!$stdout) {
  219. $varlistfile="./$varlistfile" if $fileslistfile =~ m/^\s/;
  220. open(Y,"> $varlistfile.new") ||
  221. syserr("open new substvars file \`$varlistfile.new'");
  222. chown(@fowner, "$varlistfile.new") ||
  223. syserr("chown of \`$varlistfile.new'");
  224. if (open(X,"< $varlistfile")) {
  225. while (<X>) {
  226. s/\n$//;
  227. next if m/^(\w[-:0-9A-Za-z]*):/ && $1 eq $varnameprefix;
  228. print(Y "$_\n") ||
  229. syserr("copy old entry to new varlist file \`$varlistfile.new'");
  230. }
  231. } elsif ($! != ENOENT) {
  232. syserr("open old varlist file \`$varlistfile' for reading");
  233. }
  234. $fh= 'Y';
  235. } else {
  236. $fh= 'STDOUT';
  237. }
  238. for $dv (sort keys %predefdepfdep) {
  239. $lf= $predefdepfdep{$dv};
  240. $defdepf{$lf}.= ', ' if length($defdepf{$lf});
  241. $defdepf{$lf}.= $dv;
  242. }
  243. for $lf (reverse @depfields) {
  244. next unless defined($defdepf{$lf});
  245. print($fh "$varnameprefix:$lf=$defdepf{$lf}\n")
  246. || syserr("write output entry");
  247. }
  248. for $lf (sort keys %unkdepf) {
  249. print($fh "$varnameprefix:$lf=$unkdepf{$lf}\n")
  250. || syserr("write userdef output entry");
  251. }
  252. close($fh) || syserr("close output");
  253. if (!$stdout) {
  254. rename("$varlistfile.new",$varlistfile) ||
  255. syserr("install new varlist file \`$varlistfile'");
  256. }