From dd0ac54ee5d8081b371b7b7521745e3704069482 Mon Sep 17 00:00:00 2001 From: Gregor Klevze Date: Sun, 30 Nov 2025 14:32:52 +0100 Subject: [PATCH] cmake fix --- CMakeLists.txt | 40 +++++++++++++++++++++++++++++++++++- build-production-mac.sh | 18 +++++++++++++--- cmake/MacBundleInfo.plist.in | 26 +++++++++++++++++++++++ 3 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 cmake/MacBundleInfo.plist.in diff --git a/CMakeLists.txt b/CMakeLists.txt index b530b2b..03e0186 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,7 @@ find_package(SDL3_image CONFIG REQUIRED) find_package(cpr CONFIG REQUIRED) find_package(nlohmann_json CONFIG REQUIRED) -add_executable(tetris +set(TETRIS_SOURCES src/main.cpp src/gameplay/core/Game.cpp src/core/GravityManager.cpp @@ -58,12 +58,24 @@ add_executable(tetris src/states/PlayingState.cpp ) +if(APPLE) + add_executable(tetris MACOSX_BUNDLE ${TETRIS_SOURCES}) +else() + add_executable(tetris ${TETRIS_SOURCES}) +endif() + if (WIN32) # Embed the application icon into the executable set_source_files_properties(src/app_icon.rc PROPERTIES LANGUAGE RC) target_sources(tetris PRIVATE src/app_icon.rc) endif() +if(APPLE) + set_target_properties(tetris PROPERTIES + MACOSX_BUNDLE_INFO_PLIST "${CMAKE_SOURCE_DIR}/cmake/MacBundleInfo.plist.in" + ) +endif() + if (WIN32) # Ensure favicon.ico is available in the build directory for the resource compiler set(FAVICON_SRC "${CMAKE_SOURCE_DIR}/assets/favicon/favicon.ico") @@ -88,6 +100,32 @@ if (WIN32) ) endif() +if(APPLE) + set(_mac_copy_commands) + if(EXISTS "${CMAKE_SOURCE_DIR}/assets") + list(APPEND _mac_copy_commands + COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_SOURCE_DIR}/assets" "$/assets" + ) + endif() + if(EXISTS "${CMAKE_SOURCE_DIR}/fonts") + list(APPEND _mac_copy_commands + COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_SOURCE_DIR}/fonts" "$/fonts" + ) + endif() + if(EXISTS "${CMAKE_SOURCE_DIR}/FreeSans.ttf") + list(APPEND _mac_copy_commands + COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_SOURCE_DIR}/FreeSans.ttf" "$/FreeSans.ttf" + ) + endif() + if(_mac_copy_commands) + add_custom_command(TARGET tetris POST_BUILD + ${_mac_copy_commands} + COMMENT "Copying game assets into macOS bundle" + ) + endif() +endif() +endif() + target_link_libraries(tetris PRIVATE SDL3::SDL3 SDL3_ttf::SDL3_ttf SDL3_image::SDL3_image cpr::cpr nlohmann_json::nlohmann_json) if (WIN32) diff --git a/build-production-mac.sh b/build-production-mac.sh index 94f99af..3ca9424 100644 --- a/build-production-mac.sh +++ b/build-production-mac.sh @@ -12,6 +12,7 @@ PACKAGE_DIR="" VERSION="$(date +"%Y.%m.%d")" CLEAN=0 PACKAGE_ONLY=0 +PACKAGE_RUNTIME_DIR="" print_usage() { cat <<'USAGE' @@ -127,17 +128,24 @@ copy_binary_or_bundle() { if [[ -n ${APP_BUNDLE_PATH:-} ]]; then log INFO "Copying app bundle..." rsync -a "$APP_BUNDLE_PATH" "$PACKAGE_DIR/" - PACKAGE_BINARY="${APP_BUNDLE_PATH##*/}/Contents/MacOS/${PROJECT_NAME}" + local app_name="${APP_BUNDLE_PATH##*/}" + PACKAGE_BINARY="${app_name}/Contents/MacOS/${PROJECT_NAME}" + PACKAGE_RUNTIME_DIR="$PACKAGE_DIR/${app_name}/Contents/MacOS" else log INFO "Copying executable..." cp "$EXECUTABLE_PATH" "$PACKAGE_DIR/${PROJECT_NAME}" chmod +x "$PACKAGE_DIR/${PROJECT_NAME}" PACKAGE_BINARY="${PROJECT_NAME}" + PACKAGE_RUNTIME_DIR="$PACKAGE_DIR" fi log OK "Binary ready (${PACKAGE_BINARY})" } copy_assets() { + if [[ -n ${APP_BUNDLE_PATH:-} ]]; then + log INFO "Assets already bundled inside the .app; skipping external copy." + return + fi log INFO "Copying assets..." local folders=("assets" "fonts") for folder in "${folders[@]}"; do @@ -173,7 +181,7 @@ copy_dependencies() { fi done if (( !seen )); then - cp "$dylib" "$PACKAGE_DIR/" + cp "$dylib" "$PACKAGE_RUNTIME_DIR/" copied_names+=("$name") log OK "Copied $name" fi @@ -242,7 +250,11 @@ EOF validate_package() { log INFO "Validating package contents..." local missing=() - for required in "$PACKAGE_BINARY" assets FreeSans.ttf; do + local required=("$PACKAGE_BINARY") + if [[ -z ${APP_BUNDLE_PATH:-} ]]; then + required+=(assets FreeSans.ttf) + fi + for required in "${required[@]}"; do if [[ ! -e "$PACKAGE_DIR/$required" ]]; then missing+=("$required") fi diff --git a/cmake/MacBundleInfo.plist.in b/cmake/MacBundleInfo.plist.in new file mode 100644 index 0000000..2edadcf --- /dev/null +++ b/cmake/MacBundleInfo.plist.in @@ -0,0 +1,26 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleExecutable + ${MACOSX_BUNDLE_EXECUTABLE_NAME} + CFBundleIdentifier + com.example.tetris + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + Tetris + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1.0 + LSMinimumSystemVersion + 12.0 + NSHighResolutionCapable + + +