From c4786c42e412bf884c5683a96d17a78fe157fc5f Mon Sep 17 00:00:00 2001 From: Beats Date: Sat, 12 Oct 2024 19:29:22 -0300 Subject: [PATCH] perf: build cmake (#2972) --- CMakeLists.txt | 63 +++++++-------------------- cmake/modules/BaseConfig.cmake | 79 ++++++++++++++++++++-------------- cmake/modules/CanaryLib.cmake | 57 ++++++++++-------------- src/CMakeLists.txt | 29 +++++-------- src/kv/value_wrapper.hpp | 4 +- tests/CMakeLists.txt | 9 ++++ 6 files changed, 108 insertions(+), 133 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5bee518ecd6..3d143fec490 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,33 +7,31 @@ cmake_minimum_required(VERSION 3.22 FATAL_ERROR) # VCPKG # cmake -DCMAKE_TOOLCHAIN_FILE=/opt/workspace/vcpkg/scripts/buildsystems/vcpkg.cmake .. -# Needed libs is in file vcpkg.json +# Needed libs are in file vcpkg.json # Windows required libs: .\vcpkg install --triplet x64-windows asio pugixml spdlog curl protobuf parallel-hashmap magic-enum mio luajit libmariadb mpir abseil bshoshany-thread-pool + if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE) - set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" - CACHE STRING "") + set(CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake" + CACHE STRING "") endif() if(DEFINED ENV{VCPKG_DEFAULT_TRIPLET} AND NOT DEFINED VCPKG_TARGET_TRIPLET) - set(VCPKG_TARGET_TRIPLET "$ENV{VCPKG_DEFAULT_TRIPLET}" CACHE STRING "") + set(VCPKG_TARGET_TRIPLET "$ENV{VCPKG_DEFAULT_TRIPLET}" CACHE STRING "") endif() set(VCPKG_FEATURE_FLAGS "versions") set(VCPKG_BUILD_TYPE "release") set(CMAKE_EXPORT_COMPILE_COMMANDS ON) - - # ***************************************************************************** # Project canary # ***************************************************************************** if(CMAKE_BUILD_TYPE STREQUAL "Debug") - project(canary-debug) + project(canary-debug LANGUAGES CXX) else() - project(canary) + project(canary LANGUAGES CXX) endif() - # ***************************************************************************** # Append cmake search path # ***************************************************************************** @@ -57,7 +55,7 @@ option(FEATURE_METRICS "Enable metrics feature" OFF) # Options Code # ***************************************************************************** -if(FEATURE_METRIC) +if(FEATURE_METRICS) log_option_enabled("metrics") else () log_option_disabled("metrics") @@ -69,52 +67,21 @@ if(OPTIONS_ENABLE_CCACHE) if(CCACHE) log_option_enabled("ccache") set(CMAKE_CXX_COMPILER_LAUNCHER ${CCACHE}) - else() + else() log_option_disabled("ccache") endif() endif() - # === SCCACHE === if(OPTIONS_ENABLE_SCCACHE) - find_program(SCCACHE_PATH sccache) - if(SCCACHE_PATH) - log_option_enabled("sccache") - set(CMAKE_C_COMPILER_LAUNCHER ${SCCACHE_PATH}) - set(CMAKE_CXX_COMPILER_LAUNCHER ${SCCACHE_PATH}) - else() - log_option_disabled("sccache") - endif() -endif() - - -# === IPO === -if(OPTIONS_ENABLE_IPO) - if(MSVC) - log_option_enabled("IPO/LTO") - set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} /GL") - set(CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO} /LTCG") - set(CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_STATIC_LINKER_FLAGS_RELWITHDEBINFO} /LTCG") - set(CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_MODULE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG") - set(CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO "${CMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO} /LTCG") + find_program(SCCACHE_PATH sccache) + if(SCCACHE_PATH) + log_option_enabled("sccache") + set(CMAKE_C_COMPILER_LAUNCHER ${SCCACHE_PATH}) + set(CMAKE_CXX_COMPILER_LAUNCHER ${SCCACHE_PATH}) else() - if(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo" OR CMAKE_BUILD_TYPE STREQUAL "Release") - log_option_enabled("IPO/LTO") - include(CheckIPOSupported) - check_ipo_supported(RESULT result OUTPUT output) - if(result) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto=auto") - message(STATUS "IPO/LTO enabled with -flto=auto for non-MSVC compiler.") - set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE) - else() - log_war("IPO/LTO not supported: ${output}") - endif() - else() - log_option_disabled("IPO/LTO") - endif () + log_option_disabled("sccache") endif() -else() - log_option_disabled("IPO/LTO") endif() option(BUILD_TESTS "Build tests" OFF) # By default, tests will not be built diff --git a/cmake/modules/BaseConfig.cmake b/cmake/modules/BaseConfig.cmake index f61ee9e809c..f987020db26 100644 --- a/cmake/modules/BaseConfig.cmake +++ b/cmake/modules/BaseConfig.cmake @@ -3,9 +3,7 @@ cmake_minimum_required(VERSION 3.22 FATAL_ERROR) # ***************************************************************************** # CMake Features # ***************************************************************************** -set(CMAKE_CXX_STANDARD 20) -set(GNUCXX_MINIMUM_VERSION 11) -set(MSVC_MINIMUM_VERSION "19.32") +set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_DISABLE_SOURCE_CHANGES ON) @@ -15,9 +13,6 @@ set(Boost_NO_WARN_NEW_VERSIONS ON) # Make will print more details set(CMAKE_VERBOSE_MAKEFILE OFF) -# Generate compile_commands.json -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) - # ***************************************************************************** # Packages / Libs # ***************************************************************************** @@ -50,21 +45,21 @@ find_path(BOOST_DI_INCLUDE_DIRS "boost/di.hpp") # === GCC Minimum Version === if (CMAKE_COMPILER_IS_GNUCXX) message("-- Compiler: GCC - Version: ${CMAKE_CXX_COMPILER_VERSION}") - if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS GNUCXX_MINIMUM_VERSION) - message(FATAL_ERROR "GCC version must be at least ${GNUCXX_MINIMUM_VERSION}!") + if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 11) + message(FATAL_ERROR "GCC version must be at least 11!") endif() endif() # === Minimum required version for visual studio === if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") message("-- Compiler: Visual Studio - Version: ${CMAKE_CXX_COMPILER_VERSION}") - if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS MSVC_MINIMUM_VERSION) - message(FATAL_ERROR "Visual Studio version must be at least ${MSVC_MINIMUM_VERSION}") + if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS "19.32") + message(FATAL_ERROR "Visual Studio version must be at least 19.32") endif() endif() # ***************************************************************************** -# Sanity Checks +# Options # ***************************************************************************** option(TOGGLE_BIN_FOLDER "Use build/bin folder for generate compilation files" ON) option(OPTIONS_ENABLE_OPENMP "Enable Open Multi-Processing support." ON) @@ -79,14 +74,14 @@ if(TOGGLE_BIN_FOLDER) log_option_enabled("TOGGLE_BIN_FOLDER") else() log_option_disabled("TOGGLE_BIN_FOLDER") -endif(TOGGLE_BIN_FOLDER) +endif() -# === TOGGLE_BIN_FOLDER === +# === OPTIONS_ENABLE_OPENMP === if(OPTIONS_ENABLE_OPENMP) log_option_enabled("OPTIONS_ENABLE_OPENMP") else() log_option_disabled("OPTIONS_ENABLE_OPENMP") -endif(OPTIONS_ENABLE_OPENMP) +endif() # === DEBUG LOG === # cmake -DDEBUG_LOG=ON .. @@ -95,7 +90,7 @@ if(DEBUG_LOG) log_option_enabled("DEBUG LOG") else() log_option_disabled("DEBUG LOG") -endif(DEBUG_LOG) +endif() # === ASAN === if(ASAN_ENABLED) @@ -113,7 +108,6 @@ endif() # === BUILD_STATIC_LIBRARY === if(BUILD_STATIC_LIBRARY) log_option_enabled("STATIC_LIBRARY") - if(MSVC) set(CMAKE_FIND_LIBRARY_SUFFIXES ".lib") elseif(UNIX AND NOT APPLE) @@ -130,14 +124,36 @@ if(SPEED_UP_BUILD_UNITY) log_option_enabled("SPEED_UP_BUILD_UNITY") else() log_option_disabled("SPEED_UP_BUILD_UNITY") -endif(SPEED_UP_BUILD_UNITY) +endif() # === USE_PRECOMPILED_HEADER === if(USE_PRECOMPILED_HEADER) log_option_enabled("USE_PRECOMPILED_HEADER") else() log_option_disabled("USE_PRECOMPILED_HEADER") -endif(USE_PRECOMPILED_HEADER) +endif() + +# === IPO Configuration === +function(configure_linking target_name) + if(OPTIONS_ENABLE_IPO) + include(CheckIPOSupported) + check_ipo_supported(RESULT ipo_supported OUTPUT ipo_output LANGUAGES CXX) + if(ipo_supported) + set_property(TARGET ${target_name} PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) + message(STATUS "IPO/LTO enabled for target ${target_name}.") + + if(MSVC) + target_compile_options(${target_name} PRIVATE /GL) + target_link_options(${target_name} PRIVATE /LTCG) + elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") + target_compile_options(${target_name} PRIVATE -flto) + target_link_options(${target_name} PRIVATE -flto) + endif() + else() + message(WARNING "IPO/LTO is not supported for target ${target_name}: ${ipo_output}") + endif() + endif() +endfunction() # ***************************************************************************** # Compiler Options @@ -147,39 +163,38 @@ if (MSVC) string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_${type} "${CMAKE_CXX_FLAGS_${type}}") string(REPLACE "/Zi" "/Z7" CMAKE_C_FLAGS_${type} "${CMAKE_C_FLAGS_${type}}") endforeach(type) - add_compile_options(/MP /FS /Zf /EHsc) else() add_compile_options(-Wno-unused-parameter -Wno-sign-compare -Wno-switch -Wno-implicit-fallthrough -Wno-extra) endif() -## Link compilation files to build/bin folder, else link to the main dir +# === Compiler Features === +add_library(project_options INTERFACE) +target_compile_features(project_options INTERFACE cxx_std_23) + +# ***************************************************************************** +# Output Directory Function +# ***************************************************************************** function(set_output_directory target_name) if (TOGGLE_BIN_FOLDER) set_target_properties(${target_name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" - ) + ) else() set_target_properties(${target_name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/" - ) + ) endif() endfunction() -## Setup shared target basic configurations +# ***************************************************************************** +# Setup Target Function +# ***************************************************************************** function(setup_target TARGET_NAME) if (MSVC AND BUILD_STATIC_LIBRARY) set_property(TARGET ${TARGET_NAME} PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") endif() + target_link_libraries(${TARGET_NAME} PUBLIC project_options) endfunction() - -# ***************************************************************************** -# DEBUG: Print cmake variables -# ***************************************************************************** -#get_cmake_property(_variableNames VARIABLES) -#list (SORT _variableNames) -#foreach (_variableName ${_variableNames}) -# message(STATUS "${_variableName}=${${_variableName}}") -#endforeach() diff --git a/cmake/modules/CanaryLib.cmake b/cmake/modules/CanaryLib.cmake index 84d1c96c214..fbb7aad27c0 100644 --- a/cmake/modules/CanaryLib.cmake +++ b/cmake/modules/CanaryLib.cmake @@ -22,11 +22,10 @@ add_subdirectory(utils) # Add more global sources - please add preferably in the sub_directory CMakeLists. target_sources(${PROJECT_NAME}_lib PRIVATE canary_server.cpp) -# Add public pre compiler header to lib, to pass down to related targets -target_precompile_headers(${PROJECT_NAME}_lib PUBLIC pch.hpp) - -if(NOT SPEED_UP_BUILD_UNITY AND USE_PRECOMPILED_HEADERS) - target_compile_definitions(${PROJECT_NAME}_lib PUBLIC -DUSE_PRECOMPILED_HEADERS) +# Conditional Precompiled Headers +if(USE_PRECOMPILED_HEADER) + target_precompile_headers(${PROJECT_NAME}_lib PUBLIC pch.hpp) + target_compile_definitions(${PROJECT_NAME}_lib PUBLIC USE_PRECOMPILED_HEADERS) endif() # ***************************************************************************** @@ -36,38 +35,21 @@ if (CMAKE_COMPILER_IS_GNUCXX) target_compile_options(${PROJECT_NAME}_lib PRIVATE -Wno-deprecated-declarations) endif() -# Sets the NDEBUG macro for RelWithDebInfo and Release configurations. -# This disables assertions in these configurations, optimizing the code for performance -# and reducing debugging overhead, while keeping debug information available for diagnostics. +# Sets the NDEBUG macro for Release and RelWithDebInfo configurations. target_compile_definitions(${PROJECT_NAME}_lib PUBLIC - $<$:NDEBUG> - $<$:NDEBUG> + $<$:NDEBUG> + $<$:NDEBUG> ) -# === IPO === -if(MSVC) - target_compile_options(${PROJECT_NAME}_lib PRIVATE "/GL") - set_target_properties(${PROJECT_NAME}_lib PROPERTIES - STATIC_LINKER_FLAGS "/LTCG" - SHARED_LINKER_FLAGS "/LTCG" - MODULE_LINKER_FLAGS "/LTCG" - EXE_LINKER_FLAGS "/LTCG") -else() - include(CheckIPOSupported) - check_ipo_supported(RESULT result) - if(result) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto=auto") - message(STATUS "IPO/LTO enabled with -flto=auto for non-MSVC compiler.") - set_property(TARGET ${PROJECT_NAME}_lib PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) - else() - message(WARNING "IPO/LTO is not supported: ${output}") - endif() -endif() +# Configurar IPO e Linkagem Incremental +configure_linking(${PROJECT_NAME}_lib) # === UNITY BUILD (compile time reducer) === if(SPEED_UP_BUILD_UNITY) set_target_properties(${PROJECT_NAME}_lib PROPERTIES UNITY_BUILD ON) log_option_enabled("Build unity for speed up compilation") +else() + log_option_disabled("Build unity") endif() # ***************************************************************************** @@ -80,13 +62,13 @@ target_include_directories(${PROJECT_NAME}_lib ${GMP_INCLUDE_DIRS} ${LUAJIT_INCLUDE_DIRS} ${PARALLEL_HASHMAP_INCLUDE_DIRS} - ) +) # ***************************************************************************** # Target links to external dependencies # ***************************************************************************** target_link_libraries(${PROJECT_NAME}_lib - PUBLIC + PUBLIC ${GMP_LIBRARIES} ${LUAJIT_LIBRARIES} CURL::libcurl @@ -108,7 +90,6 @@ target_link_libraries(${PROJECT_NAME}_lib if(FEATURE_METRICS) add_definitions(-DFEATURE_METRICS) - target_link_libraries(${PROJECT_NAME}_lib PUBLIC opentelemetry-cpp::common @@ -134,11 +115,10 @@ if (MSVC) else() set(VCPKG_TARGET_TRIPLET "x64-windows" CACHE STRING "") endif() - target_link_libraries(${PROJECT_NAME}_lib PUBLIC ${CMAKE_THREAD_LIBS_INIT} ${MYSQL_CLIENT_LIBS}) else() target_link_libraries(${PROJECT_NAME}_lib PUBLIC Threads::Threads) -endif (MSVC) +endif() # === OpenMP === if(OPTIONS_ENABLE_OPENMP) @@ -150,3 +130,12 @@ if(OPTIONS_ENABLE_OPENMP) else() log_option_disabled("openmp") endif() + +# === Optimization Flags === +if(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo" OR CMAKE_BUILD_TYPE STREQUAL "Release") + if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") + target_compile_options(${PROJECT_NAME}_lib PRIVATE -O3 -march=native) + elseif(MSVC) + target_compile_options(${PROJECT_NAME}_lib PRIVATE /O2) + endif() +endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2abd46f06c2..50bea4189ff 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,24 +15,17 @@ if(MSVC) target_sources(${PROJECT_NAME} PRIVATE ../cmake/canary.rc) endif() -if (UNIX) - - if(CMAKE_BUILD_TYPE STREQUAL "Debug") - target_compile_options(${PROJECT_NAME}_lib - PRIVATE - -Wall -Wextra -Wpedantic - ) - endif() - - if(CMAKE_BUILD_TYPE STREQUAL "Debug") - target_compile_options(${PROJECT_NAME} - PRIVATE - -Wall -Wextra -Wpedantic - ) - endif() - -endif (UNIX) - setup_target(${PROJECT_NAME}) set_output_directory(${PROJECT_NAME}) target_link_libraries(${PROJECT_NAME} PRIVATE ${PROJECT_NAME}_lib) + +# Configurar IPO e Linkagem Incremental para o executável +configure_linking(${PROJECT_NAME}) + +# Compiler warnings and options +if (UNIX) + if(CMAKE_BUILD_TYPE STREQUAL "Debug") + target_compile_options(${PROJECT_NAME}_lib PRIVATE -Wall -Wextra -Wpedantic) + target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wpedantic) + endif() +endif() diff --git a/src/kv/value_wrapper.hpp b/src/kv/value_wrapper.hpp index 889b1776de2..df1ad85e8df 100644 --- a/src/kv/value_wrapper.hpp +++ b/src/kv/value_wrapper.hpp @@ -169,7 +169,7 @@ inline bool ValueWrapper::operator==(const ValueWrapper &rhs) const { inline bool operator==(const ValueVariant &lhs, const ValueVariant &rhs) { return std::visit( - [](const auto &a, const auto &b) { + [](const auto &a, const auto &b) -> bool { using A = std::decay_t; using B = std::decay_t; @@ -190,6 +190,8 @@ inline bool operator==(const ValueVariant &lhs, const ValueVariant &rhs) { if constexpr (std::is_same_v) { return a == b; } + + return false; }, lhs, rhs ); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 5cf5c5d80ee..3838528bd23 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -9,6 +9,15 @@ function(setup_test TARGET_NAME DIR) target_link_libraries(${TARGET_NAME} PRIVATE Boost::ut ${PROJECT_NAME}_lib) target_include_directories(${TARGET_NAME} PRIVATE ${CMAKE_SOURCE_DIR}/tests/fixture PRIVATE ${CMAKE_SOURCE_DIR}/tests/${DIR}) + if(SPEED_UP_BUILD_UNITY AND (CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo" OR CMAKE_BUILD_TYPE STREQUAL "Release")) + set_target_properties(${TARGET_NAME} PROPERTIES UNITY_BUILD ON) + log_option_enabled("Build unity for speed up compilation") + else() + log_option_disabled("Build unity") + endif() + + configure_linking(${TARGET_NAME}) + add_test(NAME ${DIR} COMMAND ${TARGET_NAME}) if(RUN_TESTS_AFTER_BUILD) add_custom_command(