Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 895
doremi Avatar asked Sep 08 '26 07:09

doremi


1 Answers

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
like image 176
shalomb Avatar answered Sep 11 '26 13:09

shalomb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!