Search code examples
makefilecommand-lineargumentsshcommand-line-arguments

How can I pass arguments to a make rule?


Suppose I had a python project that uses make to install. I want to be able to run the project without installing it first. So I created this make rule:

run:
    @echo "Running projectname"
    @PYTHONPATH=${PYTHONPATH}:$(abs_srcdir)/..; ./projectname

Where ./projectname runs a simple python script that sets up and runs the project, but that's not important here. Like that, I can simply execute make run in the root folder of the project to execute and test my application, which works perfectly fine. Now, I want to pass some command line arguments to the program. I tried make run --help, which just printed make's help text. Running make run -- --help printed

Running projectname
make: *** No rule to make target '--help'.  Stop.

The application is run, and after I exit it, make tries to execute a target --help.

Now, how can I pass for example a --help argument to my application through make ?


Solution

  • Run make like this:

    make run ARGS=“arg1 arg2”
    

    Then in your Makefile use:

    run:
        @echo "Running projectname"
        @PYTHONPATH=${PYTHONPATH}:$(abs_srcdir)/..; ./projectname ${ARGS}