doremi
doremi

Reputation: 15329

Makefile - Catch-all Target

I'm wondering if it is possible to have a Makefile take a command to an unknown target and just run it directly.

For example, I have a script (which I'm not able to change) that issues commands to my apps Docker container like so:

docker run -it my_app:latest "env FOO=bar bundle exec rails c"

This gets translated to: make "env FOO=bar bundle exec rails c"

Which generates: Makefile:7: *** missing target pattern. Stop.

Here is my attempt at first catching an unknown target:

.DEFAULT_GOAL: passthrough

passthrough:
    @echo $(MAKECMDGOALS) # always empty
    @echo ${ARGS} # always empty

Is it possible to capture the entire string passed to the Makefile in the docker run argument and just run it directly?

Upvotes: 3

Views: 2069

Answers (1)

shalomb
shalomb

Reputation: 3675

I realize this is a year and a half too late but since I had this question and have something reasonable - I will guess others may do down-the-line.

With a Makefile and a .DEFAULT target like this

docker_cmd = docker run --rm -it bash -c

.DEFAULT:
        @$(docker_cmd) "$@"

You can invoke make with a single "target" that gets passed as-is to$@

$ make 'env FOO=bar /usr/bin/uptime'
 21:06:15 up 24 days,  9:26,  load average: 0.54, 0.46, 0.52

The key is that the argument to make here is a single argument that is quoted appropriately - otherwise make does what it does and interprets the arguments as a list of targets to call - and that would do something else entirely.

$ make env FOO=bar /usr/bin/uptime                                                                                                                  
HOSTNAME=434c34a12b25
PWD=/
_BASH_GPG_KEY=7C0135FB088AAF6C66C650B9BB5869F064EA74AB
HOME=/root
_BASH_VERSION=5.0
_BASH_PATCH_LEVEL=0
_BASH_LATEST_PATCH=0
TERM=xterm
SHLVL=0
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env
make: '/usr/bin/uptime' is up to date.
$ make badcmd env FOO=bar /usr/bin/uptime                                                                                                          
bash: badcmd: command not found
make: *** [Makefile:90: badcmd] Error 127

Upvotes: 3

Related Questions