aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/rosa/rosa.go
blob: 560f10d58101211a3e4c5ab4338002e8cc3c05e0 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
// Package rosa provides Rosa OS toolchain artifacts and miscellaneous software.
package rosa

import (
	"errors"
	"log"
	"runtime"
	"slices"
	"strconv"
	"strings"
	"sync"

	"hakurei.app/fhs"
	"hakurei.app/internal/pkg"
)

const (
	// kindEtc is the kind of [pkg.Artifact] of cureEtc.
	kindEtc = iota + pkg.KindCustomOffset

	// kindBusyboxBin is the kind of [pkg.Artifact] of busyboxBin.
	kindBusyboxBin
)

// mustDecode is like [pkg.MustDecode], but replaces the zero value and prints
// a warning.
func mustDecode(s string) pkg.Checksum {
	var fallback = pkg.Checksum{}
	if s == "" {
		log.Println(
			"falling back to",
			pkg.Encode(fallback),
			"for unpopulated checksum",
		)
		return fallback
	}
	return pkg.MustDecode(s)
}

// KV is a key-value pair of strings.
type KV [2]string

var (
	// AbsUsrSrc is the conventional directory to place source code under.
	AbsUsrSrc = fhs.AbsUsr.Append("src")

	// AbsSystem is the Rosa OS installation prefix.
	AbsSystem = fhs.AbsRoot.Append("system")
)

// linuxArch returns the architecture name used by linux corresponding to
// [runtime.GOARCH].
func linuxArch() string {
	switch runtime.GOARCH {
	case "amd64":
		return "x86_64"
	case "arm64":
		return "aarch64"
	case "riscv64":
		return "riscv64"

	default:
		panic("unsupported target " + runtime.GOARCH)
	}
}

// triplet returns the Rosa OS host triple corresponding to [runtime.GOARCH].
func triplet() string {
	return linuxArch() + "-rosa-linux-musl"
}

const (
	// EnvTriplet holds the return value of triplet.
	EnvTriplet = "ROSA_TRIPLE"
)

// earlyLDFLAGS returns LDFLAGS corresponding to triplet.
func earlyLDFLAGS(static bool) string {
	s := "-fuse-ld=lld " +
		"-L/system/lib -Wl,-rpath=/system/lib " +
		"-L/system/lib/" + triplet() + " " +
		"-Wl,-rpath=/system/lib/" + triplet() + " " +
		"-rtlib=compiler-rt " +
		"-unwindlib=libunwind " +
		"-Wl,--as-needed"
	if !static {
		s += " -Wl,--dynamic-linker=/system/bin/linker"
	}
	return s
}

// earlyCFLAGS is reference CFLAGS for the stage0 toolchain.
const earlyCFLAGS = "-Qunused-arguments " +
	"-isystem/system/include"

// earlyCXXFLAGS returns reference CXXFLAGS for the stage0 toolchain
// corresponding to [runtime.GOARCH].
func earlyCXXFLAGS() string {
	return "--start-no-unused-arguments " +
		"-stdlib=libc++ " +
		"--end-no-unused-arguments " +
		"-isystem/system/include/c++/v1 " +
		"-isystem/system/include/" + triplet() + "/c++/v1 " +
		"-isystem/system/include "
}

// Toolchain denotes the infrastructure to compile a [pkg.Artifact] on.
type Toolchain uint32

const (
	// _toolchainBusybox denotes a busybox installation from the busyboxBin
	// binary distribution. This is defined as a toolchain to make use of the
	// toolchain abstractions to preprocess toolchainGentoo and is not a real,
	// functioning toolchain. It does not contain any compilers.
	_toolchainBusybox Toolchain = iota

	// toolchainGentoo denotes the toolchain in a Gentoo stage3 tarball. Special
	// care must be taken to compile correctly against this toolchain.
	toolchainGentoo

	// toolchainIntermediateGentoo is like to toolchainIntermediate, but
	// compiled against toolchainGentoo.
	toolchainIntermediateGentoo

	// toolchainStdGentoo is like Std, but bootstrapped from toolchainGentoo.
	// This toolchain creates the first [Stage0] distribution.
	toolchainStdGentoo

	// toolchainStage0 denotes the stage0 toolchain. Special care must be taken
	// to compile correctly against this toolchain.
	toolchainStage0

	// toolchainIntermediate denotes the intermediate toolchain compiled against
	// toolchainStage0. This toolchain should be functionally identical to [Std]
	// and is used to bootstrap [Std].
	toolchainIntermediate

	// Std denotes the standard Rosa OS toolchain.
	Std

	// _toolchainEnd is the total number of toolchains available and does not
	// denote a valid toolchain.
	_toolchainEnd
)

