Skip to content

Commit

Permalink
refactor: Rework features that use external libraries into optional p…
Browse files Browse the repository at this point in the history
…lugins (WerWolv#1470)
  • Loading branch information
WerWolv authored Dec 23, 2023
1 parent 84bfd10 commit 61bfe10
Show file tree
Hide file tree
Showing 149 changed files with 2,944 additions and 2,394 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ jobs:
echo "ImHex checks for the existence of this file to determine if it is running in portable mode. You should not delete this file" > $PWD/install/PORTABLE
- name: ⬆️ Upload Windows Installer
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
if-no-files-found: error
name: Windows Installer x86_64
path: |
imhex-*.msi
- name: ⬆️ Upload Portable ZIP
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
if-no-files-found: error
name: Windows Portable x86_64
Expand All @@ -120,7 +120,7 @@ jobs:
mv opengl32.dll build/install
- name: ⬆️ Upload NoGPU Portable ZIP
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
if-no-files-found: error
name: Windows Portable NoGPU x86_64
Expand Down Expand Up @@ -238,7 +238,7 @@ jobs:
ninja package
- name: ⬆️ Upload DMG
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
if-no-files-found: error
name: macOS DMG${{matrix.suffix}} x86_64
Expand Down Expand Up @@ -270,7 +270,7 @@ jobs:
docker buildx build . -f dist/macOS/arm64.Dockerfile --progress=plain --build-arg 'JOBS=4' --build-arg "BUILD_TYPE=$(BUILD_TYPE)" --build-context imhex=$(pwd) --output out
- name: ⬆️ Upload artifacts
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: macOS ZIP arm64
path: out/
Expand Down Expand Up @@ -374,7 +374,7 @@ jobs:
mv build/DebDir.deb imhex-${{env.IMHEX_VERSION}}-Ubuntu-${{ matrix.release_num }}-x86_64.deb
- name: ⬆️ Upload DEB
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
if-no-files-found: error
name: Ubuntu ${{ matrix.release_num }} DEB x86_64
Expand Down Expand Up @@ -410,14 +410,14 @@ jobs:
- name: ⬆️ Upload AppImage
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
if-no-files-found: error
name: Linux AppImage x86_64
path: 'out/*.AppImage'

- name: ⬆️ Upload AppImage zsync
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
if-no-files-found: error
name: Linux AppImage zsync x86_64
Expand Down Expand Up @@ -520,7 +520,7 @@ jobs:
mv *.pkg.tar.zst imhex-${{env.IMHEX_VERSION}}-ArchLinux-x86_64.pkg.tar.zst
- name: ⬆️ Upload imhex-archlinux.pkg.tar.zst
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
if-no-files-found: error
name: ArchLinux .pkg.tar.zst x86_64
Expand Down Expand Up @@ -648,7 +648,7 @@ jobs:
$GITHUB_WORKSPACE/imhex-${{env.IMHEX_VERSION}}-${{matrix.name}}-${{matrix.release_num}}-x86_64.rpm
- name: ⬆️ Upload RPM
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
if-no-files-found: error
name: ${{ matrix.name }} ${{ matrix.release_num }} RPM x86_64
Expand Down
7 changes: 1 addition & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ setDefaultBuiltTypeIfUnset()
detectBadClone()
verifyCompiler()

# List plugin names here. Project name must match folder name
set(PLUGINS
builtin
windows
script_loader
)
detectBundledPlugins()

# Add various defines
detectOS()
Expand Down
83 changes: 35 additions & 48 deletions cmake/build_helpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -147,27 +147,17 @@ macro(createPackage)
foreach (plugin IN LISTS PLUGINS)
add_subdirectory("plugins/${plugin}")
if (TARGET ${plugin})
get_target_property(IS_RUST_PROJECT ${plugin} RUST_PROJECT)

set_target_properties(${plugin} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/plugins)
set_target_properties(${plugin} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/plugins)

if (IS_RUST_PROJECT)
set_target_properties(${plugin} PROPERTIES CARGO_BUILD_TARGET_DIR ${CMAKE_BINARY_DIR}/plugins)

get_target_property(PLUGIN_LOCATION ${plugin} LOCATION)

install(FILES "${PLUGIN_LOCATION}/../${plugin}.hexplug" DESTINATION "${PLUGINS_INSTALL_LOCATION}" PERMISSIONS ${LIBRARY_PERMISSIONS})
else ()
if (APPLE)
if (IMHEX_GENERATE_PACKAGE)
set_target_properties(${plugin} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PLUGINS_INSTALL_LOCATION})
else ()
set_target_properties(${plugin} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/plugins)
endif ()
if (APPLE)
if (IMHEX_GENERATE_PACKAGE)
set_target_properties(${plugin} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${PLUGINS_INSTALL_LOCATION})
else ()
install(TARGETS ${plugin} LIBRARY DESTINATION ${PLUGINS_INSTALL_LOCATION})
set_target_properties(${plugin} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/plugins)
endif ()
else ()
install(TARGETS ${plugin} LIBRARY DESTINATION ${PLUGINS_INSTALL_LOCATION})
endif ()

