Skip to content

Commit ba4920e

Browse files
committed
Revert "[CMake] Cache the compiler-rt library search results"
This reverts commit 0eed292, there are compiler-rt build failures that appear to have been introduced by this change.
1 parent a4b8979 commit ba4920e

File tree

17 files changed

+264
-130
lines changed

17 files changed

+264
-130
lines changed

cmake/Modules/HandleCompilerRT.cmake

Lines changed: 0 additions & 101 deletions
This file was deleted.

compiler-rt/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ endif()
1616
list(INSERT CMAKE_MODULE_PATH 0
1717
"${CMAKE_CURRENT_SOURCE_DIR}/cmake"
1818
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
19-
"${CMAKE_CURRENT_SOURCE_DIR}/../cmake"
20-
"${CMAKE_CURRENT_SOURCE_DIR}/../cmake/Modules"
2119
)
2220

2321
if(CMAKE_CONFIGURATION_TYPES)

compiler-rt/cmake/Modules/AddCompilerRT.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ function(add_compiler_rt_runtime name type)
254254
if(COMPILER_RT_USE_BUILTINS_LIBRARY AND NOT type STREQUAL "OBJECT" AND
255255
NOT name STREQUAL "clang_rt.builtins")
256256
get_compiler_rt_target(${arch} target)
257-
find_compiler_rt_library(builtins builtins_${libname} TARGET ${target})
257+
find_compiler_rt_library(builtins ${target} builtins_${libname})
258258
if(builtins_${libname} STREQUAL "NOTFOUND")
259259
message(FATAL_ERROR "Cannot find builtins library for the target architecture")
260260
endif()
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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
6+
function(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()
16+
endfunction()
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`.
22+
function(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)
65+
endfunction()

compiler-rt/cmake/config-ix.cmake

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ endfunction()
1616
check_library_exists(c fopen "" COMPILER_RT_HAS_LIBC)
1717
if (COMPILER_RT_USE_BUILTINS_LIBRARY)
1818
include(HandleCompilerRT)
19-
cmake_push_check_state()
20-
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${SANITIZER_COMMON_FLAGS}")
21-
find_compiler_rt_library(builtins COMPILER_RT_BUILTINS_LIBRARY)
22-
cmake_pop_check_state()
19+
find_compiler_rt_library(builtins "" COMPILER_RT_BUILTINS_LIBRARY)
2320
# TODO(PR51389): We should check COMPILER_RT_BUILTINS_LIBRARY and report an
2421
# error if the value is NOTFOUND rather than silenty continuing but we first
2522
# need to fix find_compiler_rt_library on Darwin.

compiler-rt/lib/builtins/CMakeLists.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
1111
set(COMPILER_RT_BUILTINS_STANDALONE_BUILD TRUE)
1212
list(INSERT CMAKE_MODULE_PATH 0
1313
"${CMAKE_SOURCE_DIR}/../../cmake"
14-
"${CMAKE_SOURCE_DIR}/../../cmake/Modules"
15-
"${CMAKE_SOURCE_DIR}/../../../cmake"
16-
"${CMAKE_SOURCE_DIR}/../../../cmake/Modules")
14+
"${CMAKE_SOURCE_DIR}/../../cmake/Modules")
1715
include(base-config-ix)
1816
include(CompilerRTUtils)
1917

libcxx/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ cmake_minimum_required(VERSION 3.13.4)
1414
set(CMAKE_MODULE_PATH
1515
"${CMAKE_CURRENT_SOURCE_DIR}/cmake"
1616
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
17-
"${CMAKE_CURRENT_SOURCE_DIR}/../cmake"
18-
"${CMAKE_CURRENT_SOURCE_DIR}/../cmake/Modules"
1917
${CMAKE_MODULE_PATH}
2018
)
2119

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
function(find_compiler_rt_library name dest)
2+
if (NOT DEFINED LIBCXX_COMPILE_FLAGS)
3+
message(FATAL_ERROR "LIBCXX_COMPILE_FLAGS must be defined when using this function")
4+
endif()
5+
set(dest "" PARENT_SCOPE)
6+
set(CLANG_COMMAND ${CMAKE_CXX_COMPILER} ${LIBCXX_COMPILE_FLAGS}
7+
"--rtlib=compiler-rt" "--print-libgcc-file-name")
8+
if (CMAKE_CXX_COMPILER_ID MATCHES Clang AND CMAKE_CXX_COMPILER_TARGET)
9+
list(APPEND CLANG_COMMAND "--target=${CMAKE_CXX_COMPILER_TARGET}")
10+
endif()
11+
get_property(LIBCXX_CXX_FLAGS CACHE CMAKE_CXX_FLAGS PROPERTY VALUE)
12+
string(REPLACE " " ";" LIBCXX_CXX_FLAGS "${LIBCXX_CXX_FLAGS}")
13+
list(APPEND CLANG_COMMAND ${LIBCXX_CXX_FLAGS})
14+
execute_process(
15+
COMMAND ${CLANG_COMMAND}
16+
RESULT_VARIABLE HAD_ERROR
17+
OUTPUT_VARIABLE LIBRARY_FILE
18+
)
19+
string(STRIP "${LIBRARY_FILE}" LIBRARY_FILE)
20+
file(TO_CMAKE_PATH "${LIBRARY_FILE}" LIBRARY_FILE)
21+
string(REPLACE "builtins" "${name}" LIBRARY_FILE "${LIBRARY_FILE}")
22+
if (NOT HAD_ERROR AND EXISTS "${LIBRARY_FILE}")
23+
message(STATUS "Found compiler-rt library: ${LIBRARY_FILE}")
24+
set(${dest} "${LIBRARY_FILE}" PARENT_SCOPE)
25+
else()
26+
message(STATUS "Failed to find compiler-rt library")
27+
endif()
28+
endfunction()
29+
30+
function(find_compiler_rt_dir dest)
31+
if (NOT DEFINED LIBCXX_COMPILE_FLAGS)
32+
message(FATAL_ERROR "LIBCXX_COMPILE_FLAGS must be defined when using this function")
33+
endif()
34+
set(dest "" PARENT_SCOPE)
35+
if (APPLE)
36+
set(CLANG_COMMAND ${CMAKE_CXX_COMPILER} ${LIBCXX_COMPILE_FLAGS}
37+
"-print-file-name=lib")
38+
execute_process(
39+
COMMAND ${CLANG_COMMAND}
40+
RESULT_VARIABLE HAD_ERROR
41+
OUTPUT_VARIABLE LIBRARY_DIR
42+
)
43+
string(STRIP "${LIBRARY_DIR}" LIBRARY_DIR)
44+
file(TO_CMAKE_PATH "${LIBRARY_DIR}" LIBRARY_DIR)
45+
set(LIBRARY_DIR "${LIBRARY_DIR}/darwin")
46+
else()
47+
set(CLANG_COMMAND ${CMAKE_CXX_COMPILER} ${LIBCXX_COMPILE_FLAGS}
48+
"--rtlib=compiler-rt" "--print-libgcc-file-name")
49+
execute_process(
50+
COMMAND ${CLANG_COMMAND}
51+
RESULT_VARIABLE HAD_ERROR
52+
OUTPUT_VARIABLE LIBRARY_FILE
53+
)
54+
string(STRIP "${LIBRARY_FILE}" LIBRARY_FILE)
55+
file(TO_CMAKE_PATH "${LIBRARY_FILE}" LIBRARY_FILE)
56+
get_filename_component(LIBRARY_DIR "${LIBRARY_FILE}" DIRECTORY)
57+
endif()
58+
if (NOT HAD_ERROR AND EXISTS "${LIBRARY_DIR}")
59+
message(STATUS "Found compiler-rt directory: ${LIBRARY_DIR}")
60+
set(${dest} "${LIBRARY_DIR}" PARENT_SCOPE)
61+
else()
62+
message(STATUS "Failed to find compiler-rt directory")
63+
endif()
64+
endfunction()

libcxx/cmake/config-ix.cmake

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ if (LIBCXX_SUPPORTS_NOSTDLIBXX_FLAG OR LIBCXX_SUPPORTS_NODEFAULTLIBS_FLAG)
4848
list(APPEND CMAKE_REQUIRED_LIBRARIES c)
4949
endif ()
5050
if (LIBCXX_USE_COMPILER_RT)
51-
cmake_push_check_state()
52-
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${LIBCXX_COMPILE_FLAGS}")
51+
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -rtlib=compiler-rt")
5352
find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY)
54-
cmake_pop_check_state()
5553
list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBCXX_BUILTINS_LIBRARY}")
5654
elseif (LIBCXX_HAS_GCC_LIB)
5755
list(APPEND CMAKE_REQUIRED_LIBRARIES gcc)

libcxx/src/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ if (APPLE AND LLVM_USE_SANITIZER)
163163
message(WARNING "LLVM_USE_SANITIZER=${LLVM_USE_SANITIZER} is not supported on OS X")
164164
endif()
165165
if (LIBFILE)
166-
find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY)
167-
get_filename_component(LIBDIR "${LIBCXX_BUILTINS_LIBRARY}" DIRECTORY)
166+
find_compiler_rt_dir(LIBDIR)
168167
if (NOT IS_DIRECTORY "${LIBDIR}")
169168
message(FATAL_ERROR "Cannot find compiler-rt directory on OS X required for LLVM_USE_SANITIZER")
170169
endif()

0 commit comments

Comments
 (0)