4

I am trying to set an env var which is git sha1 commit for a makefile target. I am setting a lot of env vars and only this fails with no such file or directory error. I am hell confused. Please help me pass this as env var to my makefile target. This is what I do in the beginning of my Makefile.

SHA1 := $(shell git log | head -1 | awk '{print $2}')
BUILD_STAMP := $(shell date +%s)
export SHA1
export BUILD_STAMP

And one of the makefile targets have like this.

target: dep-target
     env \
              VAR1=$(VAR1) \
              GIT_SHA1=$(SHA1) \
     my-command for the target

This fails with

env: a6d23d14b0fbb613e567cc647ebfe2808ce47af1: No such file or directory

Please help me set this as env var.

Medhamsh
  • 303

2 Answers2

3
 VAR1=$(VAR1)
 GIT_SHA1=$(SHA1)

The round braces, parentheses, used with a $ just before them in this way will cause the text between the parentheses to be sent to Bash as a command line to try to execute.

Replace with curly braces instead, i.e. GIT_SHA1=${SHA1} and you will get the intended variable assignment instead.

Hannu
  • 10,568
1

I don't have enough rep to comment on @Hannu's wrong answer (curly and round parenthesis are equivalent in Make).

To debug this sort of thing, create a separate short Makefile and try things like this:

SHA1 := $(shell git log | head -1 | awk '{print $2}')
all:
        echo "$(SHA1)"

This shows what is happening is SHA1 gets assigned the entire first line of git log output, not just the SHA, and that includes spaces. The expanded env command would then have invalid syntax.

In this case, the dollar sign in the awk command needs to be doubled (print $$2) so that expands to a literal dollar sign and not as the empty variable $(2).

Curt
  • 396