# $NetBSD: var-scope-local.mk,v 1.7 2023/04/29 10:16:24 rillig Exp $ # # Tests for target-local variables, such as ${.TARGET} or $@. These variables # are relatively short-lived as they are created just before making the # target. In contrast, global variables are typically created when the # makefiles are read in. # # The 7 built-in target-local variables are listed in the manual page. They # are defined just before the target is actually made. Additional # target-local variables can be defined in dependency lines like # 'target: VAR=value', one at a time. .MAIN: all # Target-local variables in a target rule # # In target rules, '$*' only strips the extension off the pathname if the # extension is listed in '.SUFFIXES'. # # expect: target-rule.ext: * = all: target-rule.ext dir/subdir/target-rule.ext target-rule.ext dir/subdir/target-rule.ext: .PHONY @echo '$@: @ = <${@:Uundefined}>' @echo '$@: % = <${%:Uundefined}>' @echo '$@: ? = <${?:Uundefined}>' @echo '$@: < = <${<:Uundefined}>' @echo '$@: * = <${*:Uundefined}>' .SUFFIXES: .ir-gen-from .ir-from .ir-to # In target rules, '$*' strips the extension off the pathname of the target # if the extension is listed in '.SUFFIXES'. # # expect: target-rule.ir-gen-from: * = all: target-rule.ir-gen-from dir/subdir/target-rule-dir.ir-gen-from target-rule.ir-gen-from dir/subdir/target-rule-dir.ir-gen-from: @echo '$@: @ = <${@:Uundefined}>' @echo '$@: % = <${%:Uundefined}>' @echo '$@: ? = <${?:Uundefined}>' @echo '$@: < = <${<:Uundefined}>' @echo '$@: * = <${*:Uundefined}>' .ir-from.ir-to: @echo '$@: @ = <${@:Uundefined}>' @echo '$@: % = <${%:Uundefined}>' @echo '$@: ? = <${?:Uundefined}>' @echo '$@: < = <${<:Uundefined}>' @echo '$@: * = <${*:Uundefined}>' .ir-gen-from.ir-from: @echo '$@: @ = <${@:Uundefined}>' @echo '$@: % = <${%:Uundefined}>' @echo '$@: ? = <${?:Uundefined}>' @echo '$@: < = <${<:Uundefined}>' @echo '$@: * = <${*:Uundefined}>' # Target-local variables in an inference rule all: inference-rule.ir-to dir/subdir/inference-rule.ir-to inference-rule.ir-from: .PHONY dir/subdir/inference-rule.ir-from: .PHONY # Target-local variables in a chain of inference rules all: inference-rule-chain.ir-to dir/subdir/inference-rule-chain.ir-to inference-rule-chain.ir-gen-from: .PHONY dir/subdir/inference-rule-chain.ir-gen-from: .PHONY # The run-time 'check' directives from above happen after the parse-time # 'check' directives from below. # # expect-reset # Deferred evaluation during parsing # # The target-local variables can be used in expressions, just like other # variables. When these expressions are evaluated outside of a target, these # expressions are not yet expanded, instead their text is preserved, to allow # these expressions to expand right in time when the target-local variables # are actually set. # # Conditions from .if directives are evaluated in the scope of the command # line, which means that variables from the command line, from the global # scope and from the environment are resolved, in this precedence order (but # see the command line option '-e'). In that phase, expressions involving # target-local variables need to be preserved, including the exact names of # the variables. # # Each of the built-in target-local variables has two equivalent names, for # example '@' is equivalent to '.TARGET'. The implementation might # canonicalize these aliases at some point, and that might be surprising. # This aliasing happens for single-character variable names like $@ or $< # (see VarFind, CanonicalVarname), but not for braced or parenthesized # expressions like ${@}, ${.TARGET} ${VAR:Mpattern} (see Var_Parse, # ParseVarname). # # In the following condition, make expands '$@' to the long-format alias # '$(.TARGET)'; note that the alias is not written with braces, as would be # common in BSD makefiles, but with parentheses. This alternative spelling # behaves the same though. .if $@ != "\$\(.TARGET)" . error .endif # In the long form of writing a target-local variable, the text of the # expression is preserved exactly as written, no matter whether it is written # with '{' or '('. .if ${@} != "\$\{@}" . error .endif .if $(@) != "\$\(@)" . error .endif # If the variable expression contains modifiers, the behavior depends on the # actual modifiers. The modifier ':M' keeps the expression in the state # 'undefined'. Since the expression is still undefined after evaluating all # the modifiers, the value of the expression is discarded and the expression # text is used instead. This preserves the expressions based on target-local # variables as long as possible. .if ${@:M*} != "\$\{@:M*}" . error .endif # In the following examples, the expressions are based on target-local # variables but use the modifier ':L', which turns an undefined expression # into a defined one. At the end of evaluating the expression, the state of # the expression is not 'undefined' anymore. The value of the expression # is the name of the variable, since that's what the modifier ':L' does. .if ${@:L} != "@" . error .endif .if ${.TARGET:L} != ".TARGET" . error .endif .if ${@F:L} != "@F" . error .endif .if ${@D:L} != "@D" . error .endif # Custom local variables # # Additional target-local variables may be defined in dependency lines. .MAKEFLAGS: -dv # In the following line, the ':=' may either be interpreted as an assignment # operator or as the dependency operator ':', followed by an empty variable # name and the assignment operator '='. It is the latter since in an # assignment, the left-hand side must be a single word or empty. # # The empty variable name is expanded twice, once for 'one' and once for # 'two'. # expect: Var_SetExpand: variable name "" expands to empty string, with value "three" - ignored # expect: Var_SetExpand: variable name "" expands to empty string, with value "three" - ignored one two:=three # If the two targets to the left are generated by a variable expression, the # line is parsed as a variable assignment since its left-hand side is a single # word. # expect: Global: one two = three ${:Uone two}:=three .MAKEFLAGS: -d0 .SUFFIXES: .c .o # One of the dynamic target-local variables is '.TARGET'. Since this is not # a suffix transformation rule, the variable '.IMPSRC' is not defined. # expect: : Making var-scope-local.c out of nothing. var-scope-local.c: : Making ${.TARGET} ${.IMPSRC:Dfrom ${.IMPSRC}:Uout of nothing}. # This is a suffix transformation rule, so both '.TARGET' and '.IMPSRC' are # defined. # expect: : Making var-scope-local.o from var-scope-local.c. # expect: : Making basename "var-scope-local.o" in "." from "var-scope-local.c" in ".". .c.o: : Making ${.TARGET} from ${.IMPSRC}. # The local variables @F, @D,