xref: /freebsd/contrib/bmake/unit-tests/varname.mk (revision 19261079)
1# $NetBSD: varname.mk,v 1.8 2020/11/02 22:59:48 rillig Exp $
2#
3# Tests for special variables, such as .MAKE or .PARSEDIR.
4# And for variable names in general.
5
6.MAKEFLAGS: -dv
7
8# In variable names, braces are allowed, but they must be balanced.
9# Parentheses and braces may be mixed.
10VAR{{{}}}=	3 braces
11.if "${VAR{{{}}}}" != "3 braces"
12.  error
13.endif
14
15# In variable expressions, the parser works differently.  It doesn't treat
16# braces and parentheses equally, therefore the first closing brace already
17# marks the end of the variable name.
18VARNAME=	VAR(((
19${VARNAME}=	3 open parentheses
20.if "${VAR(((}}}}" != "3 open parentheses}}}"
21.  error
22.endif
23
24# In the above test, the variable name is constructed indirectly.  Neither
25# of the following expressions produces the intended effect.
26#
27# This is not a variable assignment since the parentheses and braces are not
28# balanced.  At the end of the line, there are still 3 levels open, which
29# means the variable name is not finished.
30${:UVAR(((}=	try1
31# On the left-hand side of a variable assignments, the backslash is not parsed
32# as an escape character, therefore the parentheses still count to the nesting
33# level, which at the end of the line is still 3.  Therefore this is not a
34# variable assignment as well.
35${:UVAR\(\(\(}=	try2
36# To assign to a variable with an arbitrary name, the variable name has to
37# come from an external source, not the text that is parsed in the assignment
38# itself.  This is exactly the reason why further above, the indirect
39# ${VARNAME} works, while all other attempts fail.
40${VARNAME}=	try3
41
42.MAKEFLAGS: -d0
43
44all:
45