directive-include-guard.mk revision 1.5 1 1.5 rillig # $NetBSD: directive-include-guard.mk,v 1.5 2023/06/19 12:53:57 rillig Exp $
2 1.1 rillig #
3 1.1 rillig # Tests for multiple-inclusion guards in makefiles.
4 1.1 rillig #
5 1.1 rillig # A file that is guarded by a multiple-inclusion guard has the following form:
6 1.1 rillig #
7 1.1 rillig # .ifndef GUARD_NAME
8 1.5 rillig # ...
9 1.5 rillig # GUARD_NAME= # any value, may also be empty
10 1.1 rillig # ...
11 1.1 rillig # .endif
12 1.1 rillig #
13 1.5 rillig # When such a file is included later and the guard variable is set, including
14 1.5 rillig # the file has no effect, as all its content is skipped.
15 1.1 rillig #
16 1.2 rillig # See also:
17 1.2 rillig # https://gcc.gnu.org/onlinedocs/cppinternals/Guard-Macros.html
18 1.1 rillig
19 1.1 rillig
20 1.1 rillig # This is the canonical form of a multiple-inclusion guard.
21 1.1 rillig INCS+= guarded-ifndef
22 1.1 rillig LINES.guarded-ifndef= \
23 1.1 rillig '.ifndef GUARDED_IFNDEF' \
24 1.1 rillig 'GUARDED_IFNDEF=' \
25 1.1 rillig '.endif'
26 1.4 rillig # expect: Parse_PushInput: file guarded-ifndef.tmp, line 1
27 1.5 rillig # expect: Skipping 'guarded-ifndef.tmp' because 'GUARDED_IFNDEF' is already set
28 1.1 rillig
29 1.1 rillig # Comments and empty lines have no influence on the multiple-inclusion guard.
30 1.1 rillig INCS+= comments
31 1.1 rillig LINES.comments= \
32 1.1 rillig '\# comment' \
33 1.1 rillig '' \
34 1.3 rillig '.ifndef COMMENTS' \
35 1.1 rillig '\# comment' \
36 1.3 rillig 'COMMENTS=\#comment' \
37 1.1 rillig '.endif' \
38 1.1 rillig '\# comment'
39 1.4 rillig # expect: Parse_PushInput: file comments.tmp, line 1
40 1.5 rillig # expect: Skipping 'comments.tmp' because 'COMMENTS' is already set
41 1.1 rillig
42 1.1 rillig # An alternative form uses the 'defined' function. It is more verbose than
43 1.1 rillig # the canonical form. There are other possible forms as well, such as with a
44 1.1 rillig # triple negation, but these are not recognized as they are not common.
45 1.1 rillig INCS+= guarded-if
46 1.1 rillig LINES.guarded-if= \
47 1.1 rillig '.if !defined(GUARDED_IF)' \
48 1.1 rillig 'GUARDED_IF=' \
49 1.1 rillig '.endif'
50 1.4 rillig # expect: Parse_PushInput: file guarded-if.tmp, line 1
51 1.5 rillig # expect: Skipping 'guarded-if.tmp' because 'GUARDED_IF' is already set
52 1.1 rillig
53 1.1 rillig # Triple negation is so uncommon that it's not recognized.
54 1.1 rillig INCS+= triple-negation
55 1.1 rillig LINES.triple-negation= \
56 1.1 rillig '.if !!!defined(TRIPLE_NEGATION)' \
57 1.1 rillig 'TRIPLE_NEGATION=' \
58 1.1 rillig '.endif'
59 1.4 rillig # expect: Parse_PushInput: file triple-negation.tmp, line 1
60 1.4 rillig # expect: Parse_PushInput: file triple-negation.tmp, line 1
61 1.1 rillig
62 1.1 rillig # The variable names in the '.if' and the assignment must be the same.
63 1.1 rillig INCS+= varname-mismatch
64 1.1 rillig LINES.varname-mismatch= \
65 1.1 rillig '.ifndef VARNAME_MISMATCH' \
66 1.1 rillig 'OTHER_NAME=' \
67 1.1 rillig '.endif'
68 1.4 rillig # expect: Parse_PushInput: file varname-mismatch.tmp, line 1
69 1.4 rillig # expect: Parse_PushInput: file varname-mismatch.tmp, line 1
70 1.1 rillig
71 1.5 rillig # The variable name in the guard condition must only contain alphanumeric
72 1.5 rillig # characters and underscores. The guard variable is more flexible, it can be
73 1.5 rillig # set anywhere, as long as it is set when the guarded file is included next.
74 1.1 rillig INCS+= varname-indirect
75 1.1 rillig LINES.varname-indirect= \
76 1.1 rillig '.ifndef VARNAME_INDIRECT' \
77 1.1 rillig 'VARNAME_$${:UINDIRECT}=' \
78 1.1 rillig '.endif'
79 1.4 rillig # expect: Parse_PushInput: file varname-indirect.tmp, line 1
80 1.5 rillig # expect: Skipping 'varname-indirect.tmp' because 'VARNAME_INDIRECT' is already set
81 1.1 rillig
82 1.5 rillig # The time at which the guard variable is set doesn't matter, as long as it is
83 1.5 rillig # set when the file is included the next time.
84 1.1 rillig INCS+= late-assignment
85 1.1 rillig LINES.late-assignment= \
86 1.1 rillig '.ifndef LATE_ASSIGNMENT' \
87 1.1 rillig 'OTHER=' \
88 1.1 rillig 'LATE_ASSIGNMENT=' \
89 1.1 rillig '.endif'
90 1.4 rillig # expect: Parse_PushInput: file late-assignment.tmp, line 1
91 1.5 rillig # expect: Skipping 'late-assignment.tmp' because 'LATE_ASSIGNMENT' is already set
92 1.1 rillig
93 1.5 rillig # The time at which the guard variable is set doesn't matter, as long as it is
94 1.5 rillig # set when the file is included the next time.
95 1.1 rillig INCS+= two-conditions
96 1.1 rillig LINES.two-conditions= \
97 1.1 rillig '.ifndef TWO_CONDITIONS' \
98 1.2 rillig '. if 1' \
99 1.1 rillig 'TWO_CONDITIONS=' \
100 1.1 rillig '. endif' \
101 1.1 rillig '.endif'
102 1.4 rillig # expect: Parse_PushInput: file two-conditions.tmp, line 1
103 1.5 rillig # expect: Skipping 'two-conditions.tmp' because 'TWO_CONDITIONS' is already set
104 1.1 rillig
105 1.1 rillig # If the guard variable is already set before the file is included for the
106 1.5 rillig # first time, the file is not considered guarded, as the makefile parser skips
107 1.5 rillig # all lines in the inactive part between the '.ifndef' and the '.endif'.
108 1.1 rillig INCS+= already-set
109 1.1 rillig LINES.already-set= \
110 1.1 rillig '.ifndef ALREADY_SET' \
111 1.1 rillig 'ALREADY_SET=' \
112 1.1 rillig '.endif'
113 1.1 rillig ALREADY_SET=
114 1.4 rillig # expect: Parse_PushInput: file already-set.tmp, line 1
115 1.4 rillig # expect: Parse_PushInput: file already-set.tmp, line 1
116 1.1 rillig
117 1.1 rillig # The whole file content must be guarded by a single '.if' conditional, not by
118 1.1 rillig # several, even if they have the same effect.
119 1.1 rillig INCS+= twice
120 1.1 rillig LINES.twice= \
121 1.5 rillig '.ifndef TWICE_FIRST' \
122 1.5 rillig 'TWICE_FIRST=' \
123 1.1 rillig '.endif' \
124 1.5 rillig '.ifndef TWICE_SECOND' \
125 1.5 rillig 'TWICE_SECOND=' \
126 1.1 rillig '.endif'
127 1.4 rillig # expect: Parse_PushInput: file twice.tmp, line 1
128 1.4 rillig # expect: Parse_PushInput: file twice.tmp, line 1
129 1.1 rillig
130 1.1 rillig # When multiple files use the same guard variable name, they exclude each
131 1.5 rillig # other. It's the responsibility of the makefile authors to choose unique
132 1.5 rillig # variable names. Typical choices are ${PROJECT}_${DIR}_${FILE}_MK. This is
133 1.5 rillig # the same situation as in the 'already-set' test, and the file is not
134 1.5 rillig # considered guarded.
135 1.1 rillig INCS+= reuse
136 1.1 rillig LINES.reuse= \
137 1.1 rillig ${LINES.guarded-if}
138 1.4 rillig # expect: Parse_PushInput: file reuse.tmp, line 1
139 1.4 rillig # expect: Parse_PushInput: file reuse.tmp, line 1
140 1.1 rillig
141 1.1 rillig # The conditional must come before the assignment, otherwise the conditional
142 1.1 rillig # is useless, as it always evaluates to false.
143 1.1 rillig INCS+= swapped
144 1.1 rillig LINES.swapped= \
145 1.1 rillig 'SWAPPED=' \
146 1.1 rillig '.ifndef SWAPPED' \
147 1.1 rillig '.endif'
148 1.4 rillig # expect: Parse_PushInput: file swapped.tmp, line 1
149 1.4 rillig # expect: Parse_PushInput: file swapped.tmp, line 1
150 1.1 rillig
151 1.2 rillig # If the guard variable is undefined at some later point, the guarded file is
152 1.2 rillig # included again.
153 1.2 rillig INCS+= undef-between
154 1.2 rillig LINES.undef-between= \
155 1.2 rillig '.ifndef UNDEF_BETWEEN' \
156 1.2 rillig 'UNDEF_BETWEEN=' \
157 1.2 rillig '.endif'
158 1.4 rillig # expect: Parse_PushInput: file undef-between.tmp, line 1
159 1.4 rillig # expect: Parse_PushInput: file undef-between.tmp, line 1
160 1.2 rillig
161 1.2 rillig # If the guarded file undefines the guard variable, the guarded file is
162 1.2 rillig # included again.
163 1.2 rillig INCS+= undef-inside
164 1.2 rillig LINES.undef-inside= \
165 1.2 rillig '.ifndef UNDEF_INSIDE' \
166 1.2 rillig 'UNDEF_INSIDE=' \
167 1.2 rillig '.undef UNDEF_INSIDE' \
168 1.2 rillig '.endif'
169 1.4 rillig # expect: Parse_PushInput: file undef-inside.tmp, line 1
170 1.4 rillig # expect: Parse_PushInput: file undef-inside.tmp, line 1
171 1.2 rillig
172 1.2 rillig # The outermost '.if' must not have an '.elif' branch.
173 1.2 rillig INCS+= if-elif
174 1.2 rillig LINES.if-elif = \
175 1.2 rillig '.ifndef IF_ELIF' \
176 1.2 rillig 'IF_ELIF=' \
177 1.2 rillig '.elif 1' \
178 1.2 rillig '.endif'
179 1.4 rillig # expect: Parse_PushInput: file if-elif.tmp, line 1
180 1.4 rillig # expect: Parse_PushInput: file if-elif.tmp, line 1
181 1.2 rillig
182 1.2 rillig # The outermost '.if' must not have an '.else' branch.
183 1.2 rillig INCS+= if-else
184 1.2 rillig LINES.if-else = \
185 1.2 rillig '.ifndef IF_ELSE' \
186 1.2 rillig 'IF_ELSE=' \
187 1.2 rillig '.else' \
188 1.2 rillig '.endif'
189 1.4 rillig # expect: Parse_PushInput: file if-else.tmp, line 1
190 1.4 rillig # expect: Parse_PushInput: file if-else.tmp, line 1
191 1.2 rillig
192 1.2 rillig # The inner '.if' directives may have an '.elif' or '.else'.
193 1.2 rillig INCS+= inner-if-elif-else
194 1.2 rillig LINES.inner-if-elif-else = \
195 1.2 rillig '.ifndef INNER_IF_ELIF_ELSE' \
196 1.2 rillig 'INNER_IF_ELIF_ELSE=' \
197 1.2 rillig '. if 0' \
198 1.2 rillig '. elif 0' \
199 1.2 rillig '. else' \
200 1.2 rillig '. endif' \
201 1.2 rillig '. if 0' \
202 1.2 rillig '. elif 1' \
203 1.2 rillig '. else' \
204 1.2 rillig '. endif' \
205 1.2 rillig '. if 1' \
206 1.2 rillig '. elif 1' \
207 1.2 rillig '. else' \
208 1.2 rillig '. endif' \
209 1.2 rillig '.endif'
210 1.4 rillig # expect: Parse_PushInput: file inner-if-elif-else.tmp, line 1
211 1.5 rillig # expect: Skipping 'inner-if-elif-else.tmp' because 'INNER_IF_ELIF_ELSE' is already set
212 1.5 rillig
213 1.1 rillig
214 1.1 rillig # Include each of the files twice. The directive-include-guard.exp file
215 1.1 rillig # contains a single entry for the files whose multiple-inclusion guard works,
216 1.1 rillig # and two entries for the files that are not protected against multiple
217 1.1 rillig # inclusion.
218 1.1 rillig #
219 1.1 rillig # Some debug output lines are suppressed in the .exp file, see ./Makefile.
220 1.1 rillig .for i in ${INCS}
221 1.4 rillig . for fname in $i.tmp
222 1.1 rillig _!= printf '%s\n' ${LINES.$i} > ${fname}
223 1.1 rillig .MAKEFLAGS: -dp
224 1.1 rillig .include "${.CURDIR}/${fname}"
225 1.2 rillig .undef ${i:Mundef-between:%=UNDEF_BETWEEN}
226 1.1 rillig .include "${.CURDIR}/${fname}"
227 1.1 rillig .MAKEFLAGS: -d0
228 1.1 rillig _!= rm ${fname}
229 1.1 rillig . endfor
230 1.1 rillig .endfor
231 1.1 rillig
232 1.1 rillig all:
233