// isStage0 returns whether t is a stage0 toolchain.
func (t Toolchain) isStage0() bool {
	switch t {
	case toolchainGentoo, toolchainStage0:
		return true
	default:
		return false
	}
}

// isIntermediate returns whether t is an intermediate toolchain.
func (t Toolchain) isIntermediate() bool {
	switch t {
	case toolchainIntermediateGentoo, toolchainIntermediate:
		return true
	default:
		return false
	}
}

// isStd returns whether t is considered functionally equivalent to [Std].
func (t Toolchain) isStd() bool {
	switch t {
	case toolchainStdGentoo, Std:
		return true
	default:
		return false
	}
}

// stage0Concat concatenates s and values. If the current toolchain is
// toolchainStage0, stage0Concat returns s as is.
func stage0Concat[S ~[]E, E any](t Toolchain, s S, values ...E) S {
	if t.isStage0() {
		return s
	}
	return slices.Concat(s, values)
}

// stage0ExclConcat concatenates s and values. If the current toolchain is not
// toolchainStage0, stage0ExclConcat returns s as is.
func stage0ExclConcat[S ~[]E, E any](t Toolchain, s S, values ...E) S {
	if t.isStage0() {
		return slices.Concat(s, values)
	}
	return s
}

// lastIndexFunc is like [strings.LastIndexFunc] but for [slices].
func lastIndexFunc[S ~[]E, E any](s S, f func(E) bool) (i int) {
	if i = slices.IndexFunc(s, f); i < 0 {
		return
	}
	if i0 := lastIndexFunc[S](s[i+1:], f); i0 >= 0 {
		i = i0
	}
	return
}

// fixupEnviron fixes up PATH, prepends extras and returns the resulting slice.
func fixupEnviron(env, extras []string, paths ...string) []string {
	// some python tools try to be clever and buffers their output, making the
	// build process appear to hang
	env = append(env, "PYTHONUNBUFFERED=1")

	const pathPrefix = "PATH="
	pathVal := strings.Join(paths, ":")

	if i := lastIndexFunc(env, func(s string) bool {
		return strings.HasPrefix(s, pathPrefix)
	}); i < 0 {
		env = append(env, pathPrefix+pathVal)
	} else {
		if len(env[i]) == len(pathPrefix) {
			env[i] = pathPrefix + pathVal
		} else {
			env[i] += ":" + pathVal
		}
	}

	return append(extras, env...)
}

// absCureScript is the absolute pathname [Toolchain.New] places the fixed-up
// build script under.
var absCureScript = AbsSystem.Append(".cure-script")

const (
	// TExclusive denotes an exclusive [pkg.Artifact].
	TExclusive = 1 << iota
	// TEarly hints for an early variant of [Toybox] to be used when available.
	TEarly
	// TNoToolchain excludes the LLVM toolchain.
	TNoToolchain
)

var (
	// gentooStage3 is the url of a Gentoo stage3 tarball.
	gentooStage3 string
	// gentooStage3Checksum is the expected checksum of gentooStage3.
	gentooStage3Checksum pkg.Checksum
)

// SetGentooStage3 sets the Gentoo stage3 tarball url and checksum. It panics
// if given zero values or if these values have already been set.
func SetGentooStage3(url string, checksum pkg.Checksum) {
	if gentooStage3 != "" {
		panic(errors.New("attempting to set Gentoo stage3 url twice"))
	}
	if url == "" {
		panic(errors.New("attempting to set Gentoo stage3 url to the zero value"))
	}
	gentooStage3, gentooStage3Checksum = url, checksum
}

