输入/输出对的Makefile

输入/输出对的Makefile,makefile,Makefile,我有成对的输入/输出文件。我通过脚本生成输出文件的名称:output=$(generateinput)。例如,对可以是: in1.c out1.o in2.txt data.txt in3 config.sh sub/in1.c sub/out1.o 所有这些对都遵守makefile中的相同规则集: $(out): $(in) $(common) $(run) $< > $@ $(out):$(in)$(通用) $(运行)$$@ 编写这

我有成对的输入/输出文件。我通过脚本生成输出文件的名称:
output=$(generateinput)
。例如,对可以是:

in1.c      out1.o
in2.txt    data.txt
in3        config.sh
sub/in1.c  sub/out1.o
所有这些对都遵守makefile中的相同规则集:

$(out): $(in) $(common) 
    $(run) $< > $@
$(out):$(in)$(通用)
$(运行)$<>$@
编写这样一个Makefile的简洁高效的方法是什么?


我宁愿避免从另一个脚本生成Makefile。

我不会从脚本生成Makefile片段,但您可以使用
include

INS := in1.c in2.txt in3 sub/in1.c

include rules.mk

rules.mk: Makefile
        rm -f $@
        for f in $(INS); do \
                out=`generate "$$f"`; \
                echo -e "$$out: $$f\n\t\$$(run) \$$<> > \$$@\n\n" >> $@; \
        done
INS:=in1.c in2.txt in3 sub/in1.c
include rules.mk
rules.mk:Makefile
rm-f$@
对于f,单位为美元(英寸);做\
out=`生成“$$f”`\
echo-e“$$out:$$f\n\t\$$(运行)\$$>\$@\n\n”>>$@\
完成

如果您
包含
文件,gmake将尝试在任何其他目标之前生成并包含该文件。将其与默认规则相结合应该可以让您更接近您想要的

# makefile
gen=./generate.sh
source=a b c
run=echo

# Phony so the default rule doesn't match all
.PHONY:all
all:

# Update targets when makefile changes
targets.mk:makefile
    rm -f $@
    # Generate rules like $(target):$(source)
    for s in $(source); do echo "$$($(gen) $$s):$$s" >> $@; done
    # Generate rules like all:$(target)
    for s in $(source); do echo "all:$$($(gen) $$s)" >> $@; done

-include targets.mk

# Default pattern match rule
%:
    $(run) $< > $@
给我

$ make
rm -f targets.mk
for s in a b c; do echo "$(./generate.sh $s):$s" >> targets.mk; done
for s in a b c; do echo "all:$(./generate.sh $s)" >> targets.mk; done
echo a > 60b725f10c9c85c70d97880dfe8191b3
echo b > 3b5d5c3712955042212316173ccf37be
echo c > 2cd6ee2c70b0bde53fbe6cac3c8b8bb1
编写这样一个Makefile的简洁有效的方法是什么

可以提供一个输入列表和一个shell脚本,该脚本生成输出文件名,以使用GNU make功能生成目标、依赖项和规则:

all :

inputs := in1.c in2.txt in3 sub/in1.c
outputs :=

define make_dependency
  ${1} : ${2}
  outputs += ${1}
endef

# replace $(shell echo ${in}.out) with your $(shell generate ${in})
$(foreach in,${inputs},$(eval $(call make_dependency,$(shell echo ${in}.out),${in})))

# generic rule for all outputs, and the common dependency
# replace "echo ..." with a real rule
${outputs} : % : ${common}
    @echo "making $@ from $<"

all : ${outputs}

.PHONY : all 

在上面的makefile中,使用了一个功能强大的GNU make构造:
$(eval$(call…)
。它要求make展开宏以生成一段文本,然后将该文本作为一段makefile进行求值,即make生成苍蝇的makefile。

aarrrggghhh!我差几秒钟就提交了完全相同的答案…谢谢。所有的答案都很好,但这更适合我。谢谢!另一个很好的答案。
all :

inputs := in1.c in2.txt in3 sub/in1.c
outputs :=

define make_dependency
  ${1} : ${2}
  outputs += ${1}
endef

# replace $(shell echo ${in}.out) with your $(shell generate ${in})
$(foreach in,${inputs},$(eval $(call make_dependency,$(shell echo ${in}.out),${in})))

# generic rule for all outputs, and the common dependency
# replace "echo ..." with a real rule
${outputs} : % : ${common}
    @echo "making $@ from $<"

all : ${outputs}

.PHONY : all 
$ make
making in1.c.out from in1.c
making in2.txt.out from in2.txt
making in3.out from in3
making sub/in1.c.out from sub/in1.c