vinix-ports / tools / vinix-cc.v
60 lines · 57 sloc · 2.04 KB · 6a834d9c90c2ee0ec0a63fb3f5a9c0845323c456
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 if !os.args[1..].any(it in ['-c', '-E', '-S', '-M', '-MM', '-fsyntax-only', '-r']) {
48 // The build machine's linker also needs to find DT_NEEDED libraries
49 // named by input shared objects. Keep this path out of the target ELF.
50 args << ['-Xlinker', '-rpath-link', '-Xlinker', '${root}/usr/lib:${root}/lib']
51 // The released AArch64 kernel maps 16 KiB pages.
52 if os.is_file(os.join_path(root, 'lib/ld-musl-aarch64.so.1')) {
53 args << '-Wl,-z,max-page-size=16384'
54 }
55 }
56 os.execvp(compiler, args) or {
57 eprintln('${driver}: ${compiler}: ${err}')
58 exit(1)
59 }
60}
61