How to pass argument to Makefile from command line?

后端 未结 5 1094
慢半拍i
慢半拍i 2020-11-28 00:52

How to pass argument to Makefile from command line?

I understand I can do

$ make action VAR=\"value\"
$ value

with Makefile

5条回答
  •  孤独总比滥情好
    2020-11-28 01:51

    Here is a generic working solution based on @Beta's

    I'm using GNU Make 4.1 with SHELL=/bin/bash atop my Makefile, so YMMV!

    This allows us to accept extra arguments (by doing nothing when we get a job that doesn't match, rather than throwing an error).

    %:
        @:
    

    And this is a macro which gets the args for us:

    args = `arg="$(filter-out $@,$(MAKECMDGOALS))" && echo $${arg:-${1}}`
    

    Here is a job which might call this one:

    test:
        @echo $(call args,defaultstring)
    

    The result would be:

    $ make test
    defaultstring
    $ make test hi
    hi
    

    Note! You might be better off using a "Taskfile", which is a bash pattern that works similarly to make, only without the nuances of Maketools. See https://github.com/adriancooney/Taskfile

提交回复
热议问题