makefile-如何为特定目标上的变量分配值? [英] makefile - How to assign a value to a variable on specific target?

查看:98
本文介绍了makefile-如何为特定目标上的变量分配值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码段中:

IMAGES_TO_DELETE := $(aws ecr list-images --region $(ECR_REGION) --repository-name $(ECR_REPO) --filter "tagStatus=UNTAGGED" --query 'imageIds[*]' --output json)

.PHONY: target1 target2 cleanimage

cleanimage:
    ${DISPLAYINFO} "Clean untagged image from AWS ECR "
    aws ecr batch-delete-image --region $(ECR_REGION) --repository-name $(ECR_REPO) --image-ids "$(IMAGES_TO_DELETE)" || true
    ${DISPLAYINFO} "Done"

target1:
   # do something

target2:
   # do something


IMAGES_TO_DELETE以JSON格式提供图像列表.


IMAGES_TO_DELETE gives imagelist, in JSON format.

IMAGES_TO_DELETE应该在make cleanimage执行时分配

如何为cleanimage目标下的变量赋值?

How to assign values to a variable under cleanimage target?

推荐答案

您似乎对make变量一直有误解.

You seem to have a continuing misunderstanding about make variables.

目前尚不清楚为什么它必须是一个变量.只需将其内联到目标中即可:

It's not clear why this needs to be a variable at all. Just inline it into the target:

.PHONY: cleanimage

cleanimage:
    ${DISPLAYINFO} "Clean untagged image from AWS ECR "
    aws ecr batch-delete-image --region $(ECR_REGION) --repository-name $(ECR_REPO) \
        --image-ids "$$(aws ecr list-images --region $(ECR_REGION) --repository-name $(ECR_REPO) \
            --filter "tagStatus=UNTAGGED" --query 'imageIds[*]' --output json)" || true
    ${DISPLAYINFO} "Done"

顺便说一句,$(aws...)不是有效的make函数;你的意思是$(shell aws ...);但同样,如果仅在单个目标中需要,就无需在make中对此进行评估.

As an aside $(aws...) is not a valid make function; you mean $(shell aws ...); but again, there is no need to evaluate this in make at all if it is only needed in a single target.

在重复信息的地方,将其重构为变量是有意义的,因此您只需要在一个地方进行更改即可.

Where you have repeated information, that's where it makes sense to refactor that into a variable so you only have to change it in one place.

ECRFLAGS := --region $(ECR_REGION) --repository-name $(ECR_REPO)

.PHONY: cleanimage

cleanimage:
    ${DISPLAYINFO} "Clean untagged image from AWS ECR "
    aws ecr batch-delete-image $(ECRFLAGS) \
        --image-ids "$$(aws ecr list-images $(ECRFLAGS) \
            --filter "tagStatus=UNTAGGED" --query 'imageIds[*]' --output json)" || true
    ${DISPLAYINFO} "Done"

请记住,make计算单个美元符号.如果要在外壳上传递一个美元符号,则需要将其加倍.

Remember, a single dollar sign is evaluated by make. If you want to pass through a literal dollar sign to the shell, you need to double it.

这篇关于makefile-如何为特定目标上的变量分配值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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