How do you get the list of targets in a makefile?

后端 未结 20 1330
难免孤独
难免孤独 2020-11-30 16:50

I\'ve used rake a bit (a Ruby make program), and it has an option to get a list of all the available targets, eg

> rake --tasks
rake db:charset      # ret         


        
相关标签:
20条回答
  • 2020-11-30 17:42

    This is a modification to jsp's very helpful answer (https://stackoverflow.com/a/45843594/814145). I like the idea of getting not only a list of targets but also their descriptions. jsp's Makefile puts the description as the comment, which I found often will be repeated in the target's description echo command. So instead, I extract the description from the echo command for each target.

    Example Makefile:

    .PHONY: all
    all: build
            : "same as 'make build'"
    
    .PHONY: build
    build:
            @echo "Build the project"
    
    .PHONY: clean
    clean:
            @echo "Clean the project"
    
    .PHONY: help
    help:
            @echo -n "Common make targets"
            @echo ":"
            @cat Makefile | sed -n '/^\.PHONY: / h; /\(^\t@*echo\|^\t:\)/ {H; x; /PHONY/ s/.PHONY: \(.*\)\n.*"\(.*\)"/    make \1\t\2/p; d; x}'| sort -k2,2 |expand -t 20
    

    Output of make help:

    $ make help
    Common make targets:
        make all        same as 'make build'
        make build      Build the project
        make clean      Clean the project
        make help       Common make targets
    

    Notes:

    • Same as jsp's answer, only PHONY targets may be listed, which may or may not work for your case
    • In addition, it only lists those PHONY targets that have a echo or : command as the first command of the recipe. : means "do nothing". I use it here for those targets that no echo is needed, such as all target above.
    • There is an additional trick for the help target to add the ":" in the make help output.
    0 讨论(0)
  • 2020-11-30 17:42

    This help target will only print targets which have ## followed by a description. This allows for documenting both public and private targets. Using the .DEFAULT_GOAL makes the help more discoverable.

    Only sed, xargs and printf used which are pretty common.

    Using the < $(MAKEFILE_LIST) allows for the makefile to be called something other than Makefile for instance Makefile.github

    You can customize the output to suit your preference in the printf. This example is set up to match the OP's request for rake style output

    When cutting and pasting the below make file, don't forget to change the 4 spaces indentation to tabs.

    # vim:ft=make
    # Makefile
    
    .DEFAULT_GOAL := help
    .PHONY: test help
    
    help:  ## these help instructions
        @sed -rn 's/^([a-zA-Z_-]+):.*?## (.*)$$/"\1" "\2"/p' < $(MAKEFILE_LIST) | xargs printf "make %-20s# %s\n"
    
    lint: ## style, bug and quality checker
        pylint src test
    
    private: # for internal usage only
        @true
    
    test: private ## run pytest with coverage
        pytest --cov test
    
    
    

    Here is the output from the Makefile above. Notice the private target doesn't get output because it only has a single # for it's comment.

    $ make
    make help                # these help instructions
    make lint                # style, bug and quality checker
    make test                # run pytest with coverage
    
    0 讨论(0)
提交回复
热议问题