add_dependencies(imhex_all ${plugin})
Expand Down Expand Up @@ -395,6 +385,35 @@ function(verifyCompiler)
endif()
endfunction()

macro(detectBundledPlugins)
file(GLOB PLUGINS_DIRS "plugins/*")

if (NOT DEFINED IMHEX_INCLUDE_PLUGINS)
foreach(PLUGIN_DIR ${PLUGINS_DIRS})
if (EXISTS "${PLUGIN_DIR}/CMakeLists.txt")
get_filename_component(PLUGIN_NAME ${PLUGIN_DIR} NAME)
if (NOT (${PLUGIN_NAME} IN_LIST IMHEX_EXCLUDE_PLUGINS))
list(APPEND PLUGINS ${PLUGIN_NAME})
endif ()
endif()
endforeach()
else()
set(PLUGINS ${IMHEX_INCLUDE_PLUGINS})
endif()

foreach(PLUGIN_NAME ${PLUGINS})
message(STATUS "Enabled bundled plugin '${PLUGIN_NAME}'")
endforeach()

if (NOT PLUGINS)
message(FATAL_ERROR "No bundled plugins enabled")
endif()

if (NOT ("builtin" IN_LIST PLUGINS))
message(FATAL_ERROR "The 'builtin' plugin is required for ImHex to work!")
endif ()
endmacro()

macro(setVariableInParent variable value)
get_directory_property(hasParent PARENT_DIRECTORY)

Expand Down Expand Up @@ -561,24 +580,6 @@ macro(addBundledLibraries)
find_package(LLVM REQUIRED Demangle)
endif()

if (NOT USE_SYSTEM_YARA)
add_subdirectory(${THIRD_PARTY_LIBS_FOLDER}/yara EXCLUDE_FROM_ALL)
set_target_properties(libyara PROPERTIES POSITION_INDEPENDENT_CODE ON)
set(YARA_LIBRARIES libyara)
else()
find_package(PkgConfig REQUIRED)
pkg_check_modules(YARA REQUIRED IMPORTED_TARGET yara)
endif()

if (NOT USE_SYSTEM_MINIAUDIO)
add_subdirectory(${THIRD_PARTY_LIBS_FOLDER}/miniaudio EXCLUDE_FROM_ALL)
set_target_properties(miniaudio PROPERTIES POSITION_INDEPENDENT_CODE ON)
set(MINIAUDIO_LIBRARIES miniaudio)
else()
find_package(PkgConfig REQUIRED)
pkg_check_modules(miniaudio REQUIRED IMPORTED_TARGET miniaudio)
endif()

if (NOT USE_SYSTEM_JTHREAD)
add_subdirectory(${THIRD_PARTY_LIBS_FOLDER}/jthread EXCLUDE_FROM_ALL)
set(JTHREAD_LIBRARIES jthread)
Expand All @@ -591,20 +592,6 @@ macro(addBundledLibraries)
set(JTHREAD_LIBRARIES jthread)
endif()

if (NOT USE_SYSTEM_CAPSTONE)
set(CAPSTONE_BUILD_STATIC_RUNTIME OFF CACHE BOOL "Disable shared library building")
set(CAPSTONE_BUILD_SHARED OFF CACHE BOOL "Disable shared library building")
set(CAPSTONE_BUILD_TESTS OFF CACHE BOOL "Disable tests")
add_subdirectory(${THIRD_PARTY_LIBS_FOLDER}/capstone EXCLUDE_FROM_ALL)
set_target_properties(capstone PROPERTIES POSITION_INDEPENDENT_CODE ON)
target_compile_options(capstone PRIVATE -Wno-unused-function)
set(CAPSTONE_LIBRARIES "capstone")
set(CAPSTONE_INCLUDE_DIRS ${THIRD_PARTY_LIBS_FOLDER}/capstone/include)
else()
find_package(PkgConfig REQUIRED)
pkg_search_module(CAPSTONE 4.0.2 REQUIRED capstone)
endif()

