Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: change cmake to link static CanaryLib only for tests #3028

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,15 @@ endif()

option(BUILD_TESTS "Build tests" OFF) # By default, tests will not be built
option(RUN_TESTS_AFTER_BUILD "Run tests when building" OFF) # By default, tests will only run if requested
if(PACKAGE_TESTS)
set(BUILD_TESTS ON)
endif()

# *****************************************************************************
# Add project
# *****************************************************************************
add_subdirectory(src)

if(BUILD_TESTS OR PACKAGE_TESTS)
if(BUILD_TESTS)
add_subdirectory(tests)
endif()
48 changes: 26 additions & 22 deletions cmake/modules/CanaryLib.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Define and setup CanaryLib main library target
add_library(${PROJECT_NAME}_lib)
setup_target(${PROJECT_NAME}_lib)
set(CANARY_TARGET_NAME ${PROJECT_NAME})
if(BUILD_TESTS)
set(CANARY_TARGET_NAME "${CANARY_TARGET_NAME}_lib")
# Define and setup CanaryLib main library target
add_library(${CANARY_TARGET_NAME})
setup_target(${CANARY_TARGET_NAME})
endif()

# Add subdirectories
add_subdirectory(account)
Expand All @@ -20,43 +24,43 @@ add_subdirectory(server)
add_subdirectory(utils)

# Add more global sources - please add preferably in the sub_directory CMakeLists.
target_sources(${PROJECT_NAME}_lib PRIVATE canary_server.cpp)
target_sources(${CANARY_TARGET_NAME} PRIVATE canary_server.cpp)

# 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)
target_precompile_headers(${CANARY_TARGET_NAME} PUBLIC pch.hpp)
target_compile_definitions(${CANARY_TARGET_NAME} PUBLIC USE_PRECOMPILED_HEADERS)
endif()

# *****************************************************************************
# 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)
target_compile_options(${CANARY_TARGET_NAME} PRIVATE -Wno-deprecated-declarations)
endif()

# Sets the NDEBUG macro for Release and RelWithDebInfo configurations.
target_compile_definitions(${PROJECT_NAME}_lib PUBLIC
target_compile_definitions(${CANARY_TARGET_NAME} PUBLIC
$<$<CONFIG:Release>:NDEBUG>
$<$<CONFIG:RelWithDebInfo>:NDEBUG>
)

# Configurar IPO e Linkagem Incremental
configure_linking(${PROJECT_NAME}_lib)
configure_linking(${CANARY_TARGET_NAME})

# === 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 for taget ${PROJECT_NAME}_lib")
set_target_properties(${CANARY_TARGET_NAME} PROPERTIES UNITY_BUILD ON)
log_option_enabled("Build unity for speed up compilation for target ${CANARY_TARGET_NAME}")
else()
log_option_disabled("Build unity")
endif()

# *****************************************************************************
# Target include directories - to allow #include
# *****************************************************************************
target_include_directories(${PROJECT_NAME}_lib
PUBLIC
target_include_directories(${CANARY_TARGET_NAME}
PUBLIC
${BOOST_DI_INCLUDE_DIRS}
${CMAKE_SOURCE_DIR}/src
${GMP_INCLUDE_DIRS}
Expand All @@ -67,7 +71,7 @@ target_include_directories(${PROJECT_NAME}_lib
# *****************************************************************************
# Target links to external dependencies
# *****************************************************************************
target_link_libraries(${PROJECT_NAME}_lib
target_link_libraries(${CANARY_TARGET_NAME}
PUBLIC
${GMP_LIBRARIES}
${LUAJIT_LIBRARIES}
Expand All @@ -89,7 +93,7 @@ target_link_libraries(${PROJECT_NAME}_lib

if(FEATURE_METRICS)
add_definitions(-DFEATURE_METRICS)
target_link_libraries(${PROJECT_NAME}_lib
target_link_libraries(${CANARY_TARGET_NAME}
PUBLIC
opentelemetry-cpp::common
opentelemetry-cpp::metrics
Expand All @@ -103,9 +107,9 @@ if(FEATURE_METRICS)
endif()

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

if (MSVC)
Expand All @@ -114,17 +118,17 @@ 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})
target_link_libraries(${CANARY_TARGET_NAME} PUBLIC ${CMAKE_THREAD_LIBS_INIT} ${MYSQL_CLIENT_LIBS})
else()
target_link_libraries(${PROJECT_NAME}_lib PUBLIC Threads::Threads)
target_link_libraries(${CANARY_TARGET_NAME} PUBLIC Threads::Threads)
endif()

# === 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)
target_link_libraries(${CANARY_TARGET_NAME} PUBLIC OpenMP::OpenMP_CXX)
endif()
else()
log_option_disabled("openmp")
Expand All @@ -133,8 +137,8 @@ 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)
target_compile_options(${CANARY_TARGET_NAME} PRIVATE -O3 -march=native)
elseif(MSVC)
target_compile_options(${PROJECT_NAME}_lib PRIVATE /O2)
target_compile_options(${CANARY_TARGET_NAME} PRIVATE /O2)
endif()
endif()
19 changes: 15 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,39 @@ cmake_minimum_required(VERSION 3.22 FATAL_ERROR)
include(BaseConfig)
include(GNUInstallDirs)

# Import configurations, source definitions, and linker settings
include(CanaryLib)
# If it is tests, we need link it static library
if (BUILD_TESTS)
# Import configurations, source definitions, and linker settings
include(CanaryLib)
endif()

# Define main executable target, set it up and link to main library
add_executable(${PROJECT_NAME} main.cpp)

# If it is not tests, we can link it directly to the executable
if (NOT BUILD_TESTS)
# Import configurations, source definitions, and linker settings
include(CanaryLib)
endif()

if(MSVC)
# Add executable icon for Windows
target_sources(${PROJECT_NAME} PRIVATE ../cmake/canary.rc)
endif()

setup_target(${PROJECT_NAME})
set_output_directory(${PROJECT_NAME})
target_link_libraries(${PROJECT_NAME} PRIVATE ${PROJECT_NAME}_lib)
if(BUILD_TESTS)
target_link_libraries(${PROJECT_NAME} PRIVATE ${CANARY_TARGET_NAME})
endif(BUILD_TESTS)

# 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(${CANARY_TARGET_NAME} PRIVATE -Wall -Wextra -Wpedantic)
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wpedantic)
endif()
endif()
2 changes: 1 addition & 1 deletion src/account/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
account.cpp
account_repository.cpp
account_repository_db.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/config/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
configmanager.cpp
)
2 changes: 1 addition & 1 deletion src/creatures/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
appearance/mounts/mounts.cpp
appearance/outfit/outfit.cpp
combat/combat.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/database/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
database.cpp
databasemanager.cpp
databasetasks.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/game/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
functions/game_reload.cpp
game.cpp
bank/bank.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/io/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
fileloader.cpp
filestream.cpp
io_wheel.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/items/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
bed.cpp
containers/container.cpp
containers/depot/depotchest.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/kv/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
value_wrapper.cpp
value_wrapper_proto.cpp
kv.cpp
Expand Down
4 changes: 2 additions & 2 deletions src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
di/soft_singleton.cpp
logging/logger.cpp
logging/log_with_spd_log.cpp
thread/thread_pool.cpp
)

if(FEATURE_METRICS)
target_sources(${PROJECT_NAME}_lib PRIVATE metrics/metrics.cpp)
target_sources(${CANARY_TARGET_NAME} PRIVATE metrics/metrics.cpp)
endif()
2 changes: 1 addition & 1 deletion src/lua/callbacks/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
creaturecallback.cpp
event_callback.cpp
events_callbacks.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/lua/creature/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
actions.cpp
creatureevent.cpp
events.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/lua/functions/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
lua_functions_loader.cpp
)

Expand Down
2 changes: 1 addition & 1 deletion src/lua/functions/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
game/config_functions.cpp
game/game_functions.cpp
game/bank_functions.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/lua/functions/creatures/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
combat/combat_functions.cpp
combat/condition_functions.cpp
combat/spell_functions.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/lua/functions/events/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
action_functions.cpp
creature_event_functions.cpp
event_callback_functions.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/lua/functions/items/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
container_functions.cpp
imbuement_functions.cpp
item_classification_functions.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/lua/functions/map/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
house_functions.cpp
position_functions.cpp
teleport_functions.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/lua/global/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
baseevents.cpp
globalevent.cpp
)
2 changes: 1 addition & 1 deletion src/lua/modules/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
modules.cpp
)
2 changes: 1 addition & 1 deletion src/lua/scripts/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
lua_environment.cpp
luascript.cpp
script_environment.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/map/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
house/house.cpp
house/housetile.cpp
utils/astarnodes.cpp
Expand Down
2 changes: 0 additions & 2 deletions src/protobuf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ target_link_libraries(${PROJECT_NAME}
)
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_BINARY_DIR})

