How to pass argument to Makefile from command line?

后端 未结 5 1074
慢半拍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:50

    You probably shouldn't do this; you're breaking the basic pattern of how Make works. But here it is:

    action:
            @echo action $(filter-out $@,$(MAKECMDGOALS))
    
    %:      # thanks to chakrit
        @:    # thanks to William Pursell
    

    EDIT:
    To explain the first command,

    $(MAKECMDGOALS) is the list of "targets" spelled out on the command line, e.g. "action value1 value2".

    $@ is an automatic variable for the name of the target of the rule, in this case "action".

    filter-out is a function that removes some elements from a list. So $(filter-out bar, foo bar baz) returns foo baz (it can be more subtle, but we don't need subtlety here).

    Put these together and $(filter-out $@,$(MAKECMDGOALS)) returns the list of targets specified on the command line other than "action", which might be "value1 value2".

提交回复
热议问题