如果有空,如何有条件地将Makefile变量设置为某些值? [英] How to conditionally set Makefile variable to something if it is empty?
问题描述
我想设置一个变量,如果它是空的.我以这种方式尝试过:
I want to set a variable if it is empty. I tried in this way:
....
TEST := $(something)
...
TEST ?= $(something else)
第一个 $(something)
可能返回一个空字符串,但是条件赋值?=仅在未设置前一个变量的情况下有效,而在不为空的情况下不起作用.
The first $(something)
may return an empty string, however the conditional assignment ?= works only if the previous variable is not set, not if empty.
是否有任何优雅的解决方案来将变量设置为空?
Any elegant solution to set the variable if empty?
编辑我找到了这个解决方案:
EDIT I found this solution:
....
TEST := $(something)
...
TEST += $(something else)
TEST := $(word 1, $(TEST))
但我认为还会再有一种优雅.
but I think that there will be one more elegant.
推荐答案
是否有任何优雅的解决方案来将变量设置为空?
Any elegant solution to set the variable if empty?
GNU make鲜为人知的优雅解决方案.除非您发现活板门和雷区都很优雅.我只知道完成您想要的两种方法:
GNU make is hardly known for elegant solutions. Unless you find trapdoors and minefields to be elegant. I know only of the two ways to accomplish what you want:
-
标准
ifeq
/endif
解决方案:
ifeq ($(TEST),)
TEST := $(something else)
endif
使用 $(if)
函数:
TEST := $(if $(TEST),$(TEST),$(something else))
也可以尝试将该构造打包为一个函数,但这是不可取的.如果该函数包含
(仅存在任性的解决方法),则该函数将具有偶尔破坏 $(其他东西)
的隐患.(诸如 $(if)
之类的内置函数不受,
错误的影响.)
One can try to package that construct into a function too, but that is inadvisable. The function would have the hidden pitfall of occasionally breaking the $(something else)
if it contains the ,
(for which there are only wayward workarounds). (The built-in functions like $(if)
are immune to the ,
bug.)
优雅测试由您决定.
这篇关于如果有空,如何有条件地将Makefile变量设置为某些值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!