42

Let's say you have a project structure with lots of Makefiles and there is a top level Makefile that includes all the other.

How can you list all the possible targets?

I know writing

make 

and then tabbing to get the suggestions would generally do the trick, but in my case there are 10000 targets. Doing this passes the results through more and also for some reason scrolling the list results in a freeze. Is there another way?

3 Answers 3

55

This is how the bash completion module for make gets its list:

make -qp |
    awk -F':' '/^[a-zA-Z0-9][^$#\/\t=]*:([^=]|$)/ {split($1,A,/ /);for(i in A)print A[i]}' |
    sort -u

It prints out a newline-delimited list of targets, without paging.

3
  • 6
    Maybe add | sort -u to sort it and remove duplicates
    – BrainStone
    Commented Apr 2, 2017 at 21:25
  • like the | sort -u comment also
    – Mark Ch
    Commented Oct 11, 2018 at 10:47
  • This doesn't work for my Makefile. Another answer did stackoverflow.com/a/26339924/482382 Commented Jul 5, 2021 at 13:11
6

FreeBSD and NetBSD make(1) have a built-in variable called .ALLTARGETS. You can print out its contents like this

make -V .ALLTARGETS

That will unfortunately print out a space-delimited list. I find the following more useful

make -V .ALLTARGETS | tr ' ' '\n'

It prints out each target on its own line.

1
  • If you want to also exclude pseudo targets (.WAIT), you can change the command as follows: make -V '$(.ALLTARGETS:N.WAIT_*:O)'.
    – Bass
    Commented May 16, 2022 at 23:25
0

This is a very simplified version of what the bash-completion script does.

make -npq : 2> /dev/null | \
    awk -v RS= -F: '$1 ~ /^[^#%.]+$/ { print $1 }'
2
  • With this solution private targets such as .PHONY or .ONESHELL are displayed. Usually, one wants to ignore these.
    – x0s
    Commented Mar 9, 2023 at 18:01
  • 1
    @x0s Just add the dot. I've updated it.
    – FelipeC
    Commented Mar 9, 2023 at 19:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .