cond-func.mk revision 1.10 1 1.10 rillig # $NetBSD: cond-func.mk,v 1.10 2021/12/28 22:13:56 rillig Exp $
2 1.1 rillig #
3 1.1 rillig # Tests for those parts of the functions in .if conditions that are common
4 1.1 rillig # among several functions.
5 1.1 rillig #
6 1.1 rillig # The below test uses the function defined(...) since it has no side-effects,
7 1.9 rillig # the other functions (except empty(...)) would work equally well. The
8 1.9 rillig # function empty is special because it uses a different parsing algorithm for
9 1.9 rillig # its argument.
10 1.1 rillig
11 1.1 rillig DEF= defined
12 1.1 rillig ${:UA B}= variable name with spaces
13 1.1 rillig ${:UVAR(value)}= variable name with parentheses
14 1.6 rillig ${:UVAR{value}}= variable name with balanced braces
15 1.6 rillig
16 1.6 rillig # Really strange variable names must be given indirectly via another variable,
17 1.6 rillig # so that no unbalanced braces appear in the top-level expression.
18 1.6 rillig VARNAME_UNBALANCED_BRACES= VAR{{{value
19 1.6 rillig ${VARNAME_UNBALANCED_BRACES}= variable name with unbalanced braces
20 1.1 rillig
21 1.1 rillig .if !defined(DEF)
22 1.4 rillig . error
23 1.1 rillig .endif
24 1.1 rillig
25 1.3 rillig # Horizontal whitespace (space tab) after the opening parenthesis is ignored.
26 1.1 rillig .if !defined( DEF)
27 1.4 rillig . error
28 1.1 rillig .endif
29 1.1 rillig
30 1.3 rillig # Horizontal whitespace (space tab) before the closing parenthesis is ignored.
31 1.1 rillig .if !defined(DEF )
32 1.4 rillig . error
33 1.1 rillig .endif
34 1.1 rillig
35 1.1 rillig # The argument of a function must not directly contain whitespace.
36 1.1 rillig .if !defined(A B)
37 1.4 rillig . error
38 1.1 rillig .endif
39 1.1 rillig
40 1.1 rillig # If necessary, the whitespace can be generated by a variable expression.
41 1.1 rillig .if !defined(${:UA B})
42 1.4 rillig . error
43 1.1 rillig .endif
44 1.1 rillig
45 1.1 rillig # Characters that could be mistaken for operators must not appear directly
46 1.1 rillig # in a function argument. As with whitespace, these can be generated
47 1.1 rillig # indirectly.
48 1.1 rillig #
49 1.1 rillig # It's not entirely clear why these characters are forbidden.
50 1.1 rillig # The most plausible reason seems to be typo detection.
51 1.1 rillig .if !defined(A&B)
52 1.4 rillig . error
53 1.1 rillig .endif
54 1.1 rillig .if !defined(A|B)
55 1.4 rillig . error
56 1.1 rillig .endif
57 1.1 rillig
58 1.1 rillig # Even parentheses may appear in variable names.
59 1.1 rillig # They must be balanced though.
60 1.1 rillig .if !defined(VAR(value))
61 1.4 rillig . error
62 1.1 rillig .endif
63 1.1 rillig
64 1.1 rillig # Braces do not have any special meaning when parsing arguments.
65 1.1 rillig .if !defined(VAR{value})
66 1.4 rillig . error
67 1.1 rillig .endif
68 1.1 rillig
69 1.6 rillig # Braces do not have any special meaning when parsing arguments.
70 1.6 rillig # They don't need to be balanced.
71 1.6 rillig .if !defined(VAR{{{value)
72 1.6 rillig . error
73 1.6 rillig .endif
74 1.6 rillig
75 1.2 rillig # There may be spaces around the operators and parentheses, and even
76 1.2 rillig # inside the parentheses. The spaces inside the parentheses are not
77 1.2 rillig # allowed for the empty() function (see cond-func-empty.mk), therefore
78 1.2 rillig # they are typically omitted for the other functions as well.
79 1.2 rillig .if ! defined ( DEF )
80 1.2 rillig . error
81 1.2 rillig .endif
82 1.2 rillig
83 1.5 rillig # The following condition is interpreted as defined(A) && defined(B).
84 1.5 rillig # In lack of a function call expression, each kind of .if directive has a
85 1.5 rillig # default function that is called when a bare word is parsed. For the plain
86 1.5 rillig # .if directive, this function is defined(); see "struct If ifs" in cond.c.
87 1.5 rillig .if A&B
88 1.5 rillig . error
89 1.5 rillig .endif
90 1.5 rillig
91 1.7 rillig .if defined()
92 1.7 rillig . error
93 1.7 rillig .else
94 1.7 rillig . info The empty variable is never defined.
95 1.7 rillig .endif
96 1.7 rillig
97 1.7 rillig # The plain word 'defined' is interpreted as '!empty(defined)'.
98 1.7 rillig # That variable is not defined (yet).
99 1.7 rillig .if defined
100 1.7 rillig . error
101 1.7 rillig .else
102 1.7 rillig . info A plain function name is parsed as !empty(...).
103 1.7 rillig .endif
104 1.7 rillig
105 1.10 rillig # If a variable named 'defined' is actually defined, the bare word 'defined'
106 1.10 rillig # is interpreted as 'defined(defined)', and the condition evaluates to true.
107 1.10 rillig defined= # defined but empty
108 1.7 rillig .if defined
109 1.7 rillig . info A plain function name is parsed as !empty(...).
110 1.7 rillig .else
111 1.7 rillig . error
112 1.7 rillig .endif
113 1.7 rillig
114 1.7 rillig # A plain symbol name may start with one of the function names, in this case
115 1.7 rillig # 'defined'.
116 1.7 rillig .if defined-var
117 1.7 rillig . error
118 1.7 rillig .else
119 1.7 rillig . info Symbols may start with a function name.
120 1.7 rillig .endif
121 1.7 rillig
122 1.10 rillig defined-var= # defined but empty
123 1.7 rillig .if defined-var
124 1.7 rillig . info Symbols may start with a function name.
125 1.7 rillig .else
126 1.7 rillig . error
127 1.7 rillig .endif
128 1.7 rillig
129 1.8 rillig # Missing closing parenthesis when parsing the function argument.
130 1.8 rillig .if defined(
131 1.8 rillig . error
132 1.8 rillig .else
133 1.8 rillig . error
134 1.8 rillig .endif
135