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

import (
	"path/filepath"
	"slices"
	"strings"
)

var _cmake = H("cmake")

// CMakeHelper is the [CMake] build system helper.
type CMakeHelper struct {
	// Path elements joined with source.
	Append []string

	// Value of CMAKE_BUILD_TYPE. The zero value is equivalent to "Release".
	BuildType string
	// CMake CACHE entries.
	Cache []KV
	// Runs after install.
	Script string

	// Replaces the default test command.
	Test string
	// Whether to skip running tests.
	SkipTest bool

	// Whether to generate Makefile instead.
	Make bool
}

var _ Helper = new(CMakeHelper)

// extra returns a hardcoded slice of [CMake] and [Ninja].
func (attr *CMakeHelper) extra(int) P {
	if attr != nil && attr.Make {
		return P{_cmake, _make}
	}
	return P{_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(t Toolchain, name string) string {
	if attr == nil {
		attr = new(CMakeHelper)
	}

	generate := "Ninja"
	test := "ninja " + jobsFlagE + " test"
	if attr.Make {
		generate = "'Unix Makefiles'"
		test = "make " + jobsFlagE + " test"
	}
	if attr.Test != "" {
		test = attr.Test
	}

	script := attr.Script
	if !attr.SkipTest && t.opts&OptSkipCheck == 0 {
		script += "\n" + test
	}

	cache := make([]KV, 1, 1+len(attr.Cache))
	cache[0] = KV{"CMAKE_BUILD_TYPE", "Release"}
	if attr.BuildType != "" {
		cache[0][1] = attr.BuildType
	}
	cache = append(cache, attr.Cache...)

	return `
cmake -G ` + generate + ` \
	-DCMAKE_C_COMPILER_TARGET="${ROSA_TRIPLE}" \
	-DCMAKE_CXX_COMPILER_TARGET="${ROSA_TRIPLE}" \
	-DCMAKE_ASM_COMPILER_TARGET="${ROSA_TRIPLE}" \
	-DCMAKE_INSTALL_LIBDIR=lib \
	` + strings.Join(slices.Collect(func(yield func(string) bool) {
		for _, v := range cache {
			if !yield("-D" + v[0] + "=" + v[1]) {
				return
			}
		}
	}), " \\\n\t") + ` \
	-DCMAKE_INSTALL_PREFIX=/system \
	'/usr/src/` + name + `/` + filepath.Join(attr.Append...) + `'
cmake --build . --parallel=` + jobsE + `
cmake --install . --prefix=/work/system
` + script
}