Check If a Makefile Exists Before Including It
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...
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.
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.
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:
Rules.mak:14: *** missing separator. Stop.
2 Answers
You could do it like that (no if or 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)
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.
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..fi this way:
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