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

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

    Yet another additional answer to above.

    tested on MacOSX using only cat and awk on terminal

    cat Makefile | awk '!/SHELL/ && /^[A-z]/ {print $1}' | awk '{print substr($0, 1, length($0)-1)}'
    

    will output of the make file like below:

    target1

    target2

    target3

    in the Makefile, it should be the same statement, ensure that you escape the variables using $$variable rather than $variable.

    Explanation

    cat - spits out the contents

    | - pipe parses output to next awk

    awk - runs regex excluding "shell" and accepting only "A-z" lines then prints out the $1 first column

    awk - yet again removes the last character ":" from the list

    this is a rough output and you can do more funky stuff with just AWK. Try to avoid sed as its not as consistent in BSDs variants i.e. some works on *nix but fails on BSDs like MacOSX.

    More

    You should be able add this (with modifications) to a file for make, to the default bash-completion folder /usr/local/etc/bash-completion.d/ meaning when you "make tab tab" .. it will complete the targets based on the one liner script.

提交回复
热议问题