1*98875883SSimon J. Gerraty# $NetBSD: directive-include-guard.mk,v 1.12 2023/08/11 04:56:31 rillig Exp $ 2148ee845SSimon J. Gerraty# 3148ee845SSimon J. Gerraty# Tests for multiple-inclusion guards in makefiles. 4148ee845SSimon J. Gerraty# 5148ee845SSimon J. Gerraty# A file that is guarded by a multiple-inclusion guard has one of the 6148ee845SSimon J. Gerraty# following forms: 7148ee845SSimon J. Gerraty# 8148ee845SSimon J. Gerraty# .ifndef GUARD_VARIABLE 9148ee845SSimon J. Gerraty# .endif 10148ee845SSimon J. Gerraty# 11148ee845SSimon J. Gerraty# .if !defined(GUARD_VARIABLE) 12148ee845SSimon J. Gerraty# .endif 13148ee845SSimon J. Gerraty# 14148ee845SSimon J. Gerraty# .if !target(guard-target) 15148ee845SSimon J. Gerraty# .endif 16148ee845SSimon J. Gerraty# 17148ee845SSimon J. Gerraty# When such a file is included for the second or later time, and the guard 18148ee845SSimon J. Gerraty# variable or the guard target is defined, including the file has no effect, 19148ee845SSimon J. Gerraty# as all its content is skipped. 20148ee845SSimon J. Gerraty# 21148ee845SSimon J. Gerraty# See also: 22148ee845SSimon J. Gerraty# https://gcc.gnu.org/onlinedocs/cppinternals/Guard-Macros.html 23148ee845SSimon J. Gerraty 24148ee845SSimon J. Gerraty# Each of the following test cases creates a temporary file named after the 25148ee845SSimon J. Gerraty# test case and writes some lines of text to that file. That file is then 26148ee845SSimon J. Gerraty# included twice, to see whether the second '.include' is skipped. 27148ee845SSimon J. Gerraty 28148ee845SSimon J. Gerraty 29148ee845SSimon J. Gerraty# This is the canonical form of a variable-based multiple-inclusion guard. 30148ee845SSimon J. GerratyINCS+= variable-ifndef 31148ee845SSimon J. GerratyLINES.variable-ifndef= \ 32148ee845SSimon J. Gerraty '.ifndef VARIABLE_IFNDEF' \ 33148ee845SSimon J. Gerraty 'VARIABLE_IFNDEF=' \ 34148ee845SSimon J. Gerraty '.endif' 35148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-ifndef.tmp, line 1 36148ee845SSimon J. Gerraty# expect: Skipping 'variable-ifndef.tmp' because 'VARIABLE_IFNDEF' is defined 37148ee845SSimon J. Gerraty 38148ee845SSimon J. Gerraty# A file that reuses a guard from a previous file (or whose guard is defined 39148ee845SSimon J. Gerraty# for any other reason) is only processed once, to see whether it is guarded. 40148ee845SSimon J. Gerraty# Its content is skipped, therefore the syntax error is not detected. 41148ee845SSimon J. GerratyINCS+= variable-ifndef-reuse 42148ee845SSimon J. GerratyLINES.variable-ifndef-reuse= \ 43148ee845SSimon J. Gerraty '.ifndef VARIABLE_IFNDEF' \ 44148ee845SSimon J. Gerraty 'syntax error' \ 45148ee845SSimon J. Gerraty '.endif' 46148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-ifndef-reuse.tmp, line 1 47148ee845SSimon J. Gerraty# expect: Skipping 'variable-ifndef-reuse.tmp' because 'VARIABLE_IFNDEF' is defined 48148ee845SSimon J. Gerraty 49148ee845SSimon J. Gerraty# Comments and empty lines do not affect the multiple-inclusion guard. 50148ee845SSimon J. GerratyINCS+= comments 51148ee845SSimon J. GerratyLINES.comments= \ 52148ee845SSimon J. Gerraty '\# comment' \ 53148ee845SSimon J. Gerraty '' \ 54148ee845SSimon J. Gerraty '.ifndef COMMENTS' \ 55148ee845SSimon J. Gerraty '\# comment' \ 56148ee845SSimon J. Gerraty 'COMMENTS=\#comment' \ 57148ee845SSimon J. Gerraty '.endif' \ 58148ee845SSimon J. Gerraty '\# comment' 59148ee845SSimon J. Gerraty# expect: Parse_PushInput: file comments.tmp, line 1 60148ee845SSimon J. Gerraty# expect: Skipping 'comments.tmp' because 'COMMENTS' is defined 61148ee845SSimon J. Gerraty 62148ee845SSimon J. Gerraty# An alternative form uses the 'defined' function. It is more verbose than 63148ee845SSimon J. Gerraty# the canonical form but avoids the '.ifndef' directive, as that directive is 64148ee845SSimon J. Gerraty# not commonly used. 65148ee845SSimon J. GerratyINCS+= variable-if 66148ee845SSimon J. GerratyLINES.variable-if= \ 67148ee845SSimon J. Gerraty '.if !defined(VARIABLE_IF)' \ 68148ee845SSimon J. Gerraty 'VARIABLE_IF=' \ 69148ee845SSimon J. Gerraty '.endif' 70148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-if.tmp, line 1 71148ee845SSimon J. Gerraty# expect: Skipping 'variable-if.tmp' because 'VARIABLE_IF' is defined 72148ee845SSimon J. Gerraty 73148ee845SSimon J. Gerraty# A file that reuses a guard from a previous file (or whose guard is defined 74148ee845SSimon J. Gerraty# for any other reason) is only processed once, to see whether it is guarded. 75148ee845SSimon J. Gerraty# Its content is skipped, therefore the syntax error is not detected. 76148ee845SSimon J. GerratyINCS+= variable-if-reuse 77148ee845SSimon J. GerratyLINES.variable-if-reuse= \ 78148ee845SSimon J. Gerraty '.if !defined(VARIABLE_IF)' \ 79148ee845SSimon J. Gerraty 'syntax error' \ 80148ee845SSimon J. Gerraty '.endif' 81148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-if-reuse.tmp, line 1 82148ee845SSimon J. Gerraty# expect: Skipping 'variable-if-reuse.tmp' because 'VARIABLE_IF' is defined 83148ee845SSimon J. Gerraty 84148ee845SSimon J. Gerraty# Triple negation is so uncommon that it's not recognized, even though it has 85148ee845SSimon J. Gerraty# the same effect as a single negation. 86148ee845SSimon J. GerratyINCS+= variable-if-triple-negation 87148ee845SSimon J. GerratyLINES.variable-if-triple-negation= \ 88148ee845SSimon J. Gerraty '.if !!!defined(VARIABLE_IF_TRIPLE_NEGATION)' \ 89148ee845SSimon J. Gerraty 'VARIABLE_IF_TRIPLE_NEGATION=' \ 90148ee845SSimon J. Gerraty '.endif' 91148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-if-triple-negation.tmp, line 1 92148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-if-triple-negation.tmp, line 1 93148ee845SSimon J. Gerraty 94148ee845SSimon J. Gerraty# A conditional other than '.if' or '.ifndef' does not guard the file, even if 95148ee845SSimon J. Gerraty# it is otherwise equivalent to the above accepted forms. 96148ee845SSimon J. GerratyINCS+= variable-ifdef-negated 97148ee845SSimon J. GerratyLINES.variable-ifdef-negated= \ 98148ee845SSimon J. Gerraty '.ifdef !VARIABLE_IFDEF_NEGATED' \ 99148ee845SSimon J. Gerraty 'VARIABLE_IFDEF_NEGATED=' \ 100148ee845SSimon J. Gerraty '.endif' 101148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-ifdef-negated.tmp, line 1 102148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-ifdef-negated.tmp, line 1 103148ee845SSimon J. Gerraty 104148ee845SSimon J. Gerraty# The variable names in the '.if' and the assignment must be the same. 105148ee845SSimon J. GerratyINCS+= variable-name-mismatch 106148ee845SSimon J. GerratyLINES.variable-name-mismatch= \ 107148ee845SSimon J. Gerraty '.ifndef VARIABLE_NAME_MISMATCH' \ 108148ee845SSimon J. Gerraty 'VARIABLE_NAME_DIFFERENT=' \ 109148ee845SSimon J. Gerraty '.endif' 110148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-name-mismatch.tmp, line 1 111148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-name-mismatch.tmp, line 1 112148ee845SSimon J. Gerraty 113148ee845SSimon J. Gerraty# The variable name '!VARNAME' cannot be used in an '.ifndef' directive, as 114148ee845SSimon J. Gerraty# the '!' would be a negation. It is syntactically valid in a '.if !defined' 115*98875883SSimon J. Gerraty# condition, but this case is so uncommon that the guard mechanism doesn't 116*98875883SSimon J. Gerraty# accept '!' in the guard variable name. Furthermore, when defining the 117*98875883SSimon J. Gerraty# variable, the character '!' has to be escaped, to prevent it from being 118*98875883SSimon J. Gerraty# interpreted as the '!' dependency operator. 119148ee845SSimon J. GerratyINCS+= variable-name-exclamation 120148ee845SSimon J. GerratyLINES.variable-name-exclamation= \ 121148ee845SSimon J. Gerraty '.if !defined(!VARIABLE_NAME_EXCLAMATION)' \ 122148ee845SSimon J. Gerraty '${:U!}VARIABLE_NAME_EXCLAMATION=' \ 123148ee845SSimon J. Gerraty '.endif' 124148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-name-exclamation.tmp, line 1 125148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-name-exclamation.tmp, line 1 126148ee845SSimon J. Gerraty 127148ee845SSimon J. Gerraty# A variable name can contain a '!' in the middle, as that character is 128148ee845SSimon J. Gerraty# interpreted as an ordinary character in conditions as well as on the left 129148ee845SSimon J. Gerraty# side of a variable assignment. For guard variable names, the '!' is not 130148ee845SSimon J. Gerraty# supported in any place, though. 131148ee845SSimon J. GerratyINCS+= variable-name-exclamation-middle 132148ee845SSimon J. GerratyLINES.variable-name-exclamation-middle= \ 133148ee845SSimon J. Gerraty '.ifndef VARIABLE_NAME!MIDDLE' \ 134148ee845SSimon J. Gerraty 'VARIABLE_NAME!MIDDLE=' \ 135148ee845SSimon J. Gerraty '.endif' 136148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-name-exclamation-middle.tmp, line 1 137148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-name-exclamation-middle.tmp, line 1 138148ee845SSimon J. Gerraty 139148ee845SSimon J. Gerraty# A variable name can contain balanced parentheses, at least in conditions and 140148ee845SSimon J. Gerraty# on the left side of a variable assignment. There are enough places in make 141148ee845SSimon J. Gerraty# where parentheses or braces are handled inconsistently to make this naming 142148ee845SSimon J. Gerraty# choice a bad idea, therefore these characters are not allowed in guard 143148ee845SSimon J. Gerraty# variable names. 144148ee845SSimon J. GerratyINCS+= variable-name-parentheses 145148ee845SSimon J. GerratyLINES.variable-name-parentheses= \ 146148ee845SSimon J. Gerraty '.ifndef VARIABLE_NAME(&)PARENTHESES' \ 147148ee845SSimon J. Gerraty 'VARIABLE_NAME(&)PARENTHESES=' \ 148148ee845SSimon J. Gerraty '.endif' 149148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-name-parentheses.tmp, line 1 150148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-name-parentheses.tmp, line 1 151148ee845SSimon J. Gerraty 152148ee845SSimon J. Gerraty# The guard condition must consist of only the guard variable, nothing else. 153148ee845SSimon J. GerratyINCS+= variable-ifndef-plus 154148ee845SSimon J. GerratyLINES.variable-ifndef-plus= \ 155148ee845SSimon J. Gerraty '.ifndef VARIABLE_IFNDEF_PLUS && VARIABLE_IFNDEF_SECOND' \ 156148ee845SSimon J. Gerraty 'VARIABLE_IFNDEF_PLUS=' \ 157148ee845SSimon J. Gerraty 'VARIABLE_IFNDEF_SECOND=' \ 158148ee845SSimon J. Gerraty '.endif' 159148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-ifndef-plus.tmp, line 1 160148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-ifndef-plus.tmp, line 1 161148ee845SSimon J. Gerraty 162148ee845SSimon J. Gerraty# The guard condition must consist of only the guard variable, nothing else. 163148ee845SSimon J. GerratyINCS+= variable-if-plus 164148ee845SSimon J. GerratyLINES.variable-if-plus= \ 165148ee845SSimon J. Gerraty '.if !defined(VARIABLE_IF_PLUS) && !defined(VARIABLE_IF_SECOND)' \ 166148ee845SSimon J. Gerraty 'VARIABLE_IF_PLUS=' \ 167148ee845SSimon J. Gerraty 'VARIABLE_IF_SECOND=' \ 168148ee845SSimon J. Gerraty '.endif' 169148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-if-plus.tmp, line 1 170148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-if-plus.tmp, line 1 171148ee845SSimon J. Gerraty 172148ee845SSimon J. Gerraty# The variable name in an '.ifndef' guard must be given directly, it must not 173148ee845SSimon J. Gerraty# contain any '$' expression. 174148ee845SSimon J. GerratyINCS+= variable-ifndef-indirect 175148ee845SSimon J. GerratyLINES.variable-ifndef-indirect= \ 176148ee845SSimon J. Gerraty '.ifndef $${VARIABLE_IFNDEF_INDIRECT:L}' \ 177148ee845SSimon J. Gerraty 'VARIABLE_IFNDEF_INDIRECT=' \ 178148ee845SSimon J. Gerraty '.endif' 179148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-ifndef-indirect.tmp, line 1 180148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-ifndef-indirect.tmp, line 1 181148ee845SSimon J. Gerraty 182148ee845SSimon J. Gerraty# The variable name in an '.if' guard must be given directly, it must not 183148ee845SSimon J. Gerraty# contain any '$' expression. 184148ee845SSimon J. GerratyINCS+= variable-if-indirect 185148ee845SSimon J. GerratyLINES.variable-if-indirect= \ 186148ee845SSimon J. Gerraty '.if !defined($${VARIABLE_IF_INDIRECT:L})' \ 187148ee845SSimon J. Gerraty 'VARIABLE_IF_INDIRECT=' \ 188148ee845SSimon J. Gerraty '.endif' 189148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-if-indirect.tmp, line 1 190148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-if-indirect.tmp, line 1 191148ee845SSimon J. Gerraty 192148ee845SSimon J. Gerraty# The variable name in the guard condition must only contain alphanumeric 193*98875883SSimon J. Gerraty# characters and underscores. The place where the guard variable is defined 194*98875883SSimon J. Gerraty# is more flexible, as long as the variable is defined at the point where the 195*98875883SSimon J. Gerraty# file is included the next time. 196148ee845SSimon J. GerratyINCS+= variable-assign-indirect 197148ee845SSimon J. GerratyLINES.variable-assign-indirect= \ 198148ee845SSimon J. Gerraty '.ifndef VARIABLE_ASSIGN_INDIRECT' \ 199148ee845SSimon J. Gerraty '$${VARIABLE_ASSIGN_INDIRECT:L}=' \ 200148ee845SSimon J. Gerraty '.endif' 201148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-assign-indirect.tmp, line 1 202148ee845SSimon J. Gerraty# expect: Skipping 'variable-assign-indirect.tmp' because 'VARIABLE_ASSIGN_INDIRECT' is defined 203148ee845SSimon J. Gerraty 204148ee845SSimon J. Gerraty# The time at which the guard variable is defined doesn't matter, as long as 205148ee845SSimon J. Gerraty# it is defined at the point where the file is included the next time. 206148ee845SSimon J. GerratyINCS+= variable-assign-late 207148ee845SSimon J. GerratyLINES.variable-assign-late= \ 208148ee845SSimon J. Gerraty '.ifndef VARIABLE_ASSIGN_LATE' \ 209148ee845SSimon J. Gerraty 'VARIABLE_ASSIGN_LATE_OTHER=' \ 210148ee845SSimon J. Gerraty 'VARIABLE_ASSIGN_LATE=' \ 211148ee845SSimon J. Gerraty '.endif' 212148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-assign-late.tmp, line 1 213148ee845SSimon J. Gerraty# expect: Skipping 'variable-assign-late.tmp' because 'VARIABLE_ASSIGN_LATE' is defined 214148ee845SSimon J. Gerraty 215148ee845SSimon J. Gerraty# The time at which the guard variable is defined doesn't matter, as long as 216148ee845SSimon J. Gerraty# it is defined at the point where the file is included the next time. 217148ee845SSimon J. GerratyINCS+= variable-assign-nested 218148ee845SSimon J. GerratyLINES.variable-assign-nested= \ 219148ee845SSimon J. Gerraty '.ifndef VARIABLE_ASSIGN_NESTED' \ 220148ee845SSimon J. Gerraty '. if 1' \ 221148ee845SSimon J. Gerraty '. for i in once' \ 222148ee845SSimon J. Gerraty 'VARIABLE_ASSIGN_NESTED=' \ 223148ee845SSimon J. Gerraty '. endfor' \ 224148ee845SSimon J. Gerraty '. endif' \ 225148ee845SSimon J. Gerraty '.endif' 226148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-assign-nested.tmp, line 1 227148ee845SSimon J. Gerraty# expect: Skipping 'variable-assign-nested.tmp' because 'VARIABLE_ASSIGN_NESTED' is defined 228148ee845SSimon J. Gerraty 229148ee845SSimon J. Gerraty# If the guard variable is defined before the file is included for the first 230148ee845SSimon J. Gerraty# time, the file is considered guarded as well. In such a case, the parser 231148ee845SSimon J. Gerraty# skips almost all lines, as they are irrelevant, but the structure of the 232148ee845SSimon J. Gerraty# top-level '.if/.endif' conditional can be determined reliably enough to 233148ee845SSimon J. Gerraty# decide whether the file is guarded. 234148ee845SSimon J. GerratyINCS+= variable-already-defined 235148ee845SSimon J. GerratyLINES.variable-already-defined= \ 236148ee845SSimon J. Gerraty '.ifndef VARIABLE_ALREADY_DEFINED' \ 237148ee845SSimon J. Gerraty 'VARIABLE_ALREADY_DEFINED=' \ 238148ee845SSimon J. Gerraty '.endif' 239148ee845SSimon J. GerratyVARIABLE_ALREADY_DEFINED= 240148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-already-defined.tmp, line 1 241148ee845SSimon J. Gerraty# expect: Skipping 'variable-already-defined.tmp' because 'VARIABLE_ALREADY_DEFINED' is defined 242148ee845SSimon J. Gerraty 243148ee845SSimon J. Gerraty# If the guard variable is defined before the file is included the first time, 244148ee845SSimon J. Gerraty# the file is processed but its content is skipped. If that same guard 245148ee845SSimon J. Gerraty# variable is undefined when the file is included the second time, the file is 246148ee845SSimon J. Gerraty# processed as usual. 247148ee845SSimon J. GerratyINCS+= variable-defined-then-undefined 248148ee845SSimon J. GerratyLINES.variable-defined-then-undefined= \ 249148ee845SSimon J. Gerraty '.ifndef VARIABLE_DEFINED_THEN_UNDEFINED' \ 250148ee845SSimon J. Gerraty '.endif' 251148ee845SSimon J. GerratyVARIABLE_DEFINED_THEN_UNDEFINED= 252148ee845SSimon J. GerratyUNDEF_BETWEEN.variable-defined-then-undefined= \ 253148ee845SSimon J. Gerraty VARIABLE_DEFINED_THEN_UNDEFINED 254148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-defined-then-undefined.tmp, line 1 255148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-defined-then-undefined.tmp, line 1 256148ee845SSimon J. Gerraty 257148ee845SSimon J. Gerraty# The whole file content must be guarded by a single '.if' conditional, not by 258*98875883SSimon J. Gerraty# several, as each of these conditionals would require its separate guard. 259*98875883SSimon J. Gerraty# This case is not expected to occur in practice, as the two parts would 260*98875883SSimon J. Gerraty# rather be split into separate files. 261148ee845SSimon J. GerratyINCS+= variable-two-times 262148ee845SSimon J. GerratyLINES.variable-two-times= \ 263148ee845SSimon J. Gerraty '.ifndef VARIABLE_TWO_TIMES_1' \ 264148ee845SSimon J. Gerraty 'VARIABLE_TWO_TIMES_1=' \ 265148ee845SSimon J. Gerraty '.endif' \ 266148ee845SSimon J. Gerraty '.ifndef VARIABLE_TWO_TIMES_2' \ 267148ee845SSimon J. Gerraty 'VARIABLE_TWO_TIMES_2=' \ 268148ee845SSimon J. Gerraty '.endif' 269148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-two-times.tmp, line 1 270148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-two-times.tmp, line 1 271148ee845SSimon J. Gerraty 272148ee845SSimon J. Gerraty# When multiple files use the same guard variable name, the optimization of 273148ee845SSimon J. Gerraty# skipping the file affects each of these files. 274148ee845SSimon J. Gerraty# 275148ee845SSimon J. Gerraty# Choosing unique guard names is the responsibility of the makefile authors. 276148ee845SSimon J. Gerraty# A typical pattern of guard variable names is '${PROJECT}_${DIR}_${FILE}_MK'. 277148ee845SSimon J. Gerraty# System-provided files typically start the guard names with '_'. 278148ee845SSimon J. GerratyINCS+= variable-clash 279148ee845SSimon J. GerratyLINES.variable-clash= \ 280148ee845SSimon J. Gerraty ${LINES.variable-if} 281148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-clash.tmp, line 1 282148ee845SSimon J. Gerraty# expect: Skipping 'variable-clash.tmp' because 'VARIABLE_IF' is defined 283148ee845SSimon J. Gerraty 284148ee845SSimon J. Gerraty# The conditional must come before the assignment, otherwise the conditional 285148ee845SSimon J. Gerraty# is useless, as it always evaluates to false. 286148ee845SSimon J. GerratyINCS+= variable-swapped 287148ee845SSimon J. GerratyLINES.variable-swapped= \ 288148ee845SSimon J. Gerraty 'SWAPPED=' \ 289148ee845SSimon J. Gerraty '.ifndef SWAPPED' \ 290148ee845SSimon J. Gerraty '. error' \ 291148ee845SSimon J. Gerraty '.endif' 292148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-swapped.tmp, line 1 293148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-swapped.tmp, line 1 294148ee845SSimon J. Gerraty 295148ee845SSimon J. Gerraty# If the guard variable is undefined between the first and the second time the 296148ee845SSimon J. Gerraty# file is included, the guarded file is included again. 297148ee845SSimon J. GerratyINCS+= variable-undef-between 298148ee845SSimon J. GerratyLINES.variable-undef-between= \ 299148ee845SSimon J. Gerraty '.ifndef VARIABLE_UNDEF_BETWEEN' \ 300148ee845SSimon J. Gerraty 'VARIABLE_UNDEF_BETWEEN=' \ 301148ee845SSimon J. Gerraty '.endif' 302148ee845SSimon J. GerratyUNDEF_BETWEEN.variable-undef-between= \ 303148ee845SSimon J. Gerraty VARIABLE_UNDEF_BETWEEN 304148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-undef-between.tmp, line 1 305148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-undef-between.tmp, line 1 306148ee845SSimon J. Gerraty 307148ee845SSimon J. Gerraty# If the guard variable is undefined while the file is included the first 308148ee845SSimon J. Gerraty# time, the guard does not have an effect, and the file is included again. 309148ee845SSimon J. GerratyINCS+= variable-undef-inside 310148ee845SSimon J. GerratyLINES.variable-undef-inside= \ 311148ee845SSimon J. Gerraty '.ifndef VARIABLE_UNDEF_INSIDE' \ 312148ee845SSimon J. Gerraty 'VARIABLE_UNDEF_INSIDE=' \ 313148ee845SSimon J. Gerraty '.undef VARIABLE_UNDEF_INSIDE' \ 314148ee845SSimon J. Gerraty '.endif' 315148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-undef-inside.tmp, line 1 316148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-undef-inside.tmp, line 1 317148ee845SSimon J. Gerraty 318148ee845SSimon J. Gerraty# If the file does not define the guard variable, the guard does not have an 319148ee845SSimon J. Gerraty# effect, and the file is included again. 320148ee845SSimon J. GerratyINCS+= variable-not-defined 321148ee845SSimon J. GerratyLINES.variable-not-defined= \ 322148ee845SSimon J. Gerraty '.ifndef VARIABLE_NOT_DEFINED' \ 323148ee845SSimon J. Gerraty '.endif' 324148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-not-defined.tmp, line 1 325148ee845SSimon J. Gerraty# expect: Parse_PushInput: file variable-not-defined.tmp, line 1 326148ee845SSimon J. Gerraty 327148ee845SSimon J. Gerraty# The outermost '.if' must not have an '.elif' branch. 328*98875883SSimon J. GerratyINCS+= elif 329*98875883SSimon J. GerratyLINES.elif= \ 330*98875883SSimon J. Gerraty '.ifndef ELIF' \ 331*98875883SSimon J. Gerraty 'ELIF=' \ 332148ee845SSimon J. Gerraty '.elif 1' \ 333148ee845SSimon J. Gerraty '.endif' 334*98875883SSimon J. Gerraty# expect: Parse_PushInput: file elif.tmp, line 1 335*98875883SSimon J. Gerraty# expect: Parse_PushInput: file elif.tmp, line 1 336148ee845SSimon J. Gerraty 337148ee845SSimon J. Gerraty# When a file with an '.if/.elif/.endif' conditional at the top level is 338148ee845SSimon J. Gerraty# included, it is never optimized, as one of its branches is taken. 339*98875883SSimon J. GerratyINCS+= elif-reuse 340*98875883SSimon J. GerratyLINES.elif-reuse= \ 341*98875883SSimon J. Gerraty '.ifndef ELIF' \ 342148ee845SSimon J. Gerraty 'syntax error' \ 343148ee845SSimon J. Gerraty '.elif 1' \ 344148ee845SSimon J. Gerraty '.endif' 345*98875883SSimon J. Gerraty# expect: Parse_PushInput: file elif-reuse.tmp, line 1 346*98875883SSimon J. Gerraty# expect: Parse_PushInput: file elif-reuse.tmp, line 1 347148ee845SSimon J. Gerraty 348148ee845SSimon J. Gerraty# The outermost '.if' must not have an '.else' branch. 349*98875883SSimon J. GerratyINCS+= else 350*98875883SSimon J. GerratyLINES.else= \ 351*98875883SSimon J. Gerraty '.ifndef ELSE' \ 352*98875883SSimon J. Gerraty 'ELSE=' \ 353148ee845SSimon J. Gerraty '.else' \ 354148ee845SSimon J. Gerraty '.endif' 355*98875883SSimon J. Gerraty# expect: Parse_PushInput: file else.tmp, line 1 356*98875883SSimon J. Gerraty# expect: Parse_PushInput: file else.tmp, line 1 357148ee845SSimon J. Gerraty 358148ee845SSimon J. Gerraty# When a file with an '.if/.else/.endif' conditional at the top level is 359148ee845SSimon J. Gerraty# included, it is never optimized, as one of its branches is taken. 360*98875883SSimon J. GerratyINCS+= else-reuse 361*98875883SSimon J. GerratyLINES.else-reuse= \ 362*98875883SSimon J. Gerraty '.ifndef ELSE' \ 363148ee845SSimon J. Gerraty 'syntax error' \ 364148ee845SSimon J. Gerraty '.else' \ 365148ee845SSimon J. Gerraty '.endif' 366*98875883SSimon J. Gerraty# expect: Parse_PushInput: file else-reuse.tmp, line 1 367*98875883SSimon J. Gerraty# expect: Parse_PushInput: file else-reuse.tmp, line 1 368148ee845SSimon J. Gerraty 369148ee845SSimon J. Gerraty# The inner '.if' directives may have an '.elif' or '.else', and it doesn't 370148ee845SSimon J. Gerraty# matter which of their branches are taken. 371148ee845SSimon J. GerratyINCS+= inner-if-elif-else 372148ee845SSimon J. GerratyLINES.inner-if-elif-else= \ 373148ee845SSimon J. Gerraty '.ifndef INNER_IF_ELIF_ELSE' \ 374148ee845SSimon J. Gerraty 'INNER_IF_ELIF_ELSE=' \ 375148ee845SSimon J. Gerraty '. if 0' \ 376148ee845SSimon J. Gerraty '. elif 0' \ 377148ee845SSimon J. Gerraty '. else' \ 378148ee845SSimon J. Gerraty '. endif' \ 379148ee845SSimon J. Gerraty '. if 0' \ 380148ee845SSimon J. Gerraty '. elif 1' \ 381148ee845SSimon J. Gerraty '. else' \ 382148ee845SSimon J. Gerraty '. endif' \ 383148ee845SSimon J. Gerraty '. if 1' \ 384148ee845SSimon J. Gerraty '. elif 1' \ 385148ee845SSimon J. Gerraty '. else' \ 386148ee845SSimon J. Gerraty '. endif' \ 387148ee845SSimon J. Gerraty '.endif' 388148ee845SSimon J. Gerraty# expect: Parse_PushInput: file inner-if-elif-else.tmp, line 1 389148ee845SSimon J. Gerraty# expect: Skipping 'inner-if-elif-else.tmp' because 'INNER_IF_ELIF_ELSE' is defined 390148ee845SSimon J. Gerraty 391148ee845SSimon J. Gerraty# The guard can also be a target instead of a variable. Using a target as a 392148ee845SSimon J. Gerraty# guard has the benefit that a target cannot be undefined once it is defined. 393148ee845SSimon J. Gerraty# The target should be declared '.NOTMAIN'. Since the target names are 394148ee845SSimon J. Gerraty# usually chosen according to a pattern that doesn't interfere with real 395148ee845SSimon J. Gerraty# target names, they don't need to be declared '.PHONY' as they don't generate 396148ee845SSimon J. Gerraty# filesystem operations. 397148ee845SSimon J. GerratyINCS+= target 398148ee845SSimon J. GerratyLINES.target= \ 399148ee845SSimon J. Gerraty '.if !target(__target.tmp__)' \ 400148ee845SSimon J. Gerraty '__target.tmp__: .NOTMAIN' \ 401148ee845SSimon J. Gerraty '.endif' 402148ee845SSimon J. Gerraty# expect: Parse_PushInput: file target.tmp, line 1 403148ee845SSimon J. Gerraty# expect: Skipping 'target.tmp' because '__target.tmp__' is defined 404148ee845SSimon J. Gerraty 405148ee845SSimon J. Gerraty# When used for system files, the target name may include '<' and '>', for 406148ee845SSimon J. Gerraty# symmetry with the '.include <sys.mk>' directive. The characters '<' and '>' 407148ee845SSimon J. Gerraty# are ordinary characters. 408148ee845SSimon J. GerratyINCS+= target-sys 409148ee845SSimon J. GerratyLINES.target-sys= \ 410148ee845SSimon J. Gerraty '.if !target(__<target-sys.tmp>__)' \ 411148ee845SSimon J. Gerraty '__<target-sys.tmp>__: .NOTMAIN' \ 412148ee845SSimon J. Gerraty '.endif' 413148ee845SSimon J. Gerraty# expect: Parse_PushInput: file target-sys.tmp, line 1 414148ee845SSimon J. Gerraty# expect: Skipping 'target-sys.tmp' because '__<target-sys.tmp>__' is defined 415148ee845SSimon J. Gerraty 416148ee845SSimon J. Gerraty# The target name may include variable references. These references are 417148ee845SSimon J. Gerraty# expanded as usual. Due to the current implementation, the expressions are 418148ee845SSimon J. Gerraty# evaluated twice: Once for checking whether the condition evaluates to true, 419148ee845SSimon J. Gerraty# and once for determining the guard name. This double evaluation should not 420148ee845SSimon J. Gerraty# matter in practice, as guard expressions are expected to be simple, 421148ee845SSimon J. Gerraty# deterministic and without side effects. 422148ee845SSimon J. GerratyINCS+= target-indirect 423148ee845SSimon J. GerratyLINES.target-indirect= \ 424148ee845SSimon J. Gerraty '.if !target($${target-indirect.tmp:L})' \ 425148ee845SSimon J. Gerraty 'target-indirect.tmp: .NOTMAIN' \ 426148ee845SSimon J. Gerraty '.endif' 427148ee845SSimon J. Gerraty# expect: Parse_PushInput: file target-indirect.tmp, line 1 428148ee845SSimon J. Gerraty# expect: Skipping 'target-indirect.tmp' because 'target-indirect.tmp' is defined 429148ee845SSimon J. Gerraty 430148ee845SSimon J. Gerraty# A common form of guard target is __${.PARSEFILE}__. This form can only be 431148ee845SSimon J. Gerraty# used if all files using this form have unique basenames. To get a robust 432148ee845SSimon J. Gerraty# pattern based on the same idea, use __${.PARSEDIR}/${.PARSEFILE}__ instead. 433148ee845SSimon J. Gerraty# This form does not work when the basename contains whitespace characters, as 434148ee845SSimon J. Gerraty# it is not possible to define a target with whitespace, not even by cheating. 435148ee845SSimon J. GerratyINCS+= target-indirect-PARSEFILE 436148ee845SSimon J. GerratyLINES.target-indirect-PARSEFILE= \ 437148ee845SSimon J. Gerraty '.if !target(__$${.PARSEFILE}__)' \ 438148ee845SSimon J. Gerraty '__$${.PARSEFILE}__: .NOTMAIN' \ 439148ee845SSimon J. Gerraty '.endif' 440148ee845SSimon J. Gerraty# expect: Parse_PushInput: file target-indirect-PARSEFILE.tmp, line 1 441148ee845SSimon J. Gerraty# expect: Skipping 'target-indirect-PARSEFILE.tmp' because '__target-indirect-PARSEFILE.tmp__' is defined 442148ee845SSimon J. Gerraty 443148ee845SSimon J. Gerraty# Two files with different basenames can both use the same syntactic pattern 444148ee845SSimon J. Gerraty# for the target guard name, as the expressions expand to different strings. 445148ee845SSimon J. GerratyINCS+= target-indirect-PARSEFILE2 446148ee845SSimon J. GerratyLINES.target-indirect-PARSEFILE2= \ 447148ee845SSimon J. Gerraty '.if !target(__$${.PARSEFILE}__)' \ 448148ee845SSimon J. Gerraty '__$${.PARSEFILE}__: .NOTMAIN' \ 449148ee845SSimon J. Gerraty '.endif' 450148ee845SSimon J. Gerraty# expect: Parse_PushInput: file target-indirect-PARSEFILE2.tmp, line 1 451148ee845SSimon J. Gerraty# expect: Skipping 'target-indirect-PARSEFILE2.tmp' because '__target-indirect-PARSEFILE2.tmp__' is defined 452148ee845SSimon J. Gerraty 453148ee845SSimon J. Gerraty# Using plain .PARSEFILE without .PARSEDIR leads to name clashes. The include 454148ee845SSimon J. Gerraty# guard is the same as in the test case 'target-indirect-PARSEFILE', as the 455*98875883SSimon J. Gerraty# guard name only contains the basename but not the directory name. So even 456*98875883SSimon J. Gerraty# without defining the guard variable, the file is considered guarded. 457148ee845SSimon J. GerratyINCS+= subdir/target-indirect-PARSEFILE 458148ee845SSimon J. GerratyLINES.subdir/target-indirect-PARSEFILE= \ 459148ee845SSimon J. Gerraty '.if !target(__$${.PARSEFILE}__)' \ 460148ee845SSimon J. Gerraty '.endif' 461148ee845SSimon J. Gerraty# expect: Parse_PushInput: file subdir/target-indirect-PARSEFILE.tmp, line 1 462148ee845SSimon J. Gerraty# expect: Skipping 'subdir/target-indirect-PARSEFILE.tmp' because '__target-indirect-PARSEFILE.tmp__' is defined 463148ee845SSimon J. Gerraty 464148ee845SSimon J. Gerraty# Another common form of guard target is __${.PARSEDIR}/${.PARSEFILE}__ 465*98875883SSimon J. Gerraty# or __${.PARSEDIR:tA}/${.PARSEFILE}__ to be truly unique. 466148ee845SSimon J. GerratyINCS+= target-indirect-PARSEDIR-PARSEFILE 467148ee845SSimon J. GerratyLINES.target-indirect-PARSEDIR-PARSEFILE= \ 468148ee845SSimon J. Gerraty '.if !target(__$${.PARSEDIR}/$${.PARSEFILE}__)' \ 469148ee845SSimon J. Gerraty '__$${.PARSEDIR}/$${.PARSEFILE}__: .NOTMAIN' \ 470148ee845SSimon J. Gerraty '.endif' 471148ee845SSimon J. Gerraty# expect: Parse_PushInput: file target-indirect-PARSEDIR-PARSEFILE.tmp, line 1 472148ee845SSimon J. Gerraty# expect: Skipping 'target-indirect-PARSEDIR-PARSEFILE.tmp' because '__target-indirect-PARSEDIR-PARSEFILE.tmp__' is defined 473148ee845SSimon J. Gerraty# The actual target starts with '__${.OBJDIR}/', see the .rawout file, but the 474148ee845SSimon J. Gerraty# string '${.OBJDIR}/' gets stripped in post processing. 475148ee845SSimon J. Gerraty 476148ee845SSimon J. Gerraty# Using the combination of '.PARSEDIR' and '.PARSEFILE', a file in a 477148ee845SSimon J. Gerraty# subdirectory gets a different guard target name than the previous one. 478148ee845SSimon J. GerratyINCS+= subdir/target-indirect-PARSEDIR-PARSEFILE 479148ee845SSimon J. GerratyLINES.subdir/target-indirect-PARSEDIR-PARSEFILE= \ 480148ee845SSimon J. Gerraty '.if !target(__$${.PARSEDIR}/$${.PARSEFILE}__)' \ 481148ee845SSimon J. Gerraty '__$${.PARSEDIR}/$${.PARSEFILE}__: .NOTMAIN' \ 482148ee845SSimon J. Gerraty '.endif' 483148ee845SSimon J. Gerraty# expect: Parse_PushInput: file subdir/target-indirect-PARSEDIR-PARSEFILE.tmp, line 1 484148ee845SSimon J. Gerraty# expect: Skipping 'subdir/target-indirect-PARSEDIR-PARSEFILE.tmp' because '__subdir/target-indirect-PARSEDIR-PARSEFILE.tmp__' is defined 485148ee845SSimon J. Gerraty# The actual target starts with '__${.OBJDIR}/', see the .rawout file, but the 486148ee845SSimon J. Gerraty# string '${.OBJDIR}/' gets stripped in post processing. 487148ee845SSimon J. Gerraty 488148ee845SSimon J. Gerraty# If the guard target is not defined when including the file the next time, 489148ee845SSimon J. Gerraty# the file is processed again. 490148ee845SSimon J. GerratyINCS+= target-unguarded 491148ee845SSimon J. GerratyLINES.target-unguarded= \ 492148ee845SSimon J. Gerraty '.if !target(target-unguarded)' \ 493148ee845SSimon J. Gerraty '.endif' 494148ee845SSimon J. Gerraty# expect: Parse_PushInput: file target-unguarded.tmp, line 1 495148ee845SSimon J. Gerraty# expect: Parse_PushInput: file target-unguarded.tmp, line 1 496148ee845SSimon J. Gerraty 497148ee845SSimon J. Gerraty# The guard condition must consist of only the guard target, nothing else. 498148ee845SSimon J. GerratyINCS+= target-plus 499148ee845SSimon J. GerratyLINES.target-plus= \ 500148ee845SSimon J. Gerraty '.if !target(target-plus) && 1' \ 501148ee845SSimon J. Gerraty 'target-plus: .NOTMAIN' \ 502148ee845SSimon J. Gerraty '.endif' 503148ee845SSimon J. Gerraty# expect: Parse_PushInput: file target-plus.tmp, line 1 504148ee845SSimon J. Gerraty# expect: Parse_PushInput: file target-plus.tmp, line 1 505148ee845SSimon J. Gerraty 506148ee845SSimon J. Gerraty# If the guard target is defined before the file is included the first time, 507*98875883SSimon J. Gerraty# the file is read once and then considered guarded. 508148ee845SSimon J. GerratyINCS+= target-already-defined 509148ee845SSimon J. GerratyLINES.target-already-defined= \ 510148ee845SSimon J. Gerraty '.if !target(target-already-defined)' \ 511148ee845SSimon J. Gerraty 'target-already-defined: .NOTMAIN' \ 512148ee845SSimon J. Gerraty '.endif' 513148ee845SSimon J. Gerratytarget-already-defined: .NOTMAIN 514148ee845SSimon J. Gerraty# expect: Parse_PushInput: file target-already-defined.tmp, line 1 515148ee845SSimon J. Gerraty# expect: Skipping 'target-already-defined.tmp' because 'target-already-defined' is defined 516148ee845SSimon J. Gerraty 517148ee845SSimon J. Gerraty# A target name cannot contain the character '!'. In the condition, the '!' 518148ee845SSimon J. Gerraty# is syntactically valid, but in the dependency declaration line, the '!' is 519148ee845SSimon J. Gerraty# interpreted as the '!' dependency operator, no matter whether it occurs at 520148ee845SSimon J. Gerraty# the beginning or in the middle of a target name. Escaping it as '${:U!}' 521148ee845SSimon J. Gerraty# doesn't work, as the whole line is first expanded and then scanned for the 522148ee845SSimon J. Gerraty# dependency operator. Escaping it as '\!' doesn't work either, even though 523148ee845SSimon J. Gerraty# the '\' escapes the '!' from being a dependency operator, but when reading 524148ee845SSimon J. Gerraty# the target name, the '\' is kept, resulting in the target name 525148ee845SSimon J. Gerraty# '\!target-name-exclamation' instead of '!target-name-exclamation'. 526148ee845SSimon J. GerratyINCS+= target-name-exclamation 527148ee845SSimon J. GerratyLINES.target-name-exclamation= \ 528148ee845SSimon J. Gerraty '.if !target(!target-name-exclamation)' \ 529148ee845SSimon J. Gerraty '\!target-name-exclamation: .NOTMAIN' \ 530148ee845SSimon J. Gerraty '.endif' 531148ee845SSimon J. Gerraty# expect: Parse_PushInput: file target-name-exclamation.tmp, line 1 532148ee845SSimon J. Gerraty# expect: Parse_PushInput: file target-name-exclamation.tmp, line 1 533148ee845SSimon J. Gerraty 534*98875883SSimon J. Gerraty 535148ee845SSimon J. Gerraty# Now run all test cases by including each of the files twice and looking at 536148ee845SSimon J. Gerraty# the debug output. The files that properly guard against multiple inclusion 537148ee845SSimon J. Gerraty# generate a 'Skipping' line, the others repeat the 'Parse_PushInput' line. 538148ee845SSimon J. Gerraty# 539148ee845SSimon J. Gerraty# Some debug output lines are suppressed in the .exp file, see ./Makefile. 540148ee845SSimon J. Gerraty.for i in ${INCS} 541148ee845SSimon J. Gerraty. for fname in $i.tmp 542148ee845SSimon J. Gerraty_:= ${fname:H:N.:@dir@${:!mkdir -p ${dir}!}@} 543148ee845SSimon J. Gerraty_!= printf '%s\n' ${LINES.$i} > ${fname} 544148ee845SSimon J. Gerraty.MAKEFLAGS: -dp 545148ee845SSimon J. Gerraty.include "${.CURDIR}/${fname}" 546148ee845SSimon J. Gerraty.undef ${UNDEF_BETWEEN.$i:U} 547148ee845SSimon J. Gerraty.include "${.CURDIR}/${fname}" 548148ee845SSimon J. Gerraty.MAKEFLAGS: -d0 549148ee845SSimon J. Gerraty_!= rm ${fname} 550148ee845SSimon J. Gerraty_:= ${fname:H:N.:@dir@${:!rmdir ${dir}!}@} 551148ee845SSimon J. Gerraty. endfor 552148ee845SSimon J. Gerraty.endfor 553148ee845SSimon J. Gerraty 554148ee845SSimon J. Gerratyall: 555