bash 在包含 makefile 之前检查它是否存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8346118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a makefile exists before including it
提问by Andreas
I have a makefile that includes a Rules.mak file that holds includes to tools I want to use. Problem is that the tools folder has free options if they want to extract a version or use the "native" installation. So I want to include the tools extracted rules if it exists otherwise I want to include the native file.
我有一个 makefile,其中包含一个 Rules.mak 文件,其中包含我想要使用的工具的包含。问题是如果他们想要提取版本或使用“本机”安装,工具文件夹有免费选项。所以我想包括工具提取的规则(如果存在),否则我想包括本机文件。
something like this is the goal:
目标是这样的:
if Tool/Rules.mak exists then
include Tool/Rules.mak
else
include common/Rules-Tool.mak
fi
I have tried either the bash way or the make way but as this is preincludes to setup the enviroment I don't have a specifik target but make calls out wrong due to the check fails.
我已经尝试过 bash 方式或 make 方式,但由于这是设置环境的预包含,我没有特定的目标,但由于检查失败而导致调用错误。
if [ -f Tool/Rules.mak ]
then
echo testfile exists!
fi
also
还
if [ -d ./Tool ]
then
echo testfile exists!
fi
as well as versions with quotes and similar. Problem is that almost all the time when I type make I get the following error:
以及带引号和类似的版本。问题是,几乎所有的时间,当我输入 make 时,我都会收到以下错误:
Rules.mak:14: *** missing separator. Stop.
回答by Patrick B.
You could do it like that (no ifor else)
你可以这样做(不if或else)
-include Tool/Rules.mak
include common/Rules-Tool
like this you won't get an error if Tool/Rules.mak does not exists. (The '-' does the trick)
像这样,如果 Tool/Rules.mak 不存在,您将不会收到错误消息。('-' 起作用了)
In common/Rules-Tool you then use the ?= operator ("conditional variable assignment operator") to assign values to the variable. This operator will assign the value only if the variable does not exists yet. IOW, it will not overwrite a pre-existing value. If Tool/Rules.mak does not exist or only partially fills in variable common/Rules-Tool will complete them.
在 common/Rules-Tool 中,您然后使用 ?= 运算符(“条件变量赋值运算符”)为变量赋值。仅当变量尚不存在时,此运算符才会分配值。IOW,它不会覆盖预先存在的值。如果 Tool/Rules.mak 不存在或仅部分填写变量 common/Rules-Tool 将完成它们。
回答by Jesse Chisholm
If for some reason you don't want to use the ?=operator, (perhaps you have more action than just setting the variable) then you can do the if..then..else..fithis way:
如果由于某种原因您不想使用?=运算符(也许您有更多的操作而不仅仅是设置变量),那么您可以这样做if..then..else..fi:
ifneq ("$(wildcard Tool/Rules.mak)","")
$(info using Tools/Rules.mak)
include Tool/Rules.mak
else
$(info using common/Rules-Tool.mak)
include common/Rules-Tool.mak
endif

