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

问题描述 投票:23回答:2

我已经看过How to manually call another target from a make target?,但我的问题有点不同;考虑这个例子(注意,stackoverflow.com将选项卡更改为显示中的空格;但如果您尝试编辑,则选项卡将保留在源代码中):

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),我得到预期的输出:

$ make pdflatex 
echo the engine is pdflatex
the engine is pdflatex

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

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

我怎么能这样做?

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

$ 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中是否有这样的东西。

makefile gnu-make
2个回答
25
投票

使用target-specific variable

目标特定变量还有一个特殊功能:当您定义特定于目标的变量时,变量值对此目标的所有先决条件及其所有先决条件等都有效(除非这些先决条件使用它们覆盖该变量)拥有特定于目标的变量值)。

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!

输出是:

$ 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!

3
投票

好吧,我设法找到了一种解决方法,但我并不完全理解它 - 所以我们将会更加了解答案。对我来说,这些链接有助于:

所以这里修改了一个例子 - 显然,之后从规则中调用规则(不是作为先决条件,而是作为后续条件),我只能递归调用make,同时在其命令行上指定新的变量值:

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

输出比我想要的更冗长,但它有效:

$ 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的评论)

© www.soinside.com 2019 - 2024. All rights reserved.