[CMake] Cache the compiler-rt library search results
There's a lot of duplicated calls to find various compiler-rt libraries
from build of runtime libraries like libunwind, libc++, libc++abi and
compiler-rt. The compiler-rt helper module already implemented caching
for results avoid repeated Clang invocations.
This change moves the compiler-rt implementation into a shared location
and reuses it from other runtimes to reduce duplication and speed up
the build.
Differential Revision: https://reviews.llvm.org/D88458
GitOrigin-RevId: 0eed292fbae22a8856682b07e1cb968424b49941
diff --git a/CMakeLists.txt b/CMakeLists.txt
index ea6ac11..cc1c395 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -14,6 +14,8 @@
set(CMAKE_MODULE_PATH
"${CMAKE_CURRENT_SOURCE_DIR}/cmake"
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
+ "${CMAKE_CURRENT_SOURCE_DIR}/../cmake"
+ "${CMAKE_CURRENT_SOURCE_DIR}/../cmake/Modules"
${CMAKE_MODULE_PATH}
)
diff --git a/cmake/Modules/HandleCompilerRT.cmake b/cmake/Modules/HandleCompilerRT.cmake
deleted file mode 100644
index 1f95327..0000000
--- a/cmake/Modules/HandleCompilerRT.cmake
+++ /dev/null
@@ -1,64 +0,0 @@
-function(find_compiler_rt_library name dest)
- if (NOT DEFINED LIBCXXABI_COMPILE_FLAGS)
- message(FATAL_ERROR "LIBCXXABI_COMPILE_FLAGS must be defined when using this function")
- endif()
- set(dest "" PARENT_SCOPE)
- set(CLANG_COMMAND ${CMAKE_CXX_COMPILER} ${LIBCXXABI_COMPILE_FLAGS}
- "--rtlib=compiler-rt" "--print-libgcc-file-name")
- if (CMAKE_CXX_COMPILER_ID MATCHES Clang AND CMAKE_CXX_COMPILER_TARGET)
- list(APPEND CLANG_COMMAND "--target=${CMAKE_CXX_COMPILER_TARGET}")
- endif()
- get_property(LIBCXXABI_CXX_FLAGS CACHE CMAKE_CXX_FLAGS PROPERTY VALUE)
- string(REPLACE " " ";" LIBCXXABI_CXX_FLAGS "${LIBCXXABI_CXX_FLAGS}")
- list(APPEND CLANG_COMMAND ${LIBCXXABI_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)
- string(REPLACE "builtins" "${name}" LIBRARY_FILE "${LIBRARY_FILE}")
- if (NOT HAD_ERROR AND EXISTS "${LIBRARY_FILE}")
- message(STATUS "Found compiler-rt library: ${LIBRARY_FILE}")
- set(${dest} "${LIBRARY_FILE}" PARENT_SCOPE)
- else()
- message(STATUS "Failed to find compiler-rt library")
- endif()
-endfunction()
-
-function(find_compiler_rt_dir dest)
- if (NOT DEFINED LIBCXXABI_COMPILE_FLAGS)
- message(FATAL_ERROR "LIBCXXABI_COMPILE_FLAGS must be defined when using this function")
- endif()
- set(dest "" PARENT_SCOPE)
- if (APPLE)
- set(CLANG_COMMAND ${CMAKE_CXX_COMPILER} ${LIBCXXABI_COMPILE_FLAGS}
- "-print-file-name=lib")
- execute_process(
- COMMAND ${CLANG_COMMAND}
- RESULT_VARIABLE HAD_ERROR
- OUTPUT_VARIABLE LIBRARY_DIR
- )
- string(STRIP "${LIBRARY_DIR}" LIBRARY_DIR)
- file(TO_CMAKE_PATH "${LIBRARY_DIR}" LIBRARY_DIR)
- set(LIBRARY_DIR "${LIBRARY_DIR}/darwin")
- else()
- set(CLANG_COMMAND ${CMAKE_CXX_COMPILER} ${LIBCXXABI_COMPILE_FLAGS}
- "--rtlib=compiler-rt" "--print-libgcc-file-name")
- 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)
- get_filename_component(LIBRARY_DIR "${LIBRARY_FILE}" DIRECTORY)
- endif()
- if (NOT HAD_ERROR AND EXISTS "${LIBRARY_DIR}")
- message(STATUS "Found compiler-rt directory: ${LIBRARY_DIR}")
- set(${dest} "${LIBRARY_DIR}" PARENT_SCOPE)
- else()
- message(STATUS "Failed to find compiler-rt directory")
- endif()
-endfunction()
diff --git a/cmake/config-ix.cmake b/cmake/config-ix.cmake
index 998e254..62e6aec 100644
--- a/cmake/config-ix.cmake
+++ b/cmake/config-ix.cmake
@@ -38,8 +38,10 @@
list(APPEND CMAKE_REQUIRED_LIBRARIES c)
endif ()
if (LIBCXXABI_USE_COMPILER_RT)
- set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -rtlib=compiler-rt")
+ cmake_push_check_state()
+ set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${LIBCXXABI_COMPILE_FLAGS}")
find_compiler_rt_library(builtins LIBCXXABI_BUILTINS_LIBRARY)
+ cmake_pop_check_state()
list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBCXXABI_BUILTINS_LIBRARY}")
else ()
if (LIBCXXABI_HAS_GCC_S_LIB)
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 94182dc..c237fb1 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -136,7 +136,8 @@
message(WARNING "LLVM_USE_SANITIZER=${LLVM_USE_SANITIZER} is not supported on OS X")
endif()
if (LIBFILE)
- find_compiler_rt_dir(LIBDIR)
+ find_compiler_rt_library(builtins LIBCXXABI_BUILTINS_LIBRARY)
+ get_filename_component(LIBDIR "${LIBCXXABI_BUILTINS_LIBRARY}" DIRECTORY)
if (NOT IS_DIRECTORY "${LIBDIR}")
message(FATAL_ERROR "Cannot find compiler-rt directory on OS X required for LLVM_USE_SANITIZER")
endif()