From 7a4a1d37307693e6bc7846fa9b113142bd2583fe Mon Sep 17 00:00:00 2001 From: diviocity Date: Mon, 30 Nov 2026 00:08:43 +0100 Subject: [PATCH] Configure CMake targets against the installed Vinix SDK --- tools/vinix-cmake.v | 60 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tools/vinix-cmake.v diff --git a/tools/vinix-cmake.v b/tools/vinix-cmake.v new file mode 100644 index 0000000..9ea4917 --- /dev/null +++ b/tools/vinix-cmake.v @@ -0,0 +1,60 @@ +// Configure a Ninja build against the installed target SDK. Programs run during +// the build are host tools, except explicit target probes through vinix-run. +module main + +import os + +fn cmake_value(value string) string { + return '"' + value.replace('\\', '\\\\').replace('"', '\\"').replace('$', '\\$') + '"' +} + +fn setup(args []string) ! { + if args.len < 2 { + return error('usage: vinix-cmake SOURCE_DIR BUILD_DIR [CMAKE_OPTION ...]') + } + root := os.getenv('VPAC_MAKEPKG_ROOT') + arch := os.getenv_opt('VPAC_TARGET_ARCH') or { os.uname().machine } + if arch !in ['x86_64', 'aarch64'] || !os.is_abs_path(root) + || !os.is_file(os.join_path(root, 'lib', 'ld-musl-' + arch + '.so.1')) { + return error('vinix-cmake requires a matching ${arch} musl SDK in VPAC_MAKEPKG_ROOT') + } + mut lines := ['set(CMAKE_SYSTEM_NAME Linux)', 'set(CMAKE_SYSTEM_PROCESSOR ' + arch + ')', + 'set(CMAKE_SYSROOT ' + cmake_value(root) + ')', 'set(CMAKE_FIND_ROOT_PATH ' + + cmake_value(root) + ')', 'set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)', + 'set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)', 'set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)', + 'set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)', 'set(CMAKE_C_COMPILER vinix-cc)', + 'set(CMAKE_CXX_COMPILER vinix-c++)', 'set(CMAKE_CROSSCOMPILING_EMULATOR vinix-run)'] + prefix := os.getenv('CROSS_COMPILE') + for name in ['ar', 'ranlib', 'strip', 'nm', 'objcopy', 'objdump'] { + path := os.find_abs_path_of_executable(prefix + name)! + lines << 'set(CMAKE_' + name.to_upper() + ' ' + cmake_value(path) + ')' + } + path := os.abs_path(args[1]) + '.vinix-toolchain.cmake' + os.write_file(path, lines.join('\n') + '\n')! + for name in ['CPATH', 'C_INCLUDE_PATH', 'CPLUS_INCLUDE_PATH', 'LIBRARY_PATH', 'PKG_CONFIG_PATH'] { + os.unsetenv(name) + } + os.setenv('PKG_CONFIG_SYSROOT_DIR', root, true) + os.setenv('PKG_CONFIG_LIBDIR', os.join_path(root, 'usr/lib/pkgconfig') + ':' + + os.join_path(root, 'usr/share/pkgconfig'), true) + mut options := ['-S', args[0], '-B', args[1], '-G', 'Ninja', '-DCMAKE_TOOLCHAIN_FILE=' + path, + '-DCMAKE_BUILD_TYPE=Release', '-DCMAKE_INSTALL_PREFIX=/usr', '-DCMAKE_INSTALL_LIBDIR=lib'] + if log := os.getenv_opt('VINIX_LINK_LOG') { + if !os.is_abs_path(log) { + return error('VINIX_LINK_LOG must be an absolute path') + } + timer := os.find_abs_path_of_executable('time')! + // GNU time records peak resident KiB, wall seconds, exit code and the link command. + launcher := timer + ';-a;-o;' + log + ';-f;%M\t%e\t%x\t%C' + options << ['-DCMAKE_C_LINKER_LAUNCHER=' + launcher, '-DCMAKE_CXX_LINKER_LAUNCHER=' + launcher] + } + options << args[2..] + os.execvp('cmake', options)! +} + +fn main() { + setup(os.args[1..]) or { + eprintln(err) + exit(1) + } +} -- 2.56.1