品牌:我们可以优化品牌目标吗?

时间:2019-02-27 08:59:48

标签: makefile compilation gnu-make

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

Makefile代码段:-

$(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中有什么简化的方法吗?

预先感谢!

3 个答案:

答案 0 :(得分:1)

具有相同先决条件和配方的目标可以简单地组合在一起,就像这样:

$(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)

答案 1 :(得分:0)

这两个规则在语义上是相同的,它们仅使用不同的方式来引用“参数化”目标。为什么您不只为此使用一个目标

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

并使用正确配置的TGTDIR(我怀疑这会像“ xxxx_32”与“ xxxx_64”之类的东西)吗?

您可以通过多种方式实现这一目标;一种典型的是

ifdef choose32
  TGTDIR=xxxx_32
else
  TGTDIR=xxxx_64
endif

答案 2 :(得分:0)

我认为,这至少在顶层构建中是使用递归make的合适位置。

在这种情况下,您可以执行以下操作:

TGTDIR64 = ...
TGTDIR32 = ...

.PHONY: all all32 all64 build

all: all32 all64

all32:
        $(MAKE) TGTDIR=$(TGTDIR32) build
all64:
        $(MAKE) TGTDIR=$(TGTDIR64) build

# Things below here should just use TGTDIR

build: $(TGTDIR)/b1 $(TGTDIR)/b2

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

$(HEADERGEN_NOTSPLIT_H_COPY): $(TGTDIR)/%.h: %.h $(copy_file)
        ...
相关问题