From 7fb27c4df9382312f7a8d9999006bfc83c705f06 Mon Sep 17 00:00:00 2001 From: Rafael Kitover Date: Tue, 26 Mar 2019 14:49:48 +0000 Subject: [PATCH] cmake: check for broken LTO When using LTO with gcc or clang, try to compile and link something to make sure it works, and disable it if it doesn't. Signed-off-by: Rafael Kitover --- CMakeLists.txt | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f387bb8c..ad27af6b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -357,7 +357,7 @@ if(CMAKE_C_COMPILER_ID STREQUAL Clang AND CMAKE_CXX_COMPILER_ID STREQUAL Clang) endif() if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID STREQUAL Clang) - set(LTO_FLAGS "") + unset(LTO_FLAGS) if(ENABLE_LTO) if(CMAKE_COMPILER_IS_GNUCXX) if(num_cpus GREATER 1) @@ -368,6 +368,21 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID STREQUAL Clang) else() set(LTO_FLAGS -flto) endif() + + # check that LTO is not broken before enabling it + set(ORIG_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) + set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${LTO_FLAGS}") + + include(CheckCXXSourceCompiles) + check_cxx_source_compiles("int main(int argc, char** argv) { return 0; }" LTO_WORKS) + + set(CMAKE_REQUIRED_FLAGS ${ORIG_CMAKE_REQUIRED_FLAGS}) + + if(NOT LTO_WORKS) + message(WARNING "LTO does not seem to be working on your system, if using clang make sure LLVMGold is installed") + unset(LTO_FLAGS) + set(ENABLE_LTO OFF) + endif() endif() unset(MY_C_OPT_FLAGS)