From e370fdb0fc3bee663bd384fc887d3db42437c6ca Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 30 Nov 2023 10:22:15 +0100 Subject: [PATCH] build: Add support for unity builds --- CMakeLists.txt | 1 + cmake/build_helpers.cmake | 9 ++++++ cmake/modules/ImHexPlugin.cmake | 1 + lib/external/libwolv | 2 +- lib/libimhex/CMakeLists.txt | 1 + .../include/hex/api/plugin_manager.hpp | 15 ++------- .../include/hex/data_processor/attribute.hpp | 2 ++ .../include/hex/data_processor/link.hpp | 2 ++ .../include/hex/data_processor/node.hpp | 2 ++ .../include/hex/helpers/http_requests.hpp | 8 ++--- lib/libimhex/source/api/plugin_manager.cpp | 31 ++++++++++-------- .../source/data_processor/attribute.cpp | 6 +--- lib/libimhex/source/data_processor/link.cpp | 6 +--- lib/libimhex/source/data_processor/node.cpp | 6 +--- main/gui/source/main.cpp | 5 +-- plugins/builtin/CMakeLists.txt | 2 +- .../content/providers/disk_provider.hpp | 7 +--- .../include/content/views/view_store.hpp | 3 +- .../source/content/background_services.cpp | 1 + .../content/providers/disk_provider.cpp | 32 +++++++++---------- .../providers/process_memory_provider.hpp | 5 +-- .../include/views/view_tty_console.hpp | 4 +-- .../providers/process_memory_provider.cpp | 1 - plugins/windows/source/plugin_windows.cpp | 2 ++ .../windows/source/views/view_tty_console.cpp | 2 ++ 25 files changed, 77 insertions(+), 79 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5391b6a9b..b0f84b43f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,7 @@ option(IMHEX_USE_DEFAULT_BUILD_SETTINGS "Use default build settings" OFF) option(IMHEX_STRICT_WARNINGS "Enable most available warnings and treat them as errors" ON) option(IMHEX_STATIC_LINK_PLUGINS "Statically link all plugins into the main executable" OFF) option(IMHEX_GENERATE_PACKAGE "Specify if a native package should be built. Only usable on Windows and MacOS" OFF) +option(IMHEX_ENABLE_UNITY_BUILD "Enables building ImHex as a unity build." OFF) # Basic compiler and cmake configurations set(CMAKE_CXX_STANDARD 23) diff --git a/cmake/build_helpers.cmake b/cmake/build_helpers.cmake index e75579fa0..6d240af97 100644 --- a/cmake/build_helpers.cmake +++ b/cmake/build_helpers.cmake @@ -450,6 +450,9 @@ macro(setupCompilerFlags target) # Disable some warnings set(IMHEX_C_CXX_FLAGS "-Wno-array-bounds -Wno-deprecated-declarations") + if (IMHEX_ENABLE_UNITY_BUILD AND WIN32) + set(IMHEX_COMMON_FLAGS "${IMHEX_COMMON_FLAGS} -Wa,-mbig-obj") + endif () endif() # Disable some warnings for gcc @@ -634,6 +637,12 @@ macro(addBundledLibraries) endif () endmacro() +function(enableUnityBuild TARGET) + if (IMHEX_ENABLE_UNITY_BUILD) + set_target_properties(${TARGET} PROPERTIES UNITY_BUILD ON UNITY_BUILD_MODE BATCH) + endif () +endfunction() + function(generatePDBs) if (NOT WIN32 OR CMAKE_BUILD_TYPE STREQUAL "Debug") return() diff --git a/cmake/modules/ImHexPlugin.cmake b/cmake/modules/ImHexPlugin.cmake index 66bb67604..d141ae0b0 100644 --- a/cmake/modules/ImHexPlugin.cmake +++ b/cmake/modules/ImHexPlugin.cmake @@ -33,6 +33,7 @@ macro(add_imhex_plugin) # Enable required compiler flags set_target_properties(${IMHEX_PLUGIN_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON) + enableUnityBuild(${IMHEX_PLUGIN_NAME}) setupCompilerFlags(${IMHEX_PLUGIN_NAME}) # Configure build properties diff --git a/lib/external/libwolv b/lib/external/libwolv index 86faee9f3..43ea92a66 160000 --- a/lib/external/libwolv +++ b/lib/external/libwolv @@ -1 +1 @@ -Subproject commit 86faee9f3e0ebdf7542c51fb5233134c376e4d3d +Subproject commit 43ea92a66ccad58fa12b717113ded20dfa5c018f diff --git a/lib/libimhex/CMakeLists.txt b/lib/libimhex/CMakeLists.txt index 19c2fa270..5e77865e9 100644 --- a/lib/libimhex/CMakeLists.txt +++ b/lib/libimhex/CMakeLists.txt @@ -69,6 +69,7 @@ else() endif() set_target_properties(libimhex PROPERTIES POSITION_INDEPENDENT_CODE ON) +enableUnityBuild(libimhex) setupCompilerFlags(libimhex) include(GenerateExportHeader) diff --git a/lib/libimhex/include/hex/api/plugin_manager.hpp b/lib/libimhex/include/hex/api/plugin_manager.hpp index 00af17cd9..9fd565f41 100644 --- a/lib/libimhex/include/hex/api/plugin_manager.hpp +++ b/lib/libimhex/include/hex/api/plugin_manager.hpp @@ -1,15 +1,10 @@ #pragma once -#include - +#include #include #include -#if defined(OS_WINDOWS) - #include -#else - #include -#endif +#include struct ImGuiContext; @@ -65,11 +60,7 @@ namespace hex { [[nodiscard]] std::span getSubCommands() const; private: - #if defined(OS_WINDOWS) - HMODULE m_handle = nullptr; - #else - void *m_handle = nullptr; - #endif + uintptr_t m_handle = 0; std::fs::path m_path; mutable bool m_initialized = false; diff --git a/lib/libimhex/include/hex/data_processor/attribute.hpp b/lib/libimhex/include/hex/data_processor/attribute.hpp index e2a2de363..83979235c 100644 --- a/lib/libimhex/include/hex/data_processor/attribute.hpp +++ b/lib/libimhex/include/hex/data_processor/attribute.hpp @@ -65,6 +65,8 @@ namespace hex::dp { friend class Node; void setParentNode(Node *node) { this->m_parentNode = node; } + + static int s_idCounter; }; } \ No newline at end of file diff --git a/lib/libimhex/include/hex/data_processor/link.hpp b/lib/libimhex/include/hex/data_processor/link.hpp index 9d32b5c3d..8bc0665e6 100644 --- a/lib/libimhex/include/hex/data_processor/link.hpp +++ b/lib/libimhex/include/hex/data_processor/link.hpp @@ -17,6 +17,8 @@ namespace hex::dp { private: int m_id; int m_from, m_to; + + static int s_idCounter; }; } \ No newline at end of file diff --git a/lib/libimhex/include/hex/data_processor/node.hpp b/lib/libimhex/include/hex/data_processor/node.hpp index f803ec2ee..69e477366 100644 --- a/lib/libimhex/include/hex/data_processor/node.hpp +++ b/lib/libimhex/include/hex/data_processor/node.hpp @@ -87,6 +87,8 @@ namespace hex::dp { prv::Overlay *m_overlay = nullptr; ImVec2 m_position; + static int s_idCounter; + Attribute& getAttribute(u32 index) { if (index >= this->getAttributes().size()) throw std::runtime_error("Attribute index out of bounds!"); diff --git a/lib/libimhex/include/hex/helpers/http_requests.hpp b/lib/libimhex/include/hex/helpers/http_requests.hpp index 076a639c6..ff11b5ea0 100644 --- a/lib/libimhex/include/hex/helpers/http_requests.hpp +++ b/lib/libimhex/include/hex/helpers/http_requests.hpp @@ -10,14 +10,14 @@ #include #include +using curl_off_t = long long; + #if defined(OS_WEB) #include - - using curl_off_t = long; -#else - #include #endif +typedef void CURL; + namespace hex { class HttpRequest { diff --git a/lib/libimhex/source/api/plugin_manager.cpp b/lib/libimhex/source/api/plugin_manager.cpp index ed5ca3427..be38e355a 100644 --- a/lib/libimhex/source/api/plugin_manager.cpp +++ b/lib/libimhex/source/api/plugin_manager.cpp @@ -2,29 +2,33 @@ #include #include -#include #include #include #include -#include + +#if defined(OS_WINDOWS) + #include +#else + #include +#endif namespace hex { Plugin::Plugin(const std::fs::path &path) : m_path(path) { #if defined(OS_WINDOWS) - this->m_handle = LoadLibraryW(path.c_str()); + this->m_handle = uintptr_t(LoadLibraryW(path.c_str())); - if (this->m_handle == INVALID_HANDLE_VALUE || this->m_handle == nullptr) { + if (this->m_handle == uintptr_t(INVALID_HANDLE_VALUE) || this->m_handle == 0) { log::error("LoadLibraryW failed: {}!", std::system_category().message(::GetLastError())); return; } #else - this->m_handle = dlopen(wolv::util::toUTF8String(path).c_str(), RTLD_LAZY); + this->m_handle = uintptr_t(dlopen(wolv::util::toUTF8String(path).c_str(), RTLD_LAZY)); - if (this->m_handle == nullptr) { + if (this->m_handle == 0) { log::error("dlopen failed: {}!", dlerror()); return; } @@ -41,14 +45,14 @@ namespace hex { } Plugin::Plugin(hex::PluginFunctions functions) { - this->m_handle = nullptr; + this->m_handle = 0; this->m_functions = std::move(functions); } Plugin::Plugin(Plugin &&other) noexcept { this->m_handle = other.m_handle; - other.m_handle = nullptr; + other.m_handle = 0; this->m_path = std::move(other.m_path); @@ -58,9 +62,10 @@ namespace hex { Plugin::~Plugin() { #if defined(OS_WINDOWS) - if (this->m_handle != nullptr) - FreeLibrary(this->m_handle); + if (this->m_handle != 0) + FreeLibrary(HMODULE(this->m_handle)); #else + dlclose(reinterpret_cast(this->m_handle)); #endif } @@ -100,7 +105,7 @@ namespace hex { if (this->m_functions.getPluginNameFunction != nullptr) return this->m_functions.getPluginNameFunction(); else - return hex::format("Unknown Plugin @ 0x{0:016X}", reinterpret_cast(this->m_handle)); + return hex::format("Unknown Plugin @ 0x{0:016X}", this->m_handle); } std::string Plugin::getPluginAuthor() const { @@ -155,9 +160,9 @@ namespace hex { void *Plugin::getPluginFunction(const std::string &symbol) const { #if defined(OS_WINDOWS) - return reinterpret_cast(GetProcAddress(this->m_handle, symbol.c_str())); + return reinterpret_cast(GetProcAddress(HMODULE(this->m_handle), symbol.c_str())); #else - return dlsym(this->m_handle, symbol.c_str()); + return dlsym(reinterpret_cast(this->m_handle), symbol.c_str()); #endif } diff --git a/lib/libimhex/source/data_processor/attribute.cpp b/lib/libimhex/source/data_processor/attribute.cpp index 1caa21451..2b9ee244a 100644 --- a/lib/libimhex/source/data_processor/attribute.cpp +++ b/lib/libimhex/source/data_processor/attribute.cpp @@ -3,11 +3,7 @@ namespace hex::dp { - namespace { - - int s_idCounter = 1; - - } + int Attribute::s_idCounter = 1; Attribute::Attribute(IOType ioType, Type type, std::string unlocalizedName) : m_id(s_idCounter++), m_ioType(ioType), m_type(type), m_unlocalizedName(std::move(unlocalizedName)) { diff --git a/lib/libimhex/source/data_processor/link.cpp b/lib/libimhex/source/data_processor/link.cpp index 76024b942..7a8b5385e 100644 --- a/lib/libimhex/source/data_processor/link.cpp +++ b/lib/libimhex/source/data_processor/link.cpp @@ -3,11 +3,7 @@ namespace hex::dp { - namespace { - - int s_idCounter = 1; - - } + int Link::s_idCounter = 1; Link::Link(int from, int to) : m_id(s_idCounter++), m_from(from), m_to(to) { } diff --git a/lib/libimhex/source/data_processor/node.cpp b/lib/libimhex/source/data_processor/node.cpp index 40098518a..c1a8010bd 100644 --- a/lib/libimhex/source/data_processor/node.cpp +++ b/lib/libimhex/source/data_processor/node.cpp @@ -7,11 +7,7 @@ namespace hex::dp { - namespace { - - int s_idCounter = 1; - - } + int Node::s_idCounter = 1; Node::Node(std::string unlocalizedTitle, std::vector attributes) : m_id(s_idCounter++), m_unlocalizedTitle(std::move(unlocalizedTitle)), m_attributes(std::move(attributes)) { for (auto &attr : this->m_attributes) diff --git a/main/gui/source/main.cpp b/main/gui/source/main.cpp index b92bcb511..3b3fd638c 100644 --- a/main/gui/source/main.cpp +++ b/main/gui/source/main.cpp @@ -10,7 +10,6 @@ #include "init/tasks.hpp" #include -#include #include #include @@ -20,7 +19,9 @@ #include #include -#if defined(OS_WEB) +#if defined(OS_WINDOWS) + #include +#elif defined(OS_WEB) #include #include #endif diff --git a/plugins/builtin/CMakeLists.txt b/plugins/builtin/CMakeLists.txt index c6262ef49..7e28c3be6 100644 --- a/plugins/builtin/CMakeLists.txt +++ b/plugins/builtin/CMakeLists.txt @@ -94,7 +94,6 @@ add_imhex_plugin( source/content/views/view_command_palette.cpp source/content/views/view_settings.cpp source/content/views/view_data_processor.cpp - source/content/views/view_yara.cpp source/content/views/view_constants.cpp source/content/views/view_store.cpp source/content/views/view_diff.cpp @@ -103,6 +102,7 @@ add_imhex_plugin( source/content/views/view_theme_manager.cpp source/content/views/view_logs.cpp source/content/views/view_achievements.cpp + source/content/views/view_yara.cpp source/content/helpers/math_evaluator.cpp source/content/helpers/notification.cpp diff --git a/plugins/builtin/include/content/providers/disk_provider.hpp b/plugins/builtin/include/content/providers/disk_provider.hpp index 5c129bee5..ac46d6698 100644 --- a/plugins/builtin/include/content/providers/disk_provider.hpp +++ b/plugins/builtin/include/content/providers/disk_provider.hpp @@ -7,11 +7,6 @@ #include #include -#if defined(OS_WINDOWS) - #define WIN32_LEAN_AND_MEAN - #include -#endif - namespace hex::plugin::builtin { class DiskProvider : public hex::prv::Provider { @@ -67,7 +62,7 @@ namespace hex::plugin::builtin { std::string m_friendlyName; #if defined(OS_WINDOWS) - HANDLE m_diskHandle = INVALID_HANDLE_VALUE; + void *m_diskHandle = reinterpret_cast(-1); #else std::string m_pathBuffer; int m_diskHandle = -1; diff --git a/plugins/builtin/include/content/views/view_store.hpp b/plugins/builtin/include/content/views/view_store.hpp index 0eddc2c1f..6b979f14c 100644 --- a/plugins/builtin/include/content/views/view_store.hpp +++ b/plugins/builtin/include/content/views/view_store.hpp @@ -3,9 +3,10 @@ #include #include + +#include #include #include -#include #include #include diff --git a/plugins/builtin/source/content/background_services.cpp b/plugins/builtin/source/content/background_services.cpp index 374703d8a..9eba78144 100644 --- a/plugins/builtin/source/content/background_services.cpp +++ b/plugins/builtin/source/content/background_services.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include diff --git a/plugins/builtin/source/content/providers/disk_provider.cpp b/plugins/builtin/source/content/providers/disk_provider.cpp index 31ed4f323..654cb2446 100644 --- a/plugins/builtin/source/content/providers/disk_provider.cpp +++ b/plugins/builtin/source/content/providers/disk_provider.cpp @@ -17,24 +17,24 @@ #include #if defined(OS_WINDOWS) -#include -#include -#include - + #include + #include + #include + #include #elif defined(OS_LINUX) -#include -#include -#include -#include -#include -#include + #include + #include + #include + #include + #include + #include #elif defined(OS_MACOS) -#include -#include -#include -#include -#include -#include + #include + #include + #include + #include + #include + #include #endif #if defined(OS_LINUX) diff --git a/plugins/windows/include/content/providers/process_memory_provider.hpp b/plugins/windows/include/content/providers/process_memory_provider.hpp index f9b96af8d..bb15bd99d 100644 --- a/plugins/windows/include/content/providers/process_memory_provider.hpp +++ b/plugins/windows/include/content/providers/process_memory_provider.hpp @@ -3,13 +3,10 @@ #include #include -#include - #include #include #include -#include #include namespace hex::plugin::windows { @@ -94,7 +91,7 @@ namespace hex::plugin::windows { return hex::containsIgnoreCase(memoryRegion.name, search); }); - HANDLE m_processHandle = nullptr; + void* m_processHandle = reinterpret_cast(-1); bool m_enumerationFailed = false; }; diff --git a/plugins/windows/include/views/view_tty_console.hpp b/plugins/windows/include/views/view_tty_console.hpp index 3eed893d6..ace6a99a4 100644 --- a/plugins/windows/include/views/view_tty_console.hpp +++ b/plugins/windows/include/views/view_tty_console.hpp @@ -2,8 +2,6 @@ #include -#include - #include #include #include @@ -27,7 +25,7 @@ namespace hex::plugin::windows { void transmitData(std::vector &data); - HANDLE m_portHandle = INVALID_HANDLE_VALUE; + void* m_portHandle = reinterpret_cast(-1); std::jthread m_receiveThread; int m_selectedPort = 0; diff --git a/plugins/windows/source/content/providers/process_memory_provider.cpp b/plugins/windows/source/content/providers/process_memory_provider.cpp index d3781ab9b..65ca2b42c 100644 --- a/plugins/windows/source/content/providers/process_memory_provider.cpp +++ b/plugins/windows/source/content/providers/process_memory_provider.cpp @@ -10,7 +10,6 @@ #include #include -#include #include namespace hex::plugin::windows { diff --git a/plugins/windows/source/plugin_windows.cpp b/plugins/windows/source/plugin_windows.cpp index c23e8ff66..9f9692c7b 100644 --- a/plugins/windows/source/plugin_windows.cpp +++ b/plugins/windows/source/plugin_windows.cpp @@ -9,6 +9,8 @@ #include "views/view_tty_console.hpp" +#include + using namespace hex; namespace hex::plugin::windows { diff --git a/plugins/windows/source/views/view_tty_console.cpp b/plugins/windows/source/views/view_tty_console.cpp index f37eb833f..554ca1fa2 100644 --- a/plugins/windows/source/views/view_tty_console.cpp +++ b/plugins/windows/source/views/view_tty_console.cpp @@ -7,6 +7,8 @@ #include +#include + namespace hex::plugin::windows { ViewTTYConsole::ViewTTYConsole() : View::Window("hex.windows.view.tty_console.name") {