vinix-ports / tools / vinix-cc.v
57 lines · 54 sloc · 1.9 KB · ea171a2ea87f6bc8391287fb9787719f4613358d
Raw
1// A single compiler name for build systems such as libtool that independently
2// expand CC and its arguments. Repeating musl's specs option makes GCC fail.
3module main
4
5import os
6
7// Libtool can add -L/usr/lib while it relinks libraries under DESTDIR. GCC's
8// --sysroot does not rebase an ordinary -L argument. Keep these explicit
9// system directories inside the SDK, including their two-argument spellings.
10fn target_search_args(root string, input []string) []string {
11 mut args := []string{cap: input.len}
12 mut previous := ''
13 for arg in input {
14 if (previous == '-L' && arg in ['/usr/lib', '/lib'])
15 || (previous in ['-I', '-isystem'] && arg == '/usr/include') {
16 args << root + arg
17 } else if arg in ['-L/usr/lib', '-L/lib', '-I/usr/include'] {
18 args << arg[..2] + root + arg[2..]
19 } else {
20 args << arg
21 }
22 previous = arg
23 }
24 return args
25}
26
27fn main() {
28 driver := os.base(os.args[0])
29 root := os.getenv('VPAC_MAKEPKG_ROOT')
30 if !os.is_abs_path(root) {
31 eprintln('${driver} requires an absolute VPAC_MAKEPKG_ROOT')
32 exit(1)
33 }
34 specs := os.join_path(root, 'usr/lib/musl-gcc-sysroot.specs')
35 if !os.is_file(specs) {
36 eprintln('${driver}: missing installed musl specs at ${specs}')
37 exit(1)
38 }
39 prefix := os.getenv('CROSS_COMPILE')
40 compiler := if driver == 'vinix-c++' {
41 os.getenv_opt('VINIX_REAL_CXX') or { prefix + 'g++' }
42 } else {
43 os.getenv_opt('VINIX_REAL_CC') or { prefix + 'gcc' }
44 }
45 mut args := ['--sysroot=${root}', '-specs=${specs}']
46 args << target_search_args(root, os.args[1..])
47 // The released AArch64 kernel maps 16 KiB pages. Some cross GCC
48 // distributions select a 4 KiB linker maximum even with a musl sysroot.
49 if os.is_file(os.join_path(root, 'lib/ld-musl-aarch64.so.1'))
50 && !os.args[1..].any(it in ['-c', '-E', '-S', '-M', '-MM', '-fsyntax-only', '-r']) {
51 args << '-Wl,-z,max-page-size=16384'
52 }
53 os.execvp(compiler, args) or {
54 eprintln('${driver}: ${compiler}: ${err}')
55 exit(1)
56 }
57}
58