xref: /freebsd/contrib/bmake/unit-tests/directive-ifndef.mk (revision 725a9f47324d42037db93c27ceb40d4956872f3e)
1# $NetBSD: directive-ifndef.mk,v 1.9 2023/10/19 18:24:33 rillig Exp $
2#
3# Tests for the .ifndef directive, which can be used for multiple-inclusion
4# guards.  In contrast to C, where #ifndef and #define nicely line up the
5# macro name, there is no such syntax in make.  Therefore, it is more
6# common to use .if !defined(GUARD) instead.
7#
8# See also:
9#	directive-include-guard.mk
10
11.ifndef GUARD
12GUARD=	# defined
13# expect+1: guarded section
14.  info guarded section
15.endif
16
17.ifndef GUARD
18GUARD=	# defined
19.  info guarded section
20.endif
21
22.if !defined(GUARD)
23GUARD=	# defined
24.  info guarded section
25.endif
26
27
28# The '.ifndef' directive can be used with multiple arguments, even negating
29# them.  Since these conditions are confusing for humans, they should be
30# replaced with easier-to-understand plain '.if' directives.
31DEFINED=
32.ifndef UNDEFINED && UNDEFINED
33.else
34.  error
35.endif
36.ifndef UNDEFINED && DEFINED
37.  error
38.endif
39.ifndef DEFINED && DEFINED
40.  error
41.endif
42.ifndef !UNDEFINED && !UNDEFINED
43.  error
44.endif
45.ifndef !UNDEFINED && !DEFINED
46.  error
47.endif
48.ifndef !DEFINED && !DEFINED
49.else
50.  error
51.endif
52
53
54# The negation from the 'if-not-defined' directive only applies to bare words,
55# but not to numbers, quoted strings or expressions.  Those are evaluated
56# without extra negation, just like in a plain '.if' directive.
57.ifndef 0
58.  error
59.endif
60.ifndef 1
61.else
62.  error
63.endif
64.ifndef ""
65.  error
66.endif
67.ifndef "word"
68.else
69.  error
70.endif
71.ifndef ${:UUNDEFINED}
72.else
73.  error
74.endif
75.ifndef ${:UDEFINED}
76.  error
77.endif
78.ifndef ${:U0}
79.  error
80.endif
81.ifndef ${:U1}
82.else
83.  error
84.endif
85
86
87all:
88