[libcxx-commits] [libcxxabi] 22acda4 - [CMake] Cache the compiler-rt library search results

Petr Hosek via libcxx-commits libcxx-commits at lists.llvm.org
Wed Oct 27 17:53:27 PDT 2021


Author: Petr Hosek
Date: 2021-10-27T17:53:03-07:00
New Revision: 22acda48ff32712746dad3079579f03c6e89e04c

URL: https://github.com/llvm/llvm-project/commit/22acda48ff32712746dad3079579f03c6e89e04c
DIFF: https://github.com/llvm/llvm-project/commit/22acda48ff32712746dad3079579f03c6e89e04c.diff

LOG: [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

Added: 
    cmake/Modules/HandleCompilerRT.cmake

Modified: 
    compiler-rt/CMakeLists.txt
    compiler-rt/cmake/Modules/AddCompilerRT.cmake
    compiler-rt/cmake/config-ix.cmake
    compiler-rt/lib/builtins/CMakeLists.txt
    libcxx/CMakeLists.txt
    libcxx/cmake/config-ix.cmake
    libcxx/src/CMakeLists.txt
    libcxxabi/CMakeLists.txt
    libcxxabi/cmake/config-ix.cmake
    libcxxabi/src/CMakeLists.txt
    libunwind/CMakeLists.txt
    libunwind/cmake/config-ix.cmake

Removed: 
    compiler-rt/cmake/Modules/HandleCompilerRT.cmake
    libcxx/cmake/Modules/HandleCompilerRT.cmake
    libcxxabi/cmake/Modules/HandleCompilerRT.cmake
    libunwind/cmake/Modules/HandleCompilerRT.cmake


################################################################################
diff  --git a/cmake/Modules/HandleCompilerRT.cmake b/cmake/Modules/HandleCompilerRT.cmake
new file mode 100644
index 0000000000000..226b93ecef66c
--- /dev/null
+++ b/cmake/Modules/HandleCompilerRT.cmake
@@ -0,0 +1,101 @@
+# 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()
+
+function(get_component_name name variable)
+  if(APPLE)
+    if(NOT name MATCHES "builtins.*")
+      set(component_name "${name}_")
+    endif()
+    # TODO: Support ios, tvos and watchos as well.
+    set(component_name "${component_name}osx")
+  else()
+    set(component_name "${name}")
+  endif()
+  set(${variable} "${component_name}" PARENT_SCOPE)
+endfunction()
+
+# Find the path to compiler-rt library `name` (e.g. "builtins") for the
+# specified `TARGET` (e.g. "x86_64-linux-gnu") 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 variable)
+  cmake_parse_arguments(ARG "" "TARGET;FLAGS" "" ${ARGN})
+  # While we can use compiler-rt runtimes with other compilers, we need to
+  # query the compiler for runtime location and thus we require Clang.
+  if(NOT CMAKE_CXX_COMPILER_ID MATCHES Clang)
+    set(${variable} "NOTFOUND" PARENT_SCOPE)
+    return()
+  endif()
+  set(target "${ARG_TARGET}")
+  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} "${ARG_FLAGS}")
+    if(target)
+      list(APPEND clang_command "--target=${target}")
+    endif()
+    get_property(cxx_flags CACHE CMAKE_CXX_FLAGS PROPERTY VALUE)
+    string(REPLACE " " ";" cxx_flags "${cxx_flags}")
+    list(APPEND clang_command ${cxx_flags})
+    execute_process(
+      COMMAND ${clang_command} "--rtlib=compiler-rt" "-print-libgcc-file-name"
+      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(dirname ${library_file} DIRECTORY)
+    if(APPLE)
+      execute_process(
+        COMMAND ${clang_command} "--print-resource-dir"
+        RESULT_VARIABLE had_error
+        OUTPUT_VARIABLE resource_dir
+      )
+      string(STRIP "${resource_dir}" resource_dir)
+      set(dirname "${resource_dir}/lib/darwin")
+    endif()
+    get_filename_component(basename ${library_file} NAME)
+    if(basename MATCHES "libclang_rt\.([a-z0-9_\-]+)\.a")
+      set(from_name ${CMAKE_MATCH_1})
+      get_component_name(${CMAKE_MATCH_1} to_name)
+      string(REPLACE "${from_name}" "${to_name}" basename "${basename}")
+      set(library_file "${dirname}/${basename}")
+      cache_compiler_rt_library(${had_error} builtins "${target}" "${library_file}")
+    endif()
+  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}}")
+    if(library_file MATCHES ".*libclang_rt\.([a-z0-9_\-]+)\.a")
+      set(from_name ${CMAKE_MATCH_0})
+      get_component_name(${name} to_name)
+      string(REPLACE "${from_name}" "${to_name}" library_file "${library_file}")
+      cache_compiler_rt_library(FALSE "${name}" "${target}" "${library_file}")
+    endif()
+  endif()
+  set(${variable} "${COMPILER_RT_LIBRARY_${name}_${target}}" PARENT_SCOPE)
+endfunction()

