dpkg-shlibdeps.pl 9.7 KB

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