Enabling -O4 (link-time optimization) only if compiler/linker support it. - The -O4 option requires the linker to have bitcode support, currently for clang this means using the gold linker and the LLVM-gold plugin: http://llvm.org/docs/GoldPlugin.html. - Otherwise you get (confusing) 'file format not recognised' errors ala: http://llvm.org/bugs/show_bug.cgi?id=9897. - Adding explicit check for LTO support as at least some package installs of clang on linux do not use the gold linker by default. Change-Id: I2a4c670e470d9b48da2a15b7e91a59fb4ad3e8ad
diff --git a/CMakeLists.txt b/CMakeLists.txt index e3fcd19..c3b536b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -614,7 +614,17 @@ ENDIF (${BUILD_ANDROID}) ENDIF (CMAKE_COMPILER_IS_GNUCXX) IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") - SET(CERES_CXX_FLAGS "${CERES_CXX_FLAGS} -O4") + # Use of -O4 requires use of gold linker & LLVM-gold plugin, which might + # well not be present / in use and without which files will compile, but + # not link ('file not recognized') so explicitly check for support + INCLUDE(CheckCXXCompilerFlag) + CHECK_CXX_COMPILER_FLAG("-O4" HAVE_LTO_SUPPORT) + IF (HAVE_LTO_SUPPORT) + MESSAGE(STATUS "Enabling link-time optimization (-O4)") + SET(CERES_CXX_FLAGS "${CERES_CXX_FLAGS} -O4") + ELSE() + MESSAGE(STATUS "Compiler/linker does not support link-time optimization (-O4), disabling.") + ENDIF (HAVE_LTO_SUPPORT) ENDIF() ENDIF (CMAKE_BUILD_TYPE STREQUAL "Release")