aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/rosa/cmake.go
blob: 281a899163ae613b26851b29cd2ebda0b6a9cf28 (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
package rosa

import (
	"path"
	"slices"
	"strings"

	"hakurei.app/internal/pkg"
)

func (t Toolchain) newCMake() pkg.Artifact {
	const (
		version  = "4.2.1"
		checksum = "Y3OdbMsob6Xk2y1DCME6z4Fryb5_TkFD7knRT8dTNIRtSqbiCJyyDN9AxggN_I75"
	)
	return t.NewPackage("cmake", version, pkg.NewHTTPGetTar(
		nil, "https://github.com/Kitware/CMake/releases/download/"+
			"v"+version+"/cmake-"+version+".tar.gz",
		mustDecode(checksum),
		pkg.TarGzip,
	), &PackageAttr{
		// expected to be writable in the copy made during bootstrap
		Chmod: true,
	}, &MakeHelper{
		OmitDefaults: true,

		ConfigureName: "/usr/src/cmake/bootstrap",
		Configure: [][2]string{
			{"prefix", "/system"},
			{"parallel", `"$(nproc)"`},
			{"--"},
			{"-DCMAKE_USE_OPENSSL", "OFF"},
		},
		SkipCheck: true,
	},
		KernelHeaders,
	)
}
func init() { artifactsF[CMake] = Toolchain.newCMake }

// 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

	// CMake CACHE entries.
	Cache [][2]string
	// Runs after install.
	Script string
}

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 = &CMakeHelper{
			Cache: [][2]string{
				{"CMAKE_BUILD_TYPE", "Release"},
			},
		}
	}
	if len(attr.Cache) == 0 {
		panic("CACHE must be non-empty")
	}

	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) {
		for _, v := range attr.Cache {
			if !yield("-D" + v[0] + "=" + v[1]) {
				return
			}
		}
	}), " \\\n\t") + ` \
	-DCMAKE_INSTALL_PREFIX=/work/system \
	'/usr/src/` + name + `/` + path.Join(attr.Append...) + `'
cmake --build .
cmake --install .
` + attr.Script
}