首页 > 解决方案 > Make:我们可以优化 make 文件目标吗?

问题描述

我们在工作流程中支持 32 位和 64 位构建。为此,我们在 makefile 中有多个规则,这些规则分别用于 32 位和 64 位。让我展示一对除了字符串“32”和“64”之外相同的规则。

生成文件片段:-

$(TGTDIR32)/logdir/set_user.c: $(CURDIR)/slv/set_user.c
    $(file_transfer)

$(TGTDIR64)/logdir/set_user.c: $(CURDIR)/slv/set_user.c
    $(file_transfer)

如果您注意到,除了字符串“32”和“64”之外,我们有相同的目标,我想用单个规则/定义替换它们。因为我们的基础设施代码中有数百条类似上面的规则。

我们在 GNUmake 中是否有任何简化的方法来做到这一点?

提前致谢!

标签: makefilecompilationgnu-make

解决方案


具有相同先决条件和配方的目标可以简单地组合,如下所示:

$(TGTDIR32)/logdir/set_user.c $(TGTDIR64)/logdir/set_user.c: $(CURDIR)/slv/set_user.c
    $(file_transfer)

或更一般地说:

THESE_TARGETS := $(TGTDIR32)/logdir/set_user.c $(TGTDIR64)/logdir/set_user.c # More...?
...
$(THESE_TARGETS): $(CURDIR)/slv/set_user.c
    $(file_transfer)

如果 Make 确定 的任何成员$(THESE_TARGETS)在先决条件方面已经过时,那么它将运行该目标的配方。

这个生成文件:

.PHONY: all clean

all: a b c

a: d e
    touch $@

b: d e
    touch $@

c: d e
    touch $@

d:
    touch $@

e:
    touch $@

clean:
    $(RM) a b c d e

相当于这个:

.PHONY: all clean

all: a b c

a b c: d e
    touch $@

d e:
    touch $@

clean:
    $(RM) a b c d e

之后

有一些静态模式规则......

同样适用。这个带有静态模式规则的makefile:

.PHONY: default clean

default: a.k b.k

a.k: %.k: %.j
    cp -f $< $@

b.k: %.k: %.j
    cp -f $< $@

a.j:
    touch $@

b.j:
    touch $@


clean:
    $(RM) a.k b.k a.j b.j

相当于这个:

.PHONY: default clean

JS := a.j b.j
KS := $(JS:.j=.k)

default: $(KS)

$(KS): %.k: %.j
    cp -f $< $@

$(JS):
    touch $@

clean:
    $(RM) $(JS) $(KS)

推荐阅读