How to get flags passed to a make target

Viewed 19

I want to get the flags passed to a make target. Currently I'm able to get the words passed to it but I would need the flags also. This is the current code:

ifeq (docker,$(firstword $(MAKECMDGOALS)))
  ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
  $(eval $(ARGS):;@:)
endif
.PHONY: docker
docker:
    @${DOCKER_FILE} $(ARGS)

I want to run something liek this: make docker up -d

1 Answers

As written, your make invocation would treat up as a target and -d as a make parameter. You can't tell make to interpret the parameters differently. That being said, you can assign up -d to a variable and pass that in:

make docker ARGS="up -d"
.PHONY: docker
docker:
    @${DOCKER_FILE} $(ARGS)

And this would do what you want. If you NEED to make the syntax similar to what you are describing for some reason, then you could create a wrapper script that would parse the arguments, and reformat them to a make specific target:

#!/bin/bash
[ "$1" == "docker" ] && make $1 ARGS="${@:2}"
bash> makedocker up -d
Related