summaryrefslogtreecommitdiff
path: root/mbld/install.myr
blob: 2591c59fec5463db4e462bb4402785287894e2ab (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use std

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

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, rm
	var libarchive, libuse
	var pfx

	for tn in b.all
		match gettarg(b.targs, tn)
		| `Bin bt:
			if bt.install && !bt.istest
				movefile(b, rm, bt.dir, bt.name, config.Binpath, 0o755)
			;;
		| `Lib lt:
			if lt.install && !lt.istest
				libuse = std.fmt("lib{}.use", lt.name)
				movefile(b, rm, lt.dir, libuse, config.Libpath, 0o644)
				libarchive = std.fmt("lib{}.a", lt.name)
				movefile(b, rm, lt.dir, libarchive, config.Libpath, 0o644)
				std.slfree(libarchive)
				std.slfree(libuse)
			;;
		| `Data dt:
			for blob in dt.blobs
				if dt.path.len == 0
					pfx = std.pathcat(config.Sharepath, dt.name)
					movefile(b, rm, dt.dir, blob, pfx, 0o644)
					std.slfree(pfx)
				else
					movefile(b, rm, dt.dir, blob, dt.path, 0o644)
				;;
			;;
		| `Gen gt:
			/* nothing to do */
		| `Cmd ct:
			/* nothing to do */
		| `Man mt:
			/* FIXME: figure out man section by number */
			for m in mt.pages
				moveman(b, rm, mt.dir, m)
			;;
		;;
	;;
	-> true
}


const movefile = {b, rm, dir, file, prefix, perm
	var path

	setdir(b, dir)
	path = std.pathjoin([opt_destdir, opt_instbase, prefix, file][:])
	if rm
		std.put("\trm {}\n", path)
		if !std.remove(path)
			std.put("\t\tno such file {}\n", file)
		;;
	else
		std.remove(path)
		match std.slurp(file)
		| `std.Err m:	std.fatal("could not open {} for reading\n", file)
		| `std.Ok buf:
			if std.blat(path, buf, perm)
				std.put("\t{} => {}\n", file, path)
			else
				std.put("could not write {}\n", file)
			;;
		;;
	;;
	std.slfree(path)
}

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

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

	manrel = std.fmt("{}{}", opt_manpath, man[sect:])
	movefile(b, rm, dir, man, manrel, 0o644)
	std.slfree(manrel)
}