GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing llvm.org — How to cross-compile Clang/LLVM using Clang/LLVM - LLVM How to cross-compile Clang/LLVM using Clang/LLVM#Introduction#This document contains information about building LLVM and Clang on a host machine, targeting another platform. For more information on how to use Clang as a cross-compiler, please check https://clang.llvm.org/docs/CrossCompilation.html. This document describes cross-building a compiler in a single stage, using an
existing Note These instructions have been tested for targeting 32-bit ARM, AArch64, or 64-bit RISC-V from an x86_64 Linux host. But should be equally applicable to any other target. Setting up a sysroot#You will need a sysroot that contains essential build dependencies compiled for the target architecture. In this case, we will be using CMake and Ninja on a Linux host and compiling against a Debian sysroot. Detailed instructions on producing sysroots are outside of the scope of this documentation, but the following instructions should work on any Linux distribution with these pre-requisites:
The following snippet will initialise sysroots for 32-bit Arm, AArch64, and 64-bit RISC-V (just pick the target(s) you are interested in): sudo debootstrap --arch=armhf --variant=minbase --include=build-essential,symlinks stable sysroot-deb-armhf-stable sudo debootstrap --arch=arm64 --variant=minbase --include=build-essential,symlinks stable sysroot-deb-arm64-stable sudo debootstrap --arch=riscv64 --variant=minbase --include=build-essential,symlinks unstable sysroot-deb-riscv64-unstable The created sysroot may contain absolute symlinks, which will resolve to a location within the host when accessed during compilation, so we must convert any absolute symlinks to relative ones: sudo chroot sysroot-of-your-choice symlinks -cr . Configuring CMake and building#For more information on how to configure CMake for LLVM/Clang, see Building LLVM with CMake. Following CMake’s recommended practice, we will create a toolchain file. The following assumes you have a system install of First, set variables in your shell session that will be used throughout the build instructions: SYSROOT=$HOME/sysroot-deb-arm64-stable TARGET=aarch64-linux-gnu CFLAGS="" To customise details of the compilation target or choose a different
architecture altogether, change the Then execute the following snippet to create a toolchain file: cat - <<EOF > $TARGET-clang.cmake set(CMAKE_SYSTEM_NAME Linux) set(CMAKE_SYSROOT "$SYSROOT") set(CMAKE_C_COMPILER_TARGET $TARGET) set(CMAKE_CXX_COMPILER_TARGET $TARGET) set(CMAKE_C_FLAGS_INIT "$CFLAGS") set(CMAKE_CXX_FLAGS_INIT "$CFLAGS") set(CMAKE_LINKER_TYPE LLD) set(CMAKE_C_COMPILER clang) set(CMAKE_CXX_COMPILER clang++) set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY) EOF Then configure and build by invoking cmake -G Ninja \ -DCMAKE_BUILD_TYPE=Release \ -DLLVM_ENABLE_PROJECTS="lld;clang" \ -DCMAKE_TOOLCHAIN_FILE=$(pwd)/$TARGET-clang.cmake \ -DLLVM_HOST_TRIPLE=$TARGET \ -DCMAKE_INSTALL_PREFIX=$HOME/clang-$TARGET \ -S llvm \ -B build/$TARGET cmake --build build/$TARGET These options from the toolchain file and
See LLVM’s build documentation for more
guidance on CMake variables (e.g. Working around a ninja dependency issue#If you followed the instructions above to create a sysroot, you may run into a longstanding problem related to path canonicalization in ninja. GCC canonicalizes system headers in dependency files, so when ninja reads them it does not need to do so. Clang does not do this, and unfortunately ninja does not implement the canonicalization logic at all, meaning for some system headers with symlinks in the paths, it can incorrectly compute a non-existing path and consider it as always modified. If you are suffering from this issue, you will find any attempt at an
incremental build (including the suggested command to build the A workaround is to create a symlink so that the incorrect
sudo ln -s usr/include $SYSROOT/include Testing the just-built compiler#Confirm the $ file -L ./build/aarch64-linux-gnu/bin/clang ./build/aarch64-linux-gnu/bin/clang: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, for GNU/Linux 3.7.0, BuildID[sha1]=516b8b366a790fcd3563bee4aec0cdfcb90bb1c7, not stripped If you have $ qemu-aarch64-static -L $SYSROOT ./build/aarch64-linux-gnu/bin/clang --version clang version 21.0.0git (https://github.com/llvm/llvm-project cedfdc6e889c5c614a953ed1f44bcb45a405f8da) Target: aarch64-unknown-linux-gnu Thread model: posix InstalledDir: /home/asb/llvm-project/build/aarch64-linux-gnu/bin Or, if binfmt_misc is configured (as was necessary for debootstrap): $ export QEMU_LD_PREFIX=$SYSROOT; ./build/aarch64-linux-gnu/bin/clang --version clang version 21.0.0git (https://github.com/llvm/llvm-project cedfdc6e889c5c614a953ed1f44bcb45a405f8da) Target: aarch64-unknown-linux-gnu Thread model: posix InstalledDir: /home/asb/llvm-project/build/aarch64-linux-gnu/bin Installing and using#Note Use of the If you want to transfer a copy of the built compiler to another machine, you can first install it to a location on the host via: cmake --build build/$TARGET --target=install This will install the LLVM/Clang headers, binaries, libraries, and other files
to paths within tar -czvf clang-$TARGET.tar.gz -C $HOME clang-$TARGET The generated toolchain is portable, but requires compatible versions of any
shared libraries it links against. This means using a sysroot that is as
similar to your target operating system as possible is desirable. Other CMake
variables
may be helpful, for instance |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |