BobMorane
BobMorane

Reputation: 4306

Makefile condition fails for defined variable

I am writing makefiles (GNU make) for an application with more than one module. More precisely, for each module, I have one makefile. In addition, I have one master makefile which call each modules' makefile. I have been trying to factor out of these makefiles several variables that are needed everywhere. I have included an init.mk file at the beginning of the master makefile with the following:

export OS = linux

ifeq ($(OS),linux)
    export SHELL := /bin/bash
else ifeq ($(OS),windows)
    export SHELL := cmd
else
    export SHELL :=
endif

When I run make with this, I get an Error 127 because the comparison test fails and the SHELL variable is set to nothing. If I add /bin/bash in the else section, make does not fails.

Why is the OS variable not compared properly?

Regards

Upvotes: 0

Views: 300

Answers (1)

MadScientist
MadScientist

Reputation: 101131

Make variable assignments strip leading spaces, but the don't strip trailing spaces: those are included in the value of the variable.

So:

export OS =   linux     
                   ^^^^^

where there are trailing whitespace, either alone or with a comment afterwards, will cause the variable to contain spaces. For example the above assignment the value of OS is linux followed by 5 spaces, not just linux.

You can either be sure to avoid trailing whitespace by hand (using an editor that has a mode for editing makefiles is very helpful here) or use the strip function:

ifeq ($(strip $(OS)),linux)
    ...

Upvotes: 1

Related Questions