// New returns a [pkg.Artifact] compiled on this toolchain.
func (t Toolchain) New(
	name string,
	flag int,
	extra []pkg.Artifact,
	knownChecksum *pkg.Checksum,
	env []string,
	script string,

	paths ...pkg.ExecPath,
) pkg.Artifact {
	const lcMessages = "LC_MESSAGES=C.UTF-8"

	var support []pkg.Artifact
	switch t {
	case _toolchainBusybox:
		name += "-early"
		support = slices.Concat([]pkg.Artifact{newBusyboxBin()}, extra)
		env = fixupEnviron(env, nil, "/system/bin")

	case toolchainGentoo, toolchainStage0:
		name += "-boot"
		support = append(support, cureEtc{})
		if t == toolchainStage0 {
			support = append(support, NewStage0())
		} else {
			support = append(support, _toolchainBusybox.New("gentoo", 0, nil, nil, nil, `
tar -C /work -xf /usr/src/stage3.tar.xz
rm -rf /work/dev/ /work/proc/
ln -vs ../usr/bin /work/bin
mkdir -vp /work/system/bin
(cd /work/system/bin && ln -vs \
	../../bin/sh \
	../../usr/lib/llvm/*/bin/* \
	.)
`, pkg.Path(AbsUsrSrc.Append("stage3.tar.xz"), false,
				pkg.NewHTTPGet(
					nil, gentooStage3,
					gentooStage3Checksum,
				),
			)))
		}
		support = slices.Concat(support, extra)
		env = fixupEnviron(env, []string{
			EnvTriplet + "=" + triplet(),
			lcMessages,
			"LDFLAGS=" + earlyLDFLAGS(true),
		}, "/system/bin",
			"/usr/bin",
		)

	case toolchainIntermediateGentoo, toolchainStdGentoo,
		toolchainIntermediate, Std:
		if t.isIntermediate() {
			name += "-std"
		}

		boot := t - 1
		musl, compilerRT, runtimes, clang := boot.NewLLVM()
		toybox := Toybox
		if flag&TEarly != 0 {
			toybox = toyboxEarly
		}
		std := []pkg.Artifact{cureEtc{newIANAEtc()}, musl}
		toolchain := []pkg.Artifact{compilerRT, runtimes, clang}
		utils := []pkg.Artifact{
			boot.Load(Mksh),
			boot.Load(toybox),
		}

		if flag&TNoToolchain != 0 {
			toolchain = nil
		}

		support = slices.Concat(extra, std, toolchain, utils)
		env = fixupEnviron(env, []string{
			EnvTriplet + "=" + triplet(),
			lcMessages,

			"AR=ar",
			"RANLIB=ranlib",
			"LIBCC=/system/lib/clang/" + llvmVersionMajor + "/lib/" + triplet() +
				"/libclang_rt.builtins.a",
		}, "/system/bin", "/bin")

	default:
		panic("unsupported toolchain " + strconv.Itoa(int(t)))
	}

	return pkg.NewExec(
		name, knownChecksum, pkg.ExecTimeoutMax, flag&TExclusive != 0,
		fhs.AbsRoot, env,
		AbsSystem.Append("bin", "sh"),
		[]string{"sh", absCureScript.String()},

		slices.Concat([]pkg.ExecPath{pkg.Path(
			fhs.AbsRoot, true,
			support...,
		), pkg.Path(
			absCureScript, false,
			pkg.NewFile(".cure-script", []byte("set -eu -o pipefail\n"+script)),
		)}, paths)...,
	)
}

// NewPatchedSource returns [pkg.Artifact] of source with patches applied. If
// passthrough is true, source is returned as is for zero length patches.
func (t Toolchain) NewPatchedSource(
	name, version string,
	source pkg.Artifact,
	passthrough bool,
	patches ...KV,
) pkg.Artifact {
	if passthrough && len(patches) == 0 {
		return source
	}

	paths := make([]pkg.ExecPath, len(patches)+1)
	for i, p := range patches {
		paths[i+1] = pkg.Path(
			AbsUsrSrc.Append(name+"-patches", p[0]+".patch"), false,
			pkg.NewFile(p[0]+".patch", []byte(p[1])),
		)
	}
	paths[0] = pkg.Path(AbsUsrSrc.Append(name), false, source)

	aname := name + "-" + version + "-src"
	script := `
cp -r /usr/src/` + name + `/. /work/.
chmod -R +w /work && cd /work
`
	if len(paths) > 1 {
		script += `
cat /usr/src/` + name + `-patches/* | \
	patch \
	-p 1 \
	--ignore-whitespace
`
		aname += "-patched"
	}
	return t.New(aname, 0, stage0Concat(t, []pkg.Artifact{},
		t.Load(Patch),
	), nil, nil, script, paths...)
}

// helperInPlace is a special directory value for omitting the cd statement.
const helperInPlace = "\x00"

// Helper is a build system helper for [Toolchain.NewPackage].
type Helper interface {
	// name returns the value passed to the name argument of [Toolchain.New].
	name(name, version string) string
	// extra returns helper-specific dependencies.
	extra(flag int) P

	// wantsChmod returns whether the source directory should be made writable.
	wantsChmod() bool
	// wantsWrite returns whether the source directory should be mounted writable.
	wantsWrite() bool
	// scriptEarly returns the helper-specific segment of cure script that goes
	// before the cd statement.
	scriptEarly() string
	// createDir returns whether the path returned by wantsDir should be created.
	createDir() bool
	// wantsDir returns the directory to enter before script.
	//
	// The zero value implies source directory if [PackageAttr.ScriptEarly] is
	// also empty. The special value helperInPlace omits the cd statement.
	wantsDir() string
	// script returns the helper-specific segment of cure script.
	script(name string) string
}

const (
	// SourceKindTarXZ denotes a source tarball to be decompressed using [XZ].
	SourceKindTarXZ = 1 + iota
)

