Skip to content
Permalink
Browse files

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 <rkitover@gmail.com>
  • Loading branch information...
rkitover committed Mar 26, 2019
1 parent f2e9dc5 commit 7fb27c4df9382312f7a8d9999006bfc83c705f06
Showing with 16 additions and 1 deletion.
  1. +16 −1 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)

0 comments on commit 7fb27c4

Please sign in to comment.
You can’t perform that action at this time.