diff options
author | Brad King <brad.king@kitware.com> | 2017-05-01 13:15:40 (GMT) |
---|---|---|
committer | Kitware Robot <kwrobot@kitware.com> | 2017-05-01 13:15:46 (GMT) |
commit | a2a13e8b48b5b55f2c6fd4e457936f91fe23f699 (patch) | |
tree | d6ab0ff0dd4c62d1ceb864fcaa262dd0f50fba47 /Modules | |
parent | 6b7ec8ea1c3cfbfeb46c5f12a3304f292a1b88bb (diff) | |
parent | 7d057b2738e229192ae5afe71d50354681813235 (diff) | |
download | CMake-a2a13e8b48b5b55f2c6fd4e457936f91fe23f699.zip CMake-a2a13e8b48b5b55f2c6fd4e457936f91fe23f699.tar.gz CMake-a2a13e8b48b5b55f2c6fd4e457936f91fe23f699.tar.bz2 |
Merge topic 'ipo-clang'
7d057b27 Clang IPO (LTO) support
Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !717
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/Compiler/Clang-FindBinUtils.cmake | 25 | ||||
-rw-r--r-- | Modules/Compiler/Clang.cmake | 54 |
2 files changed, 73 insertions, 6 deletions
diff --git a/Modules/Compiler/Clang-FindBinUtils.cmake b/Modules/Compiler/Clang-FindBinUtils.cmake new file mode 100644 index 0000000..c81e77a --- /dev/null +++ b/Modules/Compiler/Clang-FindBinUtils.cmake @@ -0,0 +1,25 @@ +if(NOT DEFINED _CMAKE_PROCESSING_LANGUAGE OR _CMAKE_PROCESSING_LANGUAGE STREQUAL "") + message(FATAL_ERROR "Internal error: _CMAKE_PROCESSING_LANGUAGE is not set") +endif() + +# Try to find tools in the same directory as Clang itself +get_filename_component(__clang_hint_1 "${CMAKE_${_CMAKE_PROCESSING_LANGUAGE}_COMPILER}" REALPATH) +get_filename_component(__clang_hint_1 "${__clang_hint_1}" DIRECTORY) + +get_filename_component(__clang_hint_2 "${CMAKE_${_CMAKE_PROCESSING_LANGUAGE}_COMPILER}" DIRECTORY) + +set(__clang_hints ${__clang_hint_1} ${__clang_hint_2}) + +# http://manpages.ubuntu.com/manpages/precise/en/man1/llvm-ar.1.html +find_program(CMAKE_${_CMAKE_PROCESSING_LANGUAGE}_COMPILER_AR NAMES + "${_CMAKE_TOOLCHAIN_PREFIX}llvm-ar" + HINTS ${__clang_hints} + DOC "LLVM archiver" +) + +# http://manpages.ubuntu.com/manpages/precise/en/man1/llvm-ranlib.1.html +find_program(CMAKE_${_CMAKE_PROCESSING_LANGUAGE}_COMPILER_RANLIB NAMES + "${_CMAKE_TOOLCHAIN_PREFIX}llvm-ranlib" + HINTS ${__clang_hints} + DOC "Generate index for LLVM archive" +) diff --git a/Modules/Compiler/Clang.cmake b/Modules/Compiler/Clang.cmake index 6b99a08..8c2f87d 100644 --- a/Modules/Compiler/Clang.cmake +++ b/Modules/Compiler/Clang.cmake @@ -28,12 +28,54 @@ else() set(CMAKE_${lang}_COMPILE_OPTIONS_EXTERNAL_TOOLCHAIN "--gcc-toolchain=") endif() - set(_CMAKE_IPO_SUPPORTED_BY_CMAKE NO) - set(_CMAKE_IPO_MAY_BE_SUPPORTED_BY_COMPILER NO) + set(_CMAKE_IPO_SUPPORTED_BY_CMAKE YES) + set(_CMAKE_IPO_MAY_BE_SUPPORTED_BY_COMPILER YES) - unset(CMAKE_${lang}_COMPILE_OPTIONS_IPO) - unset(CMAKE_${lang}_ARCHIVE_CREATE_IPO) - unset(CMAKE_${lang}_ARCHIVE_APPEND_IPO) - unset(CMAKE_${lang}_ARCHIVE_FINISH_IPO) + string(COMPARE EQUAL "${CMAKE_${lang}_COMPILER_ID}" "AppleClang" __is_apple_clang) + + # '-flto=thin' available since Clang 3.9 and Xcode 8 + # * http://clang.llvm.org/docs/ThinLTO.html#clang-llvm + # * https://trac.macports.org/wiki/XcodeVersionInfo + set(_CMAKE_LTO_THIN TRUE) + if(__is_apple_clang) + if(CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 8.0) + set(_CMAKE_LTO_THIN FALSE) + endif() + else() + if(CMAKE_${lang}_COMPILER_VERSION VERSION_LESS 3.9) + set(_CMAKE_LTO_THIN FALSE) + endif() + endif() + + if(_CMAKE_LTO_THIN) + set(CMAKE_${lang}_COMPILE_OPTIONS_IPO "-flto=thin") + else() + set(CMAKE_${lang}_COMPILE_OPTIONS_IPO "-flto") + endif() + + if(ANDROID) + # https://github.com/android-ndk/ndk/issues/242 + set(CMAKE_${lang}_LINK_OPTIONS_IPO "-fuse-ld=gold") + endif() + + if(ANDROID OR __is_apple_clang) + set(__ar "${CMAKE_AR}") + set(__ranlib "${CMAKE_RANLIB}") + else() + set(__ar "${CMAKE_${lang}_COMPILER_AR}") + set(__ranlib "${CMAKE_${lang}_COMPILER_RANLIB}") + endif() + + set(CMAKE_${lang}_ARCHIVE_CREATE_IPO + "${__ar} cr <TARGET> <LINK_FLAGS> <OBJECTS>" + ) + + set(CMAKE_${lang}_ARCHIVE_APPEND_IPO + "${__ar} r <TARGET> <LINK_FLAGS> <OBJECTS>" + ) + + set(CMAKE_${lang}_ARCHIVE_FINISH_IPO + "${__ranlib} <TARGET>" + ) endmacro() endif() |