1# Check if compile-rt library file path exists.
2# If found, cache the path in:
3#    COMPILER_RT_LIBRARY-<name>-<target>
4# If err_flag is true OR path not found, emit a message and set:
5#    COMPILER_RT_LIBRARY-<name>-<target> to NOTFOUND
6function(cache_compiler_rt_library err_flag name target library_file)
7  if(err_flag OR NOT EXISTS "${library_file}")
8    message(STATUS "Failed to find compiler-rt ${name} library for ${target}")
9    set(COMPILER_RT_LIBRARY-${name}-${target} "NOTFOUND" CACHE INTERNAL
10        "compiler-rt ${name} library for ${target}")
11  else()
12    message(STATUS "Found compiler-rt ${name} library: ${library_file}")
13    set(COMPILER_RT_LIBRARY-${name}-${target} "${library_file}" CACHE INTERNAL
14        "compiler-rt ${name} library for ${target}")
15  endif()
16endfunction()
17
18# Find the path to compiler-rt library `name` (e.g. "builtins") for
19# the specified `target` (e.g. "x86_64-linux") and return it in `variable`.
20# This calls cache_compiler_rt_library that caches the path to speed up
21# repeated invocations with the same `name` and `target`.
22function(find_compiler_rt_library name target variable)
23  if(NOT CMAKE_CXX_COMPILER_ID MATCHES Clang)
24    set(${variable} "NOTFOUND" PARENT_SCOPE)
25    return()
26  endif()
27  if (NOT target AND CMAKE_CXX_COMPILER_TARGET)
28    set(target "${CMAKE_CXX_COMPILER_TARGET}")
29  endif()
30  if(NOT DEFINED COMPILER_RT_LIBRARY-builtins-${target})
31    # If the cache variable is not defined, invoke clang and then
32    # set it with cache_compiler_rt_library.
33    set(CLANG_COMMAND ${CMAKE_CXX_COMPILER} ${SANITIZER_COMMON_FLAGS}
34        "--rtlib=compiler-rt" "-print-libgcc-file-name")
35    if(target)
36      list(APPEND CLANG_COMMAND "--target=${target}")
37    endif()
38    get_property(SANITIZER_CXX_FLAGS CACHE CMAKE_CXX_FLAGS PROPERTY VALUE)
39    string(REPLACE " " ";" SANITIZER_CXX_FLAGS "${SANITIZER_CXX_FLAGS}")
40    list(APPEND CLANG_COMMAND ${SANITIZER_CXX_FLAGS})
41    execute_process(
42      COMMAND ${CLANG_COMMAND}
43      RESULT_VARIABLE HAD_ERROR
44      OUTPUT_VARIABLE LIBRARY_FILE
45    )
46    string(STRIP "${LIBRARY_FILE}" LIBRARY_FILE)
47    file(TO_CMAKE_PATH "${LIBRARY_FILE}" LIBRARY_FILE)
48    cache_compiler_rt_library(${HAD_ERROR}
49      builtins "${target}" "${LIBRARY_FILE}")
50  endif()
51  if(NOT COMPILER_RT_LIBRARY-builtins-${target})
52    set(${variable} "NOTFOUND" PARENT_SCOPE)
53    return()
54  endif()
55  if(NOT DEFINED COMPILER_RT_LIBRARY-${name}-${target})
56    # clang gives only the builtins library path. Other library paths are
57    # obtained by substituting "builtins" with ${name} in the builtins
58    # path and then checking if the resultant path exists. The result of
59    # this check is also cached by cache_compiler_rt_library.
60    set(LIBRARY_FILE "${COMPILER_RT_LIBRARY-builtins-${target}}")
61    string(REPLACE "builtins" "${name}" LIBRARY_FILE "${LIBRARY_FILE}")
62    cache_compiler_rt_library(FALSE "${name}" "${target}" "${LIBRARY_FILE}")
63  endif()
64  set(${variable} "${COMPILER_RT_LIBRARY-${name}-${target}}" PARENT_SCOPE)
65endfunction()
66