2

I want to assign the output of a shell command into a make variable. this is what I tried:

phonegap:
    #download new phonegap
    cd ${SOURCE_PHONEGAP};git pull
    PHONE_VER = $(shell cat d:/path/to/workspace/common/phonegap/VERSION)
    echo phonegap version: ${PHONE_VER}

when running MAKE from the command line I get the following:

PHONE_VER = 1.3.0 make: PHONE_VER: Command not found so the shell string is translated to the right value (1.3.0) but something fails after that.

I also tried declaring :

PHONE_VER = 

and then in the command: ${PHONE_VER} = $(shell cat d:/path/to/workspace/common/phonegap/VERSION) or using := or +=. didn't work

I'm using cygwin (on win 7) with GNU make 3.81

I found this question and answer - but this doesn't seem to work for me. I'm obviously missing something (probably basic), but after a day of experimenting I have no clue whats the missing part.

Community
  • 1
  • 1
alonisser
  • 11,542
  • 21
  • 85
  • 139

2 Answers2

7

You can't assign variables inside rule recipes. The first possible solutions is to initialize it somewhere outside the rule, and then use it as regular:

PHONE_VER = $(shell cat d:/path/to/workspace/common/phonegap/VERSION)

phonegap: pull_phonegap
    @echo phonegap version: ${PHONE_VER}

pull_phonegap:
    @cd ${SOURCE_PHONEGAP}; git pull

This will work fine as far as PHONE_VER is recursively expanded (note the = sign in assignment), and the actual invocation of shell cat ... will appear after satisfying pull_phonegap prerequisite.

The other possibility is make PHONE_VER variable target-specific for phonegap:

phonegap: PHONE_VER = $(shell cat d:/path/to/workspace/common/phonegap/VERSION)
phonegap: pull_phonegap
    @echo phonegap version: ${PHONE_VER}

Finally, if the only thing you want to do is to print the version of downloaded repo, then it would be easier to get rid of variables at all:

phonegap: pull_phonegap
    @echo phonegap version:
    @cat ${SOURCE_PHONEGAP}/VERSION

pull_phonegap:
    @cd ${SOURCE_PHONEGAP}; git pull
Eldar Abusalimov
  • 24,387
  • 4
  • 67
  • 71
1

since recipes are run in shell you can combine commands through &&:

PHONE_VER=`cat d:/path/to/workspace/common/phonegap/VERSION` && echo phonegap version: $${PHONE_VER}

don't forget to used $$ instead of a single $