英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论