Alastair
Alastair

Reputation: 1773

Using ifeq and ifndef in GNU Make

I've written a fairly simple test Makefile where I define two targets, all & clean. I've got two different conditional statements. One checks for the existence of the $(MAKECMDGOALS) special variable and the other detects whether any of the command line targets matched those listed in a variable (NODEPS). The problem I'm having is that none of the branches within my conditionals get executed. Ultimately I want to use a conditional to decide whether the target I'm supplying should include some autogenerated dependency files but at the moment I'm struggling to get either expression to even evaluate. I'm running GNU make version 3.81 and I've tried it under Ubuntu and Mac OS X to no avail.

    NODEPS := clean
    INCLUDE = $(filter $(NODEPS),$(MAKECMDGOALS))

    .PHONY : all clean

    ifndef $(MAKECMDGOALS)
        @echo "$$(MAKECMDGOALS) is not defined"
    else 
        @echo "$(MAKECMDGOALS) is defined"
    endif

    ifneq (0, $(words $(INCLUDE)))
        @echo "INCLUDE = $(INCLUDE) != 0"
    else
        @echo "INCLUDE = $(INCLUDE) == 0"
    endif


    all :
        @echo "all : $(MAKECMDGOALS)"

    clean : 
        @echo "clean : $(MAKECMDGOALS)"

Upvotes: 11

Views: 46455

Answers (2)

Alastair
Alastair

Reputation: 1773

I eventually managed to work out what was wrong. @eriktous was right, pointing out that I should be using $(info) rather than @echo. More subtly though, part of the problem was that I'd indented the @echos with a tab. It seems that tabs are mandatory for rules but not allowed in conditionals. The other mistake was I'd expanded the $(MAKECMDGOALS) variable in the test condition when it should have been written as just ifndef MAKECMDGOALS.

Upvotes: 23

eriktous
eriktous

Reputation: 6649

A makefile is not a shell script. You can not "randomly" place executable statements anywhere you like and expect them to be executed.

There are various ways of communicating with the outside world from within a makefile: $(info ...), $(warning ...), $(error ...) and $(shell @echo ...) (some or all of these may be GNU make extensions).

Ps: you misspelled PHONY.

Upvotes: 12

Related Questions