如何编写依赖项取决于目标部分的makefile规则? [英] How can I write a makefile rule with dependencies that depend on parts of the target?
问题描述
我想写一条看起来像这样的规则:
I want to write a rule that looks something like this:
A_vs_B.txt : A.txt B.txt
但是一般规则.问题是我不能有两个%s(据我所知).我当时想只制作目标%.txt,然后使用字符串函数来解析依赖项中的A和B,但这将相当复杂.我想知道是否有更好的方法来编写这样的规则.
but a general rule. The problem is I can't have two %s (as far as I know). I was thinking of just making the target %.txt and then using string functions to parse out the A and B in the dependencies, but that will be fairly complicated. I'm wondering if there's a better way to write a rule like this.
推荐答案
两个选择:
-
二次扩展(您的想法在依赖项中串扰的数量).这样的事情(可能更好/更短的方法,但这是我首先想到的).
Secondary expansion (your idea of string munging in the dependencies). Something like this (might be better/shorter ways but this is what came to me first).
A_v_B.txt: $$(addsuffix $$(suffix $$(lastword $$(subst _, ,$$@))),$$(firstword $$(subst _, ,$$@)) $$(lastword $$(basename $$(subst _, ,$$@))))
@echo $^
生成的目标/先决条件.尽管生成所需目标/先决条件的方式取决于生成对的位置/方式等.
Generated targets/prerequisites. Though how you generate the targets/prerequisites you need depends on where/how the pairs are generated/etc.
H1 := $(REP1)
H2 := $(REP2)
SEP := _vs_
JOIN := $(SEP)
define mktgt
H1 += $R.pr1
H2 += $R.pr2
JOIN += $(SEP)
endef
$(foreach R,$(REP1) $(REP2) $(REP1)$(REP2),$(eval $(mktgt)))
PAIRS := $(join $(join $(H1),$(JOIN)),$(H2))
$(foreach P,$(PAIRS),$(eval $P.txt: $(addsuffix .txt,$(subst _vs_, ,$P))))
# Debugging output
$(foreach P,$(PAIRS),$(info $P.txt: $(addsuffix .txt,$(subst _vs_, ,$P))))
这将创建目标/先决条件映射(您将从调试输出中看到).它没有给目标任何配方.我假设您已经有一个食谱,并且已将其分配给所有适当的目标.
That creates (as you'll see from the debugging output) the target/prerequisite mappings. It doesn't give the targets any recipe. I assume you have a recipe already and that you've assigned it to all the appropriate targets.
如果没有,则添加如下内容:
If not then adding something like:
$(PAIRS):
@echo 'Use $^ to generate $@'
应该工作.
这篇关于如何编写依赖项取决于目标部分的makefile规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!