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

后端 未结 20 1508
难免孤独
难免孤独 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 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
    

提交回复
热议问题