From 7e2128321500801bf61fb2bdd45e016cbebeaf57 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Fri, 5 Oct 2012 12:39:38 -0400 Subject: [PATCH] Makefile: Add print-% and printvars rules for Makefile introspection. Based on John Graham-Cumming's "Dumping Every Makefile Variable" http://www.cmcrossroads.com/ask-mr-make/6521-dumping-every-makefile-variable --- Makefile | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 74a07e0..1e31bc7 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,7 @@ TAR = tar # Declare targets that do not generate files of the same name. # http://www.gnu.org/software/make/manual/html_node/Phony-Targets.html -.PHONY: all help clean dist run +.PHONY: all help clean dist run print- printvars # target: all - the default target all: $(PROGRAM) @@ -102,3 +102,35 @@ run: $(PROGRAM) # The comments from the C++ rule above also apply here %.o: %.c $(CC) $(CFLAGS) -c $< + +# target: print-% - display a variable value (e.g. print-PROGRAMS) +# Take some of the mystery out of variable manipulation. For example, +# $ make print-hello_world_OBJECTS +# The `@` suppresses recipe echoing. See +# http://www.gnu.org/software/make/manual/html_node/Echoing.html +# The `info` function acts like the shell `echo` command. See +# http://www.gnu.org/software/make/manual/html_node/Make-Control-Functions.html +# For an explanation of $* and other special variables, see +# http://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html +print-%: + @$(info $* = $($*)) + +# target: printvars - display all non-default variables +# .VARIBALES holds a list of all global variables. See +# http://www.gnu.org/software/make/manual/html_node/Special-Variables.html +# The functions---`sort`, `if`, `filter-out`, `origin`, `warning`, and +# `value`--- are discussed, respectively, here: +# http://www.gnu.org/software/make/manual/html_node/Text-Functions.html#index-sort-580 +# http://www.gnu.org/software/make/manual/html_node/Conditional-Functions.html +# http://www.gnu.org/software/make/manual/html_node/Text-Functions.html#index-filter_002dout-577 +# http://www.gnu.org/software/make/manual/html_node/Origin-Function.html +# http://www.gnu.org/software/make/manual/html_node/Value-Function.html +printvars: + @$(foreach V, \ + $(sort $(.VARIABLES)), \ + $(if \ + $(filter-out environment default automatic, \ + $(origin $V)), \ + $(info $V=$($V) (origin: $(origin $V), value: $(value $V))) \ + ) \ + ) -- 2.26.2