Makefile目标依赖项中的变量替换

时间:2017-03-24 17:03:40

标签: makefile gnu-make

我有一个包含相关依赖关系的目标的Makefile。所以我使用查找表,如:

APPS = a b c

dependency.lookup.a := x
dependency.lookup.b := y
dependency.lookup.c := z

$(APPS): %: path/$(dependency.lookup.%).datafile
    do something with $(dependency.lookup.$@)

这个makefile给了我错误。     ***没有规则来制作目标'路径/ .datafile'

约束:只有MinGW。不能使用shell / MSYS。也支持FreeBSD。

2 个答案:

答案 0 :(得分:5)

这需要使用Secondary Expansion功能:

.SECONDEXPANSION:
$(APPS): %: path/$$(dependency.loopup.$$*).datafile
    @echo "$@ depends on $^"

%.datafile : # Create one on demand for testing.
    mkdir -p ${@D}
    touch $@

输出:

mkdir -p path/
touch path/x.datafile
a depends on path/x.datafile

或者,使用常规依赖项:

a : path/x.datafile
b : path/y.datafile
c : path/z.datafile

$(APPS): % :
    @echo "$@ depends on $^"

前3行只添加依赖项,规则是单独指定的。输出是一样的。

答案 1 :(得分:0)

我遇到了同样的问题,但是名字很长。我不想重复它们(在APPS = ...中),所以我使用了生成的辅助Makefile。

Makefile

all: build

include deps.mk
deps.mk: deps.txt deps.sh
    ./deps.sh <$< >$@

build: $(DEPS)
    echo "DEPS are up-to-date."  # or whatever

deps.sh

#!/bin/bash
echo "# This file is generated by $0"
echo "DEPS ="
while read -r dst src; do
  echo
  echo "DEPS += $dst"
  echo "$dst: $src"
  echo -e '\t''cp $< $@'  # or whatever
done

deps.txt(现在只有此文件需要更新才能获得新的依赖关系):

a x
b y
c z
相关问题