Conditionals

Posted by Beetle B. on Mon 19 August 2019

Example of a conditional:

ifeq ($(CC),gcc)
        $(CC) -o foo $(objects) $(libs_for_gcc)
else
        $(CC) -o foo $(objects) $(normal_libs)
endif

These are the four variants:

'ifeq (ARG1, ARG2)'
'ifeq 'ARG1' 'ARG2''
'ifeq "ARG1" "ARG2"'
'ifeq "ARG1" 'ARG2''
'ifeq 'ARG1' "ARG2"'

This is how you test for inequality:

'ifneq (ARG1, ARG2)'
'ifneq 'ARG1' 'ARG2''
'ifneq "ARG1" "ARG2"'
'ifneq "ARG1" 'ARG2''
'ifneq 'ARG1' "ARG2"'

To test if a variable exists, use ifdef:

ifdef foo

Can also do

ifdef $(foo)

In this case, foo is expanded to something (e.g. bar), and it tests if bar is a variable.

Note that ifdef will return true if the value is just whitespace!

Use ifndef for the opposite of ifdef.

Make tests the conditional when it reads a makefile - so don’t try to do dynamic stuff with it.

You can write a conditional that tests ‘make’ command flags such as ‘-t’ by using the variable ‘MAKEFLAGS’ together with the ‘findstring’ function

tags : make