1:1 配方的 Makefile 模式,其先决条件在目录和文件扩展名方面都与目标不同
Makefile pattern for 1:1 recipe with a prerequisite that differs from target in both directory and file extension
对于名为 src
的目录下的每个 .md
文件,我想在具有相同内部目录结构的新目录 pub
下生成一个 HTML 文件,例如
src/foo/hello.md -> pub/foo/hello.html
src/bar/world.md -> pub/bar/world.html
我一直在为每个 html 文件编写先决条件。这是我目前所拥有的:
SRCS = $(shell find src -name "*.md")
HTML = $(subst src/,pub/,$(SRCS:.md=.html))
%.html: %.md
@echo "placeholder to generate $@ from $<"
publish: $(HTML)
如上所写,我得到
$ make publish
make: *** No rule to make target `pub/foo/hello.html', needed by `publish'. Stop.
如果我删除这样的先决条件:
%.html: %.md
@echo "placeholder to generate $@ from $<"
然后我得到:
$ make publish
placeholder to generate pub/foo/hello.html from
placeholder to generate pub/bar/world.html from
哪个更接近,但不处理先决条件 Markdown 文件。
如何为具有不同根目录 和 扩展名的先决条件编写模式?
只需在模式中写上前缀和后缀:
pub/%.html : src/%.md
...
对于名为 src
的目录下的每个 .md
文件,我想在具有相同内部目录结构的新目录 pub
下生成一个 HTML 文件,例如
src/foo/hello.md -> pub/foo/hello.html
src/bar/world.md -> pub/bar/world.html
我一直在为每个 html 文件编写先决条件。这是我目前所拥有的:
SRCS = $(shell find src -name "*.md")
HTML = $(subst src/,pub/,$(SRCS:.md=.html))
%.html: %.md
@echo "placeholder to generate $@ from $<"
publish: $(HTML)
如上所写,我得到
$ make publish
make: *** No rule to make target `pub/foo/hello.html', needed by `publish'. Stop.
如果我删除这样的先决条件:
%.html: %.md
@echo "placeholder to generate $@ from $<"
然后我得到:
$ make publish
placeholder to generate pub/foo/hello.html from
placeholder to generate pub/bar/world.html from
哪个更接近,但不处理先决条件 Markdown 文件。
如何为具有不同根目录 和 扩展名的先决条件编写模式?
只需在模式中写上前缀和后缀:
pub/%.html : src/%.md
...