dpkg-shlibdeps.pl 9.4 KB

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