From 97ccd145ea5b8c4a48f1e086f1c1abe16462cb1d Mon Sep 17 00:00:00 2001 From: David Spickett Date: Thu, 31 Jul 2025 14:53:24 +0100 Subject: [PATCH 1/3] [llvm][docs] Update CMake commands for cross compiling Arm builtins This does a few things: * LLVM_CONFIG_PATH is deprecated, use LLVM_CMAKE_DIR instead. * Don't use $ before command examples. I would normally, but the key cmake commands didn't use it so I removed it from all commands. * Makes the commands shown full commands, so you don't have to piece them together. * Uses shell variables to cut down on repetition and make this easier to port to other targets. * Adds a few options to disable more compiler-rt things. * Include test options in the first cmake command, so you don't have to re-do the whole thing after you read the testing section. * Removes the section about using BaremetalARM.cmake. The closest I got to getting that cache to work was: ``` SYSROOT=/home/david.spickett/arm-gnu-toolchain-14.3.rel1-x86_64-arm-none-eabi/arm-none-eabi/libc LLVM_TOOLCHAIN=/home/david.spickett/LLVM-20.1.8-Linux-X64/ cmake \ -G Ninja \ -DCMAKE_C_COMPILER=${LLVM_TOOLCHAIN}/bin/clang \ -DBAREMETAL_ARMV6M_SYSROOT=${SYSROOT} \ -DBAREMETAL_ARMV7M_SYSROOT=${SYSROOT} \ -DBAREMETAL_ARMV7EM_SYSROOT=${SYSROOT} \ -DCMAKE_BUILD_TYPE=Release \ -DLLVM_ENABLE_RUNTIMES="compiler-rt" \ -C ../llvm-project/clang/cmake/caches/BaremetalARM.cmake \ -DCOMPILER_RT_BUILD_BUILTINS=ON \ -DCOMPILER_RT_BUILD_LIBFUZZER=OFF \ -DCOMPILER_RT_BUILD_MEMPROF=OFF \ -DCOMPILER_RT_BUILD_PROFILE=OFF \ -DCOMPILER_RT_BUILD_CTX_PROFILE=OFF \ -DCOMPILER_RT_BUILD_SANITIZERS=OFF \ -DCOMPILER_RT_BUILD_XRAY=OFF \ -DCOMPILER_RT_BUILD_ORC=OFF \ -DCOMPILER_RT_BUILD_CRT=OFF \ ../llvm-project/runtimes ``` All this does is build the x86 builtins. I tried forcing the issue with: ``` -DBUILTIN_SUPPORTED_ARCH="armv7m;armv6m;armv7em" \ ``` But again, just x86. It's probably something deep in compiler-rt failing a compiler check for the Arm targets. Even if that's the case, fixing that means adding more options to the cmake command. I can't find evidence of a full command using this cache file since the commit that introduced it and that command no longer works. I think if you ever got this to work again the command would be as long and complex as the ones already shown in the document. I would also argue that some of the other caches, for example Fuschia's, are much better example of multi-target runtimes builds. If what's in this document isn't enough, folks should be learning from those files and about the runtimes build overall before attempting anything complex (though it does not take much to be "complex"). --- llvm/docs/HowToCrossCompileBuiltinsOnArm.rst | 223 +++++++++++-------- 1 file changed, 124 insertions(+), 99 deletions(-) diff --git a/llvm/docs/HowToCrossCompileBuiltinsOnArm.rst b/llvm/docs/HowToCrossCompileBuiltinsOnArm.rst index 31ead45d052fe..43f8d970c801c 100644 --- a/llvm/docs/HowToCrossCompileBuiltinsOnArm.rst +++ b/llvm/docs/HowToCrossCompileBuiltinsOnArm.rst @@ -25,9 +25,9 @@ using as many of the LLVM tools as we can, but it is possible to use GNU equivalents. You will need: - * A build of LLVM for the llvm-tools and ``llvm-config``. + * A build of LLVM for the llvm-tools and LLVM CMake files. * A clang executable with support for the ``ARM`` target. - * compiler-rt sources. + * ``compiler-rt`` sources. * The ``qemu-arm`` user mode emulator. * An ``arm-linux-gnueabihf`` sysroot. @@ -52,78 +52,94 @@ toolchain from https://developer.arm.com/open-source/gnu-toolchain/gnu-a/downloa Building compiler-rt builtins for Arm ===================================== -We will be doing a standalone build of compiler-rt using the following cmake -options:: +We will be doing a standalone build of compiler-rt. The command is shown below. +Shell variables are used to simplify some of the options:: - cmake path/to/compiler-rt \ + LLVM_TOOLCHAIN=/ + TARGET_TRIPLE=arm-none-linux-gnueabihf + GCC_TOOLCHAIN= + SYSROOT=${GCC_TOOLCHAIN}/${TARGET_TRIPLE}/libc + COMPILE_FLAGS="-march=armv7-a" + + cmake ../llvm-project/compiler-rt \ -G Ninja \ - -DCMAKE_AR=/path/to/llvm-ar \ - -DCMAKE_ASM_COMPILER_TARGET="arm-linux-gnueabihf" \ - -DCMAKE_ASM_FLAGS="build-c-flags" \ - -DCMAKE_C_COMPILER=/path/to/clang \ - -DCMAKE_C_COMPILER_TARGET="arm-linux-gnueabihf" \ - -DCMAKE_C_FLAGS="build-c-flags" \ + -DCMAKE_AR=${LLVM_TOOLCHAIN}/bin/llvm-ar \ + -DCMAKE_NM=${LLVM_TOOLCHAIN}/bin/llvm-nm \ + -DCMAKE_RANLIB=${LLVM_TOOLCHAIN}/bin/llvm-ranlib \ + -DLLVM_CMAKE_DIR="${LLVM_TOOLCHAIN}/lib/cmake/llvm" \ + -DCMAKE_SYSROOT="${SYSROOT}" \ + -DCMAKE_ASM_COMPILER_TARGET="${TARGET_TRIPLE}" \ + -DCMAKE_ASM_FLAGS="${COMPILE_FLAGS}" \ + -DCMAKE_C_COMPILER_TARGET="${TARGET_TRIPLE}" \ + -DCMAKE_C_COMPILER_EXTERNAL_TOOLCHAIN=${GCC_TOOLCHAIN} \ + -DCMAKE_C_COMPILER=${LLVM_TOOLCHAIN}/bin/clang \ + -DCMAKE_C_FLAGS="${COMPILE_FLAGS}" \ + -DCMAKE_CXX_COMPILER_TARGET="${TARGET_TRIPLE}" \ + -DCMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN=${GCC_TOOLCHAIN} \ + -DCMAKE_CXX_COMPILER=${LLVM_TOOLCHAIN}/bin/clang \ + -DCMAKE_CXX_FLAGS="${COMPILE_FLAGS}" \ -DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=lld" \ - -DCMAKE_NM=/path/to/llvm-nm \ - -DCMAKE_RANLIB=/path/to/llvm-ranlib \ -DCOMPILER_RT_BUILD_BUILTINS=ON \ -DCOMPILER_RT_BUILD_LIBFUZZER=OFF \ -DCOMPILER_RT_BUILD_MEMPROF=OFF \ -DCOMPILER_RT_BUILD_PROFILE=OFF \ + -DCOMPILER_RT_BUILD_CTX_PROFILE=OFF \ -DCOMPILER_RT_BUILD_SANITIZERS=OFF \ -DCOMPILER_RT_BUILD_XRAY=OFF \ + -DCOMPILER_RT_BUILD_ORC=OFF \ + -DCOMPILER_RT_BUILD_CRT=OFF \ -DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON \ - -DLLVM_CONFIG_PATH=/path/to/llvm-config + -DCOMPILER_RT_EMULATOR="qemu-arm -L ${SYSROOT}" \ + -DCOMPILER_RT_INCLUDE_TESTS=ON \ + -DCOMPILER_RT_TEST_COMPILER=${LLVM_TOOLCHAIN}/bin/clang \ + -DCOMPILER_RT_TEST_COMPILER_CFLAGS="--target=${TARGET_TRIPLE} ${COMPILE_FLAGS} --gcc-toolchain=${GCC_TOOLCHAIN} --sysroot=${SYSROOT} -fuse-ld=lld" -The ``build-c-flags`` need to be sufficient to pass the C-make compiler check, -compile compiler-rt, and if you are running the tests, compile and link the -tests. When cross-compiling with clang we will need to pass sufficient -information to generate code for the Arm architecture we are targeting. +.. note:: + The command above also enables tests. Enabling tests is not required, more details + in the testing section. -We will need to select: - * The Arm target and Armv7-A architecture with ``--target=arm-linux-gnueabihf -march=armv7a``. - * Whether to generate Arm (the default) or Thumb instructions (``-mthumb``). +``CMAKE__