Makefile 动态变量作为先决条件

Makefile dynamic variables as prerequisites

也许这是我弄错的地方。基本上我的任务是使用 make 来自动构建、部署、启动、停止不同的服务。 我正在尝试做的一件事是将变量作为目标先决条件,但是必须在另一个目标中更改该变量。

这是我正在尝试做的事情的基本示例:

IMAGE_COUNT=-1

count_images:
    $(eval IMAGE_COUNT=5)

_should_build: $(if $(findstring $(IMAGE_COUNT),0), build,)

build:
    ...some procedure to build...

start: _should_build
    ...some procedure to start a service... 

显然 _should_build 检查中的 $(IMAGE_COUNT) 将保持为 -1,但我想要的是让 $(IMAGE_COUNT) 在先决条件检查期间变为 5。需要注意的是,我不能将图像计数放在 count_images 目标之外。 有谁知道这是否可能?

Perhaps it's something that I'm getting wrong.

那个"something"叫做评估顺序。

One of the things that I'm trying to do is to have a variable as a target prerequisite, however that variable has to be changed in another target.

不是目标,而是配方。配方在执行前进行预处理。先决条件在第一遍进行预处理。事实上,在 90% 的情况下,在配方中更改品牌变量的值是错误的。 (还要记住,所有预处理都是在将配方提供给 shell 之前完成的)。

Does anyone know if this is possible at all?

当然一切皆有可能,但不是这样。

A thing to note is that I cannot place the counting of images outside the count_images target.

很有可能,你可以。

无论如何,重点是一些 shell 脚本(配方,或配方的一部分)应该 return 一个数字。但是,这样的 return 值不能存储在 make 的变量中。 Re-think 你的设计,并找到另一种方式在你的目标之间进行通信。