Support delayed loading of WinFsp DLL for universal binary

This commit is contained in:
Marcus Holland-Moritz 2023-07-09 16:47:23 +02:00
parent 8dc5ea0697
commit c79369e8da
2 changed files with 31 additions and 5 deletions

View File

@ -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)

View File

@ -20,6 +20,7 @@
*/
#include <algorithm>
#include <cstdlib>
#include <filesystem>
#include <iostream>
#include <map>
@ -29,6 +30,10 @@
#include <folly/String.h>
#include <folly/gen/String.h>
#ifdef _WIN32
#include <delayimp.h>
#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<std::string> argv_strings;
std::vector<char*> argv_copy;
@ -79,6 +101,10 @@ std::map<std::string_view, int (*)(int, sys_char**)> 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]);