From 7f6e32fff6703b8fc694b8cfa996611cd8839152 Mon Sep 17 00:00:00 2001 From: Petr Hosek Date: Mon, 8 Jan 2018 23:50:59 +0000 Subject: [PATCH] [CMake] Support for cross-compilation when build runtimes When cross-compiling, we cannot use the just built toolchain, instead we need to use the host toolchain which we assume has a support for targeting the selected target platform. We also need to pass the path to the native version of llvm-config to external projects. Differential Revision: https://reviews.llvm.org/D41678 llvm-svn: 322046 --- cmake/modules/CrossCompile.cmake | 2 ++ cmake/modules/LLVMExternalProjectUtils.cmake | 14 ++++++++++++-- runtimes/CMakeLists.txt | 3 +++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/cmake/modules/CrossCompile.cmake b/cmake/modules/CrossCompile.cmake index c0c06c83357..b239816c825 100644 --- a/cmake/modules/CrossCompile.cmake +++ b/cmake/modules/CrossCompile.cmake @@ -50,6 +50,8 @@ function(llvm_create_cross_target_internal target_name toolchain buildtype) -DLLVM_TARGET_IS_CROSSCOMPILE_HOST=TRUE -DLLVM_TARGETS_TO_BUILD="${targets_to_build_arg}" -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD="${experimental_targets_to_build_arg}" + -DLLVM_DEFAULT_TARGET_TRIPLE="${TARGET_TRIPLE}" + -DLLVM_TARGET_ARCH="${LLVM_TARGET_ARCH}" ${build_type_flags} ${linker_flag} ${external_clang_dir} WORKING_DIRECTORY ${LLVM_${target_name}_BUILD} DEPENDS CREATE_LLVM_${target_name} diff --git a/cmake/modules/LLVMExternalProjectUtils.cmake b/cmake/modules/LLVMExternalProjectUtils.cmake index b84ebbb5387..65fbaa40444 100644 --- a/cmake/modules/LLVMExternalProjectUtils.cmake +++ b/cmake/modules/LLVMExternalProjectUtils.cmake @@ -102,7 +102,7 @@ function(llvm_ExternalProject_Add name source_dir) endforeach() endforeach() - if(ARG_USE_TOOLCHAIN) + if(ARG_USE_TOOLCHAIN AND NOT CMAKE_CROSSCOMPILING) if(CLANG_IN_TOOLCHAIN) set(compiler_args -DCMAKE_C_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang -DCMAKE_CXX_COMPILER=${LLVM_RUNTIME_OUTPUT_INTDIR}/clang++) @@ -136,6 +136,16 @@ function(llvm_ExternalProject_Add name source_dir) set(sysroot_arg -DCMAKE_SYSROOT=${CMAKE_SYSROOT}) endif() + if(CMAKE_CROSSCOMPILING) + set(compiler_args -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} + -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} + -DCMAKE_AR=${CMAKE_AR} + -DCMAKE_RANLIB=${CMAKE_RANLIB}) + set(llvm_config_path "${LLVM_NATIVE_BUILD}/bin/llvm-config") + else() + set(llvm_config_path "$") + endif() + ExternalProject_Add(${name} DEPENDS ${ARG_DEPENDS} llvm-config ${name}-clobber @@ -149,7 +159,7 @@ function(llvm_ExternalProject_Add name source_dir) -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX} ${sysroot_arg} -DLLVM_BINARY_DIR=${PROJECT_BINARY_DIR} - -DLLVM_CONFIG_PATH=$ + -DLLVM_CONFIG_PATH=${llvm_config_path} -DLLVM_ENABLE_WERROR=${LLVM_ENABLE_WERROR} -DLLVM_HOST_TRIPLE=${LLVM_HOST_TRIPLE} -DLLVM_HAVE_LINK_VERSION_SCRIPT=${LLVM_HAVE_LINK_VERSION_SCRIPT} diff --git a/runtimes/CMakeLists.txt b/runtimes/CMakeLists.txt index 37939658168..c020b851bb9 100644 --- a/runtimes/CMakeLists.txt +++ b/runtimes/CMakeLists.txt @@ -354,6 +354,9 @@ else() # if this is included from LLVM's CMake CMAKE_ARGS -DCOMPILER_RT_BUILD_BUILTINS=Off -DLLVM_INCLUDE_TESTS=${LLVM_INCLUDE_TESTS} -DLLVM_LIBRARY_DIR=${LLVM_LIBRARY_DIR} + -DCMAKE_C_COMPILER_TARGET=${TARGET_TRIPLE} + -DCMAKE_CXX_COMPILER_TARGET=${TARGET_TRIPLE} + -DCMAKE_ASM_COMPILER_TARGET=${TARGET_TRIPLE} -DCMAKE_C_COMPILER_WORKS=ON -DCMAKE_CXX_COMPILER_WORKS=ON -DCMAKE_ASM_COMPILER_WORKS=ON