build: enhance Windows build process by copying runtime DLLs and statically linking MinGW libraries

This commit is contained in:
2026-04-04 15:12:47 +01:00
parent 9c1d6d5e6f
commit d9c4a09972
2 changed files with 41 additions and 1 deletions

View File

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

View File

@@ -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
$<TARGET_FILE:aaruformat>
$<TARGET_FILE_DIR:aaruformattool>
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}"
$<TARGET_FILE_DIR:aaruformattool>
COMMENT "Copying ${MINGW_DLL}"
)
endif()
endforeach()
endif()
endif()