Makefile模式规则中的目录通配符

时间:2013-04-11 12:30:01

标签: makefile gnu-make

我正在尝试创建一个Makefile,它将通过tic编译驻留在目录中的terminfo文件。 tic还会将其自动创建的termcap文件复制到系统或用户特定的目标文件夹。对于常规用户,如果terminfo文件是例如screen-256color-bce-s.terminfo,它将被编译并复制到~/.terminfo/s/screen-256color-bce-s。所以它看起来像这样:

terminfo/screen-256color-bce-s.terminfo => /home/user/.terminfo/s/screen-256color-bce-s
terminfo/screen-256color-s.terminfo => /home/user/.terminfo/s/screen-256color-s

如果我把这样的东西放到我的Makefile中:

TISRC = $(wildcard terminfo/*.terminfo)
TIDST = $(foreach x, $(TISRC), $(HOME)/.terminfo/$(shell basename $x|cut -c 1)/$(shell basename $x .terminfo))

$(HOME)/.terminfo/s/%: terminfo/%.terminfo
    @echo "$< => $@"
    @tic $<

install: $(TIDST)

它有效。但是,我想使它一般,并在目标中使用通配符,即:

$(HOME)/.terminfo/**/%: terminfo/%.terminfo
    @echo "$< => $@"
    @tic $<

能够将terminfo文件添加到我的本地存储库。但是,上述方法不起作用。如何在模式规则中指定通配符目录?

2 个答案:

答案 0 :(得分:6)

您可以使用GNU Make Secondary Expansion feature

执行此操作
all : ${HOME}/.terminfo/x/a
all : ${HOME}/.terminfo/y/b

.SECONDEXPANSION:
${HOME}/.terminfo/%: terminfo/$$(notdir $$*).terminfo
    @echo "$< ---> $@"

输出:

[~/tmp] $ make
terminfo/a.terminfo ---> /home/max/.terminfo/x/a
terminfo/b.terminfo ---> /home/max/.terminfo/y/b

作为旁注,make提供some path manipulation functions,因此您不需要为此调用shell。

答案 1 :(得分:1)

我不认为你可以按照你想要的方式使用通配符,但是如果你不介意使用 eval 技巧,你可以获得你正在拍摄的效果而不必明确地拼出所有目录路径:

TISRC = $(wildcard terminfo/*.terminfo)
BASENAMES = $(notdir $(basename ${TISRC}))

MKDST = ${HOME}/.terminfo/$(shell echo $1 | cut -c 1)/$1
TIDST := $(foreach s,${BASENAMES},$(call MKDST,$s))
DIRLTRS = $(notdir $(patsubst %/,%,$(sort $(dir ${TIDST}))))

install: ${TIDST}

# $1 - Directory Name
# $2 - File name
define T
${HOME}/.terminfo/$1/$2 : terminfo/$2.terminfo
    @echo "$$< => $$@"
    tic $$<
endef

# This is the tricky part: use template T to make the rules you need.
$(foreach d,${DIRLTRS},$(foreach f,${BASENAMES},$(eval $(call T,$d,$f))))