message("Diretório de construção atual: ${CMAKE_CURRENT_BINARY_DIR}")

if (MSVC AND BUILD_STATIC_LIBRARY)
set_property(TARGET ${PROJECT_NAME} PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
Expand Down
2 changes: 1 addition & 1 deletion src/security/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
argon.cpp
rsa.cpp
)
2 changes: 1 addition & 1 deletion src/server/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
network/connection/connection.cpp
network/message/networkmessage.cpp
network/message/outputmessage.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
target_sources(${PROJECT_NAME}_lib PRIVATE
target_sources(${CANARY_TARGET_NAME} PRIVATE
pugicast.cpp
tools.cpp
wildcardtree.cpp
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function(setup_test TARGET_NAME DIR)
add_executable(${TARGET_NAME} main.cpp)

target_compile_definitions(${TARGET_NAME} PUBLIC -DDEBUG_LOG)
target_link_libraries(${TARGET_NAME} PRIVATE Boost::ut ${PROJECT_NAME}_lib)
target_link_libraries(${TARGET_NAME} PRIVATE Boost::ut ${CANARY_TARGET_NAME})
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"))
Expand Down
1 change: 1 addition & 0 deletions tests/unit/account/account_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
* Contributors: https://github.com/opentibiabr/canary/graphs/contributors
* Website: https://docs.opentibiabr.com/
*/

#include "pch.hpp"

Check failure on line 10 in tests/unit/account/account_test.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-22.04-linux-debug

pch.hpp: No such file or directory

Check failure on line 10 in tests/unit/account/account_test.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-24.04-linux-debug

pch.hpp: No such file or directory

Check failure on line 10 in tests/unit/account/account_test.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-22.04-linux-debug

pch.hpp: No such file or directory

Check failure on line 10 in tests/unit/account/account_test.cpp

View workflow job for this annotation

GitHub Actions / ubuntu-24.04-linux-debug

pch.hpp: No such file or directory

#include <boost/ut.hpp>

Expand Down
Loading