0

I am currently writing a Makefile that allows me to launch some commands easily, but I want to be able to pass additional arguments. I cant seem to find any documentation about how to do it.

My simplified Makefile is below;

.PHONY: all

info: header usage

define HEADER

MY PROJECT HEADER

endef
export HEADER

header:
    @echo "$$HEADER"

usage:
    @echo "make test          Test environment"

test: header run_test

run_test:
    phpunit 

At the moment if i type make test it will run my phpunit test suite.

What i want to be able to do is add any additional arguments after, for example make test --filter=thistest .. This argument would be passed to the phpunit command which will then look like phpunit --filter=thistest among other arguemnts.

CodeSauce
  • 255
  • 3
  • 19
  • 39
  • The use case you want to achieve is feasible with Make, but not with the very same syntax you suggested. Namely, it's possible to pass environment variables to `make` in several different ways, and specify default values for these variables. It seems this has already been covered by other questions on StackOverflow, so I'll propose to mark your question as a duplicate. – ErikMD Nov 21 '21 at 13:51

1 Answers1

0

All arguments passed to make are parsed by make. They are not passed through to some program that make invokes. It is not possible to do exactly what you want.

What you can do is set a make variable on the command line, to the arguments you want to pass, then use that variable in your recipe:

run_test:
        phpunit $(PHP_FLAGS)

then:

make PHP_FLAGS='--filter=thistest'
MadScientist
  • 92,819
  • 9
  • 109
  • 136
  • I was about to post the same explanation before falling back to a mere comment ;) – ErikMD Nov 21 '21 at 13:53
  • BTW I believe this won't work very well if there are several different phpunit arguments and there's a space in some of their arguments… but I'm unsure if it's feasible to come up with another solution actually for this small issue(?) – ErikMD Nov 21 '21 at 13:54
  • Can't format comments. But probably locating a different answer would have worked too – MadScientist Nov 21 '21 at 13:54
  • 1
    It will work fine if you get the quoting right, and that's a shell issue not a make issue. For example above I use single-quotes: I can include whitespace etc. If you need your arguments to include quotes, then you have to escape them from the shell. Etc. Any value CAN be passed through this way but it may require an extra layer of quoting. – MadScientist Nov 21 '21 at 13:56