diff options
author | Petr Hosek <phosek@chromium.org> | 2018-05-18 01:10:09 +0000 |
---|---|---|
committer | Petr Hosek <phosek@chromium.org> | 2018-05-18 01:10:09 +0000 |
commit | 2dd07cd53a1eee60554f58ab232ce9e546d0e45d (patch) | |
tree | c82797a4dc719caf290307d2f43e0fb7e647875c /cmake/Modules | |
parent | 458805f19b875ba108f5e6e1f342ecf498fb297f (diff) |
[CMake] Detect the compiler runtime and standard library
Rather then requiring the user to specify runtime the compiler
runtime and C++ standard library, or trying to guess them which is
error-prone, use auto-detection by parsing the compiler link output.
Differential Revision: https://reviews.llvm.org/D46857
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@332683 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'cmake/Modules')
-rw-r--r-- | cmake/Modules/CompilerRTUtils.cmake | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/cmake/Modules/CompilerRTUtils.cmake b/cmake/Modules/CompilerRTUtils.cmake index 7ef1fc8d9..d8bbdf638 100644 --- a/cmake/Modules/CompilerRTUtils.cmake +++ b/cmake/Modules/CompilerRTUtils.cmake @@ -318,3 +318,58 @@ function(filter_builtin_sources output_var exclude_or_include excluded_list) endforeach () set(${output_var} ${intermediate} PARENT_SCOPE) endfunction() + +function(check_runtime_library variable) + set(COMPILER_COMMAND ${CMAKE_CXX_COMPILER} ${SANITIZER_COMMON_CFLAGS} + "--print-libgcc-file-name") + if (CMAKE_CXX_COMPILER_ID MATCHES Clang AND CMAKE_CXX_COMPILER_TARGET) + list(APPEND COMPILER_COMMAND "--target=${CMAKE_CXX_COMPILER_TARGET}") + endif() + execute_process( + COMMAND ${COMPILER_COMMAND} + RESULT_VARIABLE COMMAND_RESULT + OUTPUT_VARIABLE LIBRARY_FILE + ) + string(STRIP "${LIBRARY_FILE}" LIBRARY_FILE) + if(COMMAND_RESULT EQUAL 0) + set(${variable} "${LIBRARY_FILE}" PARENT_SCOPE) + endif() +endfunction() + +function(check_link_libraries variable) + set(COMPILER_COMMAND "${CMAKE_CXX_COMPILER}" "${SANITIZER_COMMON_CFLAGS}" + "${CMAKE_ROOT}/Modules/DummyCXXFile.cxx" "-###") + if (CMAKE_CXX_COMPILER_ID MATCHES Clang AND CMAKE_CXX_COMPILER_TARGET) + list(APPEND COMPILER_COMMAND "--target=${CMAKE_CXX_COMPILER_TARGET}") + endif() + execute_process( + COMMAND ${COMPILER_COMMAND} + RESULT_VARIABLE COMMAND_RESULT + ERROR_VARIABLE COMMAND_OUTPUT + ) + if(COMMAND_RESULT EQUAL 0) + get_filename_component(linker ${CMAKE_LINKER} NAME) + string(REGEX REPLACE "([][+.*?()^$])" "\\\\\\1" linker "${linker}") + set(LINKER_REGEX "^( *|.*[/\\])(${linker}|([^/\\]+-)?ld|collect2)[^/\\]*( |$)") + set(EXCLUDE_REGEX "collect2 version |^[A-Za-z0-9_]+=") + string(REGEX REPLACE "\r?\n" ";" output_lines "${COMMAND_OUTPUT}") + foreach(line IN LISTS output_lines) + if(line MATCHES "${LINKER_REGEX}" AND NOT line MATCHES "${EXCLUDE_REGEX}") + separate_arguments(args NATIVE_COMMAND "${line}") + list(GET args 0 cmd) + endif() + if("${cmd}" MATCHES "${LINKER_REGEX}") + foreach(arg IN LISTS args) + if("${arg}" MATCHES "^-l([^:].*)$") + set(lib "${CMAKE_MATCH_1}") + list(APPEND libs ${lib}) + elseif("${arg}" MATCHES "^(.:)?[/\\].*\\.a$") + list(APPEND libs ${arg}) + endif() + endforeach() + break() + endif() + endforeach() + set(${variable} "${libs}" PARENT_SCOPE) + endif() +endfunction() |