在包含之前检查是否存在makefile

And*_*eas 15 bash makefile makefile-project

我有一个makefile,其中包含一个Rules.mak文件,该文件包含我想要使用的工具.问题是,如果工具文件夹要提取版本或使用"本机"安装,则它们具有空闲选项.所以我希望包含工具提取规则(如果存在),否则我想要包含本机文件.

这样的事情是目标:

if Tool/Rules.mak exists then
  include Tool/Rules.mak
else
  include common/Rules-Tool.mak
fi
Run Code Online (Sandbox Code Playgroud)

我已尝试过bash方式或make方式但是因为这是设置环境的前提条件我没有特定目标但由于检查失败而使调用错误.

if [ -f Tool/Rules.mak ]
then
echo testfile exists!
fi
Run Code Online (Sandbox Code Playgroud)

if [ -d ./Tool ]
then
echo testfile exists!
fi
Run Code Online (Sandbox Code Playgroud)

以及带引号和类似的版本.问题是,当我输入时几乎所有时间都会出现以下错误:

Rules.mak:14: *** missing separator.  Stop.    
Run Code Online (Sandbox Code Playgroud)

Pat*_* B. 40

你可以这样做(没有ifelse)

-include Tool/Rules.mak
include common/Rules-Tool
Run Code Online (Sandbox Code Playgroud)

像这样,如果Tool/Rules.mak不存在,您将不会收到错误.(' - '做的伎俩)

在common/Rules-Tool中,然后使用?=运算符("条件变量赋值运算符")为变量赋值.仅当变量尚不存在时,此运算符才会分配值.IOW,它不会覆盖预先存在的值.如果Tool/Rules.mak不存在或仅部分填充变量common/Rules-Tool将完成它们.


Jes*_*olm 6

如果由于某种原因您不想使用?=运算符(也许您除了设置变量之外还有更多操作),那么您可以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
Run Code Online (Sandbox Code Playgroud)