| 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. |
| 3 | module main |
| 4 | |
| 5 | import os |
| 6 | |
| 7 | // Libtool can add -L/usr/lib while it relinks libraries under DESTDIR. GCC's |
| 8 | // --sysroot does not rebase ordinary -L or -I arguments. Keep explicit |
| 9 | // system directories and their descendants inside the SDK. |
| 10 | fn target_search_path(root string, flag string, path string) string { |
| 11 | if flag == '-L' && (path == '/usr/lib' || path.starts_with('/usr/lib/') || path == '/lib' |
| 12 | || path.starts_with('/lib/')) { |
| 13 | return root + path |
| 14 | } |
| 15 | if flag in ['-I', '-isystem'] && (path == '/usr/include' || path.starts_with('/usr/include/')) { |
| 16 | return root + path |
| 17 | } |
| 18 | return path |
| 19 | } |
| 20 | |
| 21 | fn target_search_args(root string, input []string) []string { |
| 22 | mut args := []string{cap: input.len} |
| 23 | mut previous := '' |
| 24 | for arg in input { |
| 25 | if previous != '' { |
| 26 | args << target_search_path(root, previous, arg) |
| 27 | previous = '' |
| 28 | continue |
| 29 | } |
| 30 | if arg in ['-L', '-I', '-isystem'] { |
| 31 | previous = arg |
| 32 | } |
| 33 | flag := arg.all_before('/') |
| 34 | if flag in ['-L', '-I', '-isystem'] && arg.len > flag.len { |
| 35 | args << flag + target_search_path(root, flag, arg[flag.len..]) |
| 36 | } else { |
| 37 | args << arg |
| 38 | } |
| 39 | } |
| 40 | return args |
| 41 | } |
| 42 | |
| 43 | fn main() { |
| 44 | driver := os.base(os.args[0]) |
| 45 | root := os.getenv('VPAC_MAKEPKG_ROOT') |
| 46 | if !os.is_abs_path(root) { |
| 47 | eprintln('${driver} requires an absolute VPAC_MAKEPKG_ROOT') |
| 48 | exit(1) |
| 49 | } |
| 50 | specs := os.join_path(root, 'usr/lib/musl-gcc-sysroot.specs') |
| 51 | if !os.is_file(specs) { |
| 52 | eprintln('${driver}: missing installed musl specs at ${specs}') |
| 53 | exit(1) |
| 54 | } |
| 55 | prefix := os.getenv('CROSS_COMPILE') |
| 56 | compiler := if driver == 'vinix-c++' { |
| 57 | os.getenv_opt('VINIX_REAL_CXX') or { prefix + 'g++' } |
| 58 | } else { |
| 59 | os.getenv_opt('VINIX_REAL_CC') or { prefix + 'gcc' } |
| 60 | } |
| 61 | mut args := ['--sysroot=${root}', '-specs=${specs}'] |
| 62 | args << target_search_args(root, os.args[1..]) |
| 63 | if !os.args[1..].any(it in ['-c', '-E', '-S', '-M', '-MM', '-fsyntax-only', '-r']) { |
| 64 | // The build machine's linker also needs to find DT_NEEDED libraries |
| 65 | // named by input shared objects. Keep this path out of the target ELF. |
| 66 | args << ['-Xlinker', '-rpath-link', '-Xlinker', '${root}/usr/lib:${root}/lib'] |
| 67 | // The released AArch64 kernel maps 16 KiB pages. |
| 68 | if os.is_file(os.join_path(root, 'lib/ld-musl-aarch64.so.1')) { |
| 69 | args << '-Wl,-z,max-page-size=16384' |
| 70 | } |
| 71 | } |
| 72 | os.execvp(compiler, args) or { |
| 73 | eprintln('${driver}: ${compiler}: ${err}') |
| 74 | exit(1) |
| 75 | } |
| 76 | } |
| 77 | |