首页 > 解决方案 > makefile 中的“变量”目标名称

问题描述

在makefile中有“非常量”目标的好/坏做法是什么?

我工作的公司生产嵌入式设备,我是一名固件工程师。

我工作的公司的编码规则要求构建生成的文件必须有一个带有版本标记的名称,以及来自 git 存储库的信息散列(用于测试和生产部门的需要)。另一个限制是使用单个命令生成构建。

这就是为什么我每天都会遇到生成文件,其目标的名称可以在构建之间更改。

我想知道这些是否是常见问题。
我想知道我写的例子是否是解决它的好习惯:

# This is just an example to (hopefully) help to understand. 

define populate_variable_with_remote_file_content
    $(if $(findstring $(origin $(1)),undefined), \
        $(eval $(1) = $(shell git archive master --remote=git@gitserver:project.git $(2) | tar -x --to-stdout)) \
    )
endef

.PHONY: clean populate_TAG

all: populate_TAG
    $(eval TARGET = dummy_$(TAG))
    $(eval export TAG TARGET)
    $(MAKE) -f $(firstword $(MAKEFILE_LIST)) $(TARGET)

$(TARGET):
    echo hallo > $@

populate_TAG:
    $(eval $(call populate_variable_with_remote_file_content,TAG,somefile))

clean:
    rm dummy_*

我想知道是否有其他(更好的)方法来解决这个问题。

标签: makefilegnu-make

解决方案


推荐阅读