From 007847d844296e8ed43ccee36b776bc8e3184bdc Mon Sep 17 00:00:00 2001 From: jakoch Date: Sat, 14 Sep 2019 20:19:13 +0200 Subject: [PATCH] link runtime library statically (MD), tweak optimization levels for debug builds --- CMakeLists.txt | 67 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 63 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ae742d4..6bde4ef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,54 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/build") +# ================================================================================================== +# Options +# ================================================================================================== + +option(ENABLE_LTO "Enable link-time optimizations if supported by the compiler" OFF) + +# ================================================================================================== +# Link time optimizations (LTO) +# ================================================================================================== +if (ENABLE_LTO) + include(CheckIPOSupported) + + check_ipo_supported(RESULT IPO_SUPPORT) + + if (IPO_SUPPORT) + message(STATUS "LTO support is enabled") + set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) + endif() +endif() + +# ================================================================================================== +# OS specific +# ================================================================================================== + +if (WIN32) + # Link statically against c/c++ lib to avoid missing redistributables. + # Give users a choice to opt for the shared runtime if they want. + option(USE_STATIC_CRT "Link against the static runtime libraries." ON) + + # Generate the .def in order to get the .lib required when linking against dlls. + # cl.exe will not generate .lib without .def file (or without pragma + # __declspec(dllexport) in front of each functions). + set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) + + if (${USE_STATIC_CRT}) + set(CRT_FLAGS_RELEASE "/MT") + set(CRT_FLAGS_DEBUG "/MTd") + else() + set(CRT_FLAGS_RELEASE "/MD") + set(CRT_FLAGS_DEBUG "/MDd") + endif() + +endif() + +# ================================================================================================== +# Compiler flags +# ================================================================================================== + if(CMAKE_COMPILER_IS_GNUCXX) set(CMAKE_CXX_FLAGS "-std=c++14") set(CMAKE_EXE_LINKER_FLAGS "-l protobuf -pthread") @@ -28,9 +76,11 @@ if(MSVC) # Be quiet SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /nologo") - + # Optimize - SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /O2") + SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g") + SET(CMAKE_CXX_FLAGS_RELEASE "-O2") + SET(CMAKE_CXX_FLAGS_DEBUG "-O0 -g") # MSVC warning suppressions add_definitions(-D_CRT_SECURE_NO_WARNINGS) @@ -47,9 +97,18 @@ if(MSVC) message(STATUS "USING THESE CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}") endif() -# +# ================================================================================================== +# Linker flags +# ================================================================================================== + +# Link static runtime libraries +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MD") + +add_link_options("/nologo") + +# ================================================================================================== # Setup Dependencies -# +# ================================================================================================== message(STATUS "\n-- Dependencies:\n")