为多个文件创建一个 Makefile 规则

Create one Makefile rule for several files

我只想创建一个 Makefile 规则,而不是为相同类型的不同文件创建相同的规则。

考虑这个小例子,其中 files 声明了两个 Python-Notebooks,每个 Notebooks 都应该被转换成一个 Python-Script (.py)。这可以通过命令 jupyter nbconvert --to script FILE 来完成。我尝试创建的 Makefile 看起来像这样,但 工作:

files = A.ipynb B.ipynb

all: convert

convert: $(files)   
     jupyter nbconvert --to script $<      # How to define $< as the changed file?

现在,每当 A.ipynb 改变时,我想要命令

jupyter nbconvert --to script A.ipynb

待执行。这可以在上面 'suggested' 的一个简单规则内完成吗?我可以编写一个 Makefile 来完成该任务,但它的长度和缩放比例为 files:

all: A.py B.py

A.py: A.ipynb
    jupyter nbconvert --to script $<

B.py: B.ipynb
    jupyter nbconvert --to script $<

这就是 pattern rules 的用途:

all: A.py B.py

%.py: %.ipynb
        jupyter nbconvert --to script $<

您也可以使用 static pattern rules,有些人更喜欢。