Senthil Kumaran
Senthil Kumaran

Reputation: 56951

Makefile target does not recognize dependency action

I wrote this simple Makefile to illustrate my problem.

$make target

What is happening here and how should I fix this?


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)

Upvotes: 0

Views: 52

Answers (1)

Toby Speight
Toby Speight

Reputation: 30961

That test isn't subsequent. It's substituted into the Makefile when it's read, before any rules are executed.

You probably want to perform that test in the commands of the target rule:

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

We could change the command to just run docker image inspect - that will return a true status if the image exists, and false otherwise:

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

Upvotes: 1

Related Questions