vinix-ports / tools / vinix-mesa.v
186 lines · 177 sloc · 6.35 KB · 5b4bdb770ebc50bfd5efad2e6c813c463c5628c4
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 path := if legacy { '/usr/lib/xorg/modules/dri' } else { '/usr/lib/mesa24/dri' }
63 args << ['-Dplatforms=x11,wayland', '-Dglx=dri', '-Dgbm=enabled', '-Degl=enabled', '-Dgles1=enabled',
64 '-Dgles2=enabled', '-Ddri-drivers-path=' + path, '-Ddri3=enabled']
65 // The current build emits both the runtime and the optional SDK package.
66 if legacy {
67 args << ['-Dgallium-xa=disabled', '-Dosmesa=false']
68 } else {
69 args << ['-Dgallium-xa=enabled', '-Dosmesa=true']
70 if arch == 'x86_64' {
71 args << '-Dintel-clc=system'
72 }
73 }
74 os.execvp('vinix-meson', args)!
75}
76
77fn legacy_file(path string) bool {
78 return path.starts_with('usr/lib/xorg/modules/dri/') || path.starts_with('usr/lib/libGL.so')
79 || path.starts_with('usr/lib/libglapi.so') || path == 'usr/lib/pkgconfig/gl.pc'
80}
81
82fn sdk_file(path string) bool {
83 return path.starts_with('usr/lib/libOSMesa.so') || path.starts_with('usr/lib/libxatracker.so')
84 || path in ['usr/lib/pkgconfig/osmesa.pc', 'usr/lib/pkgconfig/xatracker.pc'] || path.starts_with('usr/include/xa_')
85}
86
87fn arm_alias(profile string, path string) string {
88 name := os.file_name(path)
89 return match path {
90 'usr/lib/libGL.so.1', 'usr/lib/libglapi.so.0' {
91 if profile == 'legacy' { 'usr/lib/vinix-x11-software/' + name } else { '' }
92 }
93 'usr/lib/libEGL.so.1', 'usr/lib/libgbm.so.1' {
94 if profile == 'current' { 'usr/lib/vinix-x11-software/egl/' + name } else { '' }
95 }
96 'usr/lib/xorg/modules/dri/libgallium_dri.so', 'usr/lib/xorg/modules/dri/swrast_dri.so',
97 'usr/lib/xorg/modules/dri/kms_swrast_dri.so' {
98 if profile == 'legacy' { 'usr/lib/dri/' + name } else { '' }
99 }
100 else { '' }
101 }
102}
103
104fn package_files(profile string, stage string, output string) ! {
105 if profile !in ['current', 'legacy', 'sdk'] || !os.is_dir(os.join_path(stage, 'usr')) {
106 return error('expected Mesa profile and a populated Meson installation')
107 }
108 arch := os.getenv_opt('VPAC_TARGET_ARCH') or { os.uname().machine }
109 root := os.real_path(stage)
110 mut copies := map[string]string{}
111 mut files := os.walk_ext(os.join_path(root, 'usr'), '')
112 files.sort()
113 for file in files {
114 path := file[root.len + 1..]
115 wanted := match profile {
116 'legacy' { legacy_file(path) }
117 'sdk' { sdk_file(path) }
118 else {
119 !legacy_file(path) && !sdk_file(path) && path !in [
120 'usr/include/EGL/eglplatform.h',
121 'usr/include/KHR/khrplatform.h',
122 ]
123 }
124 }
125 if !wanted {
126 continue
127 }
128 // The Vinix loader opens driver paths with O_NOFOLLOW. Preserve one
129 // copy of each Mesa DSO while making all its aliases regular files.
130 source := os.real_path(file)
131 if !source.starts_with(root + '/') || !os.is_file(source) {
132 return error('unresolved or external Mesa install path: ${path}')
133 }
134 dst := os.join_path(output, path)
135 os.mkdir_all(os.dir(dst))!
136 st := os.stat(source)!
137 identity := '${st.dev}:${st.inode}'
138 if previous := copies[identity] {
139 os.link(previous, dst)!
140 } else {
141 os.cp(source, dst)!
142 copies[identity] = dst
143 }
144 if arch == 'aarch64' {
145 alias := arm_alias(profile, path)
146 if alias != '' {
147 dest := os.join_path(output, alias)
148 os.mkdir_all(os.dir(dest))!
149 os.link(dst, dest)!
150 }
151 }
152 }
153 required := match profile {
154 'legacy' { ['libGL.so.1', 'libglapi.so.0', 'xorg/modules/dri/swrast_dri.so'] }
155 'sdk' {
156 ['libOSMesa.so', 'libxatracker.so', 'pkgconfig/osmesa.pc', 'pkgconfig/xatracker.pc']
157 }
158 else {
159 ['libEGL.so.1', 'libgbm.so.1', 'libGLESv2.so.2', 'libgallium-24.2.8.so',
160 'mesa24/dri/swrast_dri.so']
161 }
162 }
163 for name in required {
164 if !os.is_file(os.join_path(output, 'usr/lib', name)) {
165 return error('missing required ${profile} Mesa output: ${name}')
166 }
167 }
168}
169
170fn main() {
171 if os.args.len == 5 && os.args[1] == 'package' {
172 package_files(os.args[2], os.args[3], os.args[4]) or {
173 eprintln(err)
174 exit(1)
175 }
176 return
177 }
178 if os.args.len != 4 {
179 eprintln('usage: vinix-mesa PROFILE MESA_SOURCE_DIR BUILD_DIR')
180 exit(2)
181 }
182 configure(os.args[1], os.args[2], os.args[3]) or {
183 eprintln(err)
184 exit(1)
185 }
186}
187