Makefile:在配方中重用 % 的模式规则值
Makefile: reuse value of % of a pattern rule inside the recipe
在 Makefile 中我有:
images/schematic.pdf: images/schematic.svg
inkscape -D -z --file=$^ --export-pdf=$@ --export-latex
sed -i "s:schematic:images/schematic:g" $@_tex
这条规则的作用是:
- 使用 inkscape 转换为 latex-ready
.pdf
PDF 文件 + 其相应的 .pdf_tex
文本文件(参见此答案:https://tex.stackexchange.com/a/2107/104581)
- 修改提到的
.pdf_tex
文件,这样当 .pdf_tex
文件位于 ./images
时,它不会破坏从“更高”目录(即 .
编译的乳胶)
我的问题:
我有很多这种形式的规则。 e.只有 schematic
发生了变化。我想使用 pattern-rule 将 schematic
替换为 %
。并在配方中使用 %
(在 sed 命令中)。
然而规则:
images/%.pdf: images/%.svg
inkscape -D -z --file=$^ --export-pdf=$@ --export-latex
sed -i "s:%:images/%:g" $@_tex
不起作用:% 在食谱中按字面解释。
我也尝试用 $% 替换食谱中的 % 但这个变量似乎是空的。
不满意的解决方案:
在配方中添加一行以创建一个 (make) 变量来保存 notdir(removeprefix($<))
的结果(使用 this question 或调用 bash 因为没有 removeprefix在 GNU Make 中)。
你想要$*
。
来自 https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html :
$*:
The stem with which an implicit rule matches (see How Patterns Match).
If the target is dir/a.foo.b and the target pattern is a.%.b
then the stem is dir/foo. The stem is useful for constructing names of
related files.
In a static pattern rule, the stem is part of the file name that matched the ‘%’ in the target pattern.
In an explicit rule, there is no stem; so ‘$*’ cannot be determined in that way. Instead, if the target name ends with a
recognized suffix (see Old-Fashioned Suffix Rules), ‘$*’ is set to the
target name minus the suffix. For example, if the target name is
‘foo.c’, then ‘$*’ is set to ‘foo’, since ‘.c’ is a suffix. GNU make
does this bizarre thing only for compatibility with other
implementations of make. You should generally avoid using ‘$*’ except
in implicit rules or static pattern rules.
If the target name in an explicit rule does not end with a recognized suffix, ‘$*’ is set to the empty string for that rule.
$%
是一个自动变量,但它是用于存档(.a
图书馆)成员的,几乎没有用。
在 Makefile 中我有:
images/schematic.pdf: images/schematic.svg
inkscape -D -z --file=$^ --export-pdf=$@ --export-latex sed -i "s:schematic:images/schematic:g" $@_tex
这条规则的作用是:
- 使用 inkscape 转换为 latex-ready
.pdf
PDF 文件 + 其相应的.pdf_tex
文本文件(参见此答案:https://tex.stackexchange.com/a/2107/104581) - 修改提到的
.pdf_tex
文件,这样当.pdf_tex
文件位于./images
时,它不会破坏从“更高”目录(即.
编译的乳胶)
我的问题:
我有很多这种形式的规则。 e.只有 schematic
发生了变化。我想使用 pattern-rule 将 schematic
替换为 %
。并在配方中使用 %
(在 sed 命令中)。
然而规则:
images/%.pdf: images/%.svg
inkscape -D -z --file=$^ --export-pdf=$@ --export-latex sed -i "s:%:images/%:g" $@_tex
不起作用:% 在食谱中按字面解释。
我也尝试用 $% 替换食谱中的 % 但这个变量似乎是空的。
不满意的解决方案:
在配方中添加一行以创建一个 (make) 变量来保存 notdir(removeprefix($<))
的结果(使用 this question 或调用 bash 因为没有 removeprefix在 GNU Make 中)。
你想要$*
。
来自 https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html :
$*: The stem with which an implicit rule matches (see How Patterns Match). If the target is dir/a.foo.b and the target pattern is a.%.b then the stem is dir/foo. The stem is useful for constructing names of related files.
In a static pattern rule, the stem is part of the file name that matched the ‘%’ in the target pattern.
In an explicit rule, there is no stem; so ‘$*’ cannot be determined in that way. Instead, if the target name ends with a recognized suffix (see Old-Fashioned Suffix Rules), ‘$*’ is set to the target name minus the suffix. For example, if the target name is ‘foo.c’, then ‘$*’ is set to ‘foo’, since ‘.c’ is a suffix. GNU make does this bizarre thing only for compatibility with other implementations of make. You should generally avoid using ‘$*’ except in implicit rules or static pattern rules.
If the target name in an explicit rule does not end with a recognized suffix, ‘$*’ is set to the empty string for that rule.
$%
是一个自动变量,但它是用于存档(.a
图书馆)成员的,几乎没有用。