Skip to content

Commit

Permalink
Enable unity build to speed up compilation (#4545)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranisalt authored Dec 13, 2023
1 parent ffba1d8 commit fee42e4
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 117 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ jobs:
test:
runs-on: ubuntu-latest

env:
CC: gcc
CXX: g++

steps:
- uses: actions/checkout@v3

Expand All @@ -29,5 +25,5 @@ jobs:
with:
buildPreset: default
configurePreset: default
configurePresetAdditionalArgs: "['-G Ninja', '-DENABLE_TESTING=ON']"
configurePresetAdditionalArgs: "['-G Ninja', '-DBUILD_TESTING=ON']"
testPreset: default
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand All @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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})
50 changes: 50 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -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<std::string_view>& 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<std::string_view> args(argv, argv + argc);
if (!argumentsHandler(args)) {
return 1;
}

startServer();
return 0;
}
183 changes: 74 additions & 109 deletions src/otserv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include "otpch.h"

#include "otserv.h"

#include "configmanager.h"
#include "databasemanager.h"
#include "databasetasks.h"
Expand Down Expand Up @@ -40,96 +42,15 @@ std::mutex g_loaderLock;
std::condition_variable g_loaderSignal;
std::unique_lock<std::mutex> 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);
Expand Down Expand Up @@ -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;
}
10 changes: 10 additions & 0 deletions src/otserv.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions vc17/theforgottenserver.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<EnableUnitySupport>true</EnableUnitySupport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<EnableUnitySupport>true</EnableUnitySupport>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
Expand Down

1 comment on commit fee42e4

@reyaleman
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<ClCompile Include="..\src\main.cpp" /> is missing in vc17/theforgottenserver.vcxproj file

Please sign in to comment.