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

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

    Focusing on an easy syntax for describing a make target, and having a clean output, I chose this approach:

    help:
        @grep -B1 -E "^[a-zA-Z0-9_-]+\:([^\=]|$$)" Makefile \
         | grep -v -- -- \
         | sed 'N;s/\n/###/' \
         | sed -n 's/^#: \(.*\)###\(.*\):.*/\2###\1/p' \
         | column -t  -s '###'
    
    
    #: Starts the container stack
    up: a b
      command
    
    #: Pulls in new container images
    pull: c d 
        another command
    
    make-target-not-shown:
    
    # this does not count as a description, so leaving
    # your implementation comments alone, e.g TODOs
    also-not-shown:
    

    So treating the above as a Makefile and running it gives you something like

    > make help
    up          Starts the container stack
    pull        Pulls in new container images
    

    Explanation for the chain of commands:

    • First, grep all targets and their preceeding line, see https://unix.stackexchange.com/a/320709/223029.
    • Then, get rid of the group separator, see https://stackoverflow.com/a/2168139/1242922.
    • Then, we collapse each pair of lines to parse it later, see https://stackoverflow.com/a/9605559/1242922.
    • Then, we parse for valid lines and remove those which do not match, see https://stackoverflow.com/a/8255627/1242922, and also give the output our desired order: command, then description.
    • Lastly, we arrange the output like a table.

提交回复
热议问题