From 71a00fc01bce3539de92a923c961735e53a2b0d2 Mon Sep 17 00:00:00 2001 From: "Daniel J. Hofmann" Date: Fri, 4 Sep 2015 20:59:53 +0200 Subject: [PATCH] Make lto detection more robust and not resetting cxx flags when lto fails. This refines the last commit of parallelizing lto. Discussion: this is ugly as hell, dispatching 1/ on the availability of the `-flto` flag, then 2/ on the compiler since GCC allows `-flto=n` whereas Clang for example does not. I tried setting the CMake property `INTERPROCEDURAL_OPTIMIZATION`, without any effect. All I could see was some lto related utilities in the cmake debug output, but not in the actual compiler or linker invocation. This would eliminate the need for our hacks, with 1/ using an option `WITH_LTO` setting `ON` by default, and based on this value setting the `INTERPROCEDURAL_OPTIMIZATION` flag with CMake doing the actual work of selecting the best LTO method on the target platform. By the way, this also fixes a bug where we reset the `CMAKE_CXX_FLAGS` to a variable that was never defined, resulting in setting the flags to an empty string. Yay CMake, as usual. References: - http://www.cmake.org/cmake/help/v3.0/prop_tgt/INTERPROCEDURAL_OPTIMIZATION.html --- CMakeLists.txt | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b7940fab0..ec99fca20 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -124,16 +124,21 @@ endif() if(CMAKE_BUILD_TYPE MATCHES Release) message(STATUS "Configuring OSRM in release mode") # Check if LTO is available - include(ProcessorCount) - ProcessorCount(NPROC) - set(LTO_FLAGS "") - check_cxx_compiler_flag("-flto=${NPROC}" LTO_AVAILABLE) + check_cxx_compiler_flag("-flto" LTO_AVAILABLE) if(LTO_AVAILABLE) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto=${NPROC}") + set(OLD_CXX_FLAGS ${CMAKE_CXX_FLAGS}) + # GCC in addition allows parallelizing LTO + if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") + include(ProcessorCount) + ProcessorCount(NPROC) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto=${NPROC}") + else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto") + endif() set(CHECK_LTO_SRC "int main(){return 0;}") check_cxx_source_compiles("${CHECK_LTO_SRC}" LTO_WORKS) if(LTO_WORKS) - message(STATUS "LTO working") + message(STATUS "LTO working") else() message(STATUS "LTO broken") set(CMAKE_CXX_FLAGS "${OLD_CXX_FLAGS}")