diff --git a/compiler-rt/CMakeLists.txt b/compiler-rt/CMakeLists.txt --- a/compiler-rt/CMakeLists.txt +++ b/compiler-rt/CMakeLists.txt @@ -413,7 +413,6 @@ append_list_if(COMPILER_RT_HAS_Z_TEXT -Wl,-z,text SANITIZER_COMMON_LINK_FLAGS) if (COMPILER_RT_USE_BUILTINS_LIBRARY) - list(APPEND SANITIZER_COMMON_LINK_LIBS ${COMPILER_RT_BUILTINS_LIBRARY}) string(REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}") else() if (ANDROID) diff --git a/compiler-rt/cmake/Modules/AddCompilerRT.cmake b/compiler-rt/cmake/Modules/AddCompilerRT.cmake --- a/compiler-rt/cmake/Modules/AddCompilerRT.cmake +++ b/compiler-rt/cmake/Modules/AddCompilerRT.cmake @@ -1,5 +1,6 @@ include(ExternalProject) include(CompilerRTUtils) +include(HandleCompilerRT) function(set_target_output_directories target output_dir) # For RUNTIME_OUTPUT_DIRECTORY variable, Multi-configuration generators @@ -233,6 +234,10 @@ set_output_name(output_name_${libname} ${name} ${arch}) endif() endif() + if(COMPILER_RT_USE_BUILTINS_LIBRARY AND NOT type STREQUAL "OBJECT") + get_compiler_rt_target(${arch} target) + find_compiler_rt_library(builtins ${target} builtins_${libname}) + endif() set(sources_${libname} ${LIB_SOURCES}) format_object_libs(sources_${libname} ${arch} ${LIB_OBJECT_LIBS}) set(libnames ${libnames} ${libname}) @@ -326,6 +331,9 @@ if(LIB_LINK_LIBS) target_link_libraries(${libname} PRIVATE ${LIB_LINK_LIBS}) endif() + if(builtins_${libname}) + target_link_libraries(${libname} PRIVATE ${builtins_${libname}}) + endif() if(${type} STREQUAL "SHARED") if(COMMAND llvm_setup_rpath) llvm_setup_rpath(${libname}) diff --git a/compiler-rt/cmake/Modules/HandleCompilerRT.cmake b/compiler-rt/cmake/Modules/HandleCompilerRT.cmake --- a/compiler-rt/cmake/Modules/HandleCompilerRT.cmake +++ b/compiler-rt/cmake/Modules/HandleCompilerRT.cmake @@ -1,24 +1,65 @@ -function(find_compiler_rt_library name variable) - set(CLANG_COMMAND ${CMAKE_CXX_COMPILER} ${SANITIZER_COMMON_CFLAGS} - "--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}") +# Check if compile-rt library file path exists. +# If found, cache the path in: +# COMPILER_RT_LIBRARY-- +# If err_flag is true OR path not found, emit a message and set: +# COMPILER_RT_LIBRARY-- 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() - 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( + 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) - string(REPLACE "builtins" "${name}" LIBRARY_FILE "${LIBRARY_FILE}") - if (NOT HAD_ERROR AND EXISTS "${LIBRARY_FILE}") - message(STATUS "Found compiler-rt ${name} library: ${LIBRARY_FILE}") - set(${variable} "${LIBRARY_FILE}" PARENT_SCOPE) - else() - message(STATUS "Failed to find compiler-rt ${name} library") + ) + 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 --- a/compiler-rt/cmake/config-ix.cmake +++ b/compiler-rt/cmake/config-ix.cmake @@ -16,7 +16,7 @@ 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) else() if (ANDROID) check_library_exists(gcc __gcc_personality_v0 "" COMPILER_RT_HAS_GCC_LIB)