From 72ec6baf79f5c0a81dfe9a1baa4dc3a00c1176f7 Mon Sep 17 00:00:00 2001 From: Kuruyia <8174691+Kuruyia@users.noreply.github.com> Date: Sat, 9 Oct 2021 23:07:58 +0200 Subject: [PATCH] sys: Fix macOS compilation (as of 2dc1886) (#317) * sys: Updated curl to latest version * sys: Fix macOS compilation * ui: Fix splash screen OpenGL init for macOS * sys: Fix std::min compile errors * git: Re-enabled macos workflow * sys: Remove includes of the range library * build: Find OpenGL using CMake * sys/build: Fix bundled plugins on macOS * build: Copy plugins to bundle when creating a bundle * build: Fixup bundled plugins * sys: Search for plugins in the bundle instead of in Application Support * sys: Allow resources to be placed in multiple directories on macOS * build: Output built plugins to the plugins/ directory when not creating a bundle on macOS * sys: Fix Application Support paths on macOS * sys: Define ftruncate64 on macOS * sys: Fix absolute value computation for std::string::at on macOS Co-authored-by: WerWolv --- .github/workflows/build.yml | 1 - cmake/build_helpers.cmake | 8 ++- cmake/modules/PostprocessBundle.cmake | 5 +- external/ImGui/CMakeLists.txt | 6 +- external/curl | 2 +- .../source/content/pl_builtin_functions.cpp | 9 ++- .../builtin/source/content/tools_entries.cpp | 3 +- plugins/libimhex/include/hex/helpers/file.hpp | 1 + .../libimhex/include/hex/helpers/paths_mac.h | 3 +- .../libimhex/include/hex/helpers/utils.hpp | 1 + .../include/hex/pattern_language/ast_node.hpp | 8 +-- plugins/libimhex/source/helpers/paths.cpp | 34 ++++++++++- plugins/libimhex/source/helpers/paths_mac.mm | 61 +++++-------------- plugins/libimhex/source/resources.cpp | 24 ++++---- source/helpers/encoding_file.cpp | 5 +- source/init/splash_window.cpp | 2 +- source/views/view_store.cpp | 2 +- 17 files changed, 100 insertions(+), 75 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a183ca67f..2a69dd669 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -140,7 +140,6 @@ jobs: build/*.msi macos: - if: false runs-on: macos-11.0 name: 🍎 macOS 11.0 steps: diff --git a/cmake/build_helpers.cmake b/cmake/build_helpers.cmake index f8dce0c62..c1c9bf5c5 100644 --- a/cmake/build_helpers.cmake +++ b/cmake/build_helpers.cmake @@ -157,8 +157,15 @@ macro(createPackage) add_subdirectory("plugins/${plugin}") if (TARGET ${plugin}) set_target_properties(${plugin} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/plugins) + if (WIN32) install(TARGETS ${plugin} RUNTIME DESTINATION ${PLUGINS_INSTALL_LOCATION}) + elseif (APPLE) + if (CREATE_BUNDLE) + 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 () else () install(TARGETS ${plugin} LIBRARY DESTINATION ${PLUGINS_INSTALL_LOCATION}) endif () @@ -168,7 +175,6 @@ macro(createPackage) endforeach() set_target_properties(libimhex PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) - install(TARGETS libimhex RUNTIME DESTINATION ${CMAKE_INSTALL_LIBDIR}) if (WIN32) # Install binaries directly in the prefix, usually C:\Program Files\ImHex. diff --git a/cmake/modules/PostprocessBundle.cmake b/cmake/modules/PostprocessBundle.cmake index 917e0e914..8a644a917 100644 --- a/cmake/modules/PostprocessBundle.cmake +++ b/cmake/modules/PostprocessBundle.cmake @@ -26,9 +26,8 @@ get_filename_component(BUNDLE_PATH "${BUNDLE_PATH}" ABSOLUTE) message(STATUS "Fixing up application bundle: ${BUNDLE_PATH}") -# Make sure to fix up any additional shared libraries (like plugins) that are -# needed. -file(GLOB_RECURSE extra_libs "${BUNDLE_PATH}/Contents/MacOS/*.dylib") +# Make sure to fix up any included ImHex plugin. +file(GLOB_RECURSE extra_libs "${BUNDLE_PATH}/Contents/MacOS/plugins/*.hexplug") message(STATUS "Fixing up application bundle: ${extra_dirs}") diff --git a/external/ImGui/CMakeLists.txt b/external/ImGui/CMakeLists.txt index f6d3a750b..5fa06d0b7 100644 --- a/external/ImGui/CMakeLists.txt +++ b/external/ImGui/CMakeLists.txt @@ -7,6 +7,10 @@ find_package(PkgConfig REQUIRED) find_package(Freetype REQUIRED) pkg_search_module(GLFW REQUIRED glfw3) +if (UNIX) + find_package(OpenGL REQUIRED) +endif () + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") add_library(imgui STATIC @@ -43,5 +47,5 @@ target_link_directories(imgui PUBLIC ${GLFW_LIBRARY_DIRS}) if (WIN32) target_link_libraries(imgui Freetype::Freetype glfw3 opengl32.lib) elseif (UNIX) - target_link_libraries(imgui Freetype::Freetype glfw GL) + target_link_libraries(imgui Freetype::Freetype glfw OpenGL::GL) endif() diff --git a/external/curl b/external/curl index bfbde883a..aceff6088 160000 --- a/external/curl +++ b/external/curl @@ -1 +1 @@ -Subproject commit bfbde883af33397943df68a3ae01847a634d33bf +Subproject commit aceff6088cef70843de1da1dd7cc03c37c094d99 diff --git a/plugins/builtin/source/content/pl_builtin_functions.cpp b/plugins/builtin/source/content/pl_builtin_functions.cpp index 9f4aaba3e..404c47194 100644 --- a/plugins/builtin/source/content/pl_builtin_functions.cpp +++ b/plugins/builtin/source/content/pl_builtin_functions.cpp @@ -193,7 +193,14 @@ namespace hex::plugin::builtin { auto string = Token::literalToString(params[0], false); auto index = Token::literalToSigned(params[1]); - if (std::abs(index) > string.length()) +#if defined(OS_MACOS) + const auto signIndex = index >> (sizeof(index) * 8 - 1); + const auto absIndex = (index ^ signIndex) - signIndex; +#else + const auto absIndex = std::abs(index); +#endif + + if (absIndex > string.length()) LogConsole::abortEvaluation("character index out of range"); if (index >= 0) diff --git a/plugins/builtin/source/content/tools_entries.cpp b/plugins/builtin/source/content/tools_entries.cpp index 1e7cff57e..b5ed010d3 100644 --- a/plugins/builtin/source/content/tools_entries.cpp +++ b/plugins/builtin/source/content/tools_entries.cpp @@ -750,8 +750,9 @@ namespace hex::plugin::builtin { size_t fileSize = file.getSize(); for (const auto &pattern : overwritePattern) { for (u64 offset = 0; offset < fileSize; offset += 3) { - file.write(pattern.data(), std::min(pattern.size(), fileSize - offset)); + file.write(pattern.data(), std::min(pattern.size(), fileSize - offset)); } + file.flush(); } diff --git a/plugins/libimhex/include/hex/helpers/file.hpp b/plugins/libimhex/include/hex/helpers/file.hpp index d19f1f290..c1952e355 100644 --- a/plugins/libimhex/include/hex/helpers/file.hpp +++ b/plugins/libimhex/include/hex/helpers/file.hpp @@ -11,6 +11,7 @@ #define fopen64 fopen #define fseeko64 fseek #define ftello64 ftell + #define ftruncate64 ftruncate #endif namespace hex { diff --git a/plugins/libimhex/include/hex/helpers/paths_mac.h b/plugins/libimhex/include/hex/helpers/paths_mac.h index 807a6b273..6f826921e 100644 --- a/plugins/libimhex/include/hex/helpers/paths_mac.h +++ b/plugins/libimhex/include/hex/helpers/paths_mac.h @@ -4,6 +4,7 @@ #include namespace hex { - std::string getPathForMac(ImHexPath path); + std::string getMacExecutableDirectoryPath(); + std::string getMacApplicationSupportDirectoryPath(); } #endif diff --git a/plugins/libimhex/include/hex/helpers/utils.hpp b/plugins/libimhex/include/hex/helpers/utils.hpp index ae930fb96..d404d37e6 100644 --- a/plugins/libimhex/include/hex/helpers/utils.hpp +++ b/plugins/libimhex/include/hex/helpers/utils.hpp @@ -5,6 +5,7 @@ #include #include +#include #include #include #include diff --git a/plugins/libimhex/include/hex/pattern_language/ast_node.hpp b/plugins/libimhex/include/hex/pattern_language/ast_node.hpp index 3479c422d..731f0274f 100644 --- a/plugins/libimhex/include/hex/pattern_language/ast_node.hpp +++ b/plugins/libimhex/include/hex/pattern_language/ast_node.hpp @@ -4,12 +4,12 @@ #include #include +#include #include #include #include #include #include -#include #include @@ -1534,9 +1534,9 @@ namespace hex::pl { continue; } else { bool found = false; - for (const auto &variable : (searchScope | std::views::reverse)) { - if (variable->getVariableName() == name) { - auto newPattern = variable->clone(); + for (auto iter = searchScope.crbegin(); iter != searchScope.crend(); ++iter) { + if ((*iter)->getVariableName() == name) { + auto newPattern = (*iter)->clone(); delete currPattern; currPattern = newPattern; found = true; diff --git a/plugins/libimhex/source/helpers/paths.cpp b/plugins/libimhex/source/helpers/paths.cpp index e9cdb13a5..e5883e758 100644 --- a/plugins/libimhex/source/helpers/paths.cpp +++ b/plugins/libimhex/source/helpers/paths.cpp @@ -83,7 +83,39 @@ namespace hex { return results; #elif defined(OS_MACOS) - return { getPathForMac(path) }; + // Get path to special directories + const std::filesystem::path exeDir(getMacExecutableDirectoryPath()); + const std::filesystem::path applicationSupportDir(getMacApplicationSupportDirectoryPath()); + + std::vector paths = { exeDir, applicationSupportDir }; + std::vector results; + + switch (path) { + case ImHexPath::Patterns: + return { (applicationSupportDir / "patterns").string() }; + case ImHexPath::PatternsInclude: + return { (applicationSupportDir / "includes").string() }; + case ImHexPath::Magic: + return { (applicationSupportDir / "magic").string() }; + case ImHexPath::Python: + return { (applicationSupportDir / "python").string() }; + case ImHexPath::Plugins: + std::transform(paths.begin(), paths.end(), std::back_inserter(results), [](auto &path){ + return (path / "plugins").string(); + }); + break; + case ImHexPath::Yara: + return { (applicationSupportDir / "yara").string() }; + case ImHexPath::Config: + return { (applicationSupportDir / "config").string() }; + case ImHexPath::Resources: + return { (applicationSupportDir / "resources").string() }; + case ImHexPath::Constants: + return { (applicationSupportDir / "constants").string() }; + default: __builtin_unreachable(); + } + + return results; #else std::vector configDirs = xdg::ConfigDirs(); std::vector dataDirs = xdg::DataDirs(); diff --git a/plugins/libimhex/source/helpers/paths_mac.mm b/plugins/libimhex/source/helpers/paths_mac.mm index 568e0368c..656665998 100644 --- a/plugins/libimhex/source/helpers/paths_mac.mm +++ b/plugins/libimhex/source/helpers/paths_mac.mm @@ -1,58 +1,29 @@ #if defined(OS_MACOS) - #include + #include #include namespace hex { - std::string getPathForMac(ImHexPath path) { + std::string getMacExecutableDirectoryPath() { @autoreleasepool { - NSError * error = nil; - NSURL * appSupportDir = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory - inDomain:NSUserDomainMask - appropriateForURL:nil - create:YES - error:&error]; + return {[[[[[NSBundle mainBundle] executableURL] URLByDeletingLastPathComponent] path] UTF8String]}; + } + } + + std::string getMacApplicationSupportDirectoryPath() { + @autoreleasepool { + NSError* error = nil; + NSURL* dirUrl = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory + inDomain:NSUserDomainMask + appropriateForURL:nil + create:YES + error:&error]; + if (error != nil) { __builtin_unreachable(); } - NSURL * result = nil; - switch (path) { - case ImHexPath::Patterns: - result = [appSupportDir URLByAppendingPathComponent:@"imhex/patterns"]; - break; - - case ImHexPath::PatternsInclude: - result = [appSupportDir URLByAppendingPathComponent:@"imhex/patterns"]; - break; - case ImHexPath::Magic: - result = [appSupportDir URLByAppendingPathComponent:@"imhex/magic"]; - break; - case ImHexPath::Python: - result = [appSupportDir URLByAppendingPathComponent:@"imhex"]; - break; - case ImHexPath::Plugins: - result = [appSupportDir URLByAppendingPathComponent:@"imhex/plugins"]; - break; - case ImHexPath::Yara: - result = [appSupportDir URLByAppendingPathComponent:@"imhex/yara"]; - break; - case ImHexPath::Config: - result = [appSupportDir URLByAppendingPathComponent:@"imhex/config"]; - break; - case ImHexPath::Resources: - result = [appSupportDir URLByAppendingPathComponent:@"imhex/resources"]; - break; - case ImHexPath::Constants: - result = [appSupportDir URLByAppendingPathComponent:@"imhex/constants"]; - break; - } - - if (result == nil) { - __builtin_unreachable(); - } - - return std::string([[result path] UTF8String]); + return {[[[dirUrl URLByAppendingPathComponent:(@"imhex")] path] UTF8String]}; } } } diff --git a/plugins/libimhex/source/resources.cpp b/plugins/libimhex/source/resources.cpp index 0301da7ec..69da7c23d 100644 --- a/plugins/libimhex/source/resources.cpp +++ b/plugins/libimhex/source/resources.cpp @@ -27,26 +27,28 @@ #elif defined(OS_MACOS) - #define RESOURCE_NULL_TERMINATED(name, path) \ + #define RESOURCE(name, path) \ __asm__ ( \ - ".global " #name ";\n" \ - ".global " #name "_size;\n" \ - #name ":\n" \ + ".global _" #name ";\n" \ + ".global _" #name "_size;\n" \ + "_" #name ":\n" \ ".incbin \"" path "\";\n" \ - #name "_size:\n" \ - ".int " #name "_size - " #name ";\n" \ + ".align 8;\n" \ + "_" #name "_size:\n" \ + ".int _" #name "_size - _" #name ";\n" \ ".align 8;\n" \ ) #define RESOURCE_NULL_TERMINATED(name, path) \ __asm__ ( \ - ".global " #name ";\n" \ - ".global " #name "_size;\n" \ - #name ":\n" \ + ".global _" #name ";\n" \ + ".global _" #name "_size;\n" \ + "_" #name ":\n" \ ".incbin \"" path "\";\n" \ ".byte 0\n" \ - #name "_size:\n" \ - ".int " #name "_size - " #name ";\n" \ + ".align 8;\n" \ + "_" #name "_size:\n" \ + ".int _" #name "_size - _" #name ";\n" \ ".align 8;\n" \ ) diff --git a/source/helpers/encoding_file.cpp b/source/helpers/encoding_file.cpp index bc1c7b65d..9dbb475ff 100644 --- a/source/helpers/encoding_file.cpp +++ b/source/helpers/encoding_file.cpp @@ -1,6 +1,5 @@ #include "helpers/encoding_file.hpp" -#include #include #include @@ -17,7 +16,9 @@ namespace hex { } std::pair EncodingFile::getEncodingFor(const std::vector &buffer) const { - for (const auto &[size, mapping] : this->m_mapping | std::views::reverse) { + for (auto riter = this->m_mapping.crbegin(); riter != this->m_mapping.crend(); ++riter) { + const auto &[size, mapping] = *riter; + if (size > buffer.size()) continue; auto key = std::vector(buffer.begin(), buffer.begin() + size); diff --git a/source/init/splash_window.cpp b/source/init/splash_window.cpp index 16ca9d41d..a1a594056 100644 --- a/source/init/splash_window.cpp +++ b/source/init/splash_window.cpp @@ -194,7 +194,7 @@ namespace hex::init { ImGui::StyleColorsDark(); ImGui_ImplGlfw_InitForOpenGL(this->m_window, true); - ImGui_ImplOpenGL3_Init("#version 130"); + ImGui_ImplOpenGL3_Init("#version 150"); auto &io = ImGui::GetIO(); diff --git a/source/views/view_store.cpp b/source/views/view_store.cpp index 71ca4df53..fc660ccc4 100644 --- a/source/views/view_store.cpp +++ b/source/views/view_store.cpp @@ -82,7 +82,7 @@ namespace hex { std::vector buffer(0x10000); for (u64 offset = 0; offset < header.size; offset += buffer.size()) { - auto readSize = std::min(buffer.size(), header.size - offset); + auto readSize = std::min(buffer.size(), header.size - offset); mtar_read_data(&ctx, buffer.data(), readSize); buffer.resize(readSize);