From de89bd05615bb1cbf31c4e7b3c580b309874d06e Mon Sep 17 00:00:00 2001 From: Rafael Kitover Date: Tue, 19 Sep 2017 06:58:48 -0700 Subject: [PATCH] use num cpu cores to parallelize LTO link with gcc Use the `cmake` `ProcessorCount` module to determine the number of CPU threads, and set the `-flto` flag for gcc to `-flto=${num_cpus}` if the number of CPU threads is detected and is greater than `1`. clang does not support this. --- CMakeLists.txt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cddc983e..f4d20a0e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -307,13 +307,20 @@ IF(WIN32) ENDIF() ENDIF() +include(ProcessorCount) +ProcessorCount(num_cpus) + # Compiler flags IF(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID STREQUAL Clang) SET(LTO_FLAGS "") IF(ENABLE_LTO) IF(CMAKE_COMPILER_IS_GNUCXX) - SET(LTO_FLAGS -flto=10 -ffat-lto-objects) + if(num_cpus GREATER 1) + SET(LTO_FLAGS -flto=${num_cpus} -ffat-lto-objects) + else() + SET(LTO_FLAGS -flto -ffat-lto-objects) + endif() ELSE() SET(LTO_FLAGS -flto) ENDIF()