Add implementation, demo and tests for `make clean'.
[mgear/mgear.git] / mgear.mk
index 22465ce..d456fd5 100644 (file)
--- a/mgear.mk
+++ b/mgear.mk
@@ -272,6 +272,30 @@ mg-maybe-move-target={ cmp -s $@ $@.tmp || { echo >$@.g && mv -f $@.tmp $@; }; }
 mg-define-prereq=$(eval $1: $2)
 
 
+# CLEAN
+
+# A command that finds all genfiles in a given directory and deletes them and
+# the corresponding generated files.  If a file is overridden, only the genfile
+# is deleted.
+#
+# Example:
+#
+# clean:
+#         $(call mg-clean-cmd,.)
+# .PHONY: clean
+
+define mg-clean-cmd
+       @exec 3>&1 &&\
+       find $1 -name '*.g' | while read gf; do\
+       f="$${gf%.g}" &&\
+       if ! [ "$$f" -nt "$$gf" ]; then\
+               echo "rm -f '$$f'" >&3 && echo "$$f";\
+       fi &&\
+       echo "$$gf";\
+       done | xargs rm -f
+endef
+
+
 # DEPENDENCY-LOGGING COMMANDS
 
 mg-dlc-static-run-oid:=$(mg-genfile-oid)