Teach clang to find a sysroot based on target in resource-dir

This is a RFC on something I wanted to add to the clang driver a while but not sure if there is something I am missing.

I maintain and distribute an internal toolchain based on clang/llvm. This toolchain is cross compile only, so I ship a sysroot for each target with clang today. But in order for clang to use the correct sysroot per target I have to pass -sysroot to clang in each invocation. To make this easier I have to add a shell script per target that sets the right sysroot and -target arguments.

When this is done clang actually finds the compiler-rt and libc++ by looking in the resource-dir for a directory with the target name.

I propose to expand this to also look for a sysroot under the resource-dir and use that if sysroot argument is not patched. This would allow me to just symlink clang to x86_64-vendor-linux-musl-clang and not need the wrappers at all.

Can anyone see any downsides to this or something I miss? Would be happy to write this patch if it’s something people would not mind.

I propose to expand this to also look for a sysroot under the resource-dir and use that if sysroot argument is not patched. This would allow me to just symlink clang to x86_64-vendor-linux-musl-clang and not need the wrappers at all.

You can do this already, without any changes to clang, by creating a config file. See https://clang.llvm.org/docs/UsersManual.html#configuration-files

@jyknight oh that’s handy! Thanks for pointing that out. I will have a closer look tomorrow!

GCC will use ${gcc_tooldir}/sys-root as the default sysroot unless overridden with --with-sysroot= during configuration, where ${gcc_tooldir} is effectively ${prefix}/${target}. While the name of the directory is a little unfortunate (sys-root vs sysroot), it’s an existing behavior that many GCC toolchains rely on and we may want to consider supporting it in Clang for compatibility. This would hopefully address your use case as well.