动态目标中的目标变量
target variable in dynamic target
我有一个更复杂的 Makefile,其中包含动态生成的目标,如下所示:
COMPONENTS = foo bar lipsum
define TEST_template =
.PHONY: test-$(1)
test-$(1):
@echo test
endef
$(foreach cmpnt,$(COMPONENTS),$(eval $(call TEST_template,$(cmpnt))))
但现在我想向该目标添加一个特定于目标的变量。对于非动态目标,这很好用:
test: VAR_ONE=1
test:
@echo "VAR_ONE=$(VAR_ONE)"
但是结合这两个是行不通的
COMPONENTS = foo bar lipsum
define TEST_template =
.PHONY: test2-$(1)
test2-$(1): VAR_ONE=1
test2-$(1):
@echo "test - VAR_ONE=$(VAR_ONE)"
endef
$(foreach cmpnt,$(COMPONENTS),$(eval $(call TEST_template,$(cmpnt))))
现在运行make test2-foo
returnstest - VAR_ONE=
,看来变量没有设置。
这可能吗?几天来我一直在努力实现这一目标,但我找不到任何东西。
你必须写
@echo "test - VAR_ONE=$$(VAR_ONE)"
(注意双 $$
)。参见 info make
It's important to realize that the 'eval' argument is expanded
twice; first by the 'eval' function, then the results of that expansion are expanded again when they are parsed as makefile syntax.
This means you may need to provide extra levels of escaping for "$"
您必须转义 define
中您不想被 call
解释的任何变量引用。所以它是:
define TEST_template =
.PHONY: test2-$(1)
test2-$(1): VAR_ONE=1
test2-$(1):
@echo "test - VAR_ONE=$$(VAR_ONE)"
endef
一个好的经验法则(尽管与任何事情一样,也有例外)是当定义一个要与 call
/eval
对一起使用的变量时,[=12 的参数=](例如,</code>、<code>
等)应直接引用,所有其他变量应转义。
我有一个更复杂的 Makefile,其中包含动态生成的目标,如下所示:
COMPONENTS = foo bar lipsum
define TEST_template =
.PHONY: test-$(1)
test-$(1):
@echo test
endef
$(foreach cmpnt,$(COMPONENTS),$(eval $(call TEST_template,$(cmpnt))))
但现在我想向该目标添加一个特定于目标的变量。对于非动态目标,这很好用:
test: VAR_ONE=1
test:
@echo "VAR_ONE=$(VAR_ONE)"
但是结合这两个是行不通的
COMPONENTS = foo bar lipsum
define TEST_template =
.PHONY: test2-$(1)
test2-$(1): VAR_ONE=1
test2-$(1):
@echo "test - VAR_ONE=$(VAR_ONE)"
endef
$(foreach cmpnt,$(COMPONENTS),$(eval $(call TEST_template,$(cmpnt))))
现在运行make test2-foo
returnstest - VAR_ONE=
,看来变量没有设置。
这可能吗?几天来我一直在努力实现这一目标,但我找不到任何东西。
你必须写
@echo "test - VAR_ONE=$$(VAR_ONE)"
(注意双 $$
)。参见 info make
It's important to realize that the 'eval' argument is expanded twice; first by the 'eval' function, then the results of that expansion are expanded again when they are parsed as makefile syntax. This means you may need to provide extra levels of escaping for "$"
您必须转义 define
中您不想被 call
解释的任何变量引用。所以它是:
define TEST_template =
.PHONY: test2-$(1)
test2-$(1): VAR_ONE=1
test2-$(1):
@echo "test - VAR_ONE=$$(VAR_ONE)"
endef
一个好的经验法则(尽管与任何事情一样,也有例外)是当定义一个要与 call
/eval
对一起使用的变量时,[=12 的参数=](例如,</code>、<code>
等)应直接引用,所有其他变量应转义。