Martin Storsjö via llvm-dev
2020-Nov-06 22:48 UTC
[llvm-dev] Building an LLVM cross-compiler
On Fri, 6 Nov 2020, Cág via llvm-dev wrote:> The process, in my opinion, should go like this: > 1. Get the sources (llvm, lld, compiler-rt, libunwind, libcxx...). > 2. Build an LLVM cross-compiler toolchain using native distribution's > compiler (i.e. build an x86_64 clang executable that targets aarch64). > 3. Cross-compile libc and other libraries/dependencies to run the > userland. > 4. Cross-compile the userland. > > It stops after that. To build compiler-rt you need C headers, libc > runtimes, *and libclang_rt.a*. You can't cross-compile libc because you > don't have compiler-rt because you don't have libc. Chicken or egg.Yes, for such a full from-scratch setup, the procedure is a bit fiddly to get right, and exactly how to do it is very dependent on how configurable the lower level components of your target platform are for such bootstrap setups. I regularly build my toolchain that targets mingw, in exactly this way. First I build the compiler, then I install the target "libc"'s headers, then build the mingw equivalent of the libc. Here, the "libc" consists of import libraries (that are generated from symbol listings using llvm-dlltool) and static libraries, so libclang_rt isn't needed yet. (The autoconf scripts for these bits define AC_NO_EXECUTABLES, which avoids bailing out due to the compiler not being able to link an executable.) After this step, I build the compiler-rt builtins, and this is configured with -DCMAKE_C_COMPILER_WORKS=1 to (iirc) make it not bail out even though the toolchain still can't link anything. After building the compiler-rt builtins, and installing them, the toolchain finally is complete enough to link a working C program. After this stage, higher level runtimes like libc++ can be built on top, followed by other compiler-rt libraries. See https://github.com/mstorsjo/llvm-mingw for the full set of scripts I use to build my setup; built-all.sh is the toplevel script you can start following if you want to dig in. // Martin
Hello Martin, Martin Storsjö wrote:>> 1. Get the sources (llvm, lld, compiler-rt, libunwind, libcxx...). >> 2. Build an LLVM cross-compiler toolchain using native distribution's >> compiler (i.e. build an x86_64 clang executable that targets aarch64). >> 3. Cross-compile libc and other libraries/dependencies to run the >> userland. >> 4. Cross-compile the userland. > Yes, for such a full from-scratch setup, the procedure is a bit fiddly > to get right, and exactly how to do it is very dependent on how > configurable the lower level components of your target platform are > for such bootstrap setups. > > I regularly build my toolchain that targets mingw, in exactly this way. > First I build the compiler, then I install the target "libc"'s headers, > then build the mingw equivalent of the libc. > After this stage, higher level runtimes like libc++ can be built on top, > followed by other compiler-rt libraries. > See https://github.com/mstorsjo/llvm-mingw for the full set of scripts I > use to build my setup; built-all.sh is the toplevel script you can star > following if you want to dig in.I like this. I hope you don't mind if I borrow some of these ideas to play with. C_COMPILER_WORKS is something I've never heard of. Thank you! -- caóc