diff  --git a/compiler-rt/CMakeLists.txt b/compiler-rt/CMakeLists.txt
index aa14ceff495a0..17d7e4ffbdebf 100644
--- a/compiler-rt/CMakeLists.txt
+++ b/compiler-rt/CMakeLists.txt
@@ -16,6 +16,8 @@ endif()
 list(INSERT CMAKE_MODULE_PATH 0
   "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
   "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
+  "${CMAKE_CURRENT_SOURCE_DIR}/../cmake"
+  "${CMAKE_CURRENT_SOURCE_DIR}/../cmake/Modules"
   )
 
 if(CMAKE_CONFIGURATION_TYPES)

diff  --git a/compiler-rt/cmake/Modules/AddCompilerRT.cmake b/compiler-rt/cmake/Modules/AddCompilerRT.cmake
index 8b620786730b5..76e015368f5db 100644
--- a/compiler-rt/cmake/Modules/AddCompilerRT.cmake
+++ b/compiler-rt/cmake/Modules/AddCompilerRT.cmake
@@ -254,7 +254,7 @@ function(add_compiler_rt_runtime name type)
       if(COMPILER_RT_USE_BUILTINS_LIBRARY AND NOT type STREQUAL "OBJECT" AND
          NOT name STREQUAL "clang_rt.builtins")
         get_compiler_rt_target(${arch} target)
-        find_compiler_rt_library(builtins ${target} builtins_${libname})
+        find_compiler_rt_library(builtins builtins_${libname} TARGET ${target})
         if(builtins_${libname} STREQUAL "NOTFOUND")
           message(FATAL_ERROR "Cannot find builtins library for the target architecture")
         endif()

diff  --git a/compiler-rt/cmake/Modules/HandleCompilerRT.cmake b/compiler-rt/cmake/Modules/HandleCompilerRT.cmake
deleted file mode 100644
index ac9e0871489da..0000000000000
--- a/compiler-rt/cmake/Modules/HandleCompilerRT.cmake
+++ /dev/null
@@ -1,65 +0,0 @@
-# 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()

diff  --git a/compiler-rt/cmake/config-ix.cmake b/compiler-rt/cmake/config-ix.cmake
index c5ffef2bd90dc..016393618fb22 100644
--- a/compiler-rt/cmake/config-ix.cmake
+++ b/compiler-rt/cmake/config-ix.cmake
@@ -16,7 +16,8 @@ endfunction()
 check_library_exists(c fopen "" COMPILER_RT_HAS_LIBC)
 if (COMPILER_RT_USE_BUILTINS_LIBRARY)
   include(HandleCompilerRT)
-  find_compiler_rt_library(builtins "" COMPILER_RT_BUILTINS_LIBRARY)
+  find_compiler_rt_library(builtins COMPILER_RT_BUILTINS_LIBRARY
+                           FLAGS ${SANITIZER_COMMON_FLAGS})
   # TODO(PR51389): We should check COMPILER_RT_BUILTINS_LIBRARY and report an
   # error if the value is NOTFOUND rather than silenty continuing but we first
   # need to fix find_compiler_rt_library on Darwin.

diff  --git a/compiler-rt/lib/builtins/CMakeLists.txt b/compiler-rt/lib/builtins/CMakeLists.txt
index 116517ca9e7dd..971e94407a2c9 100644
--- a/compiler-rt/lib/builtins/CMakeLists.txt
+++ b/compiler-rt/lib/builtins/CMakeLists.txt
@@ -11,7 +11,9 @@ if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
   set(COMPILER_RT_BUILTINS_STANDALONE_BUILD TRUE)
   list(INSERT CMAKE_MODULE_PATH 0
     "${CMAKE_SOURCE_DIR}/../../cmake"
-    "${CMAKE_SOURCE_DIR}/../../cmake/Modules")
+    "${CMAKE_SOURCE_DIR}/../../cmake/Modules"
+    "${CMAKE_SOURCE_DIR}/../../../cmake"
+    "${CMAKE_SOURCE_DIR}/../../../cmake/Modules")
   include(base-config-ix)
   include(CompilerRTUtils)
 

