There are multiple problems with your current Makefile
.
GIT_COMMIT=$(git log -1 --pretty=format:%h)
Doesn't do what you expect it to do, because, it's a make variable expansion and not a command expansion in shell. For that it to be passed to the underlying shell, you'd have to escape the $
:
GIT_COMMIT=$$(git log -1 --pretty=format:%h)
On the next line, the $(GIT_COMMIT)
refers to GIT_COMMIT
make variable and not a shell variable.
Which then runs into a problem that:
- setting variable in shell executed from make does not set a make variables (also meaning
info
would have no access to what happened in the shell command above); that shell is a child process of make
- and each line of the recipe runs in its own shell instance (so variables from one line are not visible on the next one)
If I understand what you are after, I would probably do it like this:
GIT_COMMIT := $(shell git log -1 --pretty=format:%h)
append_git_commit: $(preName)/$(GIT_COMMIT)_FLASH.bin
$(preName)/$(GIT_COMMIT)_FLASH.bin: $(preName)/FLASH.bin
cp "$<" "$@"
Using a make variable and introducing an intermediate target, as long as there is not new revision and the $(preName)/FLASH.bin
did not get updated, there is not need to do anything for append_git_commit
either, because it now knows it needs to create $(preName)/$(GIT_COMMIT)_FLASH.bin
from $(preName)/FLASH.bin
and all details are know at the time Makefile
is being evaluated.