Makefile: Add print-% and printvars rules for Makefile introspection.
authorW. Trevor King <wking@tremily.us>
Fri, 5 Oct 2012 16:39:38 +0000 (12:39 -0400)
committerW. Trevor King <wking@tremily.us>
Fri, 5 Oct 2012 16:39:38 +0000 (12:39 -0400)
Based on John Graham-Cumming's "Dumping Every Makefile Variable"
  http://www.cmcrossroads.com/ask-mr-make/6521-dumping-every-makefile-variable

Makefile

index 74a07e0f1d18cf6dd89d9f024b23ed5bef306b88..1e31bc7fb5ce6de09bc89fde84f099f1a2a0c257 100644 (file)
--- 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))) \
+                       ) \
+               )