aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/rosa/meson.go
blob: 59029595e69dcf887dd815b2644494255b17be35 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package rosa

import (
	"slices"
	"strings"

	"hakurei.app/pkg"
)

var (
	_muon  = H("muon")
	_meson = H("meson")
)

// MesonHelper builds and tests a meson project.
type MesonHelper struct {
	// Use muon instead of meson.
	Muon bool

	// Runs after setup.
	ScriptCompileEarly string
	// Runs after compile.
	ScriptCompiled string
	// Runs after install.
	Script string

	// Flags passed to the setup command.
	Setup []KV
	// Test suites to skip.
	SkipTests []string
	// Whether to skip meson test.
	SkipTest bool
	// Run tests with interactive input/output.
	InteractiveTest bool
}

var _ Helper = new(MesonHelper)

// tools returns hardcoded meson runtime dependencies.
func (attr *MesonHelper) tools(t Toolchain) P {
	m := _muon
	if attr.isPython() {
		m = _meson
	}
	if t.cross() {
		return P{m, _qemu}
	}
	return P{m}
}

// paths returns the cross-compiler mfile, or nil if not cross-compiling.
func (*MesonHelper) paths(t Toolchain) []pkg.ExecPath {
	if !t.cross() {
		return nil
	}

	arch, mesonArch, target := t.Arch(), t.linuxArch(), t.triple()
	return []pkg.ExecPath{pkg.Path(
		AbsSystem.Append("mfile"),
		false, pkg.NewFile("mfile-"+arch, []byte(`# meson env2mfile
[binaries]
# Compilers
c = ['clang', '--target=`+target+`', '--sysroot=/system/`+arch+`']
cpp = ['clang++', '--target=`+target+`', '--sysroot=/system/`+arch+`']
pkg-config = ['pkg-config']
exe_wrapper = ['qemu-`+mesonArch+`', '-L', '/system/`+arch+`']

# Other binaries
ar = ['llvm-ar']
ranlib = ['llvm-ranlib']
strip = ['llvm-strip']

[built-in options]

[properties]
sys_root = '/system/`+arch+`'
pkg_config_libdir = '/system/`+arch+`/system/lib/pkgconfig'

[host_machine]
cpu = '`+mesonArch+`'
cpu_family = '`+mesonArch+`'
endian = 'little'
system = 'linux'
subsystem = 'linux'
kernel = 'linux'
`)),
	)}
}

// inputs is a noop.
func (*MesonHelper) inputs() P { return nil }

// wantsChmod returns false.
func (*MesonHelper) wantsChmod() bool { return false }

// wantsWrite returns false.
func (*MesonHelper) wantsWrite() bool { return false }

// scriptEarly returns the zero value.
func (*MesonHelper) scriptEarly() string { return "" }

// wantsDir requests a new directory in TMPDIR.
func (*MesonHelper) wantsDir() (string, bool) { return `"$(mktemp -d)"`, false }

// isPython returns whether the legacy python implementation is used.
func (attr *MesonHelper) isPython() bool {
	return attr == nil ||
		!attr.Muon || len(attr.SkipTests) != 0 || attr.InteractiveTest
}

// script generates the cure script.
func (attr *MesonHelper) script(t Toolchain, name string) string {
	if attr == nil {
		attr = new(MesonHelper)
	}

	scriptCompiled := attr.ScriptCompiled
	if len(scriptCompiled) > 0 && scriptCompiled[0] != '\n' {
		scriptCompiled = "\n" + scriptCompiled
	}

	python := attr.isPython()
	muon := "muon"
	compile := " samu " + jobsFlagE
	destdir := "-d"
	if python {
		muon = "meson"
		compile = " compile " + jobsFlagE + " " + loadFlagE
		destdir = "--destdir"
	}

	var scriptTest string
	if !attr.SkipTest && t.opts&OptSkipCheck == 0 {
		scriptTest = `
` + muon + ` test \
	` + jobsFlagE + ` \
	-t 10`
		if python {
			scriptTest += ` \
	--print-errorlogs`
		} else {
			scriptTest += " -Rv"
		}
		if attr.InteractiveTest {
			scriptTest += ` \
	--interactive`
		}
		for _, suite := range attr.SkipTests {
			scriptTest += " \\\n\t--no-suite='" + suite + "'"
		}
	}

	var setupArgs string
	if t.cross() {
		setupArgs += " --cross-file=/system/mfile"
	}

	return `
` + muon + ` setup` + setupArgs + ` \
	` + strings.Join(slices.Collect(func(yield func(string) bool) {
		for _, v := range append([]KV{
			{"wrap_mode", "nofallback"},
			{"prefix", "/system"},
			{"buildtype", "release"},
		}, attr.Setup...) {
			s := v[0]
			if v[1] != "" {
				s = "-D" + s + "=" + v[1]
			}
			if !yield(s) {
				return
			}
		}
	}), " \\\n\t") + ` \
	'/usr/src/` + name + `'
` + muon + compile + scriptCompiled + scriptTest + `
` + muon + ` install \
	` + destdir + ` /work
` + attr.Script
}