多个目标特定的变量值 [英] multiple Target-specific Variable Values

查看:44
本文介绍了多个目标特定的变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文档之后遵循管理目标特定的变量值

prog : CFLAGS = -g
prog : prog.o foo.o bar.o

我可以设置目标特定的变量.

I can set a target-specific variable.

现在我的问题是,如何设置多个特定于目标的变量.我必须一一设置吗?

Now my question is, how to set multiple target-specific variables. Do I have to set them one by one?

dev_deploy: env = dev
dev_deploy: image = abc
dev_deploy: tag = 1.0.4-dev
dev_deploy:
      docker run -t --rm -e env=$(env) \
         $(image):$(tag) \
         sh -c "test.sh"


prod_deploy: env = prod
prod_deploy: image = abc
prod_deploy: tag = 1.0.3-prod
prod_deploy:
      docker run -t --rm -e env=$(env) \
         $(image):$(tag) \
         sh -c "test.sh"

有什么方法可以通过简单的方法设置本地环境(特定于目标的变量)?

Are there any ways I can set the local environments (target-specific variables) with simple way?

推荐答案

第一个问题的直接答案是,您必须为每个特定于目标的变量使用单独的目标行,并逐个设置它们.

The direct answer to your first question is yes, you have to use a separate target line for each target-specific variable and set them one-by-one.

第二个问题的答案是肯定的:uzsolt描述了一个问题.那也是我推荐的方法.uzsolt解决方案与特定于目标的变量解决方案之间的区别是特定于目标的变量为

The answer to your second question is yes: one is described by uzsolt. That would be my recommended method as well. One difference between uzsolt's solution and the target-specific variable solution is that target-specific variables are inherited by prerequisites. You don't seem to need that behavior here but if you did then this solution wouldn't work for you.

如果您需要特定于目标的变量,或者每个变量分配的一行仍然太痛苦(例如,您有很多这样的变量,并且您确实希望每个组只有一行),则可能有更多选项理解起来很复杂,但以后使用起来更简单.

If you need target-specific variables or if one line per variable assignment is still too painful (for example you have very large numbers of these and you really want to have one line per group), there may be options which are more complicated to understand but simpler to use later.

例如,如果您知道所有变量值都不包含空格,则可以创建一个用户定义的宏,该宏使用eval设置特定于目标的变量,如下所示:

For example if you know that none of your variable values will contain whitespace then you can create a user-defined macro that uses eval to set target-specific variables, like this:

assign-vars = $(foreach A,$2,$(eval $1: $A))

$(call assign-vars, dev_deploy,   env=devenv  image=abc  tag=1.0.4-dev)
$(call assign-vars, prod_deploy,  env=prod    image=abc  tag=1.0.3-prod)

注意:第一个参数是目标,每个赋值必须是一个单词"(没有嵌入空格),并且赋值之间没有逗号.

Note: the first argument is the target, each assignment must be a single "word" (no embedded space) and there are no commas between assignments.

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

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