diff  --git a/libcxx/CMakeLists.txt b/libcxx/CMakeLists.txt
index 45e2ff9282dd7..e7edf2429e11b 100644
--- a/libcxx/CMakeLists.txt
+++ b/libcxx/CMakeLists.txt
@@ -14,6 +14,8 @@ cmake_minimum_required(VERSION 3.13.4)
 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/libcxx/cmake/Modules/HandleCompilerRT.cmake b/libcxx/cmake/Modules/HandleCompilerRT.cmake
deleted file mode 100644
index 1ce2565749418..0000000000000
--- a/libcxx/cmake/Modules/HandleCompilerRT.cmake
+++ /dev/null
@@ -1,64 +0,0 @@
-function(find_compiler_rt_library name dest)
-  if (NOT DEFINED LIBCXX_COMPILE_FLAGS)
-    message(FATAL_ERROR "LIBCXX_COMPILE_FLAGS must be defined when using this function")
-  endif()
-  set(dest "" PARENT_SCOPE)
-  set(CLANG_COMMAND ${CMAKE_CXX_COMPILER} ${LIBCXX_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(LIBCXX_CXX_FLAGS CACHE CMAKE_CXX_FLAGS PROPERTY VALUE)
-  string(REPLACE " " ";" LIBCXX_CXX_FLAGS "${LIBCXX_CXX_FLAGS}")
-  list(APPEND CLANG_COMMAND ${LIBCXX_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 LIBCXX_COMPILE_FLAGS)
-    message(FATAL_ERROR "LIBCXX_COMPILE_FLAGS must be defined when using this function")
-  endif()
-  set(dest "" PARENT_SCOPE)
-  if (APPLE)
-    set(CLANG_COMMAND ${CMAKE_CXX_COMPILER} ${LIBCXX_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} ${LIBCXX_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/libcxx/cmake/config-ix.cmake b/libcxx/cmake/config-ix.cmake
index a85e3b8db173f..8ca8b14151dfa 100644
--- a/libcxx/cmake/config-ix.cmake
+++ b/libcxx/cmake/config-ix.cmake
@@ -48,8 +48,9 @@ if (LIBCXX_SUPPORTS_NOSTDLIBXX_FLAG OR LIBCXX_SUPPORTS_NODEFAULTLIBS_FLAG)
     list(APPEND CMAKE_REQUIRED_LIBRARIES c)
   endif ()
   if (LIBCXX_USE_COMPILER_RT)
-    set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -rtlib=compiler-rt")
-    find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY)
+    include(HandleCompilerRT)
+    find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY
+                             FLAGS ${LIBCXX_COMPILE_FLAGS})
     list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBCXX_BUILTINS_LIBRARY}")
   elseif (LIBCXX_HAS_GCC_LIB)
     list(APPEND CMAKE_REQUIRED_LIBRARIES gcc)

diff  --git a/libcxx/src/CMakeLists.txt b/libcxx/src/CMakeLists.txt
index 0bcfb1776271c..b5a870a5a4992 100644
--- a/libcxx/src/CMakeLists.txt
+++ b/libcxx/src/CMakeLists.txt
@@ -163,7 +163,8 @@ if (APPLE AND LLVM_USE_SANITIZER)
     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 LIBCXX_BUILTINS_LIBRARY)
+    get_filename_component(LIBDIR "${LIBCXX_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()

diff  --git a/libcxxabi/CMakeLists.txt b/libcxxabi/CMakeLists.txt
index 670d97ca3524c..027dafd03fa1e 100644
--- a/libcxxabi/CMakeLists.txt
+++ b/libcxxabi/CMakeLists.txt
@@ -14,6 +14,8 @@ cmake_minimum_required(VERSION 3.13.4)
 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/libcxxabi/cmake/Modules/HandleCompilerRT.cmake b/libcxxabi/cmake/Modules/HandleCompilerRT.cmake
deleted file mode 100644
index 1f953276df00d..0000000000000
--- a/libcxxabi/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/libcxxabi/cmake/config-ix.cmake b/libcxxabi/cmake/config-ix.cmake
index 998e25415ed1b..42cfd0a7d32c9 100644
--- a/libcxxabi/cmake/config-ix.cmake
+++ b/libcxxabi/cmake/config-ix.cmake
@@ -38,8 +38,9 @@ if (LIBCXXABI_SUPPORTS_NOSTDLIBXX_FLAG OR LIBCXXABI_SUPPORTS_NODEFAULTLIBS_FLAG)
     list(APPEND CMAKE_REQUIRED_LIBRARIES c)
   endif ()
   if (LIBCXXABI_USE_COMPILER_RT)
-    set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -rtlib=compiler-rt")
-    find_compiler_rt_library(builtins LIBCXXABI_BUILTINS_LIBRARY)
+    include(HandleCompilerRT)
+    find_compiler_rt_library(builtins LIBCXXABI_BUILTINS_LIBRARY
+                             FLAGS "${LIBCXXABI_COMPILE_FLAGS}")
     list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBCXXABI_BUILTINS_LIBRARY}")
   else ()
     if (LIBCXXABI_HAS_GCC_S_LIB)

diff  --git a/libcxxabi/src/CMakeLists.txt b/libcxxabi/src/CMakeLists.txt
index 94182dccba5e3..c237fb19f84f9 100644
--- a/libcxxabi/src/CMakeLists.txt
+++ b/libcxxabi/src/CMakeLists.txt
@@ -136,7 +136,8 @@ if ( APPLE )
       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()

diff  --git a/libunwind/CMakeLists.txt b/libunwind/CMakeLists.txt
index 96477e24a2596..f16d49a8fb8a4 100644
--- a/libunwind/CMakeLists.txt
+++ b/libunwind/CMakeLists.txt
@@ -12,6 +12,8 @@ cmake_minimum_required(VERSION 3.13.4)
 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/libunwind/cmake/Modules/HandleCompilerRT.cmake b/libunwind/cmake/Modules/HandleCompilerRT.cmake
deleted file mode 100644
index 77168e599466e..0000000000000
--- a/libunwind/cmake/Modules/HandleCompilerRT.cmake
+++ /dev/null
@@ -1,64 +0,0 @@
-function(find_compiler_rt_library name dest)
-  if (NOT DEFINED LIBUNWIND_COMPILE_FLAGS)
-    message(FATAL_ERROR "LIBUNWIND_COMPILE_FLAGS must be defined when using this function")
-  endif()
-  set(dest "" PARENT_SCOPE)
-  set(CLANG_COMMAND ${CMAKE_CXX_COMPILER} ${LIBUNWIND_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(LIBUNWIND_CXX_FLAGS CACHE CMAKE_CXX_FLAGS PROPERTY VALUE)
-  string(REPLACE " " ";" LIBUNWIND_CXX_FLAGS "${LIBUNWIND_CXX_FLAGS}")
-  list(APPEND CLANG_COMMAND ${LIBUNWIND_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 LIBUNWIND_COMPILE_FLAGS)
-    message(FATAL_ERROR "LIBUNWIND_COMPILE_FLAGS must be defined when using this function")
-  endif()
-  set(dest "" PARENT_SCOPE)
-  if (APPLE)
-    set(CLANG_COMMAND ${CMAKE_CXX_COMPILER} ${LIBUNWIND_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} ${LIBUNWIND_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/libunwind/cmake/config-ix.cmake b/libunwind/cmake/config-ix.cmake
index 4ca6bdd8e95df..78f116c15e0a3 100644
--- a/libunwind/cmake/config-ix.cmake
+++ b/libunwind/cmake/config-ix.cmake
@@ -40,7 +40,9 @@ if (LIBUNWIND_SUPPORTS_NOSTDLIBXX_FLAG OR LIBUNWIND_SUPPORTS_NODEFAULTLIBS_FLAG)
     list(APPEND CMAKE_REQUIRED_LIBRARIES c)
   endif ()
   if (LIBUNWIND_USE_COMPILER_RT)
-    find_compiler_rt_library(builtins LIBUNWIND_BUILTINS_LIBRARY)
+    include(HandleCompilerRT)
+    find_compiler_rt_library(builtins LIBUNWIND_BUILTINS_LIBRARY
+                             FLAGS ${LIBUNWIND_COMPILE_FLAGS})
     list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBUNWIND_BUILTINS_LIBRARY}")
   else ()
     if (LIBUNWIND_HAS_GCC_S_LIB)


        


More information about the libcxx-commits mailing list