shithub: mc

ref: 3184654379fd4d26f949189823b054b604addcbf
dir: /clean.myr/

View raw version
use std

use "config.use"
use "deps.use"
use "opts.use"
use "parse.use"
use "subdir.use"
use "types.use"

pkg bld =
	const cleanall	: (p : parser# -> bool)
	const clean	: (p : parser#, targ : byte[:] -> bool)
;;

const cleanall = {p
	for t in p.targs
		match t
		| `Bin bt:
			cleanup(p, bt, bt.inputs, true)
		| `Lib lt:
			cleanup(p, lt, lt.inputs, true)
		| `Test tt:
			cleanup(p, tt, tt.inputs, true)
		| `Gen gt:
			for f in gt.out
				if std.remove(f)
					std.put("\tclean %s\n", f)
				;;
			;;
		| `Sub subs:
			subdirs(p, subs, `std.None)
		| `Man m:
		;;
	;;
	-> true
}

const clean = {p, targ
	for t in p.targs
		match t
		| `Bin bt:
			if std.sleq(bt.name, targ)
				cleanup(p, bt, bt.inputs, true)
			;;
		| `Lib lt:
			if std.sleq(lt.name, targ)
				cleanup(p, lt, lt.inputs, true)
			;;
		| `Test tt:
			if std.sleq(tt.name, targ)
				cleanup(p, tt, tt.inputs, true)
			;;
		| `Gen gt:
		| `Sub subs:
			subdirs(p, subs, `std.Some targ)
		| `Man m:
		;;
	;;
	-> true
}

const cleanup = {p, targ, leaves, islib
	var mchammer_files /* cant touch this */
	var keys
	var dg

	/*
	we want to automatically add 'clean' sources since otherwise,
	mbld won't be able to clean code after changing a build file.
	*/
	if !myrdeps(p, targ, islib, true, true, &dg)
		std.fatal(1, "Could not load dependencies for %s\n", targ.name)
	;;
	mchammer_files = std.mkht(std.strhash, std.streq)
	for l in leaves
		std.htput(mchammer_files, l, true)
	;;

	keys = std.htkeys(dg.deps)
	for k in keys
		if !std.htgetv(mchammer_files, k, false) && std.remove(k)
			std.put("\tclean %s\n", k)
		;;
	;;
}