首页 > 解决方案 > Makefile 目标不识别依赖动作

问题描述

我写了这个简单的 Makefile 来说明我的问题。

$make target

这里发生了什么,我应该如何解决这个问题?


IMG := hello-world

.PHONY: target
target: dep
ifeq ($(shell docker image list -q $(IMG)),)
        echo "docker image list did not recognize the pull"
endif

.PHONY: dep
dep:
        @docker pull $(IMG)

标签: makefile

解决方案


该测试不是后续的。它在读取时被替换到 Makefile 中,在执行任何规则之前。

您可能希望在规则的命令中执行该测试target

target: dep
        if test -z "$$(docker image list -q $(IMG))"; then \
            echo "docker image list did not recognize the pull" >&2; \
            false; \
        fi

我们可以将命令更改为仅运行docker image inspect- 如果图像存在则返回 true 状态,否则返回 false :

target: dep
        if ! docker image inspect "$(IMG))" >/dev/null 2>&1; then \
            echo "docker image list did not recognize the pull" >&2; \
            false; \
        fi

推荐阅读