9

I have the following code:

LOCAL_VERSION := $(shell some_binary -v | head -n 1)
REMOTE_VERSION := $(shell curl -s https://example.com/key)

all:
    ifeq($(REMOTE_VERSION), $(LOCAL_VERSION))
        @echo yes
    endfi

But I am getting this:

user:tmp user$ make
ifeq(v0.11.1, v0.11.1)
/bin/sh: -c: line 0: syntax error near unexpected token `v0.11.1,'
/bin/sh: -c: line 0: `ifeq(v0.11.1, v0.11.1)'
make: *** [all] Error

I am on Mac OSX, but it's using GNU Make anyway.

CppLearner
  • 16,273
  • 32
  • 108
  • 163

2 Answers2

20

ifeq should not be indented, e.g.

LOCAL_VERSION := $(shell some_binary -v | head -n 1)
REMOTE_VERSION := $(shell curl -s https://example.com/key)

all:
ifeq ($(REMOTE_VERSION), $(LOCAL_VERSION))
    @echo yes
else
    @echo NO
endif
Can
  • 4,516
  • 6
  • 28
  • 50
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • 1
    Hi Alex. Thank you. That solved the problem. I didn't know it wasn't supposed to be indended. – CppLearner Mar 13 '19 at 14:40
  • 8
    The trick with makefile syntax is that when a line after a target begins with a tab character, this lines is not a make statement anymore, but rather is passed (after $ variables and functions are evaluated) to the shell, as a command that is supposed to build the target. `ifeq` is not a valid command for the shell. – Alex Cohn Mar 13 '19 at 19:50
  • This worked for me, too. Fixed an "unexpected token error". – RoboBear Sep 17 '19 at 21:43
  • 2
    I wonder how this is different in `define` macro. Should I use `$$`? – pevik Jun 30 '21 at 17:38
0

The issue is not that ifeq is indented in the recipe, the problem is that it was indented using a tab. If you indent using space, the code runs as expected.

From Make manual: 5.1 Recipe Syntax

Each line in the recipe must start with a tab (or the first character in the value of the .RECIPEPREFIX variable; see Special Variables), except that the first recipe line may be attached to the target-and-prerequisites line with a semicolon in between. Any line in the makefile that begins with a tab and appears in a “rule context” (that is, after a rule has been started until another rule or variable definition) will be considered part of a recipe for that rule. Blank lines and lines of just comments may appear among the recipe lines; they are ignored.