Skip to content

Commit

Permalink
improve: Separate library configs in a different cmake
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrossi committed Aug 20, 2023
1 parent 7b5817e commit 9c28c05
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 192 deletions.
100 changes: 99 additions & 1 deletion cmake/modules/BaseConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,102 @@ if(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS MSVC_MINIMUM_VERSION)
message(FATAL_ERROR "Visual Studio version must be at least ${MSVC_MINIMUM_VERSION}")
endif()
endif()
endif()

# *****************************************************************************
# Sanity Checks
# *****************************************************************************
option(TOGGLE_BIN_FOLDER "Use build/bin folder for generate compilation files" ON)
option(OPTIONS_ENABLE_OPENMP "Enable Open Multi-Processing support." ON)
option(DEBUG_LOG "Enable Debug Log" OFF)
option(ASAN_ENABLED "Build this target with AddressSanitizer" OFF)
option(BUILD_STATIC_LIBRARY "Build using static libraries" OFF)
option(SPEED_UP_BUILD_UNITY "Compile using build unity for speed up build" ON)

# === ASAN ===
if(ASAN_ENABLED)
log_option_enabled("asan")
if(MSVC)
add_compile_options(/fsanitize=address)
else()
add_compile_options(-fsanitize=address)
link_libraries(-fsanitize=address)
endif()
else()
log_option_disabled("asan")
endif()

# Build static libs
if(BUILD_STATIC_LIBRARY)
log_option_enabled("STATIC_LIBRARY")

if(MSVC)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".lib")
elseif(UNIX AND NOT APPLE)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
elseif(APPLE)
set(CMAKE_FIND_LIBRARY_SUFFIXES ".a" ".dylib")
endif()
else()
log_option_disabled("STATIC_LIBRARY")
endif()

# === DEBUG LOG ===
# cmake -DDEBUG_LOG=ON ..
if(DEBUG_LOG)
add_definitions(-DDEBUG_LOG=ON)
log_option_enabled("DEBUG LOG")
else()
log_option_disabled("DEBUG LOG")
endif(DEBUG_LOG)

# *****************************************************************************
# Compiler Options
# *****************************************************************************
if (MSVC)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
string(REPLACE "/Zi" "/Z7" CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG}")
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
string(REPLACE "/Zi" "/Z7" CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE}")
elseif(CMAKE_BUILD_TYPE STREQUAL "RelWithDebInfo")
string(REPLACE "/Zi" "/Z7" CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
string(REPLACE "/Zi" "/Z7" CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_C_FLAGS_RELWITHDEBINFO}")
endif()

add_compile_options(/MP /FS /Zf /EHsc)
endif (MSVC)

## Link compilation files to build/bin folder, else link to the main dir
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
function(setup_target TARGET_NAME)
set_output_directory(${TARGET_NAME})

if (MSVC AND BUILD_STATIC_LIBRARY)
set_property(TARGET ${TARGET_NAME} PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
endfunction()

# *****************************************************************************
# DEBUG: Print cmake variables
# *****************************************************************************
#get_cmake_property(_variableNames VARIABLES)
#list (SORT _variableNames)
#foreach (_variableName ${_variableNames})
# message(STATUS "${_variableName}=${${_variableName}}")
#endforeach()
94 changes: 94 additions & 0 deletions cmake/modules/CanaryLib.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# Define and setup CanaryLib main library target
add_library(${PROJECT_NAME}_lib "")
setup_target(${PROJECT_NAME}_lib)

# Include sources cmake file to add source files to lib
include(Sources)

# Add public pre compiler header to lib, to pass down to related targets
target_precompile_headers(${PROJECT_NAME}_lib PUBLIC pch.hpp)

# *****************************************************************************
# Build flags - need to be set before the links and sources
# *****************************************************************************
if (CMAKE_COMPILER_IS_GNUCXX)
target_compile_options(${PROJECT_NAME}_lib PRIVATE -Wno-deprecated-declarations)
endif()

# === IPO ===
check_ipo_supported(RESULT result OUTPUT output)
if(result)
set_property(TARGET ${PROJECT_NAME}_lib PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
else()
message(WARNING "IPO is not supported: ${output}")
endif()

# === 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")
endif()

# *****************************************************************************
# Target include directories - to allow #include
# *****************************************************************************
target_include_directories(${PROJECT_NAME}_lib
PUBLIC
${BOOST_DI_INCLUDE_DIRS}
${CMAKE_SOURCE_DIR}/src
${GMP_INCLUDE_DIRS}
${LUAJIT_INCLUDE_DIRS}
${PARALLEL_HASHMAP_INCLUDE_DIRS}
)

# *****************************************************************************
# Target links to external dependencies
# *****************************************************************************
target_link_libraries(${PROJECT_NAME}_lib
PUBLIC
${GMP_LIBRARIES}
${LUAJIT_LIBRARIES}
CURL::libcurl
ZLIB::ZLIB
absl::any absl::log absl::base absl::bits
asio::asio
eventpp::eventpp
fmt::fmt
magic_enum::magic_enum
mio::mio
protobuf::libprotobuf
pugixml::pugixml
spdlog::spdlog
unofficial::argon2::libargon2
unofficial::libmariadb
unofficial::mariadbclient
)

if(CMAKE_BUILD_TYPE MATCHES Debug)
target_link_libraries(${PROJECT_NAME}_lib PUBLIC ${ZLIB_LIBRARY_DEBUG})
else()
target_link_libraries(${PROJECT_NAME}_lib PUBLIC ${ZLIB_LIBRARY_RELEASE})
endif()

if (MSVC)
if(BUILD_STATIC_LIBRARY)
target_link_libraries(${PROJECT_NAME}_lib PUBLIC jsoncpp_static)
else()
target_link_libraries(${PROJECT_NAME}_lib PUBLIC jsoncpp_lib)
endif()

target_link_libraries(${PROJECT_NAME}_lib PUBLIC ${CMAKE_THREAD_LIBS_INIT} ${MYSQL_CLIENT_LIBS})
else()
target_link_libraries(${PROJECT_NAME}_lib PUBLIC jsoncpp_static Threads::Threads)
endif (MSVC)

# === OpenMP ===
if(OPTIONS_ENABLE_OPENMP)
log_option_enabled("openmp")
find_package(OpenMP)
if(OpenMP_CXX_FOUND)
target_link_libraries(${PROJECT_NAME}_lib PUBLIC OpenMP::OpenMP_CXX)
endif()
else()
log_option_disabled("openmp")
endif()
59 changes: 0 additions & 59 deletions cmake/modules/LinkerSettings.cmake

This file was deleted.

115 changes: 0 additions & 115 deletions cmake/modules/Options.cmake

This file was deleted.

Loading

0 comments on commit 9c28c05

Please sign in to comment.