Skip to content

Commit

Permalink
perf: build cmake (#2972)
Browse files Browse the repository at this point in the history
  • Loading branch information
beats-dh authored Oct 12, 2024
1 parent 34baa0a commit c4786c4
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 133 deletions.
63 changes: 15 additions & 48 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
# *****************************************************************************
Expand All @@ -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")
Expand All @@ -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
Expand Down
79 changes: 47 additions & 32 deletions cmake/modules/BaseConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
# *****************************************************************************
Expand Down Expand Up @@ -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)
Expand All @@ -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 ..
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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$<$<CONFIG:Debug>: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()
57 changes: 23 additions & 34 deletions cmake/modules/CanaryLib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()

# *****************************************************************************
Expand All @@ -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
$<$<CONFIG:RelWithDebInfo>:NDEBUG>
$<$<CONFIG:Release>:NDEBUG>
$<$<CONFIG:Release>:NDEBUG>
$<$<CONFIG:RelWithDebInfo>: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()

# *****************************************************************************
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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()
Loading

0 comments on commit c4786c4

Please sign in to comment.