File: HandleCompilerRT.cmake

package info (click to toggle)
llvm-toolchain-13 1%3A13.0.1-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,418,840 kB
  • sloc: cpp: 5,290,826; ansic: 996,570; asm: 544,593; python: 188,212; objc: 72,027; lisp: 30,291; f90: 25,395; sh: 24,898; javascript: 9,780; pascal: 9,398; perl: 7,484; ml: 5,432; awk: 3,523; makefile: 2,913; xml: 953; cs: 573; fortran: 539
file content (65 lines) | stat: -rw-r--r-- 3,021 bytes parent folder | download | duplicates (5)
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
# Check if compile-rt library file path exists.
# If found, cache the path in:
#    COMPILER_RT_LIBRARY-<name>-<target>
# If err_flag is true OR path not found, emit a message and set:
#    COMPILER_RT_LIBRARY-<name>-<target> to NOTFOUND
function(cache_compiler_rt_library err_flag name target library_file)
  if(err_flag OR NOT EXISTS "${library_file}")
    message(STATUS "Failed to find compiler-rt ${name} library for ${target}")
    set(COMPILER_RT_LIBRARY-${name}-${target} "NOTFOUND" CACHE INTERNAL
        "compiler-rt ${name} library for ${target}")
  else()
    message(STATUS "Found compiler-rt ${name} library: ${library_file}")
    set(COMPILER_RT_LIBRARY-${name}-${target} "${library_file}" CACHE INTERNAL
        "compiler-rt ${name} library for ${target}")
  endif()
endfunction()

# Find the path to compiler-rt library `name` (e.g. "builtins") for
# the specified `target` (e.g. "x86_64-linux") and return it in `variable`.
# This calls cache_compiler_rt_library that caches the path to speed up
# repeated invocations with the same `name` and `target`.
function(find_compiler_rt_library name target variable)
  if(NOT CMAKE_CXX_COMPILER_ID MATCHES Clang)
    set(${variable} "NOTFOUND" PARENT_SCOPE)
    return()
  endif()
  if (NOT target AND CMAKE_CXX_COMPILER_TARGET)
    set(target "${CMAKE_CXX_COMPILER_TARGET}")
  endif()
  if(NOT DEFINED COMPILER_RT_LIBRARY-builtins-${target})
    # If the cache variable is not defined, invoke clang and then 
    # set it with cache_compiler_rt_library.
    set(CLANG_COMMAND ${CMAKE_CXX_COMPILER} ${SANITIZER_COMMON_FLAGS}
        "--rtlib=compiler-rt" "-print-libgcc-file-name")
    if(target)
      list(APPEND CLANG_COMMAND "--target=${target}")
    endif()
    get_property(SANITIZER_CXX_FLAGS CACHE CMAKE_CXX_FLAGS PROPERTY VALUE)
    string(REPLACE " " ";" SANITIZER_CXX_FLAGS "${SANITIZER_CXX_FLAGS}")
    list(APPEND CLANG_COMMAND ${SANITIZER_CXX_FLAGS})
    execute_process(
      COMMAND ${CLANG_COMMAND}
      RESULT_VARIABLE HAD_ERROR
      OUTPUT_VARIABLE LIBRARY_FILE
    )
    string(STRIP "${LIBRARY_FILE}" LIBRARY_FILE)
    file(TO_CMAKE_PATH "${LIBRARY_FILE}" LIBRARY_FILE)
    cache_compiler_rt_library(${HAD_ERROR}
      builtins "${target}" "${LIBRARY_FILE}")
  endif()
  if(NOT COMPILER_RT_LIBRARY-builtins-${target})
    set(${variable} "NOTFOUND" PARENT_SCOPE)
    return()
  endif()
  if(NOT DEFINED COMPILER_RT_LIBRARY-${name}-${target})
    # clang gives only the builtins library path. Other library paths are
    # obtained by substituting "builtins" with ${name} in the builtins
    # path and then checking if the resultant path exists. The result of
    # this check is also cached by cache_compiler_rt_library.
    set(LIBRARY_FILE "${COMPILER_RT_LIBRARY-builtins-${target}}")
    string(REPLACE "builtins" "${name}" LIBRARY_FILE "${LIBRARY_FILE}")
    cache_compiler_rt_library(FALSE "${name}" "${target}" "${LIBRARY_FILE}")
  endif()
  set(${variable} "${COMPILER_RT_LIBRARY-${name}-${target}}" PARENT_SCOPE)
endfunction()