internal/rosa/cmake: migrate to helper interface
All checks were successful
Test / Create distribution (push) Successful in 1m5s
Test / Sandbox (push) Successful in 2m40s
Test / Hakurei (push) Successful in 3m51s
Test / ShareFS (push) Successful in 4m2s
Test / Hpkg (push) Successful in 4m36s
Test / Sandbox (race detector) (push) Successful in 5m2s
Test / Hakurei (race detector) (push) Successful in 6m0s
Test / Flake checks (push) Successful in 1m46s

This change also removes some unused options.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2026-03-01 23:50:22 +09:00
parent 2baa9df133
commit 9deaf853f0
3 changed files with 81 additions and 108 deletions

View File

@@ -1,10 +1,10 @@
package rosa
import (
"path"
"slices"
"strings"
"hakurei.app/container/check"
"hakurei.app/internal/pkg"
)
@@ -38,49 +38,54 @@ func (t Toolchain) newCMake() pkg.Artifact {
}
func init() { artifactsF[CMake] = Toolchain.newCMake }
// CMakeAttr holds the project-specific attributes that will be applied to a new
// [pkg.Artifact] compiled via [CMake].
type CMakeAttr struct {
// CMakeHelper is the [CMake] build system helper.
type CMakeHelper struct {
// Joined with name with a dash if non-empty.
Variant string
// Path elements joined with source.
Append []string
// Use source tree as scratch space.
Writable bool
// CMake CACHE entries.
Cache [][2]string
// Additional environment variables.
Env []string
// Runs before cmake.
ScriptEarly string
// Runs after cmake, replaces default.
ScriptConfigured string
// Runs after install.
Script string
// Override the default installation prefix [AbsSystem].
Prefix *check.Absolute
// Passed through to [Toolchain.New].
Paths []pkg.ExecPath
// Passed through to [Toolchain.New].
Flag int
}
// NewViaCMake returns a [pkg.Artifact] for compiling and installing via [CMake].
func (t Toolchain) NewViaCMake(
name, version string,
source pkg.Artifact,
attr *CMakeAttr,
extra ...pkg.Artifact,
) pkg.Artifact {
if name == "" || version == "" {
panic("names must be non-empty")
var _ Helper = new(CMakeHelper)
// name returns its arguments and an optional variant string joined with '-'.
func (attr *CMakeHelper) name(name, version string) string {
if attr != nil && attr.Variant != "" {
name += "-" + attr.Variant
}
return name + "-" + version
}
// extra returns a hardcoded slice of [CMake] and [Ninja].
func (*CMakeHelper) extra(int) []PArtifact {
return []PArtifact{CMake, Ninja}
}
// wantsChmod returns false.
func (*CMakeHelper) wantsChmod() bool { return false }
// wantsWrite returns false.
func (*CMakeHelper) wantsWrite() bool { return false }
// scriptEarly returns the zero value.
func (*CMakeHelper) scriptEarly() string { return "" }
// createDir returns true.
func (*CMakeHelper) createDir() bool { return true }
// wantsDir returns a hardcoded, deterministic pathname.
func (*CMakeHelper) wantsDir() string { return "/cure/" }
// script generates the cure script.
func (attr *CMakeHelper) script(name string) string {
if attr == nil {
attr = &CMakeAttr{
attr = &CMakeHelper{
Cache: [][2]string{
{"CMAKE_BUILD_TYPE", "Release"},
},
@@ -90,45 +95,21 @@ func (t Toolchain) NewViaCMake(
panic("CACHE must be non-empty")
}
scriptConfigured := "cmake --build .\ncmake --install .\n"
if attr.ScriptConfigured != "" {
scriptConfigured = attr.ScriptConfigured
}
prefix := attr.Prefix
if prefix == nil {
prefix = AbsSystem
}
rname := name
if attr.Variant != "" {
rname += "-" + attr.Variant
}
sourcePath := AbsUsrSrc.Append(name)
return t.New(rname+"-"+version, attr.Flag, stage0Concat(t, extra,
t.Load(CMake),
t.Load(Ninja),
), nil, slices.Concat([]string{
"ROSA_SOURCE=" + sourcePath.String(),
"ROSA_CMAKE_SOURCE=" + sourcePath.Append(attr.Append...).String(),
"ROSA_INSTALL_PREFIX=/work" + prefix.String(),
}, attr.Env), attr.ScriptEarly+`
mkdir /cure && cd /cure
return `
cmake -G Ninja \
-DCMAKE_C_COMPILER_TARGET="${ROSA_TRIPLE}" \
-DCMAKE_CXX_COMPILER_TARGET="${ROSA_TRIPLE}" \
-DCMAKE_ASM_COMPILER_TARGET="${ROSA_TRIPLE}" \
`+strings.Join(slices.Collect(func(yield func(string) bool) {
` + strings.Join(slices.Collect(func(yield func(string) bool) {
for _, v := range attr.Cache {
if !yield("-D" + v[0] + "=" + v[1]) {
return
}
}
}), " \\\n\t")+` \
-DCMAKE_INSTALL_PREFIX="${ROSA_INSTALL_PREFIX}" \
"${ROSA_CMAKE_SOURCE}"
`+scriptConfigured+attr.Script, slices.Concat([]pkg.ExecPath{
pkg.Path(sourcePath, attr.Writable, source),
}, attr.Paths)...)
}), " \\\n\t") + ` \
-DCMAKE_INSTALL_PREFIX=/work/system \
'/usr/src/` + name + `/` + path.Join(attr.Append...) + `'
cmake --build .
cmake --install .
` + attr.Script
}

View File

@@ -7,33 +7,27 @@ import (
"strings"
"sync"
"hakurei.app/container/check"
"hakurei.app/internal/pkg"
)
// llvmAttr holds the attributes that will be applied to a new [pkg.Artifact]
// containing a LLVM variant.
type llvmAttr struct {
// Passed through to PackageAttr.Flag.
flags int
// Concatenated with default environment for CMakeAttr.Env.
// Concatenated with default environment for PackageAttr.Env.
env []string
// Concatenated with generated entries for CMakeAttr.Cache.
// Concatenated with generated entries for CMakeHelper.Cache.
cmake [][2]string
// Override CMakeAttr.Append.
// Override CMakeHelper.Append.
append []string
// Concatenated with default dependencies for Toolchain.NewViaCMake.
extra []pkg.Artifact
// Passed through to CMakeAttr.Paths.
// Passed through to PackageAttr.NonStage0.
nonStage0 []pkg.Artifact
// Passed through to PackageAttr.Paths.
paths []pkg.ExecPath
// Passed through to CMakeAttr.ScriptConfigured.
scriptConfigured string
// Concatenated with default fixup for CMakeAttr.Script.
// Concatenated with default fixup for CMakeHelper.Script.
script string
// Passed through to CMakeAttr.Prefix.
prefix *check.Absolute
// Passed through to CMakeAttr.Writable.
writable bool
// Patch name and body pairs.
patches [][2]string
@@ -101,7 +95,7 @@ func (t Toolchain) newLLVMVariant(variant string, attr *llvmAttr) pkg.Artifact {
}
}
var script, scriptEarly string
var script string
cache := [][2]string{
{"CMAKE_BUILD_TYPE", "Release"},
@@ -167,43 +161,41 @@ ln -s ld.lld /work/system/bin/ld
)
}
return t.NewViaCMake("llvm", version, t.NewPatchedSource(
return t.NewPackage("llvm", version, t.NewPatchedSource(
"llvmorg", version, pkg.NewHTTPGetTar(
nil, "https://github.com/llvm/llvm-project/archive/refs/tags/"+
"llvmorg-"+version+".tar.gz",
mustDecode(checksum),
pkg.TarGzip,
), true, attr.patches...,
), &CMakeAttr{
Variant: variant,
Cache: slices.Concat(cache, attr.cmake),
Append: cmakeAppend,
Prefix: attr.prefix,
), &PackageAttr{
NonStage0: attr.nonStage0,
Env: slices.Concat([]string{
"ROSA_LLVM_PROJECTS=" + strings.Join(projects, ";"),
"ROSA_LLVM_RUNTIMES=" + strings.Join(runtimes, ";"),
}, attr.env),
ScriptEarly: scriptEarly,
ScriptConfigured: attr.scriptConfigured,
Script: script + attr.script,
Writable: attr.writable,
Paths: attr.paths,
Flag: TExclusive,
}, stage0Concat(t, attr.extra,
t.Load(Libffi),
t.Load(Python),
t.Load(Perl),
t.Load(Diffutils),
t.Load(Bash),
t.Load(Gawk),
t.Load(Coreutils),
t.Load(Findutils),
}, &CMakeHelper{
Variant: variant,
t.Load(KernelHeaders),
)...)
Cache: slices.Concat(cache, attr.cmake),
Append: cmakeAppend,
Script: script + attr.script,
},
Libffi,
Python,
Perl,
Diffutils,
Bash,
Gawk,
Coreutils,
Findutils,
KernelHeaders,
)
}
// newLLVM returns LLVM toolchain across multiple [pkg.Artifact].
@@ -246,21 +238,21 @@ func (t Toolchain) newLLVM() (musl, compilerRT, runtimes, clang pkg.Artifact) {
{"COMPILER_RT_BUILD_XRAY", "OFF"},
},
append: []string{"compiler-rt"},
extra: []pkg.Artifact{t.newMusl(true, []string{
nonStage0: []pkg.Artifact{t.newMusl(true, []string{
"CC=clang",
})},
script: `
mkdir -p "${ROSA_INSTALL_PREFIX}/lib/clang/21/lib/"
mkdir -p "/work/system/lib/clang/21/lib/"
ln -s \
"../../../${ROSA_TRIPLE}" \
"${ROSA_INSTALL_PREFIX}/lib/clang/21/lib/"
"/work/system/lib/clang/21/lib/"
ln -s \
"clang_rt.crtbegin-` + linuxArch() + `.o" \
"${ROSA_INSTALL_PREFIX}/lib/${ROSA_TRIPLE}/crtbeginS.o"
"/work/system/lib/${ROSA_TRIPLE}/crtbeginS.o"
ln -s \
"clang_rt.crtend-` + linuxArch() + `.o" \
"${ROSA_INSTALL_PREFIX}/lib/${ROSA_TRIPLE}/crtendS.o"
"/work/system/lib/${ROSA_TRIPLE}/crtendS.o"
`,
})
@@ -287,7 +279,7 @@ ln -s \
{"LIBCXXABI_HAS_CXA_THREAD_ATEXIT_IMPL", "OFF"},
}, minimalDeps),
append: []string{"runtimes"},
extra: []pkg.Artifact{
nonStage0: []pkg.Artifact{
compilerRT,
musl,
},
@@ -305,7 +297,7 @@ ln -s \
{"CMAKE_CROSSCOMPILING", "OFF"},
{"CXX_SUPPORTS_CUSTOM_LINKER", "ON"},
}, minimalDeps),
extra: []pkg.Artifact{
nonStage0: []pkg.Artifact{
musl,
compilerRT,
runtimes,

View File

@@ -7,12 +7,12 @@ func (t Toolchain) newZstd() pkg.Artifact {
version = "1.5.7"
checksum = "4XhfR7DwVkwo1R-TmYDAJOcx9YXv9WSFhcFUe3hWEAMmdMLPhFaznCqYIA19_xxV"
)
return t.NewViaCMake("zstd", version, pkg.NewHTTPGetTar(
return t.NewPackage("zstd", version, pkg.NewHTTPGetTar(
nil, "https://github.com/facebook/zstd/releases/download/"+
"v"+version+"/zstd-"+version+".tar.gz",
mustDecode(checksum),
pkg.TarGzip,
), &CMakeAttr{
), nil, &CMakeHelper{
Append: []string{"build", "cmake"},
Cache: [][2]string{
{"CMAKE_BUILD_TYPE", "Release"},