To report the name of the target when it runs the rule, put a line in the rule:
foo$(VAR): $(PREREQS)
@echo now making the foo target: $@
do_other_stuff...
To spit them all out at once, you could make a separate PHONY target:
.PHONY: show_vars
show_vars:
@echo foo$(VAR)
@echo bar$(PARAM) blah$(FLAG)
# and so on
And this can be made a prerequisite of your default target:
all: show_vars
...
EDIT:
You want a way to show all possible targets of an arbitrary makefile, which I suppose means non-intrusively. Well...
To do it exactly, and be able to cope with sophisticated makefiles, e.g. involving rules constructed by eval statements, you'd have to write something close to a Make emulator. Impractical.
To see the targets of the simple rules, you could write a makefile that would act as a makefile scanner, operating on an arbitrary makefile:
Get all the target names from the makefile using sed.
`include` the makefile in order to use it to expand variables.
Use `show_%: ; echo $$*` to print all the targets
This would be an impressive piece of work. Are you sure the goal is worth the effort?
Can you parse the output from make -pn (i.e. make --print-data-base --dry-run)? It prints out all the variables, rules, implicit rules and which commands will be run in laborious detail.
This mostly works, although it might still include some non-target stuff like a vpath directive. If you don't depend on make's built-in rules, you can use make -qpR as the first command in the pipeline.
Several responders have suggested using make -pn, which will print the database of rules but not execute anything -- more or less. The problem with this approach is that -n does still invoke all recursive makes, and it does still do a lot more work than necessary, because it prints out every command that it would have invoked in a regular build. A more efficient solution would be to create a trivial makefile, dummy.mk, with these contents:
__all_targets__: ; #no-op
Now invoke make as make -p -f Makefile -f dummy.mk __all_targets__. On any substantial build, the difference in the amount of output generated by make is significant. For example:
Edit: FYI the debian bash completion git repository in another answer now incorporates an enhanced version of this script tailored to bash completion use cases.
#!/bin/bash
SCRIPT='
/^# Make data base/,/^# Files/d # skip until files section
/^# Not a target/,+1 d # following target isnt
/^\.PHONY:/ d # special target
/^\.SUFFIXES:/ d # special target
/^\.DEFAULT:/ d # special target
/^\.PRECIOUS:/ d # special target
/^\.INTERMEDIATE:/ d # special target
/^\.SECONDARY:/ d # special target
/^\.SECONDEXPANSION/ d # special target
/^\.DELETE_ON_ERROR:/ d # special target
/^\.IGNORE:/ d # special target
/^\.LOW_RESOLUTION_TIME:/ d # special target
/^\.SILENT:/ d # special target
/^\.EXPORT_ALL_VARIABLES:/ d # special target
/^\.NOTPARALLEL:/ d # special target
/^\.ONESHELL:/ d # special target
/^\.POSIX:/ d # special target
/^\.NOEXPORT:/ d # special target
/^\.MAKE:/ d # special target
# The stuff above here describes lines that are not
# explicit targets or not targets other than special ones
# The stuff below here decides whether an explicit target
# should be output.
/^[^#\t:=%]+:([^=]|$)/ { # found target block
h # hold target
d # delete line
}
/^# File is an intermediate prerequisite/ { # nope
s/^.*$//;x # unhold target
d # delete line
}
/^([^#]|$)/ { # end of target block
s/^.*$//;x # unhold target
s/:.*$//p # write current target
d # hide any bugs
}
'
make -npq .DEFAULT 2>/dev/null | sed -n -r "$SCRIPT" \
| sort | uniq
This is a much more complete script than the debian bash completion script because it provides results for generated rules which is what the question asks for and the top voted answer (debian bash completion script at the debian git server) doesn't go far enough.
This is not the original script that I linked to but it is much simpler and is a touch faster.
Im working on solaris 10 anda turbo C shell.
The given solution doesn´t work for my makefile project. even after altering the command line above to tcsh syntax.
However, I found out you can get an easy solution using
remake --tasks | grep -v "clean some static output or just grep tabs on start of line" | awk ´{print $1}´