aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/rosa/meson.go
blob: fbdb363782ffa523b3aae4af72ad2b965b61df39 (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
116
117
118
119
120
121
122
123
124
125
package rosa

import (
	"slices"
	"strings"

	"hakurei.app/internal/pkg"
)

func (t Toolchain) newMeson() pkg.Artifact {
	const (
		version  = "1.10.1"
		checksum = "w895BXF_icncnXatT_OLCFe2PYEtg4KrKooMgUYdN-nQVvbFX3PvYWHGEpogsHtd"
	)
	return t.New("meson-"+version, 0, []pkg.Artifact{
		t.Load(Python),
		t.Load(Setuptools),
	}, nil, nil, `
cd /usr/src/meson
chmod -R +w meson.egg-info
python3 setup.py \
	install \
	--prefix=/system \
	--root=/work
`, pkg.Path(AbsUsrSrc.Append("meson"), true, pkg.NewHTTPGetTar(
		nil, "https://github.com/mesonbuild/meson/releases/download/"+
			version+"/meson-"+version+".tar.gz",
		mustDecode(checksum),
		pkg.TarGzip,
	)))
}
func init() { artifactsF[Meson] = Toolchain.newMeson }

// MesonAttr holds the project-specific attributes that will be applied to a new
// [pkg.Artifact] compiled via [Meson].
type MesonAttr struct {
	// Mount the source tree writable.
	Writable bool

	// Additional environment variables.
	Env []string
	// Runs before setup.
	ScriptEarly string
	// Runs after configure.
	ScriptCompiled string
	// Runs after install.
	Script string

	// Flags passed to the setup command.
	Configure [][2]string
	// Whether to skip meson test.
	SkipCheck bool
	// Appended after the test command.
	AppendTest string

	// Suffix appended to the source pathname.
	SourceSuffix string

	// Passed through to [Toolchain.New], before source.
	Paths []pkg.ExecPath
	// Passed through to [Toolchain.New].
	Flag int
}

// NewViaMeson returns a [pkg.Artifact] for compiling and installing via [Meson].
func (t Toolchain) NewViaMeson(
	name, version string,
	source pkg.Artifact,
	attr *MesonAttr,
	extra ...pkg.Artifact,
) pkg.Artifact {
	if name == "" || version == "" {
		panic("names must be non-empty")
	}
	if attr == nil {
		attr = new(MesonAttr)
	}

	scriptCompiled := attr.ScriptCompiled
	if len(scriptCompiled) > 0 && scriptCompiled[0] != '\n' {
		scriptCompiled = "\n" + scriptCompiled
	}

	var check string
	if !attr.SkipCheck {
		check = "\nmeson test \\\n\t--print-errorlogs" + attr.AppendTest
	}

	return t.New(name+"-"+version, attr.Flag, slices.Concat([]pkg.Artifact{
		t.Load(Python),
		t.Load(Meson),
		t.Load(Ninja),

		t.Load(PkgConfig),
		t.Load(CMake),
	}, extra), nil, attr.Env, attr.ScriptEarly+`
cd "$(mktemp -d)"
meson setup \
	`+strings.Join(slices.Collect(func(yield func(string) bool) {
		for _, v := range append([][2]string{
			{"prefix", "/system"},
			{"buildtype", "release"},
		}, attr.Configure...) {
			s := "-" + v[0]
			if len(v[0]) > 0 && v[0][0] != 'D' {
				s = "-" + s
			}
			if v[1] != "" {
				s += "=" + v[1]
			}
			if !yield(s) {
				return
			}
		}
	}), " \\\n\t")+` \
	. /usr/src/`+name+`
meson compile`+scriptCompiled+check+`
meson install \
	--destdir=/work
`+attr.Script, slices.Concat(attr.Paths, []pkg.ExecPath{
		pkg.Path(AbsUsrSrc.Append(
			name+attr.SourceSuffix,
		), attr.Writable, source),
	})...)
}