Home | History | Annotate | Line # | Download | only in unit-tests
directive-include-guard.mk revision 1.6
      1  1.6  rillig # $NetBSD: directive-include-guard.mk,v 1.6 2023/06/19 20:07:35 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.6  rillig # A conditional other than '.if' or '.ifndef' marks the file as non-guarded,
     63  1.6  rillig # even if it would actually work as a multiple-inclusion guard.
     64  1.6  rillig INCS+=	ifdef-negated
     65  1.6  rillig LINES.ifdef-negated= \
     66  1.6  rillig 	'.ifdef !IFDEF_NEGATED' \
     67  1.6  rillig 	'IFDEF_NEGATED=' \
     68  1.6  rillig 	'.endif'
     69  1.6  rillig # expect: Parse_PushInput: file ifdef-negated.tmp, line 1
     70  1.6  rillig # expect: Parse_PushInput: file ifdef-negated.tmp, line 1
     71  1.6  rillig 
     72  1.1  rillig # The variable names in the '.if' and the assignment must be the same.
     73  1.1  rillig INCS+=	varname-mismatch
     74  1.1  rillig LINES.varname-mismatch= \
     75  1.1  rillig 	'.ifndef VARNAME_MISMATCH' \
     76  1.1  rillig 	'OTHER_NAME=' \
     77  1.1  rillig 	'.endif'
     78  1.4  rillig # expect: Parse_PushInput: file varname-mismatch.tmp, line 1
     79  1.4  rillig # expect: Parse_PushInput: file varname-mismatch.tmp, line 1
     80  1.1  rillig 
     81  1.6  rillig # The guard condition must consist of only the guard variable, nothing else.
     82  1.6  rillig INCS+=	ifndef-plus
     83  1.6  rillig LINES.ifndef-plus= \
     84  1.6  rillig 	'.ifndef IFNDEF_PLUS && IFNDEF_SECOND' \
     85  1.6  rillig 	'IFNDEF_PLUS=' \
     86  1.6  rillig 	'IFNDEF_SECOND=' \
     87  1.6  rillig 	'.endif'
     88  1.6  rillig # expect: Parse_PushInput: file ifndef-plus.tmp, line 1
     89  1.6  rillig # expect: Parse_PushInput: file ifndef-plus.tmp, line 1
     90  1.6  rillig 
     91  1.6  rillig # The guard condition must consist of only the guard variable, nothing else.
     92  1.6  rillig INCS+=	if-plus
     93  1.6  rillig LINES.if-plus= \
     94  1.6  rillig 	'.if !defined(IF_PLUS) && !defined(IF_SECOND)' \
     95  1.6  rillig 	'IF_PLUS=' \
     96  1.6  rillig 	'IF_SECOND=' \
     97  1.6  rillig 	'.endif'
     98  1.6  rillig # expect: Parse_PushInput: file if-plus.tmp, line 1
     99  1.6  rillig # expect: Parse_PushInput: file if-plus.tmp, line 1
    100  1.6  rillig 
    101  1.6  rillig # The variable name in an '.ifndef' guard must be given directly, it must not
    102  1.6  rillig # contain any '$' expression.
    103  1.6  rillig INCS+=	ifndef-indirect
    104  1.6  rillig LINES.ifndef-indirect= \
    105  1.6  rillig 	'.ifndef $${IFNDEF_INDIRECT:L}' \
    106  1.6  rillig 	'IFNDEF_INDIRECT=' \
    107  1.6  rillig 	'.endif'
    108  1.6  rillig # expect: Parse_PushInput: file ifndef-indirect.tmp, line 1
    109  1.6  rillig # expect: Parse_PushInput: file ifndef-indirect.tmp, line 1
    110  1.6  rillig 
    111  1.6  rillig # The variable name in an '.if' guard must be given directly, it must not contain
    112  1.6  rillig # any '$' expression.
    113  1.6  rillig INCS+=	if-indirect
    114  1.6  rillig LINES.if-indirect= \
    115  1.6  rillig 	'.if !defined($${IF_INDIRECT:L})' \
    116  1.6  rillig 	'IF_INDIRECT=' \
    117  1.6  rillig 	'.endif'
    118  1.6  rillig # expect: Parse_PushInput: file if-indirect.tmp, line 1
    119  1.6  rillig # expect: Parse_PushInput: file if-indirect.tmp, line 1
    120  1.6  rillig 
    121  1.5  rillig # The variable name in the guard condition must only contain alphanumeric
    122  1.5  rillig # characters and underscores.  The guard variable is more flexible, it can be
    123  1.5  rillig # set anywhere, as long as it is set when the guarded file is included next.
    124  1.6  rillig INCS+=	varassign-indirect
    125  1.6  rillig LINES.varassign-indirect= \
    126  1.6  rillig 	'.ifndef VARASSIGN_INDIRECT' \
    127  1.6  rillig 	'$${VARASSIGN_INDIRECT:L}=' \
    128  1.1  rillig 	'.endif'
    129  1.6  rillig # expect: Parse_PushInput: file varassign-indirect.tmp, line 1
    130  1.6  rillig # expect: Skipping 'varassign-indirect.tmp' because 'VARASSIGN_INDIRECT' is already set
    131  1.1  rillig 
    132  1.5  rillig # The time at which the guard variable is set doesn't matter, as long as it is
    133  1.5  rillig # set when the file is included the next time.
    134  1.1  rillig INCS+=	late-assignment
    135  1.1  rillig LINES.late-assignment= \
    136  1.1  rillig 	'.ifndef LATE_ASSIGNMENT' \
    137  1.1  rillig 	'OTHER=' \
    138  1.1  rillig 	'LATE_ASSIGNMENT=' \
    139  1.1  rillig 	'.endif'
    140  1.4  rillig # expect: Parse_PushInput: file late-assignment.tmp, line 1
    141  1.5  rillig # expect: Skipping 'late-assignment.tmp' because 'LATE_ASSIGNMENT' is already set
    142  1.1  rillig 
    143  1.5  rillig # The time at which the guard variable is set doesn't matter, as long as it is
    144  1.5  rillig # set when the file is included the next time.
    145  1.1  rillig INCS+=	two-conditions
    146  1.1  rillig LINES.two-conditions= \
    147  1.1  rillig 	'.ifndef TWO_CONDITIONS' \
    148  1.2  rillig 	'.  if 1' \
    149  1.1  rillig 	'TWO_CONDITIONS=' \
    150  1.1  rillig 	'.  endif' \
    151  1.1  rillig 	'.endif'
    152  1.4  rillig # expect: Parse_PushInput: file two-conditions.tmp, line 1
    153  1.5  rillig # expect: Skipping 'two-conditions.tmp' because 'TWO_CONDITIONS' is already set
    154  1.1  rillig 
    155  1.1  rillig # If the guard variable is already set before the file is included for the
    156  1.5  rillig # first time, the file is not considered guarded, as the makefile parser skips
    157  1.5  rillig # all lines in the inactive part between the '.ifndef' and the '.endif'.
    158  1.1  rillig INCS+=	already-set
    159  1.1  rillig LINES.already-set= \
    160  1.1  rillig 	'.ifndef ALREADY_SET' \
    161  1.1  rillig 	'ALREADY_SET=' \
    162  1.1  rillig 	'.endif'
    163  1.1  rillig ALREADY_SET=
    164  1.4  rillig # expect: Parse_PushInput: file already-set.tmp, line 1
    165  1.4  rillig # expect: Parse_PushInput: file already-set.tmp, line 1
    166  1.1  rillig 
    167  1.1  rillig # The whole file content must be guarded by a single '.if' conditional, not by
    168  1.1  rillig # several, even if they have the same effect.
    169  1.1  rillig INCS+=	twice
    170  1.1  rillig LINES.twice= \
    171  1.5  rillig 	'.ifndef TWICE_FIRST' \
    172  1.5  rillig 	'TWICE_FIRST=' \
    173  1.1  rillig 	'.endif' \
    174  1.5  rillig 	'.ifndef TWICE_SECOND' \
    175  1.5  rillig 	'TWICE_SECOND=' \
    176  1.1  rillig 	'.endif'
    177  1.4  rillig # expect: Parse_PushInput: file twice.tmp, line 1
    178  1.4  rillig # expect: Parse_PushInput: file twice.tmp, line 1
    179  1.1  rillig 
    180  1.1  rillig # When multiple files use the same guard variable name, they exclude each
    181  1.5  rillig # other.  It's the responsibility of the makefile authors to choose unique
    182  1.5  rillig # variable names.  Typical choices are ${PROJECT}_${DIR}_${FILE}_MK.  This is
    183  1.5  rillig # the same situation as in the 'already-set' test, and the file is not
    184  1.5  rillig # considered guarded.
    185  1.1  rillig INCS+=	reuse
    186  1.1  rillig LINES.reuse= \
    187  1.1  rillig 	${LINES.guarded-if}
    188  1.4  rillig # expect: Parse_PushInput: file reuse.tmp, line 1
    189  1.4  rillig # expect: Parse_PushInput: file reuse.tmp, line 1
    190  1.1  rillig 
    191  1.1  rillig # The conditional must come before the assignment, otherwise the conditional
    192  1.1  rillig # is useless, as it always evaluates to false.
    193  1.1  rillig INCS+=	swapped
    194  1.1  rillig LINES.swapped= \
    195  1.1  rillig 	'SWAPPED=' \
    196  1.1  rillig 	'.ifndef SWAPPED' \
    197  1.1  rillig 	'.endif'
    198  1.4  rillig # expect: Parse_PushInput: file swapped.tmp, line 1
    199  1.4  rillig # expect: Parse_PushInput: file swapped.tmp, line 1
    200  1.1  rillig 
    201  1.2  rillig # If the guard variable is undefined at some later point, the guarded file is
    202  1.2  rillig # included again.
    203  1.2  rillig INCS+=	undef-between
    204  1.2  rillig LINES.undef-between= \
    205  1.2  rillig 	'.ifndef UNDEF_BETWEEN' \
    206  1.2  rillig 	'UNDEF_BETWEEN=' \
    207  1.2  rillig 	'.endif'
    208  1.4  rillig # expect: Parse_PushInput: file undef-between.tmp, line 1
    209  1.4  rillig # expect: Parse_PushInput: file undef-between.tmp, line 1
    210  1.2  rillig 
    211  1.2  rillig # If the guarded file undefines the guard variable, the guarded file is
    212  1.2  rillig # included again.
    213  1.2  rillig INCS+=	undef-inside
    214  1.2  rillig LINES.undef-inside= \
    215  1.2  rillig 	'.ifndef UNDEF_INSIDE' \
    216  1.2  rillig 	'UNDEF_INSIDE=' \
    217  1.2  rillig 	'.undef UNDEF_INSIDE' \
    218  1.2  rillig 	'.endif'
    219  1.4  rillig # expect: Parse_PushInput: file undef-inside.tmp, line 1
    220  1.4  rillig # expect: Parse_PushInput: file undef-inside.tmp, line 1
    221  1.2  rillig 
    222  1.2  rillig # The outermost '.if' must not have an '.elif' branch.
    223  1.2  rillig INCS+=	if-elif
    224  1.2  rillig LINES.if-elif = \
    225  1.2  rillig 	'.ifndef IF_ELIF' \
    226  1.2  rillig 	'IF_ELIF=' \
    227  1.2  rillig 	'.elif 1' \
    228  1.2  rillig 	'.endif'
    229  1.4  rillig # expect: Parse_PushInput: file if-elif.tmp, line 1
    230  1.4  rillig # expect: Parse_PushInput: file if-elif.tmp, line 1
    231  1.2  rillig 
    232  1.2  rillig # The outermost '.if' must not have an '.else' branch.
    233  1.2  rillig INCS+=	if-else
    234  1.2  rillig LINES.if-else = \
    235  1.2  rillig 	'.ifndef IF_ELSE' \
    236  1.2  rillig 	'IF_ELSE=' \
    237  1.2  rillig 	'.else' \
    238  1.2  rillig 	'.endif'
    239  1.4  rillig # expect: Parse_PushInput: file if-else.tmp, line 1
    240  1.4  rillig # expect: Parse_PushInput: file if-else.tmp, line 1
    241  1.2  rillig 
    242  1.2  rillig # The inner '.if' directives may have an '.elif' or '.else'.
    243  1.2  rillig INCS+=	inner-if-elif-else
    244  1.2  rillig LINES.inner-if-elif-else = \
    245  1.2  rillig 	'.ifndef INNER_IF_ELIF_ELSE' \
    246  1.2  rillig 	'INNER_IF_ELIF_ELSE=' \
    247  1.2  rillig 	'.  if 0' \
    248  1.2  rillig 	'.  elif 0' \
    249  1.2  rillig 	'.  else' \
    250  1.2  rillig 	'.  endif' \
    251  1.2  rillig 	'.  if 0' \
    252  1.2  rillig 	'.  elif 1' \
    253  1.2  rillig 	'.  else' \
    254  1.2  rillig 	'.  endif' \
    255  1.2  rillig 	'.  if 1' \
    256  1.2  rillig 	'.  elif 1' \
    257  1.2  rillig 	'.  else' \
    258  1.2  rillig 	'.  endif' \
    259  1.2  rillig 	'.endif'
    260  1.4  rillig # expect: Parse_PushInput: file inner-if-elif-else.tmp, line 1
    261  1.5  rillig # expect: Skipping 'inner-if-elif-else.tmp' because 'INNER_IF_ELIF_ELSE' is already set
    262  1.5  rillig 
    263  1.1  rillig 
    264  1.1  rillig # Include each of the files twice.  The directive-include-guard.exp file
    265  1.1  rillig # contains a single entry for the files whose multiple-inclusion guard works,
    266  1.1  rillig # and two entries for the files that are not protected against multiple
    267  1.1  rillig # inclusion.
    268  1.1  rillig #
    269  1.1  rillig # Some debug output lines are suppressed in the .exp file, see ./Makefile.
    270  1.1  rillig .for i in ${INCS}
    271  1.4  rillig .  for fname in $i.tmp
    272  1.1  rillig _!=	printf '%s\n' ${LINES.$i} > ${fname}
    273  1.1  rillig .MAKEFLAGS: -dp
    274  1.1  rillig .include "${.CURDIR}/${fname}"
    275  1.2  rillig .undef ${i:Mundef-between:%=UNDEF_BETWEEN}
    276  1.1  rillig .include "${.CURDIR}/${fname}"
    277  1.1  rillig .MAKEFLAGS: -d0
    278  1.1  rillig _!=	rm ${fname}
    279  1.1  rillig .  endfor
    280  1.1  rillig .endfor
    281  1.1  rillig 
    282  1.1  rillig all:
    283