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