更改make变量,并从同一Makefile中的配方调用另一个规则?

Change a make variable, and call another rule, from a recipe in same Makefile?

我已经看到了如何从make目标手动调用另一个目标了?,但我的问题有点不同;请考虑这个示例(注意,stackoverflow.com将选项卡更改为显示中的空格;但如果尝试编辑,则选项卡保留在源中):

1
2
3
4
5
6
7
8
TEXENGINE=pdflatex

pdflatex:
    echo the engine is $(TEXENGINE)

lualatex:
    TEXENGINE=lualatex
    echo Here I want to call the pdflatex rule, to check $(TEXENGINE) there!

在这里,如果我运行缺省目标(pdflatex,我得到预期的输出:

1
2
3
$ make pdflatex
echo the engine is pdflatex
the engine is pdflatex

但是,对于目标lualatex,我想:

  • make变量TEXENGINE改为lualatex,然后
  • 调用与pdflatex中相同的代码(使用它)。

我怎么能做到?

很明显,在我的lualatex规则中,我甚至没有设法改变TEXENGINE变量,因为我在尝试它时会得到:

1
2
3
4
$ make lualatex
TEXENGINE=lualatex
echo Here I want to call the pdflatex rule, to check pdflatex there!
Here I want to call the pdflatex rule, to check pdflatex there!

…所以我真的想知道makefiles中是否有这样的功能。


使用目标特定变量

There is one more special feature of target-specific variables: when you define a target-specific variable that variable value is also in effect for all prerequisites of this target, and all their prerequisites, etc. (unless those prerequisites override that variable with their own target-specific variable value).

1
2
3
4
5
6
7
8
TEXENGINE=pdflatex

pdflatex:
    echo the engine is $(TEXENGINE)

lualatex: TEXENGINE=lualatex
lualatex: pdflatex
    echo Here I want to call the pdflatex rule, to check $(TEXENGINE) there!

输出是:

1
2
3
4
5
6
7
8
$ make pdflatex
echo the engine is pdflatex
the engine is pdflatex
$ make lualatex
echo the engine is lualatex
the engine is lualatex
echo Here I want to call the pdflatex rule, to check lualatex there!
Here I want to call the pdflatex rule, to check lualatex there!


嗯,我设法找到了一种解决办法,但我不太明白,所以我会感激一个更有学问的答案。对于我来说,这些链接有助于:

  • 从规则内设置变量
  • make:规则调用规则
  • 从命令行传递附加变量

下面是修改过的示例——显然,为了在以后从规则调用规则(不是作为先决条件,而是作为后决条件),我只能递归调用make,同时在命令行上指定新的变量值:

1
2
3
4
5
6
7
8
TEXENGINE=pdflatex

pdflatex:
    echo the engine is $(TEXENGINE)

lualatex:
    echo Here I want to call the pdflatex rule, to check $(TEXENGINE) there!
    $(MAKE) TEXENGINE=lualatex pdflatex

输出比我想要的要详细一些,但是它可以工作:

1
2
3
4
5
6
7
8
$ make lualatex
echo Here I want to call the pdflatex rule, to check pdflatex there!
Here I want to call the pdflatex rule, to check pdflatex there!
make TEXENGINE=lualatex pdflatex
make[1]: Entering directory `/tmp'
echo the engine is lualatex
the engine is lualatex
make[1]: Leaving directory `/tmp'

…这完全是我想要的命令行交互方式,但我知道这不是最好的解决方案(参见下面的@jonathanwakely的评论)