File: CompilerRTMockLLVMCMakeConfig.cmake

package info (click to toggle)
llvm-toolchain-16 1%3A16.0.6-15~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,634,792 kB
  • sloc: cpp: 6,179,261; ansic: 1,216,205; asm: 741,319; python: 196,614; objc: 75,325; f90: 49,640; lisp: 32,396; pascal: 12,286; sh: 9,394; perl: 7,442; ml: 5,494; awk: 3,523; makefile: 2,723; javascript: 1,206; xml: 886; fortran: 581; cs: 573
file content (75 lines) | stat: -rw-r--r-- 3,137 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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_DIR` at the source tree in the monorepo.
  set(LLVM_CMAKE_DIR "${LLVM_MAIN_SRC_DIR}/cmake/modules")
  if (NOT EXISTS "${LLVM_CMAKE_DIR}")
    message(FATAL_ERROR "LLVM_CMAKE_DIR (${LLVM_CMAKE_DIR}) does not exist")
  endif()
  list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
  message(STATUS "LLVM_CMAKE_DIR: \"${LLVM_CMAKE_DIR}\"")
endmacro()

function(compiler_rt_mock_llvm_cmake_config_set_target_triple)
  # Various bits of compiler-rt depend on the `LLVM_TARGET_TRIPLE` variable
  # being defined. This function tries to set a sensible value for the
  # variable. This is a function rather than a macro to avoid polluting the
  # variable namespace.
  set(COMPILER_OUTPUT "")

  # If the user provides `COMPILER_RT_DEFAULT_TARGET_ONLY` and `CMAKE_C_COMPILER_TARGET`
  # (see `construct_compiler_rt_default_triple`) then prefer that to examining the
  # compiler.
  if (COMPILER_RT_DEFAULT_TARGET_ONLY)
    if (NOT "${CMAKE_C_COMPILER_TARGET}" STREQUAL "")
      message(STATUS
        "Using CMAKE_C_COMPILER_TARGET (${CMAKE_C_COMPILER_TARGET}) as LLVM_TARGET_TRIPLE")
    endif()
    set(COMPILER_OUTPUT "${CMAKE_C_COMPILER_TARGET}")
  endif()

  # Try asking the compiler for its default target triple.
  set(HAD_ERROR FALSE)
  if ("${COMPILER_OUTPUT}" STREQUAL "")
    if ("${CMAKE_C_COMPILER_ID}" MATCHES "Clang|GNU")
      # Note: Clang also supports `-print-target-triple` but gcc doesn't
      # support this flag.
      execute_process(
        COMMAND "${CMAKE_C_COMPILER}" -dumpmachine
        RESULT_VARIABLE HAD_ERROR
        OUTPUT_VARIABLE COMPILER_OUTPUT
        OUTPUT_STRIP_TRAILING_WHITESPACE)
    else()
      message(FATAL_ERROR
        "Fetching target triple from compiler \"${CMAKE_C_COMPILER_ID}\" "
        "is not implemented.")
    endif()
  endif()

  if (HAD_ERROR)
    message(FATAL_ERROR "Fetching target triple from compiler failed")
  endif()
  set(LLVM_TARGET_TRIPLE "${COMPILER_OUTPUT}")
  message(STATUS "TARGET_TRIPLE: \"${LLVM_TARGET_TRIPLE}\"")
  if ("${LLVM_TARGET_TRIPLE}" STREQUAL "")
    message(FATAL_ERROR "TARGET_TRIPLE cannot be empty")
  endif()
  set(LLVM_TARGET_TRIPLE "${LLVM_TARGET_TRIPLE}" PARENT_SCOPE)
endfunction()

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_DIR}/AddLLVM.cmake")
endmacro()