set(LIBPL_BUILD_CLI_AS_EXECUTABLE OFF)
add_subdirectory(${EXTERNAL_LIBS_FOLDER}/pattern_language EXCLUDE_FROM_ALL)
set_target_properties(libpl PROPERTIES POSITION_INDEPENDENT_CODE ON)
Expand Down
15 changes: 11 additions & 4 deletions cmake/modules/ImHexPlugin.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
macro(add_imhex_plugin)
# Parse arguments
set(options "")
set(options LIBRARY_PLUGIN)
set(oneValueArgs NAME)
set(multiValueArgs SOURCES INCLUDES LIBRARIES)
cmake_parse_arguments(IMHEX_PLUGIN "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
Expand All @@ -12,8 +12,15 @@ macro(add_imhex_plugin)

configure_file(${CMAKE_SOURCE_DIR}/dist/web/plugin-bundle.cpp.in ${CMAKE_CURRENT_BINARY_DIR}/plugin-bundle.cpp @ONLY)
target_sources(main PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/plugin-bundle.cpp)
set(IMHEX_PLUGIN_SUFFIX ".hexplug")
else()
set(IMHEX_PLUGIN_LIBRARY_TYPE MODULE)
if (IMHEX_PLUGIN_LIBRARY_PLUGIN)
set(IMHEX_PLUGIN_LIBRARY_TYPE SHARED)
set(IMHEX_PLUGIN_SUFFIX ".hexpluglib")
else()
set(IMHEX_PLUGIN_LIBRARY_TYPE MODULE)
set(IMHEX_PLUGIN_SUFFIX ".hexplug")
endif()
endif()

# Define new project for plugin
Expand All @@ -24,7 +31,7 @@ macro(add_imhex_plugin)

# Add include directories and link libraries
target_include_directories(${IMHEX_PLUGIN_NAME} PUBLIC ${IMHEX_PLUGIN_INCLUDES})
target_link_libraries(${IMHEX_PLUGIN_NAME} PRIVATE libimhex ${IMHEX_PLUGIN_LIBRARIES} fmt::fmt imgui_all_includes libwolv)
target_link_libraries(${IMHEX_PLUGIN_NAME} PRIVATE libimhex ${IMHEX_PLUGIN_LIBRARIES} ${FMT_LIBRARIES} imgui_all_includes libwolv)
addIncludesFromLibrary(${IMHEX_PLUGIN_NAME} libpl)

# Add IMHEX_PROJECT_NAME and IMHEX_VERSION define
Expand All @@ -43,7 +50,7 @@ macro(add_imhex_plugin)
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/plugins
CXX_STANDARD 23
PREFIX ""
SUFFIX ".hexplug"
SUFFIX ${IMHEX_PLUGIN_SUFFIX}
)

# Setup a romfs for the plugin
Expand Down
17 changes: 9 additions & 8 deletions cmake/modules/PostprocessBundle.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,27 @@ message(STATUS "Fixing up application bundle: ${BUNDLE_PATH}")


# Make sure to fix up any included ImHex plugin.
file(GLOB_RECURSE extra_libs "${BUNDLE_PATH}/Contents/MacOS/plugins/*.hexplug")
file(GLOB_RECURSE plugins "${BUNDLE_PATH}/Contents/MacOS/plugins/*.hexplug")
file(GLOB_RECURSE plugin_libs "${BUNDLE_PATH}/Contents/MacOS/plugins/*.hexpluglib")


# BundleUtilities doesn't support DYLD_FALLBACK_LIBRARY_PATH behavior, which
# makes it sometimes break on libraries that do weird things with @rpath. Specify
# equivalent search directories until https://gitlab.kitware.com/cmake/cmake/issues/16625
# is fixed and in our minimum CMake version.
set(extra_dirs "/usr/local/lib" "/lib" "/usr/lib" ${EXTRA_BUNDLE_LIBRARY_PATHS})
set(extra_dirs "/usr/local/lib" "/lib" "/usr/lib" ${EXTRA_BUNDLE_LIBRARY_PATHS} "${BUNDLE_PATH}/Contents/MacOS/plugins")
message(STATUS "Fixing up application bundle: ${extra_dirs}")

# BundleUtilities is overly verbose, so disable most of its messages
function(message)
if(NOT ARGV MATCHES "^STATUS;")
_message(${ARGV})
endif()
endfunction()
#function(message)
# if(NOT ARGV MATCHES "^STATUS;")
# _message(${ARGV})
# endif()
#endfunction()

include(BundleUtilities)
set(BU_CHMOD_BUNDLE_ITEMS ON)
fixup_bundle("${BUNDLE_PATH}" "${extra_libs}" "${extra_dirs}")
fixup_bundle("${BUNDLE_PATH}" "${plugins};${plugin_libs}" "${extra_dirs}")

