internal/rosa: key metadata by string

For upcoming azalea integration. The API is quite ugly right now to ease migration.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2026-05-17 13:07:12 +09:00
parent c2ff9c9fa5
commit 30eb0d6a61
95 changed files with 1514 additions and 1567 deletions

View File

@@ -2,7 +2,6 @@
package rosa
import (
"errors"
"path"
"slices"
"strconv"
@@ -54,8 +53,8 @@ var (
)
// linuxArch returns the architecture name used by linux corresponding to arch.
func linuxArch() string {
switch arch {
func (s *S) linuxArch() string {
switch s.arch {
case "amd64":
return "x86_64"
case "arm64":
@@ -64,23 +63,23 @@ func linuxArch() string {
return "riscv64"
default:
panic("unsupported target " + arch)
panic("unsupported target " + s.arch)
}
}
// triplet returns the Rosa OS host triple corresponding to arch.
func triplet() string {
return linuxArch() + "-rosa-linux-musl"
// triple returns the Rosa OS host triple corresponding to arch.
func (s *S) triple() string {
return s.linuxArch() + "-rosa-linux-musl"
}
// perArch is a value that differs per architecture.
type perArch[T any] map[string]T
// unwrap returns the value for the current architecture.
func (p perArch[T]) unwrap() T {
v, ok := p[arch]
func (p perArch[T]) unwrap(s *S) T {
v, ok := p[s.arch]
if !ok {
panic("unsupported target " + arch)
panic("unsupported target " + s.arch)
}
return v
}
@@ -91,18 +90,18 @@ const (
)
// earlyLDFLAGS returns LDFLAGS corresponding to triplet.
func earlyLDFLAGS(static bool) string {
s := "-fuse-ld=lld " +
func (s *S) earlyLDFLAGS(static bool) string {
p := "-fuse-ld=lld " +
"-L/system/lib -Wl,-rpath=/system/lib " +
"-L/system/lib/" + triplet() + " " +
"-Wl,-rpath=/system/lib/" + triplet() + " " +
"-L/system/lib/" + s.triple() + " " +
"-Wl,-rpath=/system/lib/" + s.triple() + " " +
"-rtlib=compiler-rt " +
"-unwindlib=libunwind " +
"-Wl,--as-needed"
if !static {
s += " -Wl,--dynamic-linker=/system/bin/linker"
p += " -Wl,--dynamic-linker=/system/bin/linker"
}
return s
return p
}
// Toolchain denotes the infrastructure to compile a [pkg.Artifact] on.
@@ -224,27 +223,9 @@ const (
THostNet
)
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(
// New returns a [pkg.Artifact] based on a [Toolchain] via s.
func (s *S) New(
t Toolchain,
name string,
flag int,
extra []pkg.Artifact,
@@ -260,7 +241,7 @@ func (t Toolchain) New(
switch t {
case _toolchainBusybox:
name += "-early"
support = slices.Concat([]pkg.Artifact{newBusyboxBin()}, extra)
support = slices.Concat([]pkg.Artifact{s.newBusyboxBin()}, extra)
env = fixupEnviron(env, nil, "/system/bin")
case toolchainGentoo, toolchainStage0:
@@ -268,9 +249,10 @@ func (t Toolchain) New(
support = append(support, extra...)
support = append(support, cureEtc{})
if t == toolchainStage0 {
support = append(support, t.Load(stage0Dist))
a, _ := s.Load(t, stage0Dist)
support = append(support, a)
} else {
support = append(support, _toolchainBusybox.New("gentoo", 0, nil, nil, nil, `
support = append(support, s.New(_toolchainBusybox, "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
@@ -281,15 +263,15 @@ mkdir -vp /work/system/bin
.)
`, pkg.Path(AbsUsrSrc.Append("stage3.tar.xz"), false,
pkg.NewHTTPGet(
nil, gentooStage3,
gentooStage3Checksum,
nil, s.gentooStage3,
s.gentooStage3Checksum,
),
)))
}
env = fixupEnviron(env, []string{
EnvTriplet + "=" + triplet(),
EnvTriplet + "=" + s.triple(),
lcMessages,
"LDFLAGS=" + earlyLDFLAGS(true),
"LDFLAGS=" + s.earlyLDFLAGS(true),
}, "/system/bin",
"/usr/bin",
)
@@ -310,7 +292,7 @@ mkdir -vp /work/system/bin
base = Musl
}
support = slices.Concat(extra, (t-1).AppendPresets([]pkg.Artifact{
support = slices.Concat(extra, s.AppendPresets(t-1, []pkg.Artifact{
cureEtc{newIANAEtc()},
},
base,
@@ -318,7 +300,7 @@ mkdir -vp /work/system/bin
toybox,
))
env = fixupEnviron(env, []string{
EnvTriplet + "=" + triplet(),
EnvTriplet + "=" + s.triple(),
lcMessages,
}, "/system/bin", "/bin")
@@ -327,7 +309,7 @@ mkdir -vp /work/system/bin
}
return pkg.NewExec(
name, arch, knownChecksum, pkg.ExecTimeoutMax,
name, s.arch, knownChecksum, pkg.ExecTimeoutMax,
flag&THostNet != 0,
flag&TExclusive != 0,
fhs.AbsRoot, env,
@@ -346,7 +328,8 @@ mkdir -vp /work/system/bin
// 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(
func (s *S) NewPatchedSource(
t Toolchain,
name, version string,
source pkg.Artifact,
passthrough bool,
@@ -379,7 +362,7 @@ cat /usr/src/` + name + `-patches/* | \
`
aname += "-patched"
}
return t.New(aname, 0, t.AppendPresets(nil,
return s.New(t, aname, 0, s.AppendPresets(t, nil,
Patch,
), nil, nil, script, paths...)
}
@@ -407,7 +390,7 @@ type Helper interface {
// 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
script(s *S, name string) string
}
// PackageAttr holds build-system-agnostic attributes.
@@ -436,55 +419,59 @@ type PackageAttr struct {
Flag int
}
// pa holds whether a [PArtifact] is present.
type pa = [PresetEnd]bool
// 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 new(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) }
func paGet() pa { return paPool.Get().(pa) }
// paPut returns a pa to paPool.
func paPut(pv *pa) { *pv = pa{}; paPool.Put(pv) }
func paPut(pv pa) { clear(pv); paPool.Put(pv) }
// appendPreset recursively appends a [PArtifact] and its runtime dependencies.
func (t Toolchain) appendPreset(
func (s *S) appendPreset(
t Toolchain,
a []pkg.Artifact,
pv *pa, p PArtifact,
pv pa, p ArtifactH,
) []pkg.Artifact {
if pv[p] {
if _, ok := pv[p]; ok {
return a
}
pv[p] = true
pv[p] = struct{}{}
for _, d := range GetMetadata(p).Dependencies {
a = t.appendPreset(a, pv, d)
for _, d := range s.Get(p).Dependencies {
a = s.appendPreset(t, a, pv, d)
}
return append(a, t.Load(p))
d, _ := s.Load(t, p)
return append(a, d)
}
// AppendPresets recursively appends multiple [PArtifact] and their runtime
// dependencies.
func (t Toolchain) AppendPresets(
func (s *S) AppendPresets(
t Toolchain,
a []pkg.Artifact,
presets ...PArtifact,
presets ...ArtifactH,
) []pkg.Artifact {
pv := paGet()
for _, p := range presets {
a = t.appendPreset(a, pv, p)
a = s.appendPreset(t, a, pv, p)
}
paPut(pv)
return a
}
// NewPackage constructs a [pkg.Artifact] via a build system helper.
func (t Toolchain) NewPackage(
func (s *S) NewPackage(
t Toolchain,
name, version string,
source pkg.Artifact,
attr *PackageAttr,
helper Helper,
extra ...PArtifact,
extra ...ArtifactH,
) pkg.Artifact {
if attr == nil {
attr = new(PackageAttr)
@@ -501,10 +488,10 @@ func (t Toolchain) NewPackage(
{
pv := paGet()
for _, p := range helper.extra(attr.Flag) {
extraRes = t.appendPreset(extraRes, pv, p)
extraRes = s.appendPreset(t, extraRes, pv, p)
}
for _, p := range extra {
extraRes = t.appendPreset(extraRes, pv, p)
extraRes = s.appendPreset(t, extraRes, pv, p)
}
paPut(pv)
}
@@ -548,18 +535,19 @@ cd '/usr/src/` + name + `/'
panic("cannot remain in root")
}
return t.New(
return s.New(
t,
name+"-"+version,
attr.Flag,
extraRes,
attr.KnownChecksum,
attr.Env,
scriptEarly+helper.script(name),
scriptEarly+helper.script(s, 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...,
), attr.Writable || wantsWrite, s.NewPatchedSource(
t, name, version, source, !attr.Chmod && !wantsChmod, attr.Patches...,
)),
})...,
)
@@ -626,3 +614,8 @@ func newFromGitHubRelease(
compression,
)
}
var native S
// Native returns the global [S].
func Native() *S { return &native }