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 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
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.
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.