// PackageAttr holds build-system-agnostic attributes.
type PackageAttr struct {
	// Mount the source tree writable.
	Writable bool
	// Do not pass through [Toolchain.NewPatchedSource].
	Chmod bool
	// Unconditionally enter source directory early.
	EnterSource bool

	// Additional environment variables.
	Env []string
	// Runs before script emitted by [Helper]. Enters source if non-empty.
	ScriptEarly string

	// Passed to [Toolchain.NewPatchedSource].
	Patches []KV
	// Kind of source artifact.
	SourceKind int

	// Dependencies not provided by stage0.
	NonStage0 []pkg.Artifact

	// Passed through to [Toolchain.New], before source.
	Paths []pkg.ExecPath
	// Passed through to [Toolchain.New].
	Flag int
}

// pa holds whether a [PArtifact] is present.
type pa = [PresetEnd]bool

// paPool holds addresses of pa.
var paPool = sync.Pool{New: func() any { return new(pa) }}

// paGet returns the address of a new pa.
func paGet() *pa { return paPool.Get().(*pa) }

// paPut returns a pa to paPool.
func paPut(pv *pa) { *pv = pa{}; paPool.Put(pv) }

// appendPreset recursively appends a [PArtifact] and its runtime dependencies.
func (t Toolchain) appendPreset(
	a []pkg.Artifact,
	pv *pa, p PArtifact,
) []pkg.Artifact {
	if pv[p] {
		return a
	}
	pv[p] = true

	for _, d := range GetMetadata(p).Dependencies {
		a = t.appendPreset(a, pv, d)
	}
	return append(a, t.Load(p))
}

// AppendPresets recursively appends multiple [PArtifact] and their runtime
// dependencies.
func (t Toolchain) AppendPresets(
	a []pkg.Artifact,
	presets ...PArtifact,
) []pkg.Artifact {
	pv := paGet()
	for _, p := range presets {
		a = t.appendPreset(a, pv, p)
	}
	paPut(pv)
	return a
}

// NewPackage constructs a [pkg.Artifact] via a build system helper.
func (t Toolchain) NewPackage(
	name, version string,
	source pkg.Artifact,
	attr *PackageAttr,
	helper Helper,
	extra ...PArtifact,
) pkg.Artifact {
	if attr == nil {
		attr = new(PackageAttr)
	}

	if name == "" || version == "" {
		panic("name must be non-empty")
	}
	if source == nil {
		panic("source must be non-nil")
	}
	wantsChmod, wantsWrite := helper.wantsChmod(), helper.wantsWrite()
	if attr.SourceKind > 0 &&
		(attr.Writable || attr.Chmod || wantsChmod || wantsWrite || len(attr.Patches) > 0) {
		panic("source processing requested on a non-unpacked kind")
	}

	dc := len(attr.NonStage0)
	if !t.isStage0() {
		dc += 1<<3 + len(extra)
	}

	extraRes := make([]pkg.Artifact, 0, dc)
	extraRes = append(extraRes, attr.NonStage0...)
	if !t.isStage0() {
		pv := paGet()
		for _, p := range helper.extra(attr.Flag) {
			extraRes = t.appendPreset(extraRes, pv, p)
		}
		for _, p := range extra {
			extraRes = t.appendPreset(extraRes, pv, p)
		}
		paPut(pv)
	}

	var scriptEarly string

	var sourceSuffix string
	switch attr.SourceKind {
	case SourceKindTarXZ:
		sourceSuffix = ".tar.xz"
		scriptEarly += `
tar -C /usr/src/ -xf '/usr/src/` + name + `.tar.xz'
mv '/usr/src/` + name + `-` + version + `' '/usr/src/` + name + `'
`
		break
	}

	dir := helper.wantsDir()
	helperScriptEarly := helper.scriptEarly()
	if attr.EnterSource ||
		dir == "" ||
		attr.ScriptEarly != "" ||
		helperScriptEarly != "" {
		scriptEarly += `
cd '/usr/src/` + name + `/'
`
	}
	scriptEarly += attr.ScriptEarly + helperScriptEarly
	if dir != "" && dir != helperInPlace {
		if helper.createDir() {
			scriptEarly += "\nmkdir -p " + dir
		}
		scriptEarly += "\ncd " + dir + "\n"
	} else if !attr.EnterSource && attr.ScriptEarly == "" {
		panic("cannot remain in root")
	}

	return t.New(
		helper.name(name, version),
		attr.Flag,
		extraRes,
		nil,
		attr.Env,
		scriptEarly+helper.script(name),
		slices.Concat(attr.Paths, []pkg.ExecPath{
			pkg.Path(AbsUsrSrc.Append(
				name+sourceSuffix,
			), attr.Writable || wantsWrite, t.NewPatchedSource(
				name, version, source, !attr.Chmod && !wantsChmod, attr.Patches...,
			)),
		})...,
	)
}