Makefile动态变量作为前提条件 [英] Makefile dynamic variables as prerequisites

查看:108
本文介绍了Makefile动态变量作为前提条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Perhaps it's something that I'm getting wrong. Basically my task is to use make to automate a build, deploy, starting, stopping of different services. 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.

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

Here's a basic sample of what I'm trying to do:

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.需要注意的是,我无法将图像计数放置在目标之外. 有谁知道这是否有可能吗?

Obviously the $(IMAGE_COUNT) in _should_build check will stay as -1, but what I want is to have the $(IMAGE_COUNT) become a 5 during the prerequisite check. A thing to note is that I cannot place the counting of images outside the count_images target. Does anyone know if this is possible at all?

推荐答案

也许是我错了.

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

这种东西"被称为评估令.

That "something" is called an evaluation order.

我要做的一件事情是将变量作为目标先决条件,但是该变量必须在另一个目标中进行更改.

不是目标,而是食谱.配方在执行之前经过预处理.前提条件是在第一遍过程中进行了预处理.实际上,在90%的情况下更改配方中的make变量的值是一个错误. (还要记住,所有预处理都是在将配方输入外壳之前完成的.)

Not a target, but a recipe. The recipes are preprocessed before execution. While the prerequisites are preprocessed on the first pass. In fact, changing the value of a make's variable inside a recipe in 90% of cases is a mistake. (Also remember that all preprocessing is done before feeding the recipe to the shell).

有人知道这是否完全可能吗?

Does anyone know if this is possible at all?

一切皆有可能,但并非如此.

Everything is possible, of course, but not this way.

需要注意的是,我不能将图像计数放在count_images目标之外.

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

很有可能.

无论如何,关键是某些shell脚本(配方或配方的一部分)应该返回一个数字.但是,这样的返回值不能存储在make的变量中.重新考虑您的设计,并找到目标之间进行交流的另一种方式.

Anyway, the point is that some shell script (a recipe, or a part of a recipe) should return a number. However, such return values cannot be stored in a make's variable. Re-think your design and find another way for communication between your targets.

这篇关于Makefile动态变量作为前提条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