pullup 3232
[pkgsrcv2.git] / mk / bulk / printdepends
1 #!/bin/sh
2 # $NetBSD: printdepends,v 1.24 2007/01/08 21:36:07 rillig Exp $
3 #
4
5 # Copyright (c) 1999, 2000 Hubert Feyrer <hubertf@NetBSD.org>
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 # 1. Redistributions of source code must retain the above copyright
12 #    notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 #    notice, this list of conditions and the following disclaimer in the
15 #    documentation and/or other materials provided with the distribution.
16 # 3. All advertising materials mentioning features or use of this software
17 #    must display the following acknowledgement:
18 #      This product includes software developed by Hubert Feyrer for
19 #       the NetBSD Project.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #
32
33 # usage: printdepends [brokenfile [bulkfilesdir]]
34 #
35 # Generates a list of package dependencies suitable for tsort(1) on
36 # stdout. Each line of the output has two fields: the package directory
37 # of the dependency and the package directory of the depending package
38 # (both in the form category/package).
39 #
40 # If <brokenfile> is given, package-specific errors are logged to
41 # <bulkfilesdir>/${pkgdir}/<brokenfile>. Otherwise no additional logging
42 # takes place.
43 #
44 # The default for <bulkfilesdir> is the pkgsrc directory itself. If
45 # <bulkfilesdir> differs from the pkgsrc directory, all directories
46 # that are needed for the log files are created automatically.
47 #
48 # Note: printdepends must be called from a pkgsrc root directory.
49
50 set -e
51
52 # Pull in PKGLIST
53 if [ -f "$BULK_BUILD_CONF" ]; then
54         . $BULK_BUILD_CONF
55 else
56         . `dirname $0`/build.conf
57 fi
58
59 #
60 # Global variables
61 #
62
63 # The brokenfile_flag variable tells whether we want package-specific
64 # log files at all. If it is set to "yes", the mkdirs_flag
65 # variable tells whether the directories of the package-specific log
66 # files are created if necessary.
67 brokenfile_flag="yes"
68 mkdirs_flag="no"
69 pkgsrcdir="${PWD}"
70 bulkfilesdir="${pkgsrcdir}"
71 brokenfile=""
72
73 #
74 # Command line parsing
75 #
76
77 case $# in
78 0)      brokenfile_flag="no"
79         ;;
80 1)      brokenfile="$1"
81         ;;
82 2)      brokenfile="$1"
83         bulkfilesdir="$2"
84         if [ "${bulkfilesdir}" != "${pkgsrcdir}" ]; then
85                 mkdirs_flag="yes"
86         fi
87         ;;
88 *)      echo "usage: $0 [brokenfile [bulkfilesdir]]" 1>&2
89         exit 1
90         ;;
91 esac
92
93 #
94 # Sanity checks
95 #
96
97 case ${BMAKE-""} in
98 "")     echo "$0: error: BMAKE must be set and non-empty." 1>&2
99         exit 1;;
100 esac
101 case ${bulkfilesdir} in
102 /*)     ;;
103 *)      echo "$0: error: The <bulkfilesdir> argument must be absolute." 1>&2
104         exit 1;;
105 esac
106 case ${brokenfile} in
107 */*)    echo "$0: error: The <brokenfile> argument must not contain a slash." 1>&2
108         exit 1;;
109 esac
110
111 #
112 # Get additional system information
113 #
114
115 cd "${pkgsrcdir}/pkgtools/lintpkgsrc"
116 GREP=`${BMAKE} show-var VARNAME=GREP USE_TOOLS=grep`
117 MKDIR=`${BMAKE} show-var VARNAME=MKDIR USE_TOOLS=mkdir`
118 SED=`${BMAKE} show-var VARNAME=SED USE_TOOLS=sed`
119 cd "${pkgsrcdir}"
120
121 case $mkdirs_flag in
122 yes)    mkbulkdir="${MKDIR}";;
123 *)      mkbulkdir=":";;
124 esac
125
126 case ${PKGLIST} in
127 "")     # List of all packages, from pkgsrc/*/Makefile
128         list=`${GREP} '^SUBDIR+=' */Makefile | ${GREP} -v '^regress/' | ${SED} -e 's,/Makefile:SUBDIR+=[[:space:]]*,/,' -e 's,#.*,,'`
129         ;;
130 *)      list="${PKGLIST}"
131         ;;
132 esac
133
134 for pkgdir in $list; do
135         if cd "${pkgsrcdir}/${pkgdir}"; then
136                 if deps=`${BMAKE} show-depends-dirs`; then
137                         case ${deps} in
138                         "")     # Make the package depend on itself.
139                                 # Otherwise it would not show up in the
140                                 # dependency graph.
141                                 echo "${pkgdir} ${pkgdir}";;
142                         *)      for dep in $deps; do
143                                         echo "${dep} ${pkgdir}"
144                                 done;;
145                         esac
146                 else
147                         echo "$0: error: could not extract dependencies for ${pkgdir} -- skipping." 1>&2
148
149                         [ $brokenfile_flag = yes ] || continue
150
151                         ${mkbulkdir} "${bulkfilesdir}/${pkgdir}"
152                         { echo "[printdepends] command failed: ${BMAKE} show-depends-dirs"
153                           ( ${BMAKE} show-depends-dirs
154                           ) || true
155                         } >> "${bulkfilesdir}/${pkgdir}/${brokenfile}" 2>&1
156                 fi
157         else
158                 ${mkbulkdir} "${bulkfilesdir}"
159                 { echo "[printdepends] command failed: cd ${pkgsrcdir}/${pkgdir}"
160                   ( cd "${pkgsrcdir}/${pkgdir}"
161                   ) || true
162                 } 1>&2
163         fi
164 done