// Configure the two Mesa releases with separate driver search paths. module main import os fn config_tool(root string, major string, arch string) ![]string { prefix := os.join_path(root, 'usr/lib/llvm' + major) binary := os.join_path(prefix, 'bin/llvm-config') mut commands := [][]string{} if os.uname().machine == arch { commands << [binary] } // QEMU starts the requested target executable with its installed loader. // llvm-config derives include/lib paths from its own executable name. commands << ['qemu-' + arch, '-L', root, '-E', 'LD_LIBRARY_PATH=' + root + '/usr/lib:' + root + '/lib', binary] for args in commands { mut probe := args.clone() probe << '--prefix' result := os.exec(probe) if result.exit_code == 0 && os.real_path(result.output.trim_space()) == os.real_path(prefix) { return args } } return error('could not run ${binary} with its installed prefix ${prefix}') } fn machine_string(value string) string { return "'" + value.replace('\\', '\\\\').replace("'", "\\'") + "'" } fn configure(profile string, source string, build string) ! { if profile !in ['current', 'legacy'] { return error('use current (24.2.8) or legacy (24.0.9)') } legacy := profile == 'legacy' root := os.getenv('VPAC_MAKEPKG_ROOT') arch := os.getenv_opt('VPAC_TARGET_ARCH') or { os.uname().machine } if !os.is_abs_path(root) || arch !in ['x86_64', 'aarch64'] { return error('set VPAC_MAKEPKG_ROOT to the installed x86_64 or aarch64 SDK') } config := config_tool(root, if legacy { '17' } else { '19' }, arch)! cross := os.abs_path(build) + '.mesa.ini' os.write_file(cross, '[binaries]\nllvm-config = [' + config.map(machine_string(it)).join(', ') + ']\n')! mut drivers := if legacy { ['swrast'] } else { ['llvmpipe', 'softpipe'] } drivers << ['r600', 'radeonsi', 'nouveau', 'virgl', 'zink'] if arch == 'x86_64' { drivers << ['r300', 'svga', 'i915', 'iris', 'crocus'] } else { drivers << ['vc4', 'v3d', 'freedreno', 'lima', 'panfrost', 'etnaviv', 'tegra'] if legacy { drivers << ['r300', 'kmsro'] } } mut args := [source, build, '--cross-file', cross, '--wrap-mode=nofallback', '-Db_ndebug=true', '-Db_lto=false', '-Doptimization=1', '-Dbackend_max_links=1', '-Dbuild-tests=false', '-Dgallium-drivers=' + drivers.join(','), '-Dvulkan-drivers=', '-Dvulkan-layers=', '-Dgallium-rusticl=false', '-Dgallium-va=disabled', '-Dgallium-vdpau=disabled', '-Dgallium-nine=false', '-Dllvm=enabled', '-Dshared-llvm=enabled', '-Dshared-glapi=enabled', '-Dopengl=true'] if native := os.getenv_opt('VINIX_MESA_NATIVE_FILE') { if !os.is_file(native) { return error('VINIX_MESA_NATIVE_FILE does not name a Meson native file') } args << ['--native-file', os.abs_path(native)] } path := if legacy { '/usr/lib/xorg/modules/dri' } else { '/usr/lib/mesa24/dri' } args << ['-Dplatforms=x11,wayland', '-Dglx=dri', '-Dgbm=enabled', '-Degl=enabled', '-Dgles1=enabled', '-Dgles2=enabled', '-Ddri-drivers-path=' + path, '-Ddri3=enabled'] // The current build emits both the runtime and the optional SDK package. if legacy { args << ['-Dgallium-xa=disabled', '-Dosmesa=false'] } else { args << ['-Dgallium-xa=enabled', '-Dosmesa=true'] if arch == 'x86_64' { args << '-Dintel-clc=system' } } os.execvp('vinix-meson', args)! } fn legacy_file(path string) bool { return path.starts_with('usr/lib/xorg/modules/dri/') || path.starts_with('usr/lib/libGL.so') || path.starts_with('usr/lib/libglapi.so') || path == 'usr/lib/pkgconfig/gl.pc' } fn sdk_file(path string) bool { return path.starts_with('usr/lib/libOSMesa.so') || path.starts_with('usr/lib/libxatracker.so') || path in ['usr/lib/pkgconfig/osmesa.pc', 'usr/lib/pkgconfig/xatracker.pc'] || path.starts_with('usr/include/xa_') } fn arm_alias(profile string, path string) string { name := os.file_name(path) return match path { 'usr/lib/libGL.so.1', 'usr/lib/libglapi.so.0' { if profile == 'legacy' { 'usr/lib/vinix-x11-software/' + name } else { '' } } 'usr/lib/libEGL.so.1', 'usr/lib/libgbm.so.1' { if profile == 'current' { 'usr/lib/vinix-x11-software/egl/' + name } else { '' } } 'usr/lib/xorg/modules/dri/libgallium_dri.so', 'usr/lib/xorg/modules/dri/swrast_dri.so', 'usr/lib/xorg/modules/dri/kms_swrast_dri.so' { if profile == 'legacy' { 'usr/lib/dri/' + name } else { '' } } else { '' } } } fn package_files(profile string, stage string, output string) ! { if profile !in ['current', 'legacy', 'sdk'] || !os.is_dir(os.join_path(stage, 'usr')) { return error('expected Mesa profile and a populated Meson installation') } arch := os.getenv_opt('VPAC_TARGET_ARCH') or { os.uname().machine } root := os.real_path(stage) mut copies := map[string]string{} mut files := os.walk_ext(os.join_path(root, 'usr'), '') files.sort() for file in files { path := file[root.len + 1..] wanted := match profile { 'legacy' { legacy_file(path) } 'sdk' { sdk_file(path) } else { !legacy_file(path) && !sdk_file(path) && path !in [ 'usr/include/EGL/eglplatform.h', 'usr/include/KHR/khrplatform.h', ] } } if !wanted { continue } // The Vinix loader opens driver paths with O_NOFOLLOW. Preserve one // copy of each Mesa DSO while making all its aliases regular files. source := os.real_path(file) if !source.starts_with(root + '/') || !os.is_file(source) { return error('unresolved or external Mesa install path: ${path}') } dst := os.join_path(output, path) os.mkdir_all(os.dir(dst))! st := os.stat(source)! identity := '${st.dev}:${st.inode}' if previous := copies[identity] { os.link(previous, dst)! } else { os.cp(source, dst)! copies[identity] = dst } if arch == 'aarch64' { alias := arm_alias(profile, path) if alias != '' { dest := os.join_path(output, alias) os.mkdir_all(os.dir(dest))! os.link(dst, dest)! } } } required := match profile { 'legacy' { ['libGL.so.1', 'libglapi.so.0', 'xorg/modules/dri/swrast_dri.so'] } 'sdk' { ['libOSMesa.so', 'libxatracker.so', 'pkgconfig/osmesa.pc', 'pkgconfig/xatracker.pc'] } else { ['libEGL.so.1', 'libgbm.so.1', 'libGLESv2.so.2', 'libgallium-24.2.8.so', 'mesa24/dri/swrast_dri.so'] } } for name in required { if !os.is_file(os.join_path(output, 'usr/lib', name)) { return error('missing required ${profile} Mesa output: ${name}') } } } fn main() { if os.args.len == 5 && os.args[1] == 'package' { package_files(os.args[2], os.args[3], os.args[4]) or { eprintln(err) exit(1) } return } if os.args.len != 4 { eprintln('usage: vinix-mesa PROFILE MESA_SOURCE_DIR BUILD_DIR') exit(2) } configure(os.args[1], os.args[2], os.args[3]) or { eprintln(err) exit(1) } }