summaryrefslogtreecommitdiff
path: root/mbld/clean.myr
blob: 5452ae50d5ce3721c44ee4a5d8f2fd7c96962a7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use std

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

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

const cleanall = {b
	for tn in b.all
		match gettarg(b.targs, tn)
		| `Bin bt:
			cleanup(b, bt, bt.inputs, true)
		| `Lib lt:
			cleanup(b, lt, lt.inputs, true)
		| `Test tt:
			cleanup(b, tt, tt.inputs, true)
		| `Gen gt:
			for f in gt.out
				if !gt.durable && std.remove(f)
					std.put("\tclean %s\n", f)
				;;
			;;
		| `Man m:
		;;
	;;
	-> true
}

const clean = {b, targ
	for tn in b.all
		match gettarg(b.targs, tn)
		| `Bin bt:
			if std.sleq(bt.name, targ)
				cleanup(b, bt, bt.inputs, true)
			;;
		| `Lib lt:
			if std.sleq(lt.name, targ)
				cleanup(b, lt, lt.inputs, true)
			;;
		| `Test tt:
			if std.sleq(tt.name, targ)
				cleanup(b, tt, tt.inputs, true)
			;;
		| `Gen gt:
			for f in gt.out
				if !gt.durable && std.remove(f)
					std.put("\tclean %s\n", f)
				;;
			;;
		| `Man m:
		;;
	;;
	-> true
}

const cleanup = {b, 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.
	*/
	pushdir(b, targ.dir)
	if !myrdeps(b, 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)
		;;
	;;
	popdir(b)
}