geek
geek

Reputation: 816

string comparison in makefile

how do we compare strings in makefile?

in makefile

MAKECMDGOALS ?= debug
DBGDIR = build/debug
RLSDIR = build/release

ifeq  ($(MAKECMDGOALS),"debug")
    O = $(DBGDIR)
else
    O = $(RLSDIR)
endif

I tried following comparisons:

($(MAKECMDGOALS),"debug")
($(MAKECMDGOALS),debug)
($(MAKECMDGOALS),'debug')
($(MAKECMDGOALS), ' debug')

also

ifeq "$(strip $(filter debug,$(MAKECMDGOALS)))" "debug"    

but everytime it enter else block and does release build. Where am i going wrong?

Upvotes: 0

Views: 1913

Answers (1)

raspy
raspy

Reputation: 4261

For sure you should not have any quotation marks. The following works for me:

$ cat Makefile
MAKECMDGOALS ?= debug
DBGDIR = build/debug
RLSDIR = build/release

ifeq  ($(MAKECMDGOALS),debug)
    O = $(DBGDIR)
else
    O = $(RLSDIR)
endif

all:
%:
        @echo $(O)

$ make
build/debug

$ make foo
build/release

But I would advise against checking it this way, as $(MAKECMDGOALS) may have more than one target and the test will fail:

$ make debug
build/debug
$ make debug foo
build/release
build/release

If you still wish to check this variable, it would be better to check if an interesting target shows among others, i.e.:

$ cat Makefile
MAKECMDGOALS ?= debug
DBGDIR = build/debug
RLSDIR = build/release

ifeq ($(findstring debug,$(MAKECMDGOALS)),debug)
    O = $(DBGDIR)
else
    O = $(RLSDIR)
endif

all:
%:
        @echo $(O)

$ make debug
build/debug

$ make debug foo
build/debug
build/debug

Upvotes: 2

Related Questions