在Makefile中,如何获取并为变量分配git commit hash?

use*_*618 5 git makefile

A 使所有克隆成为git repo.我想知道提交哈希是什么,并将git commit hash分配给一个变量,该变量稍后可以在Makefile中使用

例如

all: download
      echo '$(GIT_COMMIT)'

download:
      cd buildarea && git clone git@github.com:proj/project.git
      $(eval GIT_COMMIT = $(shell cd buildarea/project && git log -l --pretty=format:"%H"))
      echo '$(GIT_COMMIT)'
Run Code Online (Sandbox Code Playgroud)

小智 5

使用shell函数和2个目标怎么样?

all: download getver

getver: VER=$(shell cd buildarea/project && git log -1 --pretty=format:"%H")
getver:
        @echo GIT_COMMIT=$(VER)

download:
        mkdir -p buildarea && cd buildarea && git@github.com:proj/project.git
Run Code Online (Sandbox Code Playgroud)

  • 我需要制作的唯一模组是`getver: $(eval GIT_COMMIT=$(shell cd buildarea/project && git log -1 --pretty=format:"%H")` (2认同)
  • 我对‘VER=$(...)’形式的这个“先决条件”感到困惑。对于未来的访问者:这是一个[特定于目标的变量值](https://www.gnu.org/software/make/manual/html_node/Target_002dspecific.html)。 (2认同)