dpkg-shlibdeps.pl 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. Copyright (C) 1996
  20. Ian Jackson. This is free software; see the GNU General Public Licence
  21. version 2 or later for copying conditions. There is NO warranty.
  22. Usage:
  23. dpkg-shlibdeps [<option> ...] <executable>|-e<executable> [<option>] ...
  24. Positional arguments/options (order is significant):
  25. <executable> } include dependencies for <executable>
  26. -e<executable> } (use -e if <executable> starts with \`-')
  27. -d<dependencyfield> next executable(s) set shlibs:<dependencyfield>
  28. Overall options (have global effect no matter where placed):
  29. -p<varnameprefix> set <varnameprefix>:* instead of shlibs:*.
  30. -O print variable settings to stdout
  31. -L<localshlibsfile> shlibs override file, not debian/shlibs.local
  32. -T<varlistfile> update variables here, not debian/substvars
  33. Dependency fields recognised are ".join("/",@depfields)."
  34. ";
  35. }
  36. $i=0; grep($depstrength{$_}= ++$i, @depfields);
  37. while (@ARGV) {
  38. $_=shift(@ARGV);
  39. if (m/^-T/) {
  40. $varlistfile= $';
  41. } elsif (m/^-p(\w[-:0-9A-Za-z]*)$/) {
  42. $varnameprefix= $1;
  43. } elsif (m/^-L/) {
  44. $shlibslocal= $';
  45. } elsif (m/^-O$/) {
  46. $stdout= 1;
  47. } elsif (m/^-h$/) {
  48. usageversion; exit(0);
  49. } elsif (m/^-d/) {
  50. $dependencyfield= capit($');
  51. defined($depstrength{$dependencyfield}) ||
  52. &warn("unrecognised dependency field \`$dependencyfield'");
  53. } elsif (m/^-e/) {
  54. push(@exec,$'); push(@execf,$dependencyfield);
  55. } elsif (m/^-/) {
  56. usageerr("unknown option \`$_'");
  57. } else {
  58. push(@exec,$_); push(@execf,$dependencyfield);
  59. }
  60. }
  61. @exec || usageerr("need at least one executable");
  62. sub isbin {
  63. open (F, $_[0]) || die("unable to open '$_[0]' for test");
  64. if (read (F, $d, 4) != 4) {
  65. die ("unable to read first four bytes of '$_[0]' as magic number");
  66. }
  67. if ($d =~ /^\177ELF$/) { # ELF binary
  68. return 1;
  69. } elsif ($d =~ /^\#\!..$/) { # shell script
  70. return 0;
  71. } elsif (unpack ('N', $d) == 0xcafebabe) { # JAVA binary
  72. return 0;
  73. } else {
  74. die("unrecognized file type for '$_[0]'");
  75. }
  76. }
  77. for ($i=0;$i<=$#exec;$i++) {
  78. if (!isbin ($exec[$i])) { next; }
  79. defined($c= open(P,"-|")) || syserr("cannot fork for ldd");
  80. if (!$c) { exec("ldd","--",$exec[$i]); syserr("cannot exec ldd"); }
  81. $nthisldd=0;
  82. while (<P>) {
  83. chomp;
  84. if (m,^\s+(\S+)\s+\=\>\s+\1$,) {
  85. # shared libraries depend on themselves (unsure why)
  86. # Only under old ld.so
  87. $nthisldd++;
  88. } elsif (m,\s+statically linked(\s+\(ELF\))?$,) {
  89. $nthisldd++;
  90. } elsif (m,^\s+(\S+)\.so\.(\S+)\s+=>\s+(/\S+)(\s+\(0x.+\))?$,) {
  91. push(@libname,$1); push(@libsoname,$2); push(@libpath,$3);
  92. push(@libf,$execf[$i]);
  93. push(@libpaths,$3) if !$libpathadded{$3}++;
  94. $nthisldd++;
  95. } else {
  96. &warn("unknown output from ldd on \`$exec[$i]': \`$_'");
  97. }
  98. }
  99. close(P); $? && subprocerr("ldd on \`$exec[$i]'");
  100. $nthisldd || &warn("ldd on \`$exec[$i]' gave nothing on standard output");
  101. }
  102. if ($#libpaths >= 0) {
  103. grep(s/\[\?\*/\\$&/g, @libpaths);
  104. defined($c= open(P,"-|")) || syserr("cannot fork for dpkg --search");
  105. if (!$c) { exec("dpkg","--search","--",@libpaths); syserr("cannot exec dpkg"); }
  106. while (<P>) {
  107. s/\n$//;
  108. if (m/^local diversion |^diversion by/) {
  109. &warn("diversions involved - output may be incorrect");
  110. print(STDERR " $_\n") || syserr("write diversion info to stderr");
  111. } elsif (m=^(\S+(, \S+)*): (/.+)$=) {
  112. $pathpackages{$+}= $1;
  113. } else {
  114. &warn("unknown output from dpkg --search: \`$_'");
  115. }
  116. }
  117. close(P); $? && subprocerr("dpkg --search");
  118. }
  119. LIB: for ($i=0;$i<=$#libname;$i++) {
  120. scanshlibsfile($shlibslocal,$libname[$i],$libsoname[$i],$libf[$i]) && next;
  121. scanshlibsfile($shlibsoverride,$libname[$i],$libsoname[$i],$libf[$i]) && next;
  122. if (!defined($pathpackages{$libpath[$i]})) {
  123. &warn("could not find any packages for $libpath[$i]".
  124. " ($libname[$i].so.$libsoname[$i])");
  125. } else {
  126. @packages= split(/, /,$pathpackages{$libpath[$i]});
  127. for $p (@packages) {
  128. scanshlibsfile("$shlibsppdir/$p$shlibsppext",
  129. $libname[$i],$libsoname[$i],$libf[$i])
  130. && next LIB;
  131. }
  132. }
  133. scanshlibsfile($shlibsdefault,$libname[$i],$libsoname[$i],$libf[$i]) && next;
  134. &warn("unable to find dependency information for ".
  135. "shared library $libname[$i] (soname $libsoname[$i], path $libpath[$i], ".
  136. "dependency field $libf[$i])");
  137. }
  138. sub scanshlibsfile {
  139. my ($fn,$ln,$lsn,$lf) = @_;
  140. my ($da,$dv,$dk);
  141. $fn= "./$fn" if $fn =~ m/^\s/;
  142. if (!open(SLF,"< $fn")) {
  143. $! == ENOENT || syserr("unable to open shared libs info file \`$fn'");
  144. #print STDERR "scanshlibsfile($fn,$ln,$lsn,$lf) ... ENOENT\n";
  145. return 0;
  146. }
  147. #print STDERR "scanshlibsfile($fn,$ln,$lsn,$lf) ...\n";
  148. while (<SLF>) {
  149. s/\s*\n$//; next if m/^\#/;
  150. if (!m/^\s*(\S+)\s+(\S+)/) {
  151. &warn("shared libs info file \`$fn' line $.: bad line \`$_'");
  152. next;
  153. }
  154. next if $1 ne $ln || $2 ne $lsn;
  155. $da= $';
  156. for $dv (split(/,/,$da)) {
  157. $dv =~ s/^\s+//; $dv =~ s/\s+$//;
  158. if (defined($depstrength{$lf})) {
  159. if (!defined($predefdepfdep{$dv}) ||
  160. $depstrength{$predefdepfdep{$dv}} < $depstrength{$lf}) {
  161. $predefdepfdep{$dv}= $lf;
  162. }
  163. } else {
  164. $dk= "$lf: $dv";
  165. if (!defined($unkdepfdone{$dk})) {
  166. $unkdepfdone{$dk}= 1;
  167. $unkdepf{$lf}.= ', ' if length($unkdepf{$lf});
  168. $unkdepf{$lf}.= $dv;
  169. }
  170. }
  171. }
  172. return 1;
  173. }
  174. close(SLF);
  175. return 0;
  176. }
  177. if (!$stdout) {
  178. $varlistfile="./$varlistfile" if $fileslistfile =~ m/^\s/;
  179. open(Y,"> $varlistfile.new") ||
  180. syserr("open new substvars file \`$varlistfile.new'");
  181. chown(@fowner, "$varlistfile.new") ||
  182. syserr("chown of \`$varlistfile.new'");
  183. if (open(X,"< $varlistfile")) {
  184. while (<X>) {
  185. s/\n$//;
  186. next if m/^(\w[-:0-9A-Za-z]*):/ && $1 eq $varnameprefix;
  187. print(Y "$_\n") ||
  188. syserr("copy old entry to new varlist file \`$varlistfile.new'");
  189. }
  190. } elsif ($! != ENOENT) {
  191. syserr("open old varlist file \`$varlistfile' for reading");
  192. }
  193. $fh= 'Y';
  194. } else {
  195. $fh= 'STDOUT';
  196. }
  197. for $dv (sort keys %predefdepfdep) {
  198. $lf= $predefdepfdep{$dv};
  199. $defdepf{$lf}.= ', ' if length($defdepf{$lf});
  200. $defdepf{$lf}.= $dv;
  201. }
  202. for $lf (reverse @depfields) {
  203. next unless defined($defdepf{$lf});
  204. print($fh "$varnameprefix:$lf=$defdepf{$lf}\n")
  205. || syserr("write output entry");
  206. }
  207. for $lf (sort keys %unkdepf) {
  208. print($fh "$varnameprefix:$lf=$unkdepf{$lf}\n")
  209. || syserr("write userdef output entry");
  210. }
  211. close($fh) || syserr("close output");
  212. if (!$stdout) {
  213. rename("$varlistfile.new",$varlistfile) ||
  214. syserr("install new varlist file \`$varlistfile'");
  215. }