clean.mk 1.96 KB
Newer Older
1 2 3 4 5
# this makefile can be used standalone

# drop stock predefined rules
.DEFAULT:

6 7
SYMLINK = build

8 9
# tmpfs-sparing extra rule: cleanup workdir after completing each stage
# (as packed results are saved this only lowers RAM pressure)
10 11 12 13
# NB: it's useful enough to be enabled by default in DEBUG abscence
ifndef DEBUG
CLEAN ?= 1
endif
14 15 16
ifdef CLEAN
export GLOBAL_CLEAN_WORKDIR = clean-current
ifdef DEBUG
17
WARNING = (NB: DEBUG scope is limited when CLEAN is enabled)
18 19 20 21
endif
endif

# ordinary clean: destroys workdirs but not the corresponding results
22
# NB: our output MUST go into stderr to escape POSTPROC
23
clean:
24 25 26
	@{ \
	find -name '*~' -delete >&/dev/null ||:; \
	if [ -L "$(SYMLINK)" -a -d "$(SYMLINK)"/ ]; then \
27 28 29
		if [ -z "$(QUIET)" ]; then \
			echo "$(TIME) cleaning up $(WARNING)"; \
		fi; \
30 31
		$(MAKE) -C "$(SYMLINK)" $@ \
			GLOBAL_BUILDDIR="$(realpath $(SYMLINK))" $(LOG) ||:; \
32 33
	fi; \
	} >&2
34

35 36 37
# there can be some sense in writing log here even if normally
# $(BUILDDIR)/ gets purged: make might have failed,
# and BUILDLOG can be specified by hand either
38
distclean: clean
39 40
	@{ \
	if [ -L "$(SYMLINK)" -a -d "$(SYMLINK)"/ ]; then \
41 42 43 44 45 46 47 48 49
		build="$(realpath $(SYMLINK)/)"; \
		if [ "$$build" = / ]; then \
			echo "** ERROR: invalid \`"$(SYMLINK)"' symlink" >&2; \
			exit 128; \
		else \
			$(MAKE) -C "$(SYMLINK)" $@ \
				GLOBAL_BUILDDIR="$$build" $(LOG) ||: \
			rm -rf "$$build"; \
		fi; \
50 51 52
	fi; \
	rm -f "$(SYMLINK)"; \
	} >&2
53 54

# builddir existing outside read-only metaprofile is less ephemeral
55
# than BUILDDIR variable is -- usually it's unneeded afterwards
56
postclean: build-image
57
	@{ \
58 59 60 61 62
	if [ "$(CLEAN)" != 0 ] && \
	   [ "0$(DEBUG)" -lt 2 ] && \
	   [ -z "$(CHECK)" ] && \
	   [ -z "$(REPORT)" ] && \
	   [ "$(NUM_TARGETS)" -gt 1 \
63
	       -o ! -t 1 \
64 65
	       -o ! -L "$(SYMLINK)" ]; \
	then \
66 67 68
		if [ -z "$(QUIET)" ]; then \
			echo "$(TIME) cleaning up after build"; \
		fi; \
69 70 71
		$(MAKE) -C "$(BUILDDIR)" distclean \
			GLOBAL_BUILDDIR="$(BUILDDIR)" $(LOG) ||:; \
		rm -rf "$(BUILDDIR)"; \
72 73
	fi; \
	} >&2