Skip to content

Commit b50245c

Browse files
committed
[Chore] (CMake): Enable LTO.
1 parent 016d374 commit b50245c

1 file changed

Lines changed: 46 additions & 3 deletions

File tree

MobileGlues-cpp/CMakeLists.txt

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,59 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
1212

1313
if(MACOS)
1414
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w -fvisibility=default -funwind-tables -g -D_THREAD_SAFE -fPIC -stdlib=libc++")
15-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -w -g -std=gnu99 -funwind-tables -O3 -fvisibility=hidden")
15+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -w -g -std=gnu99 -funwind-tables -fvisibility=hidden")
1616
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++")
1717
else()
1818
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -w -fvisibility=hidden -funwind-tables -g -D_THREAD_SAFE -fPIC")
19-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -w -g -std=gnu99 -funwind-tables -O3 -fvisibility=hidden")
19+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -w -g -std=gnu99 -funwind-tables -fvisibility=hidden")
2020
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++")
2121
set(CMAKE_ANDROID_STL_TYPE c++_static)
2222
endif()
2323

24-
#set(CMAKE_BUILD_TYPE Release)
24+
if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
25+
# Check if ThinLTO or LTO is suppported
26+
include(CheckIPOSupported)
27+
include(CheckCCompilerFlag)
28+
include(CheckCXXCompilerFlag)
29+
30+
check_ipo_supported(RESULT LTOSupported OUTPUT LTOError)
31+
32+
check_c_compiler_flag("-flto" HAS_LTO_C)
33+
check_cxx_compiler_flag("-flto" HAS_LTO_CXX)
34+
35+
if (LTOSupported OR (HAS_LTO_C AND HAS_LTO_CXX))
36+
# Check ThinLTO
37+
check_c_compiler_flag("-flto=thin" HAS_THINLTO_C)
38+
check_cxx_compiler_flag("-flto=thin" HAS_THINLTO_CXX)
39+
if (HAS_THINLTO_C AND HAS_THINLTO_CXX)
40+
message(STATUS "ThinLTO supported, using -flto=thin")
41+
42+
add_compile_options(-flto=thin)
43+
add_link_options(-flto=thin)
44+
else()
45+
# ThinLTO is not supported
46+
message(STATUS "ThinLTO not available, fallback to CMAKE IPO")
47+
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
48+
endif()
49+
else()
50+
message(STATUS "IPO not supported: ${LTOError}")
51+
endif()
52+
endif()
53+
54+
55+
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang" AND NOT MATCHES "AppleClang")
56+
add_compile_options(-O3 -ffunction-sections -fdata-sections)
57+
add_link_options(-Wl,--gc-sections)
58+
elseif (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
59+
# add_compile_options(/O2)
60+
else ()
61+
add_compile_options(-O2)
62+
endif()
63+
64+
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
65+
add_compile_options(/Zc:preprocessor)
66+
add_compile_options(/Zc:__cplusplus)
67+
endif()
2568

2669
set(PROFILING OFF)
2770

0 commit comments

Comments
 (0)