xref: /freebsd/contrib/bmake/unit-tests/deptgt-makeflags.mk (revision 79ac3c12a714bcd3f2354c52d948aed9575c46d6)
1# $NetBSD: deptgt-makeflags.mk,v 1.6 2020/11/15 20:20:58 rillig Exp $
2#
3# Tests for the special target .MAKEFLAGS in dependency declarations,
4# which adds command line options later, at parse time.
5#
6# In these unit tests, it is often used to temporarily toggle the debug log
7# during parsing.
8
9# The -D option sets a variable in the "Global" scope and thus can be
10# undefined later.
11.MAKEFLAGS: -D VAR
12.if ${VAR} != 1
13.  error
14.endif
15
16# Variables that are set via the -D command line option are normal global
17# variables and can thus be undefined later.
18.undef VAR
19.if defined(VAR)
20.  error
21.endif
22
23# The -D command line option can define a variable again, after it has been
24# undefined.
25.MAKEFLAGS: -D VAR
26.if ${VAR} != 1
27.  error
28.endif
29
30# The "dependency" for .MAKEFLAGS is split into words, interpreting the usual
31# quotes and escape sequences from the backslash.
32.MAKEFLAGS: VAR="value"' with'\ spaces
33.if ${VAR} != "value with spaces"
34.  error
35.endif
36
37# Variables set on the command line as VAR=value are placed in the
38# "Command" scope and thus cannot be undefined.
39.undef VAR
40.if ${VAR} != "value with spaces"
41.  error
42.endif
43
44# When parsing this line, each '$$' becomes '$', resulting in '$$$$'.
45# This is assigned to the variable DOLLAR.
46# In the condition, that variable is expanded, and at that point, each '$$'
47# becomes '$' again, the final expression is thus '$$'.
48.MAKEFLAGS: -dcv
49.MAKEFLAGS: DOLLAR=$$$$$$$$
50.if ${DOLLAR} != "\$\$"
51.endif
52.MAKEFLAGS: -d0
53
54# An empty command line is skipped.
55.MAKEFLAGS: # none
56
57# Escape sequences like \n are interpreted.
58# The following line looks as if it assigned a newline to nl, but it doesn't.
59# Instead, the \n ends up as a line that is then interpreted as a variable
60# assignment.  At that point, the line is simply "nl=\n", and the \n is
61# skipped since it is whitespace (see Parse_IsVar).
62.MAKEFLAGS: nl="\n"
63.if ${nl} != ""
64.  error
65.endif
66
67# Next try at defining another newline variable.  Since whitespace around the
68# variable value is trimmed, two empty variable expressions surround the
69# literal newline now.  This prevents the newline from being skipped during
70# parsing.  The ':=' assignment operator expands the empty variable
71# expressions, leaving only the newline as the variable value.
72#
73# This is one of the very few ways (maybe even the only one) to inject literal
74# newlines into a line that is being parsed.  This may confuse the parser.
75# For example, in cond.c the parser only expects horizontal whitespace (' '
76# and '\t'), but no newlines.
77#.MAKEFLAGS: -dcpv
78.MAKEFLAGS: nl:="$${:U}\n$${:U}"
79.if ${nl} != ${.newline}
80.  error
81.endif
82#.MAKEFLAGS: -d0
83
84# Unbalanced quotes produce an error message.  If they occur anywhere in the
85# command line, the whole command line is skipped.
86.MAKEFLAGS: VAR=previous
87.MAKEFLAGS: VAR=initial UNBALANCED='
88.if ${VAR} != "previous"
89.  error
90.endif
91
92all:
93	@:;
94