vinix-ports / tools / vinix-mesa.v
192 lines · 183 sloc · 6.56 KB
Raw
1// Configure the two Mesa releases with separate driver search paths.
2module main
3
4import os
5
6fn config_tool(root string, major string, arch string) ![]string {
7 prefix := os.join_path(root, 'usr/lib/llvm' + major)
8 binary := os.join_path(prefix, 'bin/llvm-config')
9 mut commands := [][]string{}
10 if os.uname().machine == arch {
11 commands << [binary]
12 }
13 // QEMU starts the requested target executable with its installed loader.
14 // llvm-config derives include/lib paths from its own executable name.
15 commands << ['qemu-' + arch, '-L', root, '-E',
16 'LD_LIBRARY_PATH=' + root + '/usr/lib:' + root + '/lib', binary]
17 for args in commands {
18 mut probe := args.clone()
19 probe << '--prefix'
20 result := os.exec(probe)
21 if result.exit_code == 0 && os.real_path(result.output.trim_space()) == os.real_path(prefix) {
22 return args
23 }
24 }
25 return error('could not run ${binary} with its installed prefix ${prefix}')
26}
27
28fn machine_string(value string) string {
29 return "'" + value.replace('\\', '\\\\').replace("'", "\\'") + "'"
30}
31
32fn configure(profile string, source string, build string) ! {
33 if profile !in ['current', 'legacy'] {
34 return error('use current (24.2.8) or legacy (24.0.9)')
35 }
36 legacy := profile == 'legacy'
37 root := os.getenv('VPAC_MAKEPKG_ROOT')
38 arch := os.getenv_opt('VPAC_TARGET_ARCH') or { os.uname().machine }
39 if !os.is_abs_path(root) || arch !in ['x86_64', 'aarch64'] {
40 return error('set VPAC_MAKEPKG_ROOT to the installed x86_64 or aarch64 SDK')
41 }
42 config := config_tool(root, if legacy { '17' } else { '19' }, arch)!
43 cross := os.abs_path(build) + '.mesa.ini'
44 os.write_file(cross, '[binaries]\nllvm-config = [' +
45 config.map(machine_string(it)).join(', ') + ']\n')!
46 mut drivers := if legacy { ['swrast'] } else { ['llvmpipe', 'softpipe'] }
47 drivers << ['r600', 'radeonsi', 'nouveau', 'virgl', 'zink']
48 if arch == 'x86_64' {
49 drivers << ['r300', 'svga', 'i915', 'iris', 'crocus']
50 } else {
51 drivers << ['vc4', 'v3d', 'freedreno', 'lima', 'panfrost', 'etnaviv', 'tegra']
52 if legacy {
53 drivers << ['r300', 'kmsro']
54 }
55 }
56 mut args := [source, build, '--cross-file', cross, '--wrap-mode=nofallback', '-Db_ndebug=true',
57 '-Db_lto=false', '-Doptimization=1', '-Dbackend_max_links=1', '-Dbuild-tests=false',
58 '-Dgallium-drivers=' + drivers.join(','), '-Dvulkan-drivers=', '-Dvulkan-layers=',
59 '-Dgallium-rusticl=false', '-Dgallium-va=disabled', '-Dgallium-vdpau=disabled',
60 '-Dgallium-nine=false', '-Dllvm=enabled', '-Dshared-llvm=enabled', '-Dshared-glapi=enabled',
61 '-Dopengl=true']
62 if native := os.getenv_opt('VINIX_MESA_NATIVE_FILE') {
63 if !os.is_file(native) {
64 return error('VINIX_MESA_NATIVE_FILE does not name a Meson native file')
65 }
66 args << ['--native-file', os.abs_path(native)]
67 }
68 path := if legacy { '/usr/lib/xorg/modules/dri' } else { '/usr/lib/mesa24/dri' }
69 args << ['-Dplatforms=x11,wayland', '-Dglx=dri', '-Dgbm=enabled', '-Degl=enabled', '-Dgles1=enabled',
70 '-Dgles2=enabled', '-Ddri-drivers-path=' + path, '-Ddri3=enabled']
71 // The current build emits both the runtime and the optional SDK package.
72 if legacy {
73 args << ['-Dgallium-xa=disabled', '-Dosmesa=false']
74 } else {
75 args << ['-Dgallium-xa=enabled', '-Dosmesa=true']
76 if arch == 'x86_64' {
77 args << '-Dintel-clc=system'
78 }
79 }
80 os.execvp('vinix-meson', args)!
81}
82
83fn legacy_file(path string) bool {
84 return path.starts_with('usr/lib/xorg/modules/dri/') || path.starts_with('usr/lib/libGL.so')
85 || path.starts_with('usr/lib/libglapi.so') || path == 'usr/lib/pkgconfig/gl.pc'
86}
87
88fn sdk_file(path string) bool {
89 return path.starts_with('usr/lib/libOSMesa.so') || path.starts_with('usr/lib/libxatracker.so')
90 || path in ['usr/lib/pkgconfig/osmesa.pc', 'usr/lib/pkgconfig/xatracker.pc'] || path.starts_with('usr/include/xa_')
91}
92
93fn arm_alias(profile string, path string) string {
94 name := os.file_name(path)
95 return match path {
96 'usr/lib/libGL.so.1', 'usr/lib/libglapi.so.0' {
97 if profile == 'legacy' { 'usr/lib/vinix-x11-software/' + name } else { '' }
98 }
99 'usr/lib/libEGL.so.1', 'usr/lib/libgbm.so.1' {
100 if profile == 'current' { 'usr/lib/vinix-x11-software/egl/' + name } else { '' }
101 }
102 'usr/lib/xorg/modules/dri/libgallium_dri.so', 'usr/lib/xorg/modules/dri/swrast_dri.so',
103 'usr/lib/xorg/modules/dri/kms_swrast_dri.so' {
104 if profile == 'legacy' { 'usr/lib/dri/' + name } else { '' }
105 }
106 else { '' }
107 }
108}
109
110fn package_files(profile string, stage string, output string) ! {
111 if profile !in ['current', 'legacy', 'sdk'] || !os.is_dir(os.join_path(stage, 'usr')) {
112 return error('expected Mesa profile and a populated Meson installation')
113 }
114 arch := os.getenv_opt('VPAC_TARGET_ARCH') or { os.uname().machine }
115 root := os.real_path(stage)
116 mut copies := map[string]string{}
117 mut files := os.walk_ext(os.join_path(root, 'usr'), '')
118 files.sort()
119 for file in files {
120 path := file[root.len + 1..]
121 wanted := match profile {
122 'legacy' { legacy_file(path) }
123 'sdk' { sdk_file(path) }
124 else {
125 !legacy_file(path) && !sdk_file(path) && path !in [
126 'usr/include/EGL/eglplatform.h',
127 'usr/include/KHR/khrplatform.h',
128 ]
129 }
130 }
131 if !wanted {
132 continue
133 }
134 // The Vinix loader opens driver paths with O_NOFOLLOW. Preserve one
135 // copy of each Mesa DSO while making all its aliases regular files.
136 source := os.real_path(file)
137 if !source.starts_with(root + '/') || !os.is_file(source) {
138 return error('unresolved or external Mesa install path: ${path}')
139 }
140 dst := os.join_path(output, path)
141 os.mkdir_all(os.dir(dst))!
142 st := os.stat(source)!
143 identity := '${st.dev}:${st.inode}'
144 if previous := copies[identity] {
145 os.link(previous, dst)!
146 } else {
147 os.cp(source, dst)!
148 copies[identity] = dst
149 }
150 if arch == 'aarch64' {
151 alias := arm_alias(profile, path)
152 if alias != '' {
153 dest := os.join_path(output, alias)
154 os.mkdir_all(os.dir(dest))!
155 os.link(dst, dest)!
156 }
157 }
158 }
159 required := match profile {
160 'legacy' { ['libGL.so.1', 'libglapi.so.0', 'xorg/modules/dri/swrast_dri.so'] }
161 'sdk' {
162 ['libOSMesa.so', 'libxatracker.so', 'pkgconfig/osmesa.pc', 'pkgconfig/xatracker.pc']
163 }
164 else {
165 ['libEGL.so.1', 'libgbm.so.1', 'libGLESv2.so.2', 'libgallium-24.2.8.so',
166 'mesa24/dri/swrast_dri.so']
167 }
168 }
169 for name in required {
170 if !os.is_file(os.join_path(output, 'usr/lib', name)) {
171 return error('missing required ${profile} Mesa output: ${name}')
172 }
173 }
174}
175
176fn main() {
177 if os.args.len == 5 && os.args[1] == 'package' {
178 package_files(os.args[2], os.args[3], os.args[4]) or {
179 eprintln(err)
180 exit(1)
181 }
182 return
183 }
184 if os.args.len != 4 {
185 eprintln('usage: vinix-mesa PROFILE MESA_SOURCE_DIR BUILD_DIR')
186 exit(2)
187 }
188 configure(os.args[1], os.args[2], os.args[3]) or {
189 eprintln(err)
190 exit(1)
191 }
192}
193