Fixup fromcvs/togit conversion
[minix-pkgsrc.git] / mk / pkgformat / pkg / reduce-depends.awk
blob88a18b36f1b1ee3085800fdcf647ff2a0501f7c9
1 #!/usr/bin/awk -f
3 # $NetBSD: reduce-depends.awk,v 1.1 2011/10/15 00:23:09 reed Exp $
5 # Copyright (c) 2006 The NetBSD Foundation, Inc.
6 # All rights reserved.
8 # This code is derived from software contributed to The NetBSD Foundation
9 # by Johnny C. Lam.
11 # Redistribution and use in source and binary forms, with or without
12 # modification, are permitted provided that the following conditions
13 # are met:
14 # 1. Redistributions of source code must retain the above copyright
15 # notice, this list of conditions and the following disclaimer.
16 # 2. Redistributions in binary form must reproduce the above copyright
17 # notice, this list of conditions and the following disclaimer in the
18 # documentation and/or other materials provided with the distribution.
19 # 3. All advertising materials mentioning features or use of this software
20 # must display the following acknowledgement:
21 # This product includes software developed by the NetBSD
22 # Foundation, Inc. and its contributors.
23 # 4. Neither the name of The NetBSD Foundation nor the names of its
24 # contributors may be used to endorse or promote products derived
25 # from this software without specific prior written permission.
27 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 # POSSIBILITY OF SUCH DAMAGE.
40 ######################################################################
42 # NAME
43 # reduce-depends.awk -- reduce a list of dependencies
45 # SYNOPSIS
46 # reduce-depends.awk "depends_list"
48 # DESCRIPTION
49 # reduce-depends.awk removes some extraneous dependencies from the
50 # dependency list. The dependency list should be passed as a single
51 # argument, and the output will be a list of the reduced dependencies,
52 # echo one on a new line.
54 # ENVIRONMENT
55 # CAT
56 # PKG_ADMIN
57 # PWD_CMD
58 # TEST
60 ######################################################################
62 BEGIN {
63 CAT = ENVIRON["CAT"] ? ENVIRON["CAT"] : "cat"
64 PKG_ADMIN = ENVIRON["PKG_ADMIN"] ? ENVIRON["PKG_ADMIN"] : "pkg_admin"
65 PWD_CMD = ENVIRON["PWD_CMD"] ? ENVIRON["PWD_CMD"] : "pwd -P"
66 TEST = ENVIRON["TEST"] ? ENVIRON["TEST"] : "test"
68 PROGNAME = "reduce-depends.awk"
69 ERRCAT = CAT " 1>&2"
71 # Gather all dependencies into the depends array. Index 0 of the
72 # depends[pkgpath] array is the number of patterns associated with
73 # that pkgpath.
75 args = ARGV[1]
76 ARGC = split(args, ARGV); ARGC++
77 for (i = 1; i < ARGC; i++) {
78 pattern = ARGV[i]; sub(":.*", "", pattern)
79 dir = ARGV[i]; sub(".*:", "", dir)
80 if (pattern ":" dir != ARGV[i]) {
81 print "ERROR: [" PROGNAME "] invalid dependency pattern: " ARGV[i] | ERRCAT
82 exit 1
84 cmd = TEST " -d " dir
85 if (system(cmd) == 0) {
86 cmd = "cd " dir " && " PWD_CMD
87 while (cmd | getline pkgpath)
88 if (!(pkgpath in pkgsrcdirs)) {
89 pkgpaths[P++] = pkgpath
90 pkgsrcdirs[pkgpath] = dir
92 depends[pkgpath, 0]++;
93 depends[pkgpath, depends[pkgpath, 0]] = pattern
94 close(cmd)
95 } else {
96 print "ERROR: [" PROGNAME "] " dir " does not exist." | ERRCAT
97 exit 1
101 # Reduce dependencies to the strictest set of dependencies it
102 # can derive from all of depends[...]. It only understands
103 # dependencies of the form foo>=1.0, and leaves the other
104 # dependencies undisturbed.
106 # The algorithm takes dependencies of the form foo>=1.0 and
107 # converts them to foo-1.0. It then compares this pkg name against
108 # each dependency to see if it satisfies them all. The key fact
109 # is the the strictest dependency, when converted to a pkg name,
110 # will satisfy every dependency.
112 for (p = 0; p < P; p++) {
113 pkgpath = pkgpaths[p]
114 D = depends[pkgpath, 0];
115 match_all = 1;
116 for (d = 1; d <= D; d++) {
117 dep = depends[pkgpath, d]
118 if (dep ~ /[{]/ || \
119 dep ~ />=[0-9][0-9\.]*(nb[0-9]+)?<[0-9]+/ || \
120 dep !~ />=[0-9]+/)
122 reduced[N++] = dep ":" pkgsrcdirs[pkgpath]
123 continue
125 ge_depends[dep] = dep
127 for (dep in ge_depends) {
128 dep2pkg = dep; sub(">=", "-", dep2pkg)
129 match_all = 1
130 for (pattern in ge_depends) {
131 cmd = PKG_ADMIN " pmatch \"" pattern "\" " dep2pkg
132 if (system(cmd) != 0) {
133 match_all = 0
134 break
137 if (match_all == 0) continue
138 reduced[N++] = dep ":" pkgsrcdirs[pkgpath]
139 break
142 # If there are conflicting dependencies, then just pass them
143 # through and let the rest of the pkgsrc machinery handle it.
145 if (match_all == 0) {
146 for (d = 1; d <= D; d++) {
147 dep = depends[pkgpath, d]
148 reduced[N++] = dep ":" pkgsrcdirs[pkgpath]
151 for (dep in ge_depends)
152 delete ge_depends[dep]
155 # Output reduced dependencies.
156 for (n = 0; n < N; n++)
157 print reduced[n];