How to make a conditional statement in a makefile

I am trying to use the var command line to select the tools we use to compile. When on the command line I use a line like:

make all-arm OUR_TOOLKIT=1 

And in every makefile it is implied that I include

 include ARM_Compiler.inc 

Then in each makefile

 all: setToolkit $(otherOperations) 

And the contents of ARM_Compiler is the compiler choice logic:

 setToolkit: ifdef OUR_TOOLKIT TOOLKIT=1 endif ifdef CUSTOMER_TOOLKIT TOOLKIT=2 endif ifeq ($(TOOLKIT), 1) $(info "=========Our toolkit selected======================") rm=/bin/rm -f CC= arm-linux-c++ -fPIC CXX= arm-linux-c++ -fPIC LINK= arm-linux-c++ -shared -Wl AR= ar cq RANLIB= ranlib STRIP=arm-linux-strip # para que se utilicen las herramientas y librerias del cross compiler PATH:=$(PATH):/path/to/our/toolkit LD_LIBRAY_PATH:=$(LD_LIBRAY_PATH):/path/to/our/toolkit endif ifeq ($(TOOLKIT), 2) $(info "================Customer toolkit selected====================") rm=/bin/rm -f CC= arm-none-linux-gnueabi-c++ -fPIC CXX= arm-none-linux-gnueabi-c++ -fPIC LINK= arm-none-linux-gnueabi-c++ -shared -Wl AR= ar cq RANLIB= ranlib STRIP= arm-none-linux-gnueabi-strip # para que se utilicen las herramientas y librerias del cross compiler PATH:=$(PATH):/path/to/other/toolkit LD_LIBRAY_PATH:=$(LD_LIBRAY_PATH):/path/to/other/toolkit endif 

Thanks to the help of 0A0D, I found that the TOOLKIT value is always empty. I changed the code a bit. Now the problem is that make throws an error

 ../makefile-includes/ARM-compiler.inc:10: *** commands commence before first target 

in this line:

 ifeq ($(TOOLKIT), 1) 

Does anyone have an idea? Thanks

+4
source share
1 answer

The options for this issue come a lot.

Each command is executed in its own subshell; a variable set in one command cannot be used in another.

But you can set variables outside the rules: just remove all leading TABs from the conditional statements above. This will work for everything except PATH and LD_LIBRARY_PATH . None of them, in my opinion, should be related to the fact that Make, but there are ways to achieve the desired effect. You can handle PATH as follows:

 ifeq ($(TOOLKIT), 1) TOOLKITPATH = /path/to/our/toolkit endif ... sometarget: $(TOOLKITPATH)/sometool somearg 

Or like this:

 all: export PATH=$$PATH:$(TOOLKITPATH) ; $(MAKE) $(otherOperations) 

And you probably shouldn't use LD_LIBRARY_PATH at all.

+6
source

Source: https://habr.com/ru/post/1415982/


All Articles