Makefile refactoring and cleanup.
[pwl6.git] / rules / default.mk
diff --git a/rules/default.mk b/rules/default.mk
new file mode 100644 (file)
index 0000000..2bfa76d
--- /dev/null
@@ -0,0 +1,59 @@
+# This is free and unencumbered software released into the public
+# domain. To the extent possible under law, the author of this file
+# waives all copyright and related or neighboring rights to it.
+
+# Should be defaults.
+.DELETE_ON_ERROR:
+.DEFAULT_GOAL: all
+
+# Force these to be double-colon targets, so individual makefiles can
+# extend them as necessary (particularly for clean).
+all::
+clean::
+distclean::
+
+# http://blog.jgc.org/2015/04/the-one-line-you-should-add-to-every.html
+# Depends on FORCE because "implicit [pattern] rule search is skipped
+# for .PHONY targets."
+print-%: FORCE ; @echo $*=$($*)
+
+.PHONY: FORCE all clean distclean
+
+# Use with include to ensure files are included only once. e.g.:
+# $(call include-once,example.mk more-rules/*.mk)
+include-once = $(if $(wildcard $1),\
+    $(foreach f,$(sort $(wildcard $1)),\
+        $(eval include $(filter-out $(MAKEFILE_LIST),$f))),\
+    $(error $1: No such file or directory))
+
+# Call before including anything for the current makefile's directory.
+where-am-i = $(dir $(lastword $(MAKEFILE_LIST)))
+
+# Like include-once, but the files are searched for relative to the most
+# recently included file (usually the current one) not the current
+# working directory.
+include-once. = $(call include-once,$(addprefix $(call where-am-i),$1))
+
+
+# Quiet the output of a command, unless an error occurs. This is
+# similar to the shell 'chronic' / 'cronic' programs.
+#
+# To hide output unless an error occurs, pass a single argument:
+# $(call quiet,munge somefile)
+#
+# To store output to a file unless an error occurs, pass a second
+# argument of the filename. If an error occurs, this file will not
+# exist. Such a file will often be the rule's target.
+# $(call quiet,munge somefile,$@)
+
+define quiet
+@echo "$1" $(if $2,"> $2")
+$(eval QUIETTMP := $(firstword $2 $(shell mktemp -t make-quiet.XXXXX)))
+@($1) > $(QUIETTMP) \
+    || (STATUS=$$? \
+        && cat "$(QUIETTMP)" \
+        && $(RM) "$(QUIETTMP)" \
+        && exit $$STATUS)
+$(if $2,,$(RM) "$(QUIETTMP)")
+endef
+