dpkg-shlibdeps.pl 8.5 KB

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