1*d5e0a182SSimon J. Gerraty# $NetBSD: deptgt-makeflags.mk,v 1.9 2023/11/19 22:32:44 rillig Exp $ 22c3632d1SSimon J. Gerraty# 3956e45f6SSimon J. Gerraty# Tests for the special target .MAKEFLAGS in dependency declarations, 4956e45f6SSimon J. Gerraty# which adds command line options later, at parse time. 5e2eeea75SSimon J. Gerraty# 6e2eeea75SSimon J. Gerraty# In these unit tests, it is often used to temporarily toggle the debug log 7e2eeea75SSimon J. Gerraty# during parsing. 82c3632d1SSimon J. Gerraty 9956e45f6SSimon J. Gerraty# The -D option sets a variable in the "Global" scope and thus can be 10956e45f6SSimon J. Gerraty# undefined later. 11956e45f6SSimon J. Gerraty.MAKEFLAGS: -D VAR 12956e45f6SSimon J. Gerraty.if ${VAR} != 1 13956e45f6SSimon J. Gerraty. error 14956e45f6SSimon J. Gerraty.endif 15956e45f6SSimon J. Gerraty 16e2eeea75SSimon J. Gerraty# Variables that are set via the -D command line option are normal global 17e2eeea75SSimon J. Gerraty# variables and can thus be undefined later. 18956e45f6SSimon J. Gerraty.undef VAR 19956e45f6SSimon J. Gerraty.if defined(VAR) 20956e45f6SSimon J. Gerraty. error 21956e45f6SSimon J. Gerraty.endif 22956e45f6SSimon J. Gerraty 23e2eeea75SSimon J. Gerraty# The -D command line option can define a variable again, after it has been 24e2eeea75SSimon J. Gerraty# undefined. 25956e45f6SSimon J. Gerraty.MAKEFLAGS: -D VAR 26956e45f6SSimon J. Gerraty.if ${VAR} != 1 27956e45f6SSimon J. Gerraty. error 28956e45f6SSimon J. Gerraty.endif 29956e45f6SSimon J. Gerraty 30e2eeea75SSimon J. Gerraty# The "dependency" for .MAKEFLAGS is split into words, interpreting the usual 31e2eeea75SSimon J. Gerraty# quotes and escape sequences from the backslash. 32956e45f6SSimon J. Gerraty.MAKEFLAGS: VAR="value"' with'\ spaces 33956e45f6SSimon J. Gerraty.if ${VAR} != "value with spaces" 34956e45f6SSimon J. Gerraty. error 35956e45f6SSimon J. Gerraty.endif 36956e45f6SSimon J. Gerraty 37956e45f6SSimon J. Gerraty# Variables set on the command line as VAR=value are placed in the 38956e45f6SSimon J. Gerraty# "Command" scope and thus cannot be undefined. 39956e45f6SSimon J. Gerraty.undef VAR 40956e45f6SSimon J. Gerraty.if ${VAR} != "value with spaces" 41956e45f6SSimon J. Gerraty. error 42956e45f6SSimon J. Gerraty.endif 43956e45f6SSimon J. Gerraty 44956e45f6SSimon J. Gerraty# When parsing this line, each '$$' becomes '$', resulting in '$$$$'. 45956e45f6SSimon J. Gerraty# This is assigned to the variable DOLLAR. 46956e45f6SSimon J. Gerraty# In the condition, that variable is expanded, and at that point, each '$$' 47956e45f6SSimon J. Gerraty# becomes '$' again, the final expression is thus '$$'. 48956e45f6SSimon J. Gerraty.MAKEFLAGS: -dcv 49956e45f6SSimon J. Gerraty.MAKEFLAGS: DOLLAR=$$$$$$$$ 50956e45f6SSimon J. Gerraty.if ${DOLLAR} != "\$\$" 51956e45f6SSimon J. Gerraty.endif 52956e45f6SSimon J. Gerraty.MAKEFLAGS: -d0 532c3632d1SSimon J. Gerraty 54e2eeea75SSimon J. Gerraty# An empty command line is skipped. 55e2eeea75SSimon J. Gerraty.MAKEFLAGS: # none 56e2eeea75SSimon J. Gerraty 57e2eeea75SSimon J. Gerraty# Escape sequences like \n are interpreted. 58e2eeea75SSimon J. Gerraty# The following line looks as if it assigned a newline to nl, but it doesn't. 59e2eeea75SSimon J. Gerraty# Instead, the \n ends up as a line that is then interpreted as a variable 60e2eeea75SSimon J. Gerraty# assignment. At that point, the line is simply "nl=\n", and the \n is 61e2eeea75SSimon J. Gerraty# skipped since it is whitespace (see Parse_IsVar). 62e2eeea75SSimon J. Gerraty.MAKEFLAGS: nl="\n" 63e2eeea75SSimon J. Gerraty.if ${nl} != "" 64e2eeea75SSimon J. Gerraty. error 65e2eeea75SSimon J. Gerraty.endif 66e2eeea75SSimon J. Gerraty 67e2eeea75SSimon J. Gerraty# Next try at defining another newline variable. Since whitespace around the 68*d5e0a182SSimon J. Gerraty# variable value is trimmed, two empty expressions ${:U} surround the 69e2eeea75SSimon J. Gerraty# literal newline now. This prevents the newline from being skipped during 70*d5e0a182SSimon J. Gerraty# parsing. The ':=' assignment operator expands the empty 71e2eeea75SSimon J. Gerraty# expressions, leaving only the newline as the variable value. 72e2eeea75SSimon J. Gerraty# 73e2eeea75SSimon J. Gerraty# This is one of the very few ways (maybe even the only one) to inject literal 74e2eeea75SSimon J. Gerraty# newlines into a line that is being parsed. This may confuse the parser. 75e2eeea75SSimon J. Gerraty# For example, in cond.c the parser only expects horizontal whitespace (' ' 76e2eeea75SSimon J. Gerraty# and '\t'), but no newlines. 77e2eeea75SSimon J. Gerraty#.MAKEFLAGS: -dcpv 78e2eeea75SSimon J. Gerraty.MAKEFLAGS: nl:="$${:U}\n$${:U}" 79e2eeea75SSimon J. Gerraty.if ${nl} != ${.newline} 80e2eeea75SSimon J. Gerraty. error 81e2eeea75SSimon J. Gerraty.endif 82e2eeea75SSimon J. Gerraty#.MAKEFLAGS: -d0 83e2eeea75SSimon J. Gerraty 8412904384SSimon J. Gerraty# Now do the same for the other escape sequences; see Substring_Words. 8512904384SSimon J. Gerraty.MAKEFLAGS: CHAR_BS:="$${:U}\b$${:U}" 8612904384SSimon J. Gerraty.MAKEFLAGS: CHAR_FF:="$${:U}\f$${:U}" 8712904384SSimon J. Gerraty.MAKEFLAGS: CHAR_NL:="$${:U}\n$${:U}" 8812904384SSimon J. Gerraty.MAKEFLAGS: CHAR_CR:="$${:U}\r$${:U}" 8912904384SSimon J. Gerraty.MAKEFLAGS: CHAR_TAB:="$${:U}\t$${:U}" 9012904384SSimon J. Gerraty 9112904384SSimon J. Gerraty# Note: backspace is not whitespace, it is a control character. 9212904384SSimon J. Gerraty.if ${CHAR_BS:C,^[[:cntrl:]]$,found,W} != "found" 9312904384SSimon J. Gerraty. error 9412904384SSimon J. Gerraty.endif 9512904384SSimon J. Gerraty.if ${CHAR_FF:C,^[[:space:]]$,found,W} != "found" 9612904384SSimon J. Gerraty. error 9712904384SSimon J. Gerraty.endif 9812904384SSimon J. Gerraty.if ${CHAR_NL:C,^[[:space:]]$,found,W} != "found" 9912904384SSimon J. Gerraty. error 10012904384SSimon J. Gerraty.endif 10112904384SSimon J. Gerraty.if ${CHAR_CR:C,^[[:space:]]$,found,W} != "found" 10212904384SSimon J. Gerraty. error 10312904384SSimon J. Gerraty.endif 10412904384SSimon J. Gerraty.if ${CHAR_TAB:C,^[[:space:]]$,found,W} != "found" 10512904384SSimon J. Gerraty. error 10612904384SSimon J. Gerraty.endif 10712904384SSimon J. Gerraty 10812904384SSimon J. Gerraty 109e2eeea75SSimon J. Gerraty# Unbalanced quotes produce an error message. If they occur anywhere in the 110e2eeea75SSimon J. Gerraty# command line, the whole command line is skipped. 111e2eeea75SSimon J. Gerraty.MAKEFLAGS: VAR=previous 112e2eeea75SSimon J. Gerraty.MAKEFLAGS: VAR=initial UNBALANCED=' 113e2eeea75SSimon J. Gerraty.if ${VAR} != "previous" 114e2eeea75SSimon J. Gerraty. error 115e2eeea75SSimon J. Gerraty.endif 116e2eeea75SSimon J. Gerraty 1172c3632d1SSimon J. Gerratyall: 1182c3632d1SSimon J. Gerraty @:; 119