GNU make:在递归扩展变量之前添加? [英] GNU make: prepend a recursively expanded variable?
问题描述
在GNU Makefile
中,有两种类型的变量:
In a GNU Makefile
, there are two types of variables:
# simple variable (immediate evaluation):
VAR := some_assignment
# recursively expanded variable (deferred evaluation):
VAR = some_assignment
可以使用以下方法将其附加到递归扩展的变量上:
One may append to a recursively expanded variable using:
IMMEDIATE += DEFERRED or IMMEDIATE
对于append运算符'+ =',认为右侧 如果先前已将变量设置为简单变量,则立即执行 (':='或':: ='),否则推迟.
For the append operator, '+=', the right-hand side is considered immediate if the variable was previously set as a simple variable (':=' or '::='), and deferred otherwise.
是否可以在之前添加递归扩展变量?
Is there any way to prepend to a recursively expanded variable?
我的激励性示例是在$(LDLIBS)
中的其他库之前引入一个新库:
My motivating example is to introduce a new library ahead of other ones in $(LDLIBS)
:
# Unfortunately, newlib gets added to the end rather than the beginning.
LDLIBS += $(if $(later_condition),newlib.a)
# Unfortunately, the expression is evaluated now rather than being deferred.
LDLIBS := $(if $(later_condition),newlib.a) $(LDLIBS)
推荐答案
我遇到了同样的问题.我的解决方案很笨拙,并使用$(value)和$(eval)函数.
I've came across the same problem. My solution is quite hacky, and makes use of $(value) and $(eval) functions.
对我来说重要的是,它保留了变量的风格(递归),因此在执行此操作期间,既不会扩展前置变量,也不会扩展原始变量:
What was important for me, it preserves flavor (recursive) of the variable, so that neither prepended variable, nor original variable is expanded during this action:
# Macro for prepending to a recursively expanded variable.
#
# Usage:
# * Prepending "text" to the VAR variable:
# $(call prepend,VAR,text)
#
# * Prepending "a word list" to the VAR variable -- remember
# to add any trailing separator character (e.g. space):
# $(call prepend,VAR,a word list )
#
# * Prepending OTHER_VAR variable to the VAR variable -- use $$
# to defer any variable expansions:
# $(call prepend,VAR,$$(OTHER_VAR))
define prepend
$(eval $(1) = $(2)$(value $(1)))
endef
# Macro for appending to a recursively expanded variable.
#
# Usage:
# * Appending "text" to the VAR variable:
# $(call append,VAR,text)
#
# * Appending "a word list" to the VAR variable -- remember
# to add any heading separator character (e.g. space):
# $(call append,VAR, a word list)
#
# * Appending OTHER_VAR variable to the VAR variable -- use $$
# to defer any variable expansions:
# $(call append,VAR,$$(OTHER_VAR))
define append
$(eval $(1) = $(value $(1))$(2))
endef
快速测试用例:
A = A
B = B
VAR = $(A)
$(info before: VAR=$(VAR) | value(VAR)=$(value VAR) | $(flavor VAR))
$(call prepend,VAR,$$(B))
$(info after : VAR=$(VAR) | value(VAR)=$(value VAR) | $(flavor VAR))
及其执行:
before: VAR=A | value(VAR)=$(A) | recursive
after : VAR=BA | value(VAR)=$(B)$(A) | recursive
make: *** No targets. Stop.
其他说明:
- 我的问题实际上与GNU make在附加时添加空格分隔符这一事实有关.
- 棘手的解决方案,但没有针对此问题的本地GNU make功能.
这篇关于GNU make:在递归扩展变量之前添加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!