1# CrossWinToARMLinux.cmake 2# 3# Set up a CMakeCache for a cross Windows to ARM Linux toolchain build. 4# 5# This cache file can be used to build a cross toolchain to ARM Linux 6# on Windows platform. 7# 8# NOTE: the build requires a development ARM Linux root filesystem to use 9# proper target platform depended library and header files. 10# 11# The build generates a proper clang configuration file with stored 12# --sysroot argument for specified target triple. Also it is possible 13# to specify configuration path via CMake arguments, such as 14# -DCLANG_CONFIG_FILE_USER_DIR=<full-path-to-clang-configs> 15# and/or 16# -DCLANG_CONFIG_FILE_SYSTEM_DIR=<full-path-to-clang-configs> 17# 18# See more details here: https://clang.llvm.org/docs/UsersManual.html#configuration-files 19# 20# Configure: 21# cmake -G Ninja ^ 22# -DTOOLCHAIN_TARGET_TRIPLE=aarch64-unknown-linux-gnu ^ 23# -DTOOLCHAIN_TARGET_SYSROOTFS=<path-to-develop-arm-linux-root-fs> ^ 24# -DTOOLCHAIN_SHARED_LIBS=OFF ^ 25# -DCMAKE_INSTALL_PREFIX=../install ^ 26# -DCMAKE_CXX_FLAGS="-D__OPTIMIZE__" ^ 27# -DREMOTE_TEST_HOST="<hostname>" ^ 28# -DREMOTE_TEST_USER="<ssh_user_name>" ^ 29# -C<llvm_src_root>/llvm-project/clang/cmake/caches/CrossWinToARMLinux.cmake ^ 30# <llvm_src_root>/llvm-project/llvm 31# Build: 32# cmake --build . --target install 33# Tests: 34# cmake --build . --target check-llvm 35# cmake --build . --target check-clang 36# cmake --build . --target check-lld 37# cmake --build . --target check-compiler-rt-<TOOLCHAIN_TARGET_TRIPLE> 38# cmake --build . --target check-cxxabi-<TOOLCHAIN_TARGET_TRIPLE> 39# cmake --build . --target check-unwind-<TOOLCHAIN_TARGET_TRIPLE> 40# cmake --build . --target check-cxx-<TOOLCHAIN_TARGET_TRIPLE> 41# (another way to execute the tests) 42# python bin/llvm-lit.py -v --threads=32 runtimes/runtimes-<TOOLCHAIN_TARGET_TRIPLE>bins/libunwind/test 2>&1 | tee libunwind-tests.log 43# python bin/llvm-lit.py -v --threads=32 runtimes/runtimes-<TOOLCHAIN_TARGET_TRIPLE>-bins/libcxxabi/test 2>&1 | tee libcxxabi-tests.log 44# python bin/llvm-lit.py -v --threads=32 runtimes/runtimes-<TOOLCHAIN_TARGET_TRIPLE>-bins/libcxx/test 2>&1 | tee libcxx-tests.log 45 46 47# LLVM_PROJECT_DIR is the path to the llvm-project directory. 48# The right way to compute it would probably be to use "${CMAKE_SOURCE_DIR}/../", 49# but CMAKE_SOURCE_DIR is set to the wrong value on earlier CMake versions 50# that we still need to support (for instance, 3.10.2). 51get_filename_component(LLVM_PROJECT_DIR 52 "${CMAKE_CURRENT_LIST_DIR}/../../../" 53 ABSOLUTE) 54 55if (NOT DEFINED LLVM_ENABLE_ASSERTIONS) 56 set(LLVM_ENABLE_ASSERTIONS ON CACHE BOOL "") 57endif() 58if (NOT DEFINED LLVM_ENABLE_PROJECTS) 59 set(LLVM_ENABLE_PROJECTS "clang;clang-tools-extra;lld" CACHE STRING "") 60endif() 61if (NOT DEFINED LLVM_ENABLE_RUNTIMES) 62 set(LLVM_ENABLE_RUNTIMES "compiler-rt;libunwind;libcxxabi;libcxx" CACHE STRING "") 63endif() 64 65if (NOT DEFINED TOOLCHAIN_TARGET_TRIPLE) 66 set(TOOLCHAIN_TARGET_TRIPLE "aarch64-unknown-linux-gnu") 67else() 68 #NOTE: we must normalize specified target triple to a fully specified triple, 69 # including the vendor part. It is necessary to synchronize the runtime library 70 # installation path and operable target triple by Clang to get a correct runtime 71 # path through `-print-runtime-dir` Clang option. 72 string(REPLACE "-" ";" TOOLCHAIN_TARGET_TRIPLE "${TOOLCHAIN_TARGET_TRIPLE}") 73 list(LENGTH TOOLCHAIN_TARGET_TRIPLE TOOLCHAIN_TARGET_TRIPLE_LEN) 74 if (TOOLCHAIN_TARGET_TRIPLE_LEN LESS 3) 75 message(FATAL_ERROR "invalid target triple") 76 endif() 77 # We suppose missed vendor's part. 78 if (TOOLCHAIN_TARGET_TRIPLE_LEN LESS 4) 79 list(INSERT TOOLCHAIN_TARGET_TRIPLE 1 "unknown") 80 endif() 81 string(REPLACE ";" "-" TOOLCHAIN_TARGET_TRIPLE "${TOOLCHAIN_TARGET_TRIPLE}") 82endif() 83 84message(STATUS "Toolchain target triple: ${TOOLCHAIN_TARGET_TRIPLE}") 85 86if (DEFINED TOOLCHAIN_TARGET_SYSROOTFS) 87 message(STATUS "Toolchain target sysroot: ${TOOLCHAIN_TARGET_SYSROOTFS}") 88 # Store the --sysroot argument for the compiler-rt test flags. 89 set(sysroot_flags --sysroot='${TOOLCHAIN_TARGET_SYSROOTFS}') 90 # Generate the clang configuration file for the specified target triple 91 # and store --sysroot in this file. 92 file(WRITE "${CMAKE_BINARY_DIR}/bin/${TOOLCHAIN_TARGET_TRIPLE}.cfg" ${sysroot_flags}) 93endif() 94 95# Build the shared libraries for libc++/libc++abi/libunwind. 96if (NOT DEFINED TOOLCHAIN_SHARED_LIBS) 97 set(TOOLCHAIN_SHARED_LIBS OFF) 98endif() 99# Enable usage of the static libunwind and libc++abi libraries. 100if (NOT DEFINED TOOLCHAIN_USE_STATIC_LIBS) 101 set(TOOLCHAIN_USE_STATIC_LIBS ON) 102endif() 103 104if (NOT DEFINED LLVM_TARGETS_TO_BUILD) 105 if ("${TOOLCHAIN_TARGET_TRIPLE}" MATCHES "^(armv|arm32)+") 106 set(LLVM_TARGETS_TO_BUILD "ARM" CACHE STRING "") 107 endif() 108 if ("${TOOLCHAIN_TARGET_TRIPLE}" MATCHES "^(aarch64|arm64)+") 109 set(LLVM_TARGETS_TO_BUILD "AArch64" CACHE STRING "") 110 endif() 111endif() 112 113message(STATUS "Toolchain target to build: ${LLVM_TARGETS_TO_BUILD}") 114 115# Allow to override libc++ ABI version (1 is default). 116if (NOT DEFINED LIBCXX_ABI_VERSION) 117 set(LIBCXX_ABI_VERSION 1) 118endif() 119 120message(STATUS "Toolchain's Libc++ ABI version: ${LIBCXX_ABI_VERSION}") 121 122if (NOT DEFINED CMAKE_BUILD_TYPE) 123 set(CMAKE_BUILD_TYPE "Release" CACHE STRING "") 124endif() 125 126set(CMAKE_CL_SHOWINCLUDES_PREFIX "Note: including file: " CACHE STRING "") 127# Required if COMPILER_RT_DEFAULT_TARGET_ONLY is ON 128set(CMAKE_C_COMPILER_TARGET "${TOOLCHAIN_TARGET_TRIPLE}" CACHE STRING "") 129set(CMAKE_CXX_COMPILER_TARGET "${TOOLCHAIN_TARGET_TRIPLE}" CACHE STRING "") 130 131set(LLVM_DEFAULT_TARGET_TRIPLE "${TOOLCHAIN_TARGET_TRIPLE}" CACHE STRING "") 132set(LLVM_TARGET_ARCH "${TOOLCHAIN_TARGET_TRIPLE}" CACHE STRING "") 133set(LLVM_LIT_ARGS "-vv ${LLVM_LIT_ARGS}" CACHE STRING "" FORCE) 134 135set(CLANG_DEFAULT_CXX_STDLIB "libc++" CACHE STRING "") 136set(CLANG_DEFAULT_LINKER "lld" CACHE STRING "") 137set(CLANG_DEFAULT_OBJCOPY "llvm-objcopy" CACHE STRING "") 138set(CLANG_DEFAULT_RTLIB "compiler-rt" CACHE STRING "") 139set(CLANG_DEFAULT_UNWINDLIB "libunwind" CACHE STRING "") 140 141if (NOT DEFINED CMAKE_MSVC_RUNTIME_LIBRARY AND WIN32) 142 #Note: Always specify MT DLL for the LLDB build configurations on Windows host. 143 if (CMAKE_BUILD_TYPE STREQUAL "Debug") 144 set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDebugDLL" CACHE STRING "") 145 else() 146 set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDLL" CACHE STRING "") 147 endif() 148 # Grab all ucrt/vcruntime related DLLs into the binary installation folder. 149 set(CMAKE_INSTALL_UCRT_LIBRARIES ON CACHE BOOL "") 150endif() 151 152# Set up RPATH for the target runtime/builtin libraries. 153# See some details here: https://reviews.llvm.org/D91099 154if (NOT DEFINED RUNTIMES_INSTALL_RPATH) 155 set(RUNTIMES_INSTALL_RPATH "\$ORIGIN/../lib;${CMAKE_INSTALL_PREFIX}/lib") 156endif() 157 158set(LLVM_BUILTIN_TARGETS "${TOOLCHAIN_TARGET_TRIPLE}" CACHE STRING "") 159 160set(BUILTINS_${TOOLCHAIN_TARGET_TRIPLE}_CMAKE_SYSTEM_NAME "Linux" CACHE STRING "") 161set(BUILTINS_${TOOLCHAIN_TARGET_TRIPLE}_CMAKE_INSTALL_RPATH "${RUNTIMES_INSTALL_RPATH}" CACHE STRING "") 162set(BUILTINS_${TOOLCHAIN_TARGET_TRIPLE}_CMAKE_BUILD_WITH_INSTALL_RPATH ON CACHE BOOL "") 163set(BUILTINS_${TOOLCHAIN_TARGET_TRIPLE}_LLVM_CMAKE_DIR "${LLVM_PROJECT_DIR}/llvm/cmake/modules" CACHE PATH "") 164 165if (DEFINED TOOLCHAIN_TARGET_COMPILER_FLAGS) 166 foreach(lang C;CXX;ASM) 167 set(BUILTINS_${TOOLCHAIN_TARGET_TRIPLE}_CMAKE_${lang}_FLAGS "${TOOLCHAIN_TARGET_COMPILER_FLAGS}" CACHE STRING "") 168 endforeach() 169endif() 170foreach(type SHARED;MODULE;EXE) 171 set(BUILTINS_${TOOLCHAIN_TARGET_TRIPLE}_CMAKE_${type}_LINKER_FLAGS "-fuse-ld=lld" CACHE STRING "") 172endforeach() 173 174set(LLVM_RUNTIME_TARGETS "${TOOLCHAIN_TARGET_TRIPLE}" CACHE STRING "") 175set(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR ON CACHE BOOL "") 176 177set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LLVM_ENABLE_RUNTIMES "${LLVM_ENABLE_RUNTIMES}" CACHE STRING "") 178 179set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_CMAKE_SYSTEM_NAME "Linux" CACHE STRING "") 180set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_CMAKE_INSTALL_RPATH "${RUNTIMES_INSTALL_RPATH}" CACHE STRING "") 181set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_CMAKE_BUILD_WITH_INSTALL_RPATH ON CACHE BOOL "") 182 183if (DEFINED TOOLCHAIN_TARGET_COMPILER_FLAGS) 184 foreach(lang C;CXX;ASM) 185 set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_CMAKE_${lang}_FLAGS "${TOOLCHAIN_TARGET_COMPILER_FLAGS}" CACHE STRING "") 186 endforeach() 187endif() 188foreach(type SHARED;MODULE;EXE) 189 set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_CMAKE_${type}_LINKER_FLAGS "-fuse-ld=lld" CACHE STRING "") 190endforeach() 191 192set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_COMPILER_RT_BUILD_BUILTINS ON CACHE BOOL "") 193set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_COMPILER_RT_BUILD_SANITIZERS OFF CACHE BOOL "") 194set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_COMPILER_RT_BUILD_XRAY OFF CACHE BOOL "") 195set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_COMPILER_RT_BUILD_LIBFUZZER OFF CACHE BOOL "") 196set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_COMPILER_RT_BUILD_PROFILE OFF CACHE BOOL "") 197set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_COMPILER_RT_BUILD_CRT ON CACHE BOOL "") 198set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_COMPILER_RT_BUILD_ORC OFF CACHE BOOL "") 199set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_COMPILER_RT_DEFAULT_TARGET_ONLY ON CACHE BOOL "") 200set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_COMPILER_RT_INCLUDE_TESTS ON CACHE BOOL "") 201set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_COMPILER_RT_CAN_EXECUTE_TESTS ON CACHE BOOL "") 202 203set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_COMPILER_RT_USE_BUILTINS_LIBRARY ON CACHE BOOL "") 204set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_COMPILER_RT_CXX_LIBRARY libcxx CACHE STRING "") 205# The compiler-rt tests disable the clang configuration files during the execution by setting CLANG_NO_DEFAULT_CONFIG=1 206# and drops out the --sysroot from there. Provide it explicity via the test flags here if target sysroot has been specified. 207set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_COMPILER_RT_TEST_COMPILER_CFLAGS "--stdlib=libc++ ${sysroot_flags}" CACHE STRING "") 208 209set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBUNWIND_USE_COMPILER_RT ON CACHE BOOL "") 210set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBUNWIND_ENABLE_SHARED ${TOOLCHAIN_SHARED_LIBS} CACHE BOOL "") 211 212set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBCXXABI_USE_LLVM_UNWINDER ON CACHE BOOL "") 213set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBCXXABI_ENABLE_STATIC_UNWINDER ${TOOLCHAIN_USE_STATIC_LIBS} CACHE BOOL "") 214set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBCXXABI_USE_COMPILER_RT ON CACHE BOOL "") 215set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBCXXABI_ENABLE_NEW_DELETE_DEFINITIONS OFF CACHE BOOL "") 216set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBCXXABI_ENABLE_SHARED ${TOOLCHAIN_SHARED_LIBS} CACHE BOOL "") 217 218set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBCXX_USE_COMPILER_RT ON CACHE BOOL "") 219set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBCXX_ENABLE_SHARED ${TOOLCHAIN_SHARED_LIBS} CACHE BOOL "") 220set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBCXX_ABI_VERSION ${LIBCXX_ABI_VERSION} CACHE STRING "") 221set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBCXX_CXX_ABI "libcxxabi" CACHE STRING "") #!!! 222set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS ON CACHE BOOL "") 223# Merge libc++ and libc++abi libraries into the single libc++ library file. 224set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBCXX_ENABLE_STATIC_ABI_LIBRARY ${TOOLCHAIN_USE_STATIC_LIBS} CACHE BOOL "") 225# Forcely disable the libc++ benchmarks on Windows build hosts 226# (current benchmark test configuration does not support the cross builds there). 227if (WIN32) 228 set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBCXX_INCLUDE_BENCHMARKS OFF CACHE BOOL "") 229endif(WIN32) 230 231# Avoid searching for the python3 interpreter during the runtimes configuration for the cross builds. 232# It starts searching the python3 package using the target's sysroot path, that usually is not compatible with the build host. 233find_package(Python3 COMPONENTS Interpreter) 234set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_Python3_EXECUTABLE ${Python3_EXECUTABLE} CACHE PATH "") 235 236set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBUNWIND_TEST_PARAMS_default "${RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_TEST_PARAMS}") 237set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBCXXABI_TEST_PARAMS_default "${RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_TEST_PARAMS}") 238set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBCXX_TEST_PARAMS_default "${RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_TEST_PARAMS}") 239 240# Remote test configuration. 241if(DEFINED REMOTE_TEST_HOST) 242 # Allow override with the custom values. 243 if(NOT DEFINED DEFAULT_TEST_EXECUTOR) 244 set(DEFAULT_TEST_EXECUTOR "\\\"${Python3_EXECUTABLE}\\\" \\\"${LLVM_PROJECT_DIR}/libcxx/utils/ssh.py\\\" --host=${REMOTE_TEST_USER}@${REMOTE_TEST_HOST}") 245 endif() 246 247 set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_COMPILER_RT_EMULATOR 248 "\\\"${Python3_EXECUTABLE}\\\" \\\"${LLVM_PROJECT_DIR}/llvm/utils/remote-exec.py\\\" --host=${REMOTE_TEST_USER}@${REMOTE_TEST_HOST}" 249 CACHE STRING "") 250 251 list(APPEND RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBUNWIND_TEST_PARAMS_default "executor=${DEFAULT_TEST_EXECUTOR}") 252 list(APPEND RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBCXXABI_TEST_PARAMS_default "executor=${DEFAULT_TEST_EXECUTOR}") 253 list(APPEND RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBCXX_TEST_PARAMS_default "executor=${DEFAULT_TEST_EXECUTOR}") 254endif() 255 256set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBUNWIND_TEST_PARAMS "${RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBUNWIND_TEST_PARAMS_default}" CACHE INTERNAL "") 257set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBCXXABI_TEST_PARAMS "${RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBCXXABI_TEST_PARAMS_default}" CACHE INTERNAL "") 258set(RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBCXX_TEST_PARAMS "${RUNTIMES_${TOOLCHAIN_TARGET_TRIPLE}_LIBCXX_TEST_PARAMS_default}" CACHE INTERNAL "") 259 260set(LLVM_INSTALL_TOOLCHAIN_ONLY ON CACHE BOOL "") 261set(LLVM_TOOLCHAIN_TOOLS 262 llvm-ar 263 llvm-cov 264 llvm-cxxfilt 265 llvm-dwarfdump 266 llvm-lib 267 llvm-nm 268 llvm-objdump 269 llvm-pdbutil 270 llvm-profdata 271 llvm-ranlib 272 llvm-readobj 273 llvm-size 274 llvm-symbolizer 275 CACHE STRING "") 276 277set(LLVM_DISTRIBUTION_COMPONENTS 278 clang 279 lld 280 LTO 281 clang-format 282 builtins 283 runtimes 284 ${LLVM_TOOLCHAIN_TOOLS} 285 CACHE STRING "") 286