具有多个语句的 Makefile 函数
Makefile function with multiple statements
[注意]:Whosebug 上已经出现了一些类似的问题,但它们似乎没有完全解决我的问题。因此,我发布了这个问题。
我正在尝试编写一个 makefile 函数,该函数应将值设置为作为参数传递给函数的变量。
所以,我将此函数称为 -
RESULT :=
$(eval $(call myfunction,RESULT,value,res1,res2))
此处 'res1' 和 'res2' 是 RESULT 的两个可能结果值,参数 'value' 将用于某些测试条件。
以下是我对myfunction定义的尝试。不过好像不行。
define myfunction
TEST1 := $(shell test `mybinary` -ge 5 && printf "TEST")
TEST2 := $(findstring $(2),$(SOME_SHELL_ENV))
$(info "$(TEST1)")
$(info "$(TEST2)")
ifneq "$$(or $(TEST1),$(TEST2)" ""
LOCAL_RESULT := true
else
LOCAL_RESULT := false
endif
ifeq($(LOCAL_RESULT),true)
$(1) = $(3)
else
$(1) = $(4)
endif
endef
在我看来,局部变量 TEST1 和 TEST2 似乎还没有设置。
有人能告诉我为什么我的功能无法正常工作吗?我需要做哪些更改才能解决这些问题?
引用 eval manual page:
The eval
function is very special: [...] The argument to the eval
function is expanded, then the results of that expansion are parsed as makefile syntax.
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 “$” characters when using eval
.
这发生在 $(call)
参数被替换之后,因此 </code> 等在 <code>$(eval)
被调用时已经展开,不需要 $
转义.
要在 $(eval)
的最后(解析)阶段扩展变量,请通过加倍转义 $
非数字变量。
define myfunction
TEST1 := $$(shell test `echo 6` -ge 5 && printf "TEST")
TEST2 := $$(findstring $(2),$$(PATH))
$$(info "$$(TEST1)")
$$(info "$$(TEST2)")
ifneq "$$(or $$(TEST1),$$(TEST2))" ""
LOCAL_RESULT := true
else
LOCAL_RESULT := false
endif
ifeq ($$(LOCAL_RESULT), true)
$(1) = $(3)
else
$(1) = $(4)
endif
endef
$(eval $(call myfunction,RESULT,value,res1,res2))
test:
echo "$(LOCAL_RESULT)"
此外,您在 $(or)
中缺少右大括号。
不清楚res1
和res2
是变量名还是值;取决于此,最后两个作业需要或不需要看起来像 $(1) := $($(4))
.
尝试始终使用急切赋值::=
,以减少惰性变量扩展带来的意外。
[注意]:Whosebug 上已经出现了一些类似的问题,但它们似乎没有完全解决我的问题。因此,我发布了这个问题。
我正在尝试编写一个 makefile 函数,该函数应将值设置为作为参数传递给函数的变量。
所以,我将此函数称为 -
RESULT :=
$(eval $(call myfunction,RESULT,value,res1,res2))
此处 'res1' 和 'res2' 是 RESULT 的两个可能结果值,参数 'value' 将用于某些测试条件。
以下是我对myfunction定义的尝试。不过好像不行。
define myfunction
TEST1 := $(shell test `mybinary` -ge 5 && printf "TEST")
TEST2 := $(findstring $(2),$(SOME_SHELL_ENV))
$(info "$(TEST1)")
$(info "$(TEST2)")
ifneq "$$(or $(TEST1),$(TEST2)" ""
LOCAL_RESULT := true
else
LOCAL_RESULT := false
endif
ifeq($(LOCAL_RESULT),true)
$(1) = $(3)
else
$(1) = $(4)
endif
endef
在我看来,局部变量 TEST1 和 TEST2 似乎还没有设置。
有人能告诉我为什么我的功能无法正常工作吗?我需要做哪些更改才能解决这些问题?
引用 eval manual page:
The
eval
function is very special: [...] The argument to theeval
function is expanded, then the results of that expansion are parsed as makefile syntax.It’s important to realize that the
eval
argument is expanded twice; first by theeval
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 “$” characters when usingeval
.
这发生在 $(call)
参数被替换之后,因此 </code> 等在 <code>$(eval)
被调用时已经展开,不需要 $
转义.
要在 $(eval)
的最后(解析)阶段扩展变量,请通过加倍转义 $
非数字变量。
define myfunction
TEST1 := $$(shell test `echo 6` -ge 5 && printf "TEST")
TEST2 := $$(findstring $(2),$$(PATH))
$$(info "$$(TEST1)")
$$(info "$$(TEST2)")
ifneq "$$(or $$(TEST1),$$(TEST2))" ""
LOCAL_RESULT := true
else
LOCAL_RESULT := false
endif
ifeq ($$(LOCAL_RESULT), true)
$(1) = $(3)
else
$(1) = $(4)
endif
endef
$(eval $(call myfunction,RESULT,value,res1,res2))
test:
echo "$(LOCAL_RESULT)"
此外,您在 $(or)
中缺少右大括号。
不清楚res1
和res2
是变量名还是值;取决于此,最后两个作业需要或不需要看起来像 $(1) := $($(4))
.
尝试始终使用急切赋值::=
,以减少惰性变量扩展带来的意外。