Search code examples
makefilegnu-make

Makefile string comparison inside target


I have the following Makefile

FIRSTARG := $(firstword $(MAKECMDGOALS))
# use the rest as arguments
RUNARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
# ...and turn them into do-nothing targets
$(eval $(RUNARGS):;@:)


test:
    if [[ "$(FIRSTARG)" == "test" ]]; then \
        pytest --cov --cov-report term-missing ;\
    elif [[ "docker" == "$(FIRSTARG)" ]]; then \
        sudo docker run merlot:latest ;\
    fi; 

docker : $(RUNARGS)
    @echo first word: $(FIRSTARG)


.PHONY: docker build test prune

but I'm getting the error

if [[ "test" == "test" ]]; then \
    pytest --cov --cov-report term-missing ;\
elif [[ "docker" == "test" ]]; then \
    sudo docker run merlot:latest ;\
fi; 
/bin/sh: 1: [[: not found
/bin/sh: 3: [[: not found

I have also tried using ifeq ("test","@(FIRSTARG)") which threw different errors. I've searched this up quite a bit but can't seem to find the proper way to compare strings inside of a target. Can you please help me get the proper way to do string comparison in a make target?

Thank you!


Solution

  • All make programs invoke /bin/sh which is a shell that conforms to the POSIX spec for shell scripts. The POSIX standard doesn't define the operator [[, so you can't use it.

    The best thing to do is use POSIX standard scripting, so just use [ instead of [[ and only use = instead of ==.