Skip to content

Commit 24241e4

Browse files
committed
cmake: use -flto=auto compiler flag when supported
Use -flto=auto compiler flag when supported, this silence this GCC warning: > lto-wrapper: warning: using serial compilation of # LTRANS jobs This also greatly speeds-up the linkage time as it enables LTO multithreading in GCC (either by using Make jobserver if detected, either by detecting CPU cores). Also always set LTO if enabled.
1 parent 3ab50c4 commit 24241e4

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

CMakeLists.txt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.5)
22

33
set(CMAKE_CXX_STANDARD 11)
44

5+
include(CheckCXXCompilerFlag)
6+
57
set(CRUNCH_PROJECT_NAME crunch)
68
set(CRUNCH_LIBRARY_NAME crn)
79
set(CRUNCH_EXE_NAME crunch)
@@ -50,6 +52,14 @@ macro(set_linker_flag FLAG)
5052
endif()
5153
endmacro()
5254

55+
macro(try_cxx_flag PROP FLAG)
56+
check_CXX_compiler_flag(${FLAG} FLAG_${PROP})
57+
58+
if (FLAG_${PROP})
59+
set_cxx_flag(${FLAG})
60+
endif()
61+
endmacro()
62+
5363
# This option decides if crunch is dynamically linked against libcrn.so
5464
# statically linked against libcrn.o, enabling it always build libcrn.so.
5565
# This option is a builtin CMake one, the name means “build executables
@@ -125,9 +135,12 @@ else()
125135
# It should be done at the very end because it copies all compiler flags
126136
# to the linker flags.
127137
if (USE_LTO)
128-
set_cxx_flag("-flto" RELEASE)
129-
set_cxx_flag("-flto" RELWITHDEBINFO)
130-
set_cxx_flag("-flto" MINSIZEREL)
138+
try_cxx_flag(FLTO_AUTO "-flto=auto")
139+
140+
if (NOT FLAG_FLTO_AUTO)
141+
try_cxx_flag(FLTO "-flto")
142+
endif()
143+
131144
set_linker_flag("${CMAKE_CXX_FLAGS}" RELEASE)
132145
set_linker_flag("${CMAKE_CXX_FLAGS}" RELWITHDEBINFO)
133146
set_linker_flag("${CMAKE_CXX_FLAGS}" MINSIZEREL)

0 commit comments

Comments
 (0)