1 # This macro mocks enough of the changes `LLVMConfig.cmake` makes so that
2 # compiler-rt can successfully configure itself when a LLVM toolchain is
3 # available but the corresponding CMake build files are not.
5 # The motivation for this is to be able to generate the compiler-rt
6 # lit tests suites and run them against an arbitrary LLVM toolchain
7 # which doesn't ship the LLVM CMake build files.
8 macro(compiler_rt_mock_llvm_cmake_config)
9 message(STATUS "Attempting to mock the changes made by LLVMConfig.cmake")
10 compiler_rt_mock_llvm_cmake_config_set_cmake_path()
11 compiler_rt_mock_llvm_cmake_config_set_target_triple()
12 compiler_rt_mock_llvm_cmake_config_include_cmake_files()
15 macro(compiler_rt_mock_llvm_cmake_config_set_cmake_path)
16 # Point `LLVM_CMAKE_DIR` at the source tree in the monorepo.
17 set(LLVM_CMAKE_DIR "${LLVM_MAIN_SRC_DIR}/cmake/modules")
18 if (NOT EXISTS "${LLVM_CMAKE_DIR}")
19 message(FATAL_ERROR "LLVM_CMAKE_DIR (${LLVM_CMAKE_DIR}) does not exist")
21 list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
22 message(STATUS "LLVM_CMAKE_DIR: \"${LLVM_CMAKE_DIR}\"")
25 function(compiler_rt_mock_llvm_cmake_config_set_target_triple)
26 # Various bits of compiler-rt depend on the `LLVM_TARGET_TRIPLE` variable
27 # being defined. This function tries to set a sensible value for the
28 # variable. This is a function rather than a macro to avoid polluting the
30 set(COMPILER_OUTPUT "")
32 # If the user provides `COMPILER_RT_DEFAULT_TARGET_ONLY` and `CMAKE_C_COMPILER_TARGET`
33 # (see `construct_compiler_rt_default_triple`) then prefer that to examining the
35 if (COMPILER_RT_DEFAULT_TARGET_ONLY)
36 if (NOT "${CMAKE_C_COMPILER_TARGET}" STREQUAL "")
38 "Using CMAKE_C_COMPILER_TARGET (${CMAKE_C_COMPILER_TARGET}) as LLVM_TARGET_TRIPLE")
40 set(COMPILER_OUTPUT "${CMAKE_C_COMPILER_TARGET}")
43 # Try asking the compiler for its default target triple.
45 if ("${COMPILER_OUTPUT}" STREQUAL "")
46 if ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang|GNU")
47 # Note: Clang also supports `-print-target-triple` but gcc doesn't
49 set(DUMPMACHINE_ARG -dumpmachine)
51 # Use /clang:-dumpmachine for clang-cl.
52 set(DUMPMACHINE_ARG /clang:-dumpmachine)
55 COMMAND "${CMAKE_C_COMPILER}" ${DUMPMACHINE_ARG}
56 RESULT_VARIABLE HAD_ERROR
57 OUTPUT_VARIABLE COMPILER_OUTPUT
58 OUTPUT_STRIP_TRAILING_WHITESPACE)
61 "Fetching target triple from compiler \"${CMAKE_C_COMPILER_ID}\" "
62 "is not implemented.")
67 message(FATAL_ERROR "Fetching target triple from compiler failed")
69 set(LLVM_TARGET_TRIPLE "${COMPILER_OUTPUT}")
70 message(STATUS "TARGET_TRIPLE: \"${LLVM_TARGET_TRIPLE}\"")
71 if ("${LLVM_TARGET_TRIPLE}" STREQUAL "")
72 message(FATAL_ERROR "TARGET_TRIPLE cannot be empty")
74 set(LLVM_TARGET_TRIPLE "${LLVM_TARGET_TRIPLE}" PARENT_SCOPE)
77 macro(compiler_rt_mock_llvm_cmake_config_include_cmake_files)
78 # Some compiler-rt CMake code needs to call code in this file.
79 include("${LLVM_CMAKE_DIR}/AddLLVM.cmake")