summaryrefslogtreecommitdiff
path: root/mbld/install.myr
blob: ca053db1be9db81e672c12650532d278652b877b (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
91
92
93
94
use std

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

pkg bld =
	const install	: (p : build#	-> bool)
	const uninstall	: (p : build#	-> bool)
;;

const install = {b
	buildall(b)
	-> movetargs(b, false)
}

const uninstall = {b
	-> movetargs(b, true)
}

const movetargs = {b, delete
	var libarchive

	for tn in b.all
		match gettarg(b.targs, tn)
		| `Bin bt:
			movefile(b, delete, bt.dir, bt.name, opt_instroot, opt_destdir, "bin", 0o755)
		| `Lib lt:
			movefile(b, delete, lt.dir, lt.name, opt_instroot, opt_destdir, "lib/myr", 0o644)
			libarchive = std.fmt("lib%s.a", lt.name)
			movefile(b, delete, lt.dir, libarchive, opt_instroot, opt_destdir, "lib/myr", 0o644)
			std.slfree(libarchive)
		| `Gen gt:
			/* nothing to do (?) */
		| `Man mt:
			/* FIXME: figure out man section by number */
			for m in mt.pages
				moveman(b, delete, mt.dir, m)
			;;
		| `Test tt:	/* nothing */
		;;
	;;
	-> true
}


const movefile = {b, delete, dir, file, instdir, destdir, prefix, perm
	var path

	pushdir(b, dir)
	path = std.pathjoin([destdir, instdir, prefix, file][:])
	if delete
		std.put("\tdelete %s\n", path)
		if !std.remove(path)
			std.put("\t\tno such file %s\n", file)
		;;
	else
		std.put("\t%s => %s\n", file, path)
		std.remove(path)
		match std.slurp(file)
		| `std.Fail m:	std.fatal(1, "Could not open %s for reading\n", file)
		| `std.Ok buf:
			if !std.blat(path, buf, perm)
				std.put("Could not write %s\n", file)
			;;
			std.slfree(buf)
		;;
	;;
	std.slfree(path)
	popdir(b)
}

const moveman = {b, delete, dir, man
	var sect, manrel

	match std.strrfind(man, ".")
	| `std.None:
		std.fatal(1, "manpage %s has no section\n", man)
	| `std.Some s:
		sect = s + 1
		if s + 1 == man.len
			std.fatal(1, "manpage %s missing suffix\n", man)
		;;
	;;

	manrel = std.fmt("%s%s", opt_manpath, man[sect:])
	movefile(b, delete, dir, man, opt_instroot, opt_destdir, manrel, 0o644)
	std.slfree(manrel)
}