xref: /llvm-project/compiler-rt/cmake/Modules/CompilerRTMockLLVMCMakeConfig.cmake (revision 8470430107d03b5b501b47112750c1905e082c35)
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.
4#
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.
8macro(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()
13endmacro()
14
15macro(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")
20  endif()
21  list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
22  message(STATUS "LLVM_CMAKE_DIR: \"${LLVM_CMAKE_DIR}\"")
23endmacro()
24
25function(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
29  # variable namespace.
30  set(COMPILER_OUTPUT "")
31
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
34  # compiler.
35  if (COMPILER_RT_DEFAULT_TARGET_ONLY)
36    if (NOT "${CMAKE_C_COMPILER_TARGET}" STREQUAL "")
37      message(STATUS
38        "Using CMAKE_C_COMPILER_TARGET (${CMAKE_C_COMPILER_TARGET}) as LLVM_TARGET_TRIPLE")
39    endif()
40    set(COMPILER_OUTPUT "${CMAKE_C_COMPILER_TARGET}")
41  endif()
42
43  # Try asking the compiler for its default target triple.
44  set(HAD_ERROR FALSE)
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
48      # support this flag.
49      set(DUMPMACHINE_ARG -dumpmachine)
50      if(MSVC)
51        # Use /clang:-dumpmachine for clang-cl.
52        set(DUMPMACHINE_ARG /clang:-dumpmachine)
53      endif()
54      execute_process(
55        COMMAND "${CMAKE_C_COMPILER}" ${DUMPMACHINE_ARG}
56        RESULT_VARIABLE HAD_ERROR
57        OUTPUT_VARIABLE COMPILER_OUTPUT
58        OUTPUT_STRIP_TRAILING_WHITESPACE)
59    else()
60      message(FATAL_ERROR
61        "Fetching target triple from compiler \"${CMAKE_C_COMPILER_ID}\" "
62        "is not implemented.")
63    endif()
64  endif()
65
66  if (HAD_ERROR)
67    message(FATAL_ERROR "Fetching target triple from compiler failed")
68  endif()
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")
73  endif()
74  set(LLVM_TARGET_TRIPLE "${LLVM_TARGET_TRIPLE}" PARENT_SCOPE)
75endfunction()
76
77macro(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")
80endmacro()
81