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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
|
// Package rosa provides Rosa OS toolchain artifacts and miscellaneous software.
package rosa
import (
"embed"
"io/fs"
"os"
"slices"
"strconv"
"strings"
"sync"
"time"
"unsafe"
"hakurei.app/fhs"
"hakurei.app/internal/pkg"
)
// Extension is the variant identification string of custom artifact
// implementations registered by package rosa.
const Extension = "rosa"
func init() { pkg.SetExtension(Extension) }
const (
// kindEtc is the kind of [pkg.Artifact] of cureEtc.
kindEtc = iota + pkg.KindCustomOffset
// kindGentooOverlay is the kind of [pkg.Artifact] of gentooOverlay.
kindGentooOverlay
)
// 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 == "" {
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 arch.
func (s *S) linuxArch() string {
switch s.arch {
case "amd64":
return "x86_64"
case "arm64":
return "aarch64"
case "riscv64":
return "riscv64"
default:
panic("unsupported target " + s.arch)
}
}
// triple returns the Rosa OS host triple corresponding to arch.
func (s *S) triple() string {
return s.linuxArch() + "-rosa-linux-musl"
}
const (
// EnvTriple holds the Rosa OS host triple.
EnvTriple = "ROSA_TRIPLE"
)
// earlyLDFLAGS returns LDFLAGS corresponding to triplet.
func (s *S) earlyLDFLAGS(static bool) string {
p := "-fuse-ld=lld " +
"-L/system/lib -Wl,-rpath=/system/lib " +
"-L/system/lib/" + s.triple() + " " +
"-Wl,-rpath=/system/lib/" + s.triple() + " " +
"-rtlib=compiler-rt " +
"-unwindlib=libunwind " +
"-Wl,--as-needed"
if !static {
p += " -Wl,--dynamic-linker=/system/bin/linker"
}
return p
}
const (
// stageGentoo denotes the toolchain in a Gentoo stage3 tarball. Special
// care must be taken to compile correctly against this stage.
stageGentoo Stage = iota
// stageIntermediateGentoo is like stageIntermediate, but compiled against
// stageGentoo.
stageIntermediateGentoo
// stageStdGentoo is like Std, but bootstrapped from stageGentoo. This
// toolchain creates the first stage0 distribution.
stageStdGentoo
// stageEarly denotes the stage0 toolchain. Special care must be taken
// to compile correctly against this toolchain.
stageEarly
// stageIntermediate denotes the intermediate toolchain compiled against
// stageEarly. This toolchain should be functionally identical to [Std]
// and is used to bootstrap [Std].
stageIntermediate
// Std denotes the standard Rosa OS toolchain.
Std
// Stage3 denotes the stage3 Rosa OS toolchain built on [Std]. Software
// built on this toolchain should be identical to [Std]. This is generally
// for validation of LLVM 3-stage nondeterminism only, due to dynamic
// linking complications.
Stage3
// _stageEnd is the total number of stages available and does not denote a
// valid toolchain.
_stageEnd
)
// isStage0 returns whether t is a stage0 toolchain.
func (t Stage) isStage0() bool {
switch t {
case stageGentoo, stageEarly:
return true
default:
return false
}
}
// isIntermediate returns whether t is an intermediate toolchain.
func (t Stage) isIntermediate() bool {
switch t {
case stageIntermediateGentoo, stageIntermediate:
return true
default:
return false
}
}
// isStd returns whether t is considered functionally equivalent to [Std].
func (t Stage) isStd() bool {
switch t {
case stageStdGentoo, Std, Stage3:
return true
default:
return false
}
}
// 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...)
}
// scriptName is the name of the fixed-up build script.
const scriptName = "all"
// absCureScript is the absolute pathname [Toolchain.New] places the fixed-up
// build script under.
var absCureScript = AbsSystem.Append(scriptName)
var (
_stage0Dist = H("stage0-dist")
_mksh = H("mksh")
_toybox = H("toybox")
_toyboxEarly = H("toybox-early")
_llvm = H("llvm")
_patch = H("patch")
)
// HasStageEarly returns whether a stage0 distribution is available.
func (s *S) HasStageEarly() (ok bool) {
func() {
defer func() { ok = recover() == nil }()
s.New(stageEarly).MustLoad(_stage0Dist)
}()
return
}
// 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 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(
fhs.AbsRoot.Append("patches", p[0]), false,
pkg.NewFile(p[0], unsafe.Slice(unsafe.StringData(p[1]), len(p[1]))),
)
}
paths[0] = pkg.Path(fhs.AbsRoot.Append("src"), false, source)
var buf strings.Builder
buf.WriteString(`
cp -r /src/. /work/.
chmod -R +w /work && cd /work
`)
if len(paths) > 1 {
buf.WriteString(`
function apply {
echo "applying $1"
patch -p 1 < "/patches/$1"
}
`)
for _, p := range patches {
buf.WriteString("apply '")
buf.WriteString(p[0])
buf.WriteString("'\n")
}
}
return t.New(name+"-src", Unversioned, nil, &PackageAttr{
Paths: paths,
}, &GenericHelper{
Build: buf.String(),
},
_patch,
)
}
// 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 {
// extra returns helper-specific dependencies.
extra() 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
// 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() (pathname string, create bool)
// script returns the helper-specific segment of cure script.
script(t Toolchain, name string) string
}
// PackageAttr holds build-system-agnostic attributes.
type PackageAttr struct {
// Measure output if populated. Required by [THostNet].
KnownChecksum *pkg.Checksum
// 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
// Programs to make available in /bin/.
Bin []string
// Whether to replace /usr/bin/ with a symlink to /bin/.
PopulateUsrBin bool
// Whether this package is disallowed from bootstrap stages.
Std bool
// Hint for an early variant of toybox to be used when available.
Early bool
// Whether to omit output colour environment variables.
NoColor bool
// Exclude the LLVM toolchain.
NoToolchain bool
// Whether the resulting [pkg.Artifact] is exclusive.
Exclusive bool
// Whether to create [pkg.KindExecNet] instead of [pkg.KindExec].
HostNet bool
// Unregistered extras.
Extra []pkg.Artifact
// Passed through to [Toolchain.New], before source.
Paths []pkg.ExecPath
}
// pa holds whether an [ArtifactH] is present.
type pa = map[ArtifactH]struct{}
// paPool holds addresses of pa.
var paPool = sync.Pool{New: func() any { return make(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) { clear(pv); paPool.Put(pv) }
// appendHandle recursively appends an [Artifact] named by its handle, and its
// runtime dependencies.
func (t Toolchain) appendHandle(a []pkg.Artifact, pv pa, p ArtifactH) []pkg.Artifact {
if _, ok := pv[p]; ok {
return a
}
pv[p] = struct{}{}
meta, u := t.MustLoad(p)
for _, d := range meta.Dependencies {
a = t.appendHandle(a, pv, d)
}
return append(a, u)
}
// Append recursively appends multiple [Artifact] named by their handles, and
// their runtime dependencies.
func (t Toolchain) Append(a []pkg.Artifact, handles ...ArtifactH) []pkg.Artifact {
pv := paGet()
for _, p := range handles {
a = t.appendHandle(a, pv, p)
}
paPut(pv)
return a
}
// A BootstrapError describes a package disallowed from bootstrap stages.
type BootstrapError string
func (e BootstrapError) Error() string {
return "package " + strconv.Quote(string(e)) +
" must not be built by bootstrap stages"
}
// New constructs a [pkg.Artifact] via a build system helper.
func (t Toolchain) New(
name, version string,
source pkg.Artifact,
attr *PackageAttr,
helper Helper,
extra ...ArtifactH,
) pkg.Artifact {
if attr == nil {
attr = new(PackageAttr)
}
if attr.Std && !t.stage.isStd() {
panic(BootstrapError(name))
}
if name == "" || version == "" {
panic("name must be non-empty")
}
rn := name
if version != Unversioned {
rn = name + "-" + version
}
root := make([]pkg.Artifact, 0, 1<<3+len(attr.Extra)+len(extra))
root = append(root, attr.Extra...)
const lcMessages = "LC_MESSAGES=C.UTF-8"
srn := rn
env := slices.Clone(attr.Env)
if !attr.NoColor {
env = append(env,
"CLICOLOR_FORCE=1",
"FORCE_COLOR=1",
)
}
var extraBoot []ArtifactH
switch t.stage {
case stageGentoo, stageEarly:
srn += "-boot"
root = append(root, NewEtc(true))
if t.stage == stageEarly {
extraBoot = append(extraBoot, _stage0Dist)
} else if t.gentooStage3 == nil {
panic(os.ErrInvalid)
} else {
env = append(env,
"CC=clang",
"CXX=clang++",
)
}
env = fixupEnviron(env, []string{
EnvTriple + "=" + t.triple(),
lcMessages,
"LDFLAGS=" + t.earlyLDFLAGS(true),
}, "/system/bin",
"/usr/bin",
)
case stageIntermediateGentoo, stageStdGentoo,
stageIntermediate, Std, Stage3:
if t.stage.isIntermediate() {
srn += "-std"
}
if t.stage == Stage3 {
srn += "-stage4"
}
toybox := _toybox
if attr.Early {
toybox = _toyboxEarly
}
base := _llvm
if attr.NoToolchain {
base = _musl
}
root = append(root, NewEtc(false))
extraBoot = append(extraBoot,
base,
_mksh,
toybox,
)
env = fixupEnviron(env, []string{
EnvTriple + "=" + t.triple(),
lcMessages,
}, "/system/bin", "/bin")
default:
panic("unsupported toolchain " + strconv.Itoa(int(t.stage)))
}
wantsChmod, wantsWrite := helper.wantsChmod(), helper.wantsWrite()
{
pv := paGet()
for _, p := range helper.extra() {
root = t.appendHandle(root, pv, p)
}
for _, p := range extra {
root = t.appendHandle(root, pv, p)
}
boot := t.S.New(t.stage - 1)
for _, p := range extraBoot {
root = boot.appendHandle(root, pv, p)
}
paPut(pv)
}
if t.stage == stageGentoo {
root = append(root,
t.gentooStage3,
gentooOverlay{t.gentooStage3},
)
}
var scriptEarly string
dir, create := 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 create {
scriptEarly += "\nmkdir -p " + dir
}
scriptEarly += "\ncd " + dir + "\n"
} else if !attr.EnterSource && attr.ScriptEarly == "" && dir != "" {
panic("cannot remain in root")
}
paths := attr.Paths
if source != nil {
paths = slices.Concat(attr.Paths, []pkg.ExecPath{
pkg.Path(AbsUsrSrc.Append(
name,
), attr.Writable || wantsWrite, t.NewPatchedSource(
rn, source, !attr.Chmod && !wantsChmod, attr.Patches...,
)),
})
}
bin := attr.Bin
if attr.PopulateUsrBin {
scriptEarly += `
chmod +w /usr/ /usr/bin/
rm -rf /usr/bin/
ln -s ../bin /usr/`
bin = append(bin, "env")
}
if t.stage != stageGentoo && len(bin) > 0 {
scriptEarly += "\nchmod +w /bin/" +
"\n(set -o braceexpand && ln -sf ../system/bin/"
if len(bin) > 1 {
scriptEarly += "{'" + strings.Join(bin, "','") + "'}"
} else {
scriptEarly += "'" + bin[0] + "'"
}
scriptEarly += " /bin/)\n"
}
return pkg.NewExec(
srn, t.arch, attr.KnownChecksum, pkg.ExecTimeoutMax,
attr.HostNet,
attr.Exclusive,
fhs.AbsRoot, env,
absCureScript,
nil,
slices.Concat([]pkg.ExecPath{pkg.Path(
fhs.AbsRoot, true,
root...,
), pkg.Path(
absCureScript, false,
pkg.NewFile(scriptName, []byte(
"#!/system/bin/sh\n"+
"set -eu -o pipefail\n"+
scriptEarly+helper.script(t, name),
)),
)}, paths)...,
)
}
// GenericHelper directly passes build script segments for invocations of
// one-off build scripts too specific to fit into any other [Helper]
// implementation, but can still benefit from [Toolchain.NewPackage].
type GenericHelper struct {
// Remain in current directory.
InPlace bool
// Concatenated for [Toolchain].
Build, Check, Install string
}
var _ Helper = new(GenericHelper)
// extra is a noop.
func (*GenericHelper) extra() P { return nil }
// wantsChmod returns false.
func (*GenericHelper) wantsChmod() bool { return false }
// wantsWrite returns false.
func (*GenericHelper) wantsWrite() bool { return false }
// scriptEarly returns the zero value.
func (*GenericHelper) scriptEarly() string { return "" }
// wantsDir requests a new directory, or omits the cd statement if InPlace.
func (attr *GenericHelper) wantsDir() (string, bool) {
if attr == nil || !attr.InPlace {
return "/cure/", true
}
return helperInPlace, false
}
// script concatenates specified segments.
func (attr *GenericHelper) script(t Toolchain, _ string) string {
if attr == nil {
attr = new(GenericHelper)
}
script := attr.Build
if t.opts&OptSkipCheck == 0 {
script += attr.Check
}
script += attr.Install
return script
}
// native contains natively-implemented and built-in azalea-based [Artifact].
// It is generally recommended to clone this instance for custom [Artifact]
// registrations.
var native S
// Native returns the global [S].
func Native() *S { return &native }
// parseTime is the duration of early parsing of built-in azalea expressions.
var parseTime time.Duration
// ParseTime returns the time taken by early parsing of built-in azalea expressions.
func ParseTime() time.Duration { return parseTime }
// nativeB is the backing directory of built-in azalea-based [Artifact]
// implementations.
//
//go:embed package
var nativeB embed.FS
func init() {
sub, err := fs.Sub(nativeB, "package")
if err != nil {
panic(err)
}
t := time.Now()
if err = native.RegisterFS(sub); err != nil {
println(err.Error())
os.Exit(1)
}
parseTime = time.Since(t)
}
|