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