From: Matt McCutchen Date: Wed, 13 Jun 2007 22:36:55 +0000 (-0400) Subject: Add implementation, demo and tests for `make clean'. X-Git-Url: https://mattmccutchen.net/mgear/mgear.git/commitdiff_plain/59ba5775171ada02731636b6ec3877f493085153 Add implementation, demo and tests for `make clean'. --- diff --git a/demo/Makefile b/demo/Makefile index 75eaf85..41f7ef1 100644 --- a/demo/Makefile +++ b/demo/Makefile @@ -16,3 +16,8 @@ $(call mg-define-rule,%.h,%,$(value cmd-addheader)) mainoid := $(newoid) all: $(mainoid) $(mainoid)@opr:=foo.x.h + +clean: + $(call mg-clean-cmd,.) +.PHONY: clean + \ No newline at end of file diff --git a/demo/clean b/demo/clean deleted file mode 100755 index c7f9885..0000000 --- a/demo/clean +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -rm -f foo.x foo.x.g foo.x.h foo.x.h.g diff --git a/mgear.mk b/mgear.mk index 22465ce..d456fd5 100644 --- 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) diff --git a/testsuite b/testsuite index 210274e..a0dc8aa 100755 --- a/testsuite +++ b/testsuite @@ -70,6 +70,15 @@ function assert_generated { assert_saw "$1.tmp" } +function assert { + if eval $*; then + echo "Good, $* returned true." + else + echo "Expected $* to return true but it returned false!" + fail + fi +} + function remember_mtime { while [ $# != 0 ]; do eval "orig_mtime_${1//[^A-Za-z]/}=$(stat --format=%Y $1)" @@ -104,13 +113,20 @@ function assert_not_touched { # Simple makefile for stripping two kinds of comments. # Tests an implicit rule and two competing explicit rules. # Watch those dollar signs! + cat >Makefile <<'EOF' include mgear.mk .SECONDARY: + include hc-rule.mk $(call mg-define-rule,%,%.ssc,sleep 1 && grep 'warn' $$< && sed -e 's_//.*$$$$__' $$< >$$t) $(call mg-define-rule,index,index.in,sort $$< >$$t) + +clean: + $(call mg-clean-cmd,.) +.PHONY: clean EOF + cat >hc-rule.mk <<'EOF' $(call mg-define-rule,%,%.hc,sed -e 's_#.*$$$$__' $$< >$$t) EOF @@ -232,6 +248,45 @@ assert_generated foo assert_touched foo.g assert_not_touched foo +# Test cleaning. Remember, bar is still overridden. + +start_section "Cleaning" +do_mgear clean + +assert_saw 'rm.*foo' # Some sort of indication of foo's deletion +assert ! [ -f foo ] # foo deleted +assert ! [ -f foo.g ] # foo.g deleted +assert [ -f bar ] # bar not actually deleted... +assert_contents bar <<bar +do_mgear bar + +assert_saw -i overrid +assert_contents bar <<