代码生成和规则扩展

bit*_*ask 1 code-generation makefile

假设我有一个 make 规则:

.PHONY:gen
gen: auto.template
        generate-sources auto.template
Run Code Online (Sandbox Code Playgroud)

创建一批文件,例如auto1.srcauto2.srcauto3.src等等。

如果我现在有从*.src文件构建目标的规则,如下所示:

$(patsubst %.src,%.target,$(wildcard *.src)): %.target: %.src
        build $< > $@
Run Code Online (Sandbox Code Playgroud)

如何告诉 make 首先执行gen规则,然后扩展第二个规则模板的前提条件?欢迎使用 GNU 扩展。

注意:我想将它保留在一次 make调用中;对此的一个简单解决方案是将第二条规则放在次要规则中Makefile.secondrun$(MAKE) -f Makefile.secondrungen处理后调用。但我想知道是否有更好的选择。

Eri*_*ski 6

构建 Beta 的答案,这里是您如何在 GNU make 中使用makefile remaking来完成它,这与递归 make 不同。相反,它使用主 makefile 中的规则更新包含的 makefile,然后重新启动原始 make 实例。这就是*.d通常生成和使用依赖文件的方式。

# Get the list of auto-generated sources.  If this file doesn't exist, or if it is older 
# than auto.template, it will get built using the rule defined below, according to the 
# standard behavior of GNU make.  If autosrcs.mk is rebuilt, GNU make will automatically 
# restart itself after autosrcs.mk is updated.

include autosrcs.mk

# Once we have the list of auto-generated sources, getting the list of targets to build 
# from them is a simple pattern substitution.

TARGETS=$(patsubst %.src,%.target,$(AUTO_SRCS))

all: $(TARGETS)

# Rule describing how to build autosrcs.mk.  This generates the sources, then computes 
# the list of autogenerated sources and writes that to autosrcs.mk in the form of a 
# make variable.  Note that we use *shell* constructs to get the list of sources, not
# make constructs like $(wildcard), which could be expanded at the wrong time relative
# to when the source files are actually created.

autosrcs.mk: auto.template
        ./generate-sources auto.template
        echo "AUTO_SRCS=`echo *.src`" > autosrcs.mk

# How to build *.target files from *.src files.

%.target: %.src
        @echo 'build $< > $@'
Run Code Online (Sandbox Code Playgroud)