首页 > 解决方案 > ifndef 变量在定义变量时触发

问题描述

为什么,当我运行时make run-e2e-dockerifndef 被激活?应该设置变量,但 make 确定它不是。它可用于运行的 echo 命令。如果我删除 ifndef 并运行 make 它会打印变量。

run-e2e:
ifndef environment
$(error "You can't run run-e2e directly, choose one of the run-e2e-* tasks ${environment}")
endif
    @echo extra_profiles=${extra_profiles}
    @echo environment=${environment}

run-e2e-docker: override extra_profiles = docker-e2e,dev-performance
run-e2e-docker: override environment = docker
run-e2e-docker: run-e2e

标签: makefilegnu-make

解决方案


您不能在上下文中使用 make 条件。当您明显思考时,它们不会被评估。但是您可以使用 shell 条件,而不是:

run-e2e:
    @if [ -z "$(environment)" ]; then \
        echo "You can't run run-e2e directly, choose one of the run-e2e-* tasks"; \
        exit 1; \
    fi
    @echo extra_profiles=${extra_profiles}
    @echo environment=${environment}

GNU Make 手册说……

条件控制 make 在 makefile 中实际“看到”的内容,因此它们不能用于在执行时控制配方。

https://www.gnu.org/software/make/manual/html_node/Conditionals.html


推荐阅读