gpt4 book ai didi

makefile - GNU make : prepend a recursively expanded variable?

转载 作者:行者123 更新时间:2023-12-02 08:49:42 24 4
gpt4 key购买 nike

在 GNU Makefile 中,有两种类型的变量:

# simple variable (immediate evaluation):
VAR := some_assignment

# recursively expanded variable (deferred evaluation):
VAR = some_assignment

可以使用以下方法附加到递归扩展变量:

  IMMEDIATE += DEFERRED or IMMEDIATE

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.

有没有办法添加到递归扩展变量?

我的激励示例是在 $(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)

最佳答案

我也遇到了同样的问题。我的解决方案非常 hacky,并使用 $(value) 和 $(eval) 函数。

对我来说重要的是,它保留了变量的 flavor (递归),因此在此操作期间前置变量和原始变量都不会扩展:

# 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 在附加时添加空格分隔符有关。
  • Hacky 解决方案,但没有针对此类问题的原生 GNU make 功能。

关于makefile - GNU make : prepend a recursively expanded variable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35516996/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com