if (CODE_SIGN_CERTIFICATE_ID)
# Hack around Apple Silicon signing bugs by copying the real app, signing it and moving it back.
Expand Down
2 changes: 1 addition & 1 deletion dist/Arch/PKGBUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package() {
install -Dm755 "$srcdir/usr/bin/imhex" "$pkgdir/usr/bin/imhex"
install -Dm644 "$srcdir/usr/lib/libimhex.so.$pkgver" "$pkgdir/usr/lib/libimhex.so.$pkgver"

for plugin in "$srcdir/usr/lib/imhex/plugins/"*.hexplug; do
for plugin in "$srcdir/usr/lib/imhex/plugins/"*.hexplug*; do
install -Dm644 "$plugin" "$pkgdir/usr/lib/imhex/plugins/${plugin##*/}"
done

Expand Down
1 change: 1 addition & 0 deletions dist/web/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ ccache -zs
cmake /imhex \
-DIMHEX_OFFLINE_BUILD=ON \
-DIMHEX_STATIC_LINK_PLUGINS=ON \
-DIMHEX_EXCLUDE_PLUGINS="script_loader" \
-DNATIVE_CMAKE_C_COMPILER=gcc \
-DNATIVE_CMAKE_CXX_COMPILER=g++ \
-DCMAKE_C_COMPILER_LAUNCHER=ccache \
Expand Down
10 changes: 8 additions & 2 deletions lib/libimhex/include/hex/api/plugin_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <string>

#include <wolv/io/fs.hpp>
#include <hex/helpers/logger.hpp>

struct ImGuiContext;

Expand All @@ -18,6 +19,7 @@ namespace hex {

struct PluginFunctions {
using InitializePluginFunc = void (*)();
using InitializeLibraryFunc = void (*)();
using GetPluginNameFunc = const char *(*)();
using GetPluginAuthorFunc = const char *(*)();
using GetPluginDescriptionFunc = const char *(*)();
Expand All @@ -27,6 +29,7 @@ namespace hex {
using GetSubCommandsFunc = void* (*)();

InitializePluginFunc initializePluginFunction = nullptr;
InitializeLibraryFunc initializeLibraryFunction = nullptr;
GetPluginNameFunc getPluginNameFunction = nullptr;
GetPluginAuthorFunc getPluginAuthorFunction = nullptr;
GetPluginDescriptionFunc getPluginDescriptionFunction = nullptr;
Expand All @@ -38,8 +41,8 @@ namespace hex {

class Plugin {
public:
explicit Plugin(const std::fs::path &path);
explicit Plugin(PluginFunctions functions);
explicit Plugin(const std::fs::path &path, bool libraryPlugin);
explicit Plugin(PluginFunctions functions, bool libraryPlugin);

Plugin(const Plugin &) = delete;
Plugin(Plugin &&other) noexcept;
Expand All @@ -59,9 +62,12 @@ namespace hex {

[[nodiscard]] std::span<SubCommand> getSubCommands() const;

[[nodiscard]] bool isLibraryPlugin() const { return m_libraryPlugin; }

private:
uintptr_t m_handle = 0;
std::fs::path m_path;
bool m_libraryPlugin;

mutable bool m_initialized = false;

Expand Down
19 changes: 19 additions & 0 deletions lib/libimhex/include/hex/plugin.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@
* Name, Author and Description will be displayed in the in the plugin list on the Welcome screen.
*/
#define IMHEX_PLUGIN_SETUP(name, author, description) IMHEX_PLUGIN_SETUP_IMPL(name, author, description)
#define IMHEX_LIBRARY_SETUP() IMHEX_LIBRARY_SETUP_IMPL()

#define IMHEX_LIBRARY_SETUP_IMPL() \
IMHEX_PLUGIN_VISIBILITY_PREFIX void initializeLibrary(); \
extern "C" [[gnu::visibility("default")]] void WOLV_TOKEN_CONCAT(forceLinkPlugin_, IMHEX_PLUGIN_NAME)() { \
hex::PluginManager::addPlugin(hex::PluginFunctions { \
nullptr, \
initializeLibrary, \
nullptr, \
nullptr, \
nullptr, \
nullptr, \
nullptr, \
nullptr, \
nullptr \
}); \
} \
IMHEX_PLUGIN_VISIBILITY_PREFIX void initializeLibrary()

#define IMHEX_PLUGIN_SETUP_IMPL(name, author, description) \
IMHEX_PLUGIN_VISIBILITY_PREFIX const char *getPluginName() { return name; } \
Expand All @@ -36,6 +54,7 @@
extern "C" [[gnu::visibility("default")]] void WOLV_TOKEN_CONCAT(forceLinkPlugin_, IMHEX_PLUGIN_NAME)() { \
hex::PluginManager::addPlugin(hex::PluginFunctions { \
initializePlugin, \
nullptr, \
getPluginName, \
getPluginAuthor, \
getPluginDescription, \
Expand Down
Loading

0 comments on commit 61bfe10

Please sign in to comment.