From c79369e8da4d66a0da1619d04c4ca2b8a6f28cb9 Mon Sep 17 00:00:00 2001 From: Marcus Holland-Moritz Date: Sun, 9 Jul 2023 16:47:23 +0200 Subject: [PATCH] Support delayed loading of WinFsp DLL for universal binary --- CMakeLists.txt | 10 +++++----- src/universal.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 898b6991..f20db53e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -518,6 +518,8 @@ if(FUSE3_FOUND OR WINFSP) DWARFS_FUSE_LOWLEVEL=0) target_include_directories(dwarfs_main PRIVATE "${WINFSP_PATH}/inc") target_link_libraries(dwarfs_main ${WINFSP}) + target_link_libraries(dwarfsuniversal delayimp.lib) + target_link_options(dwarfsuniversal PRIVATE /DELAYLOAD:winfsp-x64.dll) else() target_compile_definitions(dwarfs_main PRIVATE FUSE_USE_VERSION=35) target_link_libraries(dwarfs_main PkgConfig::FUSE3) @@ -526,16 +528,14 @@ if(FUSE3_FOUND OR WINFSP) target_link_libraries(dwarfs-bin dwarfs_main) set_target_properties(dwarfs-bin PROPERTIES OUTPUT_NAME dwarfs) target_link_libraries(dwarfsuniversal dwarfs_main) - if(NOT WIN32) + if(WINFSP) + install(TARGETS dwarfs-bin RUNTIME DESTINATION bin) + else() add_custom_command(OUTPUT mount.dwarfs COMMAND ${CMAKE_COMMAND} -E create_symlink dwarfs mount.dwarfs DEPENDS dwarfs-bin) list(APPEND SYMLINKS mount.dwarfs) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/mount.dwarfs DESTINATION sbin) - endif() - if(WIN32) - install(TARGETS dwarfs-bin RUNTIME DESTINATION bin) - else() install(TARGETS dwarfs-bin RUNTIME DESTINATION sbin) endif() list(APPEND BINARY_TARGETS dwarfs-bin) diff --git a/src/universal.cpp b/src/universal.cpp index ffe2d1dc..9e4586f5 100644 --- a/src/universal.cpp +++ b/src/universal.cpp @@ -20,6 +20,7 @@ */ #include +#include #include #include #include @@ -29,6 +30,10 @@ #include #include +#ifdef _WIN32 +#include +#endif + #include "dwarfs/error.h" #include "dwarfs/tool.h" #include "dwarfs_tool_main.h" @@ -50,6 +55,23 @@ std::string to_narrow_string(sys_char const* str) { } #ifdef _WIN32 +FARPROC WINAPI delay_hook(unsigned dliNotify, PDelayLoadInfo pdli) { + switch (dliNotify) { + case dliFailLoadLib: + std::cerr << "failed to load " << pdli->szDll << "\n"; + break; + + case dliFailGetProc: + std::cerr << "failed to load symbol from " << pdli->szDll << "\n"; + break; + + default: + return NULL; + } + + ::exit(1); +} + int dwarfs_main_helper(int argc, sys_char** argv) { std::vector argv_strings; std::vector argv_copy; @@ -79,6 +101,10 @@ std::map const functions{ } // namespace +#ifdef _WIN32 +extern "C" const PfnDliHook __pfnDliFailureHook2 = delay_hook; +#endif + int SYS_MAIN(int argc, sys_char** argv) { if (argc > 1) { auto tool_arg = to_narrow_string(argv[1]);