Christian Stewart
Christian Stewart

Reputation: 15519

Makefile conditional error

I am attempting to do a make statement to check the architecture. I am very close to getting it to work:

test:
        ifeq ("$(shell arch)", "armv7l")
                echo "This is an arm system"
        else
                echo "This is not an arm system."
        endif

I have one issue: although this seems to resolve to ifeq ("i386", "armv7l") which should be false, I get the following error:

$ make
ifeq ("i386", "armv7l")
/bin/sh: -c: line 0: syntax error near unexpected token `"i386",'
/bin/sh: -c: line 0: `ifeq ("i386", "armv7l")'
make: *** [test] Error 2

So, it is resolving to two strings comparing to each other, but there is a syntax error. What's wrong here?

Upvotes: 10

Views: 10311

Answers (2)

Idelic
Idelic

Reputation: 15572

As MadScientist said, make is passing the ifeq lines to the shell, but if you write it properly, you can definitely mix make constructs like ifeq with commands within a recipe. You just need to understand how make parses a Makefile:

If a line begins with a TAB, it is considered a command for the shell regardless of where the line is within the file.

If it doesn't begin with a TAB, make interprets it as part of its own language.

So, to fix your file, just avoid starting the make conditionals with a TAB:

test:
ifeq ("$(shell arch)", "armv7l")
        echo "This is an arm system"
else
        echo "This is not an arm system."
endif

Upvotes: 21

MadScientist
MadScientist

Reputation: 100781

You cannot use make statements like ifeq inside a recipe. Recipes (the lines that begin with TAB) are passed to the shell. The shell doesn't understand ifeq; that's a make construct.

You'll have to use shell if-statements inside a recipe. And, you don't have to use $(shell ...) in a recipe, because you're already in a shell.

test:
        if [ `arch` = armv7l ]; then \
            echo "This is an arm system"; \
        else \
            echo "This is not an arm system."; \
        fi

This is likely not the best way to handle this, but since you didn't provide any info on what you're really trying to do with this it's all we can say.

Upvotes: 21

Related Questions