我有一个makefile
,在其中我试图设置一些变量,这些变量已经存在于我编写的bash脚本set-vars.sh
中(它只包含变量名和值)。
我在其他几个bash
脚本中使用这些变量。我还需要能够在我的makefile
中使用它们。
经过大量研究和尝试,我找到了一种可行的方法,可以从bash
脚本中设置makefile
中的变量,具体如下:
set-vars.sh
:
# set vars
foo=FOO1
bar=BAR1
baz=BAZ1
# if a command line arg is given (e.g. foo)
# echo value command line arg (e.g. echo $foo)
if ! [ -z ${1+x} ]; then echo ${!1}; fi
这里的奥妙在于最后一行,它通过输出一个命令行参数给定的变量名的值(如果有命令行参数的话)。我会在
makefile
中调用这个脚本,提供变量名并使用输出的值来设置makefile
变量。.PHONY: target1 target2 target3
export foo = $(shell sh set-vars.sh foo)
target1:
@echo $(foo)
# ... other targets which reference $(foo) ...
这样做可以实现,但可能是一种比较“hacky”的方式。我想知道是否有更简单、优雅的方法来实现我的需求。
我在StackOverflow上遇到了几个类似的问题和答案,但没有找到与我的示例相匹配的内容。如果已经有我错过的答案,请指引我。
非常感谢!
make
的shell中设置变量,也不能将它们放在make
命令行上吗? - Barmarif ! [ -z...
行吗? - Betamake
的shell中设置变量是什么意思?对不起,我不太精通shell脚本。 - tamjd1