llvm cross compilation issue

I am trying to compile llvm using a toolchain file where I have
specified the target system and compilers as suggested at
https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compilation-tool
chain:

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_C_COMPILER arm-unknown-linux-gnueabi-gcc)
set(CMAKE_CXX_COMPILER arm-unknown-linux-gnueabi-g++)

But the problem is that native tools like llvm-config, llvm-tblgen and
clang-tblgen, which must be built by the native compiler, are also
created by the target compiler.

What cmake / "toolchain file" options should I use to use the native
(host) compiler for these native tools (which are placed in the NATIVE
subdirectory), and target compilers for other binaries and libraries?

Hello,

There might be a smarter way - but I just build the tree twice:

mkdir native ; cd native ; cmake -GNinja .. ; ninja
mkdir target ; cd target ; cmake -GNinja
-DCMAKE_TOOLCHAIN_FILE=cross.cmake
-DLLVM_TABLEGEN_PATH=../native/bin/llvm-tblgen ..

I know there are runtime builds as well - but I am not familiar with it.

-- Tobias

But besides the fact that building llvm is very time-consuming, there
is another serious problem with this method: llvm-config built in this
way will return incorrect results that are related to the host, not
the target.

You only need to build the tools you need from native - so run

“ninja llvm-tblgen clang-tblgen llvm-config”

No idea about how to get a native llvm-config return targets config - never used that tool, hopefully someone else knows.

You need to pass -DCROSS_TOOLCHAIN_FLAGS_NATIVE="" during the cross build, to fix your build of the llvm-config/llvm-tblgen/clang-tblgen native tools.

The reason for this is that if CROSS_TOOLCHAIN_FLAGS_NATIVE isn't set, it's default initialized to pass on your CMAKE_CXX_COMPILER to the native build process as well, which won't do the right thing in this kind of cross setup.

// Martin