check-indirect-dependencies 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/perl
  2. # $OpenBSD: check-indirect-dependencies,v 1.4 2007/01/16 18:15:53 sturm Exp $
  3. # Copyright (c) 2006 Nikolay Sturm <sturm@openbsd.org>.
  4. #
  5. # Redistribution and use in source and binary forms, with or without
  6. # modification, are permitted provided that the following conditions
  7. # are met:
  8. # 1. Redistributions of source code must retain the above copyright
  9. # notice, this list of conditions and the following disclaimer.
  10. # 2. Redistributions in binary form must reproduce the above copyright
  11. # notice, this list of conditions and the following disclaimer in the
  12. # documentation and/or other materials provided with the distribution.
  13. #
  14. # THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
  15. # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
  18. # PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. # reduce the amount of direct dependencies by pointing out those which
  26. # are pulled in as indirect dependencies
  27. use strict;
  28. use warnings;
  29. use Cwd;
  30. my $cwd = getcwd;
  31. my $mainport;
  32. my $make = $ENV{MAKE} || "make";
  33. my $portsdir = $ENV{PORTSDIR} || "/usr/ports";
  34. sub check_build_deps()
  35. {
  36. my %directdeps;
  37. my %indirectdeps;
  38. print "===> Checking build dependencies\n";
  39. open(my $builddirdepends, "-|", "$make build-dir-depends") or
  40. die "Cannot execute '$make build-dir-depends': $!\n";
  41. while (<$builddirdepends>) {
  42. chomp;
  43. my ($port, $builddep) = split;
  44. my @dirstack;
  45. $mainport = $port if not defined $mainport;
  46. next if $port ne $mainport;
  47. chdir("$portsdir/$builddep") or
  48. die "Cannot chdir to $portsdir/$builddep\n";
  49. open(my $rundirdepends, "-|", "$make run-dir-depends") or
  50. die "Cannot execute '$make run-dir-depends': $!\n";
  51. while (<$rundirdepends>) {
  52. chomp;
  53. my ($innerport, $rundep) = split;
  54. next if $innerport eq $rundep;
  55. if (not @dirstack) {
  56. push @dirstack, $mainport, $innerport, $rundep;
  57. } elsif ($dirstack[-1] eq $innerport) {
  58. push @dirstack, $rundep;
  59. } else {
  60. while ($dirstack[-1] ne $innerport) {
  61. pop @dirstack;
  62. }
  63. push @dirstack, $rundep;
  64. }
  65. if (exists $directdeps{$rundep}) {
  66. print "probably unneeded dependency: $rundep\n";
  67. print join(' -> ', @dirstack) . "\n\n";
  68. }
  69. $indirectdeps{$rundep} = [ @dirstack ];
  70. }
  71. close $rundirdepends;
  72. if (exists $indirectdeps{$builddep}) {
  73. print "probably unneeded dependency: $builddep\n";
  74. print join(' -> ', @{$indirectdeps{$builddep}});
  75. print "\n\n";
  76. next;
  77. }
  78. $directdeps{$builddep} = 1;
  79. }
  80. close $builddirdepends;
  81. chdir($cwd) or die "Cannot chdir to $cwd\n";
  82. }
  83. sub check_run_deps()
  84. {
  85. my %directdeps;
  86. my %indirectdeps;
  87. my @dirstack;
  88. print "===> Checking run dependencies\n";
  89. open(my $rundirdepends, "-|", "$make run-dir-depends") or
  90. die "Cannot execute '$make run-dir-depends': $!\n";
  91. while (<$rundirdepends>) {
  92. chomp;
  93. my ($port, $rundep) = split;
  94. $mainport = $port if not defined $mainport;
  95. next if $port eq $rundep;
  96. if (not @dirstack) {
  97. push @dirstack, $port, $rundep;
  98. } elsif ($dirstack[-1] eq $port) {
  99. push @dirstack, $rundep;
  100. } else {
  101. while ($dirstack[-1] ne $port) {
  102. pop @dirstack;
  103. }
  104. push @dirstack, $rundep;
  105. }
  106. if ($port eq $mainport) {
  107. if (exists $indirectdeps{$rundep}) {
  108. print "probably unneeded dependency: $rundep\n";
  109. print join(' -> ', @{$indirectdeps{$rundep}});
  110. print "\n\n";
  111. next;
  112. }
  113. $directdeps{$rundep} = 1;
  114. } else {
  115. if (exists $directdeps{$rundep}) {
  116. print "probably unneeded dependency: $rundep\n";
  117. print join(' -> ', @dirstack) . "\n\n";
  118. }
  119. $indirectdeps{$rundep} = [ @dirstack ];
  120. }
  121. }
  122. close $rundirdepends;
  123. }
  124. check_build_deps();
  125. check_run_deps();