ifndef with multiple AND conditions in makefile gnu

huangapple go评论45阅读模式
英文:

ifndef with multiple AND conditions in makefile gnu

问题

如何确保make命令中传递了参数ENV

例如:make destroy ENV=test 如果没有传递ENV参数,我应该抛出错误。

代码:

ENV ?=prod
ifndef ENV
   $(error ENV未定义,请在您的make命令中提供ENV参数)
ifndef FORCE
   @/bin/echo -n "您确定要销毁${ENV}的配置吗? [y/N] " && read ans && [ $${ans:-N} = y ]
endif
endif
   terraform destroy ${ARGS}
英文:

How do I ensure that make command has the parameter ENV passed in it?

Eg: make destroy ENV=test if ENV is not passed I should throw error.

Code:

ENV ?=prod
ifndef ENV
   $(error ENV is not defined, Please provide ENV paramter in your make command)
ifndef FORCE
   @/bin/echo -n "Are you sure to DESTROY configuration for ${ENV}? [y/N] " && read ans && [ $${ans:-N} = y ]
endif
endif
   terraform destroy ${ARGS}

答案1

得分: 1

以下是翻译好的部分:

The question is whether you want to test whether they are empty, or whether they are defined. If you want to test that at least one is set to a non-empty value, you can use the following trick:

如果您想测试它们是否为空,或者它们是否已定义。如果您想测试是否至少有一个设置为非空值,您可以使用以下技巧:

ifeq ($(CONDITION1)$(CONDITION2),)
# Both CONDITION1 and CONDITION2 are empty or not defined
endif

If you want to test if either is defined (but potentially emtpy), you could would use the origin function:

如果您想测试它们是否已定义(但可能为空),您可以使用origin 函数:

ifeq ($(origin CONDITION1)$(origin CONDITION2),undefinedundefined)
# Both CONDITION1 and CONDITION2 are not defined
endif

For more complex expressions, you can also use the $(if ...), $(and ...) and $(or ...) functions (see here). eg:

对于更复杂的表达式,您还可以使用 $(if ...), $(and ...)$(or ...) 函数(参见这里)。例如:

ifeq ($(or $(CONDITION1),$(CONDITION2)),)
# Both CONDITION1 and CONDITION2 are empty or not defined
endif

EDIT:

As to the updated question, it is a bit different than what you were originally asking. The cleanest way to do this is to add the checks in a recipe rather than conditional parts of the make:

至于更新后的问题,它与您最初的问题有点不同。最干净的方法是在配方中添加检查,而不是在 make 的条件部分中进行检查:

checkenv:
   @[ "$(origin ENV)" -eq "command line" ] \
       || echo "ERROR ENV not defined on command line" >&2 \
       && false
   @[ $(FORCE) ] \
       || echo -n "Are you sure to DESTROY configuration for ${ENV}? [y/N]" \
       && read ans \
       && [ $${ans:-N} = y ] 

maintarget: checkenv
    terraform destroy ${ARGS}
英文:

The question is whether you want to test whether they are empty, or whether they are defined. If you want to test that at least one is set to a non-empty value, you can use the following trick:

ifeq ($(CONDITION1)$(CONDITION2),)
# Both CONDITION1 and CONDITION2 are empty or not defined
endif

If you want to test if either is defined (but potentially emtpy), you could would use the origin function:

ifeq ($(origin CONDITION1)$(origin CONDITION2),undefinedundefined)
# Both CONDITION1 and CONDITION2 are not defined
endif

For more complex expressions, you can also use the $(if ...), $(and ...) and $(or ...) functions (see here). eg:

ifeq ($(or $(CONDITION1),$(CONDITION2)),)
# Both CONDITION1 and CONDITION2 are empty or not defined
endif

EDIT:

As to the updated question, it is a bit different than what you were originally asking. The cleanest way to do this is to add the checks in a recipe rather than conditional parts of the make:

checkenv:
   @[ "$(origin ENV)" -eq "command line" ] \
       || echo "ERROR ENV not defined on command line" >&2 \
       && false
   @[ $(FORCE) ] \
       || echo -n "Are you sure to DESTROY configuration for ${ENV}? [y/N]" \
       && read ans \
       && [ $${ans:-N} = y ] 

maintarget: checkenv
    terraform destroy ${ARGS}

答案2

得分: 0

Make不支持这样做。你可以嵌套它们。这具有相同的效果

ifndef CONDITION1
ifndef CONDITION2
<做某事>
endif
endif
<什么都不做>
英文:

Make doesn't support that. You can nest them. That has the same effect

ifndef CONDITION1
ifndef CONDITION2
<do something>
endif
endif
<do nothing>

huangapple
  • 本文由 发表于 2023年2月27日 18:22:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579217.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定