Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a makefile exists before including it

Tags:

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.     
like image 988
Andreas Avatar asked Dec 01 '11 18:12

Andreas


People also ask

What is $@ in makefile?

The file name of the target of the rule. If the target is an archive member, then ' $@ ' is the name of the archive file. In a pattern rule that has multiple targets (see Introduction to Pattern Rules), ' $@ ' is the name of whichever target caused the rule's recipe to be run.

Is makefile executed sequentially?

Usually, there's just one action line, but there can be as many as you want; each line is executed sequentially, and if any one of them fails, the remainder are not executed. The rule ends at the first line which is not indented.

How do you check if a variable is defined in makefile?

Check if variable is defined in a Makefilecheck_defined = \ $(strip $(foreach 1,$1, \ $(call __check_defined,$1,$(strip $(value 2))))) __check_defined = \ $(if $(value $1),, \ $(error Undefined $1$(if $2, ($2)))) install: $(call check_defined, var1) $(call check_defined, var2) # do stuff here..

What is include in makefile?

The include directive tells make to suspend reading the current makefile and read one or more other makefiles before continuing. The directive is a line in the makefile that looks like this: include filenames … filenames can contain shell file name patterns.


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

like image 94
Patrick B. Avatar answered Sep 28 '22 05:09

Patrick B.