me

/

vinix-ports Public
382 commits 0 issues 0 pull requests 1 contributor Discussions Projects Snippets LOC history Platform CI

Configure CMake targets against the installed Vinix SDK

diviocity committed 6 days ago · 7a4a1d37307693e6bc7846fa9b113142bd2583fe View patch
Unverified signature 1 files changed +60 -0
tools/vinix-cmake.v new file +60 -0

@@ -0,0 +1,60 @@

1+// Configure a Ninja build against the installed target SDK. Programs run during

2+// the build are host tools, except explicit target probes through vinix-run.

3+module main

4+

5+import os

6+

7+fn cmake_value(value string) string {

8+ return '"' + value.replace('\\', '\\\\').replace('"', '\\"').replace('$', '\\$') + '"'

9+}

10+

11+fn setup(args []string) ! {

12+ if args.len < 2 {

13+ return error('usage: vinix-cmake SOURCE_DIR BUILD_DIR [CMAKE_OPTION ...]')

14+ }

15+ root := os.getenv('VPAC_MAKEPKG_ROOT')

16+ arch := os.getenv_opt('VPAC_TARGET_ARCH') or { os.uname().machine }

17+ if arch !in ['x86_64', 'aarch64'] || !os.is_abs_path(root)

18+ || !os.is_file(os.join_path(root, 'lib', 'ld-musl-' + arch + '.so.1')) {

19+ return error('vinix-cmake requires a matching ${arch} musl SDK in VPAC_MAKEPKG_ROOT')

20+ }

21+ mut lines := ['set(CMAKE_SYSTEM_NAME Linux)', 'set(CMAKE_SYSTEM_PROCESSOR ' + arch + ')',

22+ 'set(CMAKE_SYSROOT ' + cmake_value(root) + ')', 'set(CMAKE_FIND_ROOT_PATH ' +

23+ cmake_value(root) + ')', 'set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)',

24+ 'set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)', 'set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)',

25+ 'set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)', 'set(CMAKE_C_COMPILER vinix-cc)',

26+ 'set(CMAKE_CXX_COMPILER vinix-c++)', 'set(CMAKE_CROSSCOMPILING_EMULATOR vinix-run)']

27+ prefix := os.getenv('CROSS_COMPILE')

28+ for name in ['ar', 'ranlib', 'strip', 'nm', 'objcopy', 'objdump'] {

29+ path := os.find_abs_path_of_executable(prefix + name)!

30+ lines << 'set(CMAKE_' + name.to_upper() + ' ' + cmake_value(path) + ')'

31+ }

32+ path := os.abs_path(args[1]) + '.vinix-toolchain.cmake'

33+ os.write_file(path, lines.join('\n') + '\n')!

34+ for name in ['CPATH', 'C_INCLUDE_PATH', 'CPLUS_INCLUDE_PATH', 'LIBRARY_PATH', 'PKG_CONFIG_PATH'] {

35+ os.unsetenv(name)

36+ }

37+ os.setenv('PKG_CONFIG_SYSROOT_DIR', root, true)

38+ os.setenv('PKG_CONFIG_LIBDIR', os.join_path(root, 'usr/lib/pkgconfig') + ':' +

39+ os.join_path(root, 'usr/share/pkgconfig'), true)

40+ mut options := ['-S', args[0], '-B', args[1], '-G', 'Ninja', '-DCMAKE_TOOLCHAIN_FILE=' + path,

41+ '-DCMAKE_BUILD_TYPE=Release', '-DCMAKE_INSTALL_PREFIX=/usr', '-DCMAKE_INSTALL_LIBDIR=lib']

42+ if log := os.getenv_opt('VINIX_LINK_LOG') {

43+ if !os.is_abs_path(log) {

44+ return error('VINIX_LINK_LOG must be an absolute path')

45+ }

46+ timer := os.find_abs_path_of_executable('time')!

47+ // GNU time records peak resident KiB, wall seconds, exit code and the link command.

48+ launcher := timer + ';-a;-o;' + log + ';-f;%M\t%e\t%x\t%C'

49+ options << ['-DCMAKE_C_LINKER_LAUNCHER=' + launcher, '-DCMAKE_CXX_LINKER_LAUNCHER=' + launcher]

50+ }

51+ options << args[2..]

52+ os.execvp('cmake', options)!

53+}

54+

55+fn main() {

56+ setup(os.args[1..]) or {

57+ eprintln(err)

58+ exit(1)

59+ }

60+}