Add CPack for creating releases for windows (zip)

On windows a release can be created by running 'cpack' in build folder.
It creates a .zip file with the .exe file as well as all
required libraries and assets.
This commit is contained in:
jonny-vb 2023-12-19 09:44:54 -08:00
parent 778c69f67a
commit 0999c5888e

View File

@ -62,6 +62,8 @@ add_executable(Snake ${SOURCES})
#--- link libraries --- #--- link libraries ---
if(WIN32) if(WIN32)
# Link libraries statically on Windows to prevent missing basic libraries on other systems.
target_link_options(Snake PRIVATE -static)
target_link_libraries(Snake ${SDL2_LIBRARIES} ${SDL_TTF_LIBRARIES}) target_link_libraries(Snake ${SDL2_LIBRARIES} ${SDL_TTF_LIBRARIES})
else() else()
target_link_libraries(Snake SDL2::SDL2 SDL2_ttf::SDL2_ttf) target_link_libraries(Snake SDL2::SDL2 SDL2_ttf::SDL2_ttf)
@ -78,4 +80,43 @@ if(WIN32)
add_custom_command(TARGET Snake POST_BUILD COMMAND add_custom_command(TARGET Snake POST_BUILD COMMAND
${CMAKE_COMMAND} -E copy_if_different ${DLL} $<TARGET_FILE_DIR:Snake>) ${CMAKE_COMMAND} -E copy_if_different ${DLL} $<TARGET_FILE_DIR:Snake>)
endforeach() endforeach()
endif()
############################################
######## generate release .zip file ########
############################################
# Generate a distributable ZIP archive for sharing the game.
# Note: currently only intended for windows systems
# Usage:
# 1. Build the project: cd build && cmake .. && make
# 2. Generate ZIP file: cpack
# 3. Share the ZIP file for others to run the game effortlessly.
if(WIN32)
# generator for zip archive TODO add other generator e.g. installer
set(CPACK_GENERATOR "ZIP")
# Specify to exclude the top-level directory from the archive.
set(CPACK_INCLUDE_TOPLEVEL_DIRECTORY ON)
# Specify the components to be included in the package
install(TARGETS Snake
RUNTIME DESTINATION .
COMPONENT Runtime
)
# copy DLL files
install(FILES ${SDL2_DLLS} ${SDL2_TTF_DLLS}
DESTINATION .
COMPONENT Runtime
)
# copy assets folder
install(DIRECTORY ${CMAKE_SOURCE_DIR}/assets
DESTINATION .
COMPONENT Assets
)
include(CPack)
endif() endif()