diff --git a/CMakeLists.txt b/CMakeLists.txt index ec36625..3eb229a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,7 +102,11 @@ if("${CMAKE_C_PLATFORM_ID}" MATCHES "MinGW") if("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "aarch64" OR "${CMAKE_SYSTEM_PROCESSOR}" MATCHES "arm") set(WIN32 TRUE) endif() - add_link_options(-static-libgcc) + # Statically link the MinGW/GCC runtime and winpthread so the executable + # does not depend on libgcc_s_*.dll or libwinpthread-1.dll at runtime. + add_link_options(-static-libgcc -static-libstdc++ + -Wl,-Bstatic,--whole-archive -lwinpthread + -Wl,--no-whole-archive,-Bdynamic) endif() message(STATUS "Detected system processor: ${CMAKE_SYSTEM_PROCESSOR}") diff --git a/tool/CMakeLists.txt b/tool/CMakeLists.txt index cd70384..7366312 100644 --- a/tool/CMakeLists.txt +++ b/tool/CMakeLists.txt @@ -80,3 +80,39 @@ endif() if(APPLE) target_link_libraries(aaruformattool PRIVATE "-lc++") endif() + +# On Windows, copy libaaruformat.dll and MinGW runtime DLLs to the output directory +if(WIN32) + # Copy libaaruformat.dll + add_custom_command(TARGET aaruformattool POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + $ + $ + COMMENT "Copying libaaruformat.dll" + ) + + # Copy MinGW runtime DLLs (GCC/MinGW builds only) + if(CMAKE_C_COMPILER_ID MATCHES "GNU") + get_filename_component(MINGW_BIN_DIR "${CMAKE_C_COMPILER}" DIRECTORY) + + # DLLs to copy — cover both SEH (64-bit) and DWARF (32-bit) libgcc variants + set(MINGW_RUNTIME_DLLS + libgcc_s_seh-1.dll + libgcc_s_dw2-1.dll + libstdc++-6.dll + libwinpthread-1.dll + ) + + foreach(MINGW_DLL IN LISTS MINGW_RUNTIME_DLLS) + if(EXISTS "${MINGW_BIN_DIR}/${MINGW_DLL}") + add_custom_command(TARGET aaruformattool POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${MINGW_BIN_DIR}/${MINGW_DLL}" + $ + COMMENT "Copying ${MINGW_DLL}" + ) + endif() + endforeach() + endif() +endif() +