Correct CMAKE parameters for building clang and lld for riscv

Hello,

I’ve found a few very helpful posts regarding building clang and lld for riscv e.g. build-clang-correctly-for-target-riscv64-unknown-linux-gnu.

I’ve been able to build successfully for the target, but this has required specifying “–sysroot=$PATH” and “–gcc-toolchain=$PATH” when I invoke clang later.

I’d really like to not have to pass these arguments when invoking clang, so I looked at quite a few cmake configurations on llvm discourse and around the web.

However, there seems to be some inconsistency in the precise paths that need to be specified for both “-DGCC_INSTALL_PREFIX” and “-DDEFAULT_SYSROOT”. I have of course installed the RISC-V Linux GNU toolchain, hence the successful builds when specifying the extra arguments to clang.

Is there a up-to-date guide somewhere with the canonical cmake arguments to complete the build, so that i do not have to pass “–sysroot” and “–gcc-toolchain” when i invoke clang?

My apologies if there is an obvious and clear guide somewhere, if so I must work on my searching skills.

Thank you for any assistance.

1 Like

IIRC if the sysroot is installed at /usr/<target triple> (which is the case if you install via apt install or something similar) the driver should able to find it out of the box. Though providing the right triple is the key, for instance this won’t work for baremetal target.

If you want to use a custom sysroot path, theoretically DEFAULT_SYSROOT should work and supersede GCC_INSTALL_PREFIX.

Compiler driver and cross compilation | MaskRay covers the information you need, but it can probably be reorganized.

The --sysroot= behavior is very convenient. Say, you have a Debian rootfs /tmp/debian (with {,usr/}lib/gcc-cross/powerpc64le-linux-gnu ), or a prebuilt GCC with glibc system headers, you can specify clang++ --sysroot=/tmp/debian --target=powerpc64le-linux-gnu . It should just work.

In general, --gcc-toolchain= is discouraged. Just use --sysroot=, which Clang uses to find libc and GCC lib/include directories.

DEFAULT_SYSROOT and GCC_INSTALL_PREFIX should be considered as deprecated.
Replace them with a Clang configuration file. Clang in Gentoo now sets default runtimes via config file – Michał Górny.

I’ll send a patch to deprecate the two CMake variables.

2 Likes