11

I'm trying to do an ifeq inside of a define within a Makefile, but I seem to be running into some errors, and I'm wondering if I'm missing something. I have the following Makefile:

$(info ---- start ----)
ifeq ("X","Y")
$(info DOES not appear_1)
endif

define TESTDEF
ifeq ("X","Y")
$(info SHOULD not appear)
# $(error DEFINITELY SHOULD not error...)
endif
endef

$(eval $(call TESTDEF, 1,2,3))

I'm getting the following error:

---- start ----
SHOULD not appear
Makefile:14: *** DEFINITELY SHOULD not error....  Stop.

Is there some trick that I'm missing? Is it possible to do ifeq's inside define? (note: this happens on both my native GNU 3.81 make, and on my mips uclibc cross-compiler)

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
John
  • 3,400
  • 3
  • 31
  • 47

1 Answers1

14

When you call this function, Make evaluates the definition, using whatever parameters you provide (irrelevant in this case). So if the definition includes something like $(info ...) or $(error ...), even in a comment, Make will evaluate it and you'll see the result (see documentation; I've tested it in GNUMake 3.81).

To get the behavior you want, add a couple of dollar signs:

define TESTDEF
ifeq ("X","Y")
$$(info SHALL not appear)
# $$(info DEFINITELY SHALL not error...)
endif
endef

$(eval $(call TESTDEF))
jdknight
  • 1,801
  • 32
  • 52
Beta
  • 96,650
  • 16
  • 149
  • 150
  • 5
    Additionally, the `ifeq` is evaluated during the first pass. If `ifeq` references call variables like `$(1)` they're expanded during initial evaluation of `TESTDEF`, when they don't exist. Use `$(if ...)` instead. http://www.gnu.org/software/make/manual/make.html#Conditional-Functions – Craig Ringer Dec 25 '15 at 01:51
  • made my day, simple & precise !! – anoop Aug 09 '17 at 22:27
  • @CraigRinger -- just looking at this. If you do an `ifeq ($1,)` inside of the macro, the `$1` is not expanded until the `$(call` is expanded, and thus it is valid to use the `ifeq` inside of the define (though I wouldn't recommend that practice)... – HardcoreHenry Oct 15 '20 at 16:23