From fee42e4fb14737d7301ccd49a9ee4edaf18c0adc Mon Sep 17 00:00:00 2001 From: Ranieri Althoff <1993083+ranisalt@users.noreply.github.com> Date: Wed, 13 Dec 2023 01:56:46 +0100 Subject: [PATCH] Enable unity build to speed up compilation (#4545) --- .github/workflows/test.yml | 6 +- CMakeLists.txt | 5 +- src/CMakeLists.txt | 5 +- src/main.cpp | 50 +++++++++ src/otserv.cpp | 183 +++++++++++++------------------- src/otserv.h | 10 ++ vc17/theforgottenserver.vcxproj | 2 + 7 files changed, 144 insertions(+), 117 deletions(-) create mode 100644 src/main.cpp create mode 100644 src/otserv.h diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2166968218..b5a2b688df 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -6,10 +6,6 @@ jobs: test: runs-on: ubuntu-latest - env: - CC: gcc - CXX: g++ - steps: - uses: actions/checkout@v3 @@ -29,5 +25,5 @@ jobs: with: buildPreset: default configurePreset: default - configurePresetAdditionalArgs: "['-G Ninja', '-DENABLE_TESTING=ON']" + configurePresetAdditionalArgs: "['-G Ninja', '-DBUILD_TESTING=ON']" testPreset: default diff --git a/CMakeLists.txt b/CMakeLists.txt index f0e22c88c7..3ca04cbbb4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,7 +67,7 @@ endif () find_package(Boost 1.66.0 REQUIRED COMPONENTS system iostreams) -option(ENABLE_TESTING "Build unit tests" OFF) +option(BUILD_TESTING "Build unit tests" OFF) include_directories(${Boost_INCLUDE_DIRS} ${Crypto++_INCLUDE_DIR} ${LUA_INCLUDE_DIR} ${MYSQL_INCLUDE_DIR} ${PUGIXML_INCLUDE_DIR}) @@ -78,7 +78,8 @@ add_subdirectory(src) add_executable(tfs ${tfs_MAIN}) target_link_libraries(tfs tfslib) -if (ENABLE_TESTING) +if (BUILD_TESTING) + message(STATUS "Building unit tests") enable_testing() add_subdirectory(src/tests) endif() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e5bedaa438..e650b94b60 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -43,6 +43,7 @@ set(tfs_SRC ${CMAKE_CURRENT_LIST_DIR}/movement.cpp ${CMAKE_CURRENT_LIST_DIR}/networkmessage.cpp ${CMAKE_CURRENT_LIST_DIR}/npc.cpp + ${CMAKE_CURRENT_LIST_DIR}/otserv.cpp ${CMAKE_CURRENT_LIST_DIR}/outfit.cpp ${CMAKE_CURRENT_LIST_DIR}/outputmessage.cpp ${CMAKE_CURRENT_LIST_DIR}/party.cpp @@ -129,6 +130,7 @@ set(tfs_HDR ${CMAKE_CURRENT_LIST_DIR}/movement.h ${CMAKE_CURRENT_LIST_DIR}/networkmessage.h ${CMAKE_CURRENT_LIST_DIR}/npc.h + ${CMAKE_CURRENT_LIST_DIR}/otserv.h ${CMAKE_CURRENT_LIST_DIR}/outfit.h ${CMAKE_CURRENT_LIST_DIR}/outputmessage.h ${CMAKE_CURRENT_LIST_DIR}/party.h @@ -167,7 +169,7 @@ set(tfs_HDR ${CMAKE_CURRENT_LIST_DIR}/xtea.h ) -set(tfs_MAIN ${CMAKE_CURRENT_LIST_DIR}/otserv.cpp PARENT_SCOPE) +set(tfs_MAIN ${CMAKE_CURRENT_LIST_DIR}/main.cpp PARENT_SCOPE) add_library(tfslib ${tfs_SRC}) target_link_libraries(tfslib PRIVATE @@ -180,5 +182,6 @@ target_link_libraries(tfslib PRIVATE ${LUA_LIBRARIES} ${MYSQL_CLIENT_LIBS} ) +set_target_properties(tfslib PROPERTIES UNITY_BUILD ON) add_custom_target(format COMMAND /usr/bin/clang-format -style=file -i ${tfs_HDR} ${tfs_SRC} ${tfs_MAIN}) diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000000..fd23a867b9 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,50 @@ +#include "otpch.h" + +#include "configmanager.h" +#include "otserv.h" +#include "tools.h" + +extern ConfigManager g_config; + +static bool argumentsHandler(const std::vector& args) +{ + for (const auto& arg : args) { + if (arg == "--help") { + std::clog << "Usage:\n" + "\n" + "\t--config=$1\t\tAlternate configuration file path.\n" + "\t--ip=$1\t\t\tIP address of the server.\n" + "\t\t\t\tShould be equal to the global IP.\n" + "\t--login-port=$1\tPort for login server to listen on.\n" + "\t--game-port=$1\tPort for game server to listen on.\n"; + return false; + } else if (arg == "--version") { + printServerVersion(); + return false; + } + + auto tmp = explodeString(arg, "="); + + if (tmp[0] == "--config") + g_config.setString(ConfigManager::CONFIG_FILE, tmp[1]); + else if (tmp[0] == "--ip") + g_config.setString(ConfigManager::IP, tmp[1]); + else if (tmp[0] == "--login-port") + g_config.setNumber(ConfigManager::LOGIN_PORT, std::stoi(tmp[1].data())); + else if (tmp[0] == "--game-port") + g_config.setNumber(ConfigManager::GAME_PORT, std::stoi(tmp[1].data())); + } + + return true; +} + +int main(int argc, const char** argv) +{ + std::vector args(argv, argv + argc); + if (!argumentsHandler(args)) { + return 1; + } + + startServer(); + return 0; +} diff --git a/src/otserv.cpp b/src/otserv.cpp index 99851898e2..2d6223740c 100644 --- a/src/otserv.cpp +++ b/src/otserv.cpp @@ -3,6 +3,8 @@ #include "otpch.h" +#include "otserv.h" + #include "configmanager.h" #include "databasemanager.h" #include "databasetasks.h" @@ -40,96 +42,15 @@ std::mutex g_loaderLock; std::condition_variable g_loaderSignal; std::unique_lock g_loaderUniqueLock(g_loaderLock); +namespace { + void startupErrorMessage(const std::string& errorStr) { fmt::print(fg(fmt::color::crimson) | fmt::emphasis::bold, "> ERROR: {:s}\n", errorStr); g_loaderSignal.notify_all(); } -void mainLoader(int argc, char* argv[], ServiceManager* services); -bool argumentsHandler(const StringVector& args); - -[[noreturn]] void badAllocationHandler() -{ - // Use functions that only use stack allocation - puts("Allocation failed, server out of memory.\nDecrease the size of your map or compile in 64 bits mode.\n"); - getchar(); - exit(-1); -} - -int main(int argc, char* argv[]) -{ - StringVector args = StringVector(argv, argv + argc); - if (argc > 1 && !argumentsHandler(args)) { - return 0; - } - - // Setup bad allocation handler - std::set_new_handler(badAllocationHandler); - - ServiceManager serviceManager; - - g_dispatcher.start(); - g_scheduler.start(); - - g_dispatcher.addTask([=, services = &serviceManager]() { mainLoader(argc, argv, services); }); - - g_loaderSignal.wait(g_loaderUniqueLock); - - if (serviceManager.is_running()) { - std::cout << ">> " << g_config.getString(ConfigManager::SERVER_NAME) << " Server Online!" << std::endl - << std::endl; - serviceManager.run(); - } else { - std::cout << ">> No services running. The server is NOT online." << std::endl; - g_scheduler.shutdown(); - g_databaseTasks.shutdown(); - g_dispatcher.shutdown(); - } - - g_scheduler.join(); - g_databaseTasks.join(); - g_dispatcher.join(); - return 0; -} - -void printServerVersion() -{ -#if defined(GIT_RETRIEVED_STATE) && GIT_RETRIEVED_STATE - std::cout << STATUS_SERVER_NAME << " - Version " << GIT_DESCRIBE << std::endl; - std::cout << "Git SHA1 " << GIT_SHORT_SHA1 << " dated " << GIT_COMMIT_DATE_ISO8601 << std::endl; -#if GIT_IS_DIRTY - std::cout << "*** DIRTY - NOT OFFICIAL RELEASE ***" << std::endl; -#endif -#else - std::cout << STATUS_SERVER_NAME << " - Version " << STATUS_SERVER_VERSION << std::endl; -#endif - std::cout << std::endl; - - std::cout << "Compiled with " << BOOST_COMPILER << std::endl; - std::cout << "Compiled on " << __DATE__ << ' ' << __TIME__ << " for platform "; -#if defined(__amd64__) || defined(_M_X64) - std::cout << "x64" << std::endl; -#elif defined(__i386__) || defined(_M_IX86) || defined(_X86_) - std::cout << "x86" << std::endl; -#elif defined(__arm__) - std::cout << "ARM" << std::endl; -#else - std::cout << "unknown" << std::endl; -#endif -#if defined(LUAJIT_VERSION) - std::cout << "Linked with " << LUAJIT_VERSION << " for Lua support" << std::endl; -#else - std::cout << "Linked with " << LUA_RELEASE << " for Lua support" << std::endl; -#endif - std::cout << std::endl; - - std::cout << "A server developed by " << STATUS_SERVER_DEVELOPERS << std::endl; - std::cout << "Visit our forum for updates, support, and resources: https://otland.net/." << std::endl; - std::cout << std::endl; -} - -void mainLoader(int, char*[], ServiceManager* services) +void mainLoader(ServiceManager* services) { // dispatcher thread g_game.setGameState(GAME_STATE_STARTUP); @@ -336,34 +257,78 @@ void mainLoader(int, char*[], ServiceManager* services) g_loaderSignal.notify_all(); } -bool argumentsHandler(const StringVector& args) +[[noreturn]] void badAllocationHandler() { - for (const auto& arg : args) { - if (arg == "--help") { - std::clog << "Usage:\n" - "\n" - "\t--config=$1\t\tAlternate configuration file path.\n" - "\t--ip=$1\t\t\tIP address of the server.\n" - "\t\t\t\tShould be equal to the global IP.\n" - "\t--login-port=$1\tPort for login server to listen on.\n" - "\t--game-port=$1\tPort for game server to listen on.\n"; - return false; - } else if (arg == "--version") { - printServerVersion(); - return false; - } + // Use functions that only use stack allocation + puts("Allocation failed, server out of memory.\nDecrease the size of your map or compile in 64 bits mode.\n"); + getchar(); + exit(-1); +} + +} // namespace + +void startServer() +{ + // Setup bad allocation handler + std::set_new_handler(badAllocationHandler); + + ServiceManager serviceManager; - auto tmp = explodeString(arg, "="); + g_dispatcher.start(); + g_scheduler.start(); - if (tmp[0] == "--config") - g_config.setString(ConfigManager::CONFIG_FILE, tmp[1]); - else if (tmp[0] == "--ip") - g_config.setString(ConfigManager::IP, tmp[1]); - else if (tmp[0] == "--login-port") - g_config.setNumber(ConfigManager::LOGIN_PORT, std::stoi(tmp[1].data())); - else if (tmp[0] == "--game-port") - g_config.setNumber(ConfigManager::GAME_PORT, std::stoi(tmp[1].data())); + g_dispatcher.addTask([services = &serviceManager]() { mainLoader(services); }); + + g_loaderSignal.wait(g_loaderUniqueLock); + + if (serviceManager.is_running()) { + std::cout << ">> " << g_config.getString(ConfigManager::SERVER_NAME) << " Server Online!" << std::endl + << std::endl; + serviceManager.run(); + } else { + std::cout << ">> No services running. The server is NOT online." << std::endl; + g_scheduler.shutdown(); + g_databaseTasks.shutdown(); + g_dispatcher.shutdown(); } - return true; + g_scheduler.join(); + g_databaseTasks.join(); + g_dispatcher.join(); +} + +void printServerVersion() +{ +#if defined(GIT_RETRIEVED_STATE) && GIT_RETRIEVED_STATE + std::cout << STATUS_SERVER_NAME << " - Version " << GIT_DESCRIBE << std::endl; + std::cout << "Git SHA1 " << GIT_SHORT_SHA1 << " dated " << GIT_COMMIT_DATE_ISO8601 << std::endl; +#if GIT_IS_DIRTY + std::cout << "*** DIRTY - NOT OFFICIAL RELEASE ***" << std::endl; +#endif +#else + std::cout << STATUS_SERVER_NAME << " - Version " << STATUS_SERVER_VERSION << std::endl; +#endif + std::cout << std::endl; + + std::cout << "Compiled with " << BOOST_COMPILER << std::endl; + std::cout << "Compiled on " << __DATE__ << ' ' << __TIME__ << " for platform "; +#if defined(__amd64__) || defined(_M_X64) + std::cout << "x64" << std::endl; +#elif defined(__i386__) || defined(_M_IX86) || defined(_X86_) + std::cout << "x86" << std::endl; +#elif defined(__arm__) + std::cout << "ARM" << std::endl; +#else + std::cout << "unknown" << std::endl; +#endif +#if defined(LUAJIT_VERSION) + std::cout << "Linked with " << LUAJIT_VERSION << " for Lua support" << std::endl; +#else + std::cout << "Linked with " << LUA_RELEASE << " for Lua support" << std::endl; +#endif + std::cout << std::endl; + + std::cout << "A server developed by " << STATUS_SERVER_DEVELOPERS << std::endl; + std::cout << "Visit our forum for updates, support, and resources: https://otland.net/." << std::endl; + std::cout << std::endl; } diff --git a/src/otserv.h b/src/otserv.h new file mode 100644 index 0000000000..6a57013a42 --- /dev/null +++ b/src/otserv.h @@ -0,0 +1,10 @@ +// Copyright 2023 The Forgotten Server Authors. All rights reserved. +// Use of this source code is governed by the GPL-2.0 License that can be found in the LICENSE file. + +#ifndef FS_OTSERV_H +#define FS_OTSERV_H + +void printServerVersion(); +void startServer(); + +#endif diff --git a/vc17/theforgottenserver.vcxproj b/vc17/theforgottenserver.vcxproj index 0d040e5932..e358a52a6b 100644 --- a/vc17/theforgottenserver.vcxproj +++ b/vc17/theforgottenserver.vcxproj @@ -39,11 +39,13 @@ Application false v143 + true Application false v143 + true