diff --git a/compiler-rt/cmake/Modules/CompilerRTMockLLVMCMakeConfig.cmake b/compiler-rt/cmake/Modules/CompilerRTMockLLVMCMakeConfig.cmake new file mode 100644 --- /dev/null +++ b/compiler-rt/cmake/Modules/CompilerRTMockLLVMCMakeConfig.cmake @@ -0,0 +1,43 @@ +# This macro mocks enough of the changes `LLVMConfig.cmake` makes so that +# compiler-rt can successfully configure itself when a LLVM toolchain is +# available but the corresponding CMake build files are not. +# +# The motivation for this is to be able to generate the compiler-rt +# lit tests suites and run them against an arbitrary LLVM toolchain +# which doesn't ship the LLVM CMake build files. +macro(compiler_rt_mock_llvm_cmake_config) + message(STATUS "Attempting to mock the changes made by LLVMConfig.cmake") + compiler_rt_mock_llvm_cmake_config_set_cmake_path() + compiler_rt_mock_llvm_cmake_config_set_target_triple() + compiler_rt_mock_llvm_cmake_config_include_cmake_files() +endmacro() + +macro(compiler_rt_mock_llvm_cmake_config_set_cmake_path) + # Point `LLVM_CMAKE_PATH` at the source tree in the monorepo. + set(LLVM_CMAKE_PATH "${LLVM_MAIN_SRC_DIR}/cmake/modules") + if (NOT EXISTS "${LLVM_CMAKE_PATH}") + message(FATAL_ERROR "LLVM_CMAKE_PATH (${LLVM_CMAKE_PATH}) does not exist") + endif() + list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}") + message(STATUS "LLVM_CMAKE_PATH: \"${LLVM_CMAKE_PATH}\"") +endmacro() + +macro(compiler_rt_mock_llvm_cmake_config_set_target_triple) + # Various bits of compiler-rt depend on this variable being defined. + execute_process( + COMMAND ${LLVM_CONFIG_PATH} --host-target + RESULT_VARIABLE HAD_ERROR + OUTPUT_VARIABLE CONFIG_OUTPUT + OUTPUT_STRIP_TRAILING_WHITESPACE) + if(NOT HAD_ERROR) + set(TARGET_TRIPLE "${CONFIG_OUTPUT}") + else() + message(FATAL_ERROR "FAILED to run llvm-config to determine host target") + endif() + message(STATUS "TARGET_TRIPLE: \"${TARGET_TRIPLE}\"") +endmacro() + +macro(compiler_rt_mock_llvm_cmake_config_include_cmake_files) + # Some compiler-rt CMake code needs to call code in this file. + include("${LLVM_CMAKE_PATH}/AddLLVM.cmake") +endmacro() diff --git a/compiler-rt/cmake/Modules/CompilerRTUtils.cmake b/compiler-rt/cmake/Modules/CompilerRTUtils.cmake --- a/compiler-rt/cmake/Modules/CompilerRTUtils.cmake +++ b/compiler-rt/cmake/Modules/CompilerRTUtils.cmake @@ -323,9 +323,18 @@ set(LLVM_CMAKE_PATH "${LLVM_BINARY_DIR_CMAKE_STYLE}/lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm") endif() - list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}") - # Get some LLVM variables from LLVMConfig. - include("${LLVM_CMAKE_PATH}/LLVMConfig.cmake") + if (EXISTS "${LLVM_CMAKE_PATH}") + list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}") + # Get some LLVM variables from LLVMConfig. + include("${LLVM_CMAKE_PATH}/LLVMConfig.cmake") + else() + # This configuration tries to configure without the prescence of `LLVMConfig.cmake`. It is + # intended for testing purposes (generating the lit test suites) and will likely not support + # a build of the runtimes in compiler-rt. + message(WARNING "LLVM CMake path (${LLVM_CMAKE_PATH}) reported by llvm-config does not exist") + include(CompilerRTMockLLVMCMakeConfig) + compiler_rt_mock_llvm_cmake_config() + endif() set(LLVM_LIBRARY_OUTPUT_INTDIR ${LLVM_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})