vinix-ports / tools / vinix-meson.v
49 lines · 45 sloc · 1.82 KB · 1d216756d038c9472c177cf293cdfb258258e64e
Raw
1// Set up a Meson cross build against an installed Vinix SDK.
2module main
3
4import os
5
6fn quoted(value string) string {
7 return "'" + value.replace('\\', '\\\\').replace("'", "\\'") + "'"
8}
9
10fn setup(args []string) ! {
11 if args.len < 2 {
12 return error('usage: vinix-meson SOURCE_DIR BUILD_DIR [MESON_OPTION ...]')
13 }
14 host := os.uname().machine
15 arch := os.getenv_opt('VPAC_TARGET_ARCH') or { host }
16 root := os.getenv('VPAC_MAKEPKG_ROOT')
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-meson requires a matching ${arch} musl SDK in VPAC_MAKEPKG_ROOT')
20 }
21 prefix := os.getenv('CROSS_COMPILE')
22 mut machine := ['[binaries]', "c = 'vinix-cc'", "cpp = 'vinix-c++'",
23 "pkg-config = 'pkg-config'", "exe_wrapper = 'vinix-run'"]
24 for name in ['ar', 'ranlib', 'strip', 'nm', 'objcopy', 'objdump'] {
25 machine << '${name} = ' + quoted(prefix + name)
26 }
27 machine << ['', '[properties]', 'needs_exe_wrapper = true', 'sys_root = ' + quoted(root),
28 'pkg_config_libdir = [' + quoted(os.join_path(root, 'usr/lib/pkgconfig')) + ', ' +
29 quoted(os.join_path(root, 'usr/share/pkgconfig')) + ']', '', '[host_machine]',
30 "system = 'linux'", 'cpu_family = ' + quoted(arch), 'cpu = ' + quoted(arch), "endian = 'little'"]
31 file := os.abs_path(args[1]) + '.vinix-cross.ini'
32 os.write_file(file, machine.join('\n') + '\n')!
33 for name in ['CPATH', 'C_INCLUDE_PATH', 'CPLUS_INCLUDE_PATH', 'LIBRARY_PATH', 'PKG_CONFIG_PATH',
34 'PKG_CONFIG_LIBDIR', 'PKG_CONFIG_SYSROOT_DIR'] {
35 os.unsetenv(name)
36 }
37 mut options := ['setup', '--cross-file', file, '--prefix=/usr', '--sysconfdir=/etc',
38 '--libdir=lib', '--buildtype=release']
39 options << args[2..]
40 options << [args[1], args[0]]
41 os.execvp('meson', options)!
42}
43
44fn main() {
45 setup(os.args[1..]) or {
46 eprintln(err)
47 exit(1)
48 }
49}
50