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

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

    To expand on the answer given by @jsp, you can even evaluate variables in your help text with the $(eval) function.

    The proposed version below has these enhanced properties:

    • Will scan any makefiles (even included)
    • Will expand live variables referenced in the help comment
    • Adds documentation anchor for real targets (prefixed with # TARGETDOC:)
    • Adds column headers

    So to document, use this form:

    RANDOM_VARIABLE := this will be expanded in help text
    
    .PHONY: target1 # Target 1 help with $(RANDOM_VARIABLE)
    target1: deps
        [... target 1 build commands]
    
    # TARGETDOC: $(BUILDDIR)/real-file.txt # real-file.txt help text
    $(BUILDDIR)/real-file.txt:
        [... $(BUILDDIR)/real-file.txt build commands]
    

    Then, somewhere in your makefile:

    .PHONY: help # Generate list of targets with descriptions
    help:
        @# find all help in targets and .PHONY and evaluate the embedded variables
        $(eval doc_expanded := $(shell grep -E -h '^(.PHONY:|# TARGETDOC:) .* #' $(MAKEFILE_LIST) | sed -E -n 's/(\.PHONY|# TARGETDOC): (.*) # (.*)/\2  \3\\n/'p | expand -t40))
        @echo
        @echo ' TARGET   HELP' | expand -t40
        @echo ' ------   ----' | expand -t40
        @echo -e ' $(doc_expanded)'
    

提交回复
热议问题