Skip to content

Commit ee0ac9f

Browse files
committed
Use nicer CMake functions for enabling LTO
Signed-off-by: Juan Cruz Viotti <[email protected]>
1 parent 54c5540 commit ee0ac9f

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

cmake/common/defaults.cmake

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,21 @@ endif()
9797
# Note we don't enable LTO on RelWithDebInfo, as it breaks debugging symbols
9898
# on at least AppleClang, making stepping through source code impossible.
9999

100+
# LTO is applied globally because it is a whole-program optimization.
101+
# Every translation unit must be compiled with LTO flags for the linker
102+
# to perform cross-module optimization effectively.
100103
if(CMAKE_BUILD_TYPE STREQUAL "Release")
101104
if(SOURCEMETA_COMPILER_GCC AND NOT BUILD_SHARED_LIBS)
102105
message(STATUS "Enabling Fat LTO")
103-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto -ffat-lto-objects")
104-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto")
105-
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -flto")
106+
add_compile_options(-flto -ffat-lto-objects)
107+
add_link_options(-flto)
106108
endif()
107109

108110
# TODO: Make this work on Linux on LLVM
109111
if(SOURCEMETA_COMPILER_LLVM AND NOT BUILD_SHARED_LIBS AND APPLE)
110112
message(STATUS "Enabling Fat LTO")
111-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto=full")
112-
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto=full")
113-
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -flto=full")
113+
add_compile_options(-flto=full)
114+
add_link_options(-flto=full)
114115
endif()
115116
endif()
116117

0 commit comments

Comments
 (0)