Reputation: 2682
I am trying to assign the result of a command to a variable in GNU make. It works if I do it outside the rule:
$ cat stack.mk
GIT_BRANCH=$(shell git rev-parse --abbrev-ref HEAD)
all:
@echo Git branch is $(GIT_BRANCH)
$ make -f stack.mk all
Git branch is dev
But not if I put it in the rule body:
$ cat stack.mk
all:
export GIT_BRANCH=$(shell git rev-parse --abbrev-ref HEAD)
@echo Git branch is $(GIT_BRANCH)
$ make -f stack.mk all
export GIT_BRANCH=dev
Git branch is
Is it possible to assign variables in a rule. At this point I would like to assign the results of a couple of git
commands to shell/Makefile
variables.
Upvotes: 8
Views: 15383
Reputation: 75575
Yes, if you are trying to set a Makefile
variable, you can do it using eval
function.
all:
$(eval GIT_BRANCH=$(shell git rev-parse --abbrev-ref HEAD))
echo Git branch is $(GIT_BRANCH)
Upvotes: 35