pullup 3232
[pkgsrcv2.git] / mk / find-prefix.mk
1 # $NetBSD: find-prefix.mk,v 1.6 2008/02/10 10:12:13 rillig Exp $
2 #
3 # This is a "subroutine" that can be included to find the installation
4 # prefix of an installed package.
5 #
6 # Parameters:
7 #
8 # FIND_PREFIX
9 #       A list of <VARNAME>=<pattern> pairs, where
10 #
11 #       * <pattern> is a package wildcard pattern (see pkg_info(8)),
12 #       * <VARNAME> is the variable that will be set to the
13 #         installation prefix for the package.
14 #
15 # Returns:
16 #
17 # <VARNAME>
18 #       The prefix of the corresponding package, or ${LOCALBASE}
19 #       if the package is not installed.
20 #
21 # Example:
22 #
23 # FIND_PREFIX:= M4DIR=gm4
24 # .include "../../mk/find-prefix.mk"
25 # # ${M4DIR} now contains the installation prefix for the "gm4" package.
26 #
27
28 .for i in ${FIND_PREFIX}
29 .  for varname in ${i:C/=.*$//}
30 .    for pattern in ${i:C/^[^=]*=//}
31 .      if !defined(${varname})
32
33 # XXX: Is this *_DEFAULT variable really necessary? The default value
34 # can be easily embedded in the shell code. What if VARNAME is set to
35 # PKG_APACHE, for example?
36 #
37 ${varname}_DEFAULT?=    ${LOCALBASE}
38 _${varname}_cmd=                \
39         ${PKG_INFO} -qp ${pattern:Q} 2>/dev/null |                      \
40         { read cmd arg;                                                 \
41           case "$$arg" in                                               \
42           "")   ${ECHO} ${${varname}_DEFAULT:Q} ;;                      \
43           *)    ${ECHO} "$$arg" ;;                                      \
44           esac; }
45 ${varname}=             ${_${varname}_cmd:sh}
46 .      endif
47 MAKEVARS+=      ${varname}
48 .    endfor
49 .  endfor
50 .endfor