From 79002a58f22b1d11d0a29a06d90e13060c1c5b45 Mon Sep 17 00:00:00 2001 From: guevaruli Date: Mon, 17 Nov 2025 21:43:06 +0800 Subject: [PATCH] feature: can build on MACOS ARM --- CMakeLists.txt | 32 ++++++++++++ README.md | 32 +++++++++--- build_macos.sh | 80 +++++++++++++++++++++++------ engine/source/editor/CMakeLists.txt | 7 +++ 4 files changed, 128 insertions(+), 23 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 97474124f..a07cd5218 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,38 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(BUILD_SHARED_LIBS OFF) +# Support for macOS Universal Binary (x86_64 + arm64) +if(APPLE) + # Option to build universal binary + option(BUILD_UNIVERSAL_BINARY "Build universal binary for macOS (x86_64 + arm64)" OFF) + + if(BUILD_UNIVERSAL_BINARY) + set(CMAKE_OSX_ARCHITECTURES "x86_64;arm64" CACHE STRING "Build architectures for macOS" FORCE) + message(STATUS "Building Universal Binary for macOS (x86_64 + arm64)") + else() + # Auto-detect architecture if not building universal binary + if(NOT CMAKE_OSX_ARCHITECTURES) + execute_process( + COMMAND uname -m + OUTPUT_VARIABLE NATIVE_ARCH + OUTPUT_STRIP_TRAILING_WHITESPACE + ) + if(NATIVE_ARCH STREQUAL "arm64") + set(CMAKE_OSX_ARCHITECTURES "arm64" CACHE STRING "Build architectures for macOS" FORCE) + message(STATUS "Building for Apple Silicon (arm64)") + else() + set(CMAKE_OSX_ARCHITECTURES "x86_64" CACHE STRING "Build architectures for macOS" FORCE) + message(STATUS "Building for Intel (x86_64)") + endif() + endif() + endif() + + # Set minimum macOS version + if(NOT CMAKE_OSX_DEPLOYMENT_TARGET) + set(CMAKE_OSX_DEPLOYMENT_TARGET "10.15" CACHE STRING "Minimum macOS deployment version" FORCE) + endif() +endif() + include(CMakeDependentOption) # ---- Include guards ---- diff --git a/README.md b/README.md index 3e6450f4b..36a8421c1 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ To build Piccolo, you must first install the following tools. - CMake 3.19 (or more recent) - Git 2.1 (or more recent) -### macOS >= 10.15 (x86_64) +### macOS >= 10.15 (x86_64 and Apple Silicon/ARM64) - Xcode 12.3 (or more recent) - CMake 3.19 (or more recent) - Git 2.1 (or more recent) @@ -62,20 +62,38 @@ cmake -S . -B build ### Build on macOS -> The following build instructions only tested on specific hardware of x86_64, and do not support M1 chips. For M1 compatible, we will release later. - To compile Piccolo, you must have the most recent version of Xcode installed. -Then run 'cmake' from the project's root directory, to generate a project of Xcode. +#### Option 1: Using the build script (Recommended) + +**For native architecture (automatic detection):** +```bash +./build_macos.sh release ``` -cmake -S . -B build -G "Xcode" + +**For universal binary (x86_64 + arm64):** +```bash +./build_macos.sh release --universal ``` -and you can build the project with + +**For debug build:** +```bash +./build_macos.sh debug ``` + +#### Option 2: Using CMake directly + +**For native architecture:** +```bash +cmake -S . -B build -G "Xcode" cmake --build build --config Release ``` -Or you can execute the **build_macos.sh** to build the binaries. +**For universal binary:** +```bash +cmake -S . -B build -G "Xcode" -DBUILD_UNIVERSAL_BINARY=ON +cmake --build build --config Release +``` ### Build on Ubuntu 20.04 You can execute the **build_linux.sh** to build the binaries. diff --git a/build_macos.sh b/build_macos.sh index 457748fb5..64670c0e5 100755 --- a/build_macos.sh +++ b/build_macos.sh @@ -1,31 +1,79 @@ #!/bin/bash -if test \( $# -ne 1 \); -then - echo "Usage: ./build_macos.sh config" +# Default to native architecture +BUILD_UNIVERSAL=0 + +# Parse arguments +while [[ $# -gt 0 ]]; do + case $1 in + debug|release) + if [ -z "$CONFIG" ]; then + if [ "$1" = "debug" ]; then + CONFIG="Debug" + else + CONFIG="Release" + fi + fi + shift + ;; + --universal) + BUILD_UNIVERSAL=1 + shift + ;; + *) + echo "Unknown option: $1" + echo "" + echo "Usage: ./build_macos.sh [config] [options]" + echo "" + echo "config:" + echo " debug - build with the debug configuration" + echo " release - build with the release configuration" + echo "" + echo "options:" + echo " --universal - build universal binary (x86_64 + arm64)" + echo "" + echo "Examples:" + echo " ./build_macos.sh release # Build for native architecture" + echo " ./build_macos.sh release --universal # Build universal binary" + echo "" + exit 1 + ;; + esac +done + +# Check if config is set +if [ -z "$CONFIG" ]; then + echo "Error: Configuration not specified!" + echo "" + echo "Usage: ./build_macos.sh [config] [options]" echo "" echo "config:" echo " debug - build with the debug configuration" echo " release - build with the release configuration" echo "" + echo "options:" + echo " --universal - build universal binary (x86_64 + arm64)" + echo "" exit 1 fi +# Detect current architecture +ARCH=$(uname -m) +echo "Detected architecture: $ARCH" -if test \( \( -n "$1" \) -a \( "$1" = "debug" \) \);then - CONFIG=" Debug" -elif test \( \( -n "$1" \) -a \( "$1" = "release" \) \);then - CONFIG=" Release" +# Configure CMake +if [ $BUILD_UNIVERSAL -eq 1 ]; then + echo "Building Universal Binary (x86_64 + arm64)..." + cmake -S . -B build -G "Xcode" -DBUILD_UNIVERSAL_BINARY=ON else - echo "The config \"$1\" is not supported!" - echo "" - echo "Configs:" - echo " debug - build with the debug configuration" - echo " release - build with the release configuration" - echo "" - exit 1 + echo "Building for native architecture ($ARCH)..." + cmake -S . -B build -G "Xcode" fi -cmake -S . -B build -G "Xcode" - +# Build +echo "Building with $CONFIG configuration..." cmake --build build --config "${CONFIG}" + +echo "" +echo "Build completed successfully!" +echo "Binary location: bin/" diff --git a/engine/source/editor/CMakeLists.txt b/engine/source/editor/CMakeLists.txt index 460cbf5b0..0b1dc8570 100644 --- a/engine/source/editor/CMakeLists.txt +++ b/engine/source/editor/CMakeLists.txt @@ -41,6 +41,13 @@ if(ENABLE_PHYSICS_DEBUG_RENDERER) ) endif() +# Code signing for macOS (required for Apple Silicon) +if(APPLE) + set(POST_BUILD_COMMANDS ${POST_BUILD_COMMANDS} + COMMAND codesign --force --deep --sign - "${BINARY_ROOT_DIR}/PiccoloEditor" || true + ) +endif() + add_custom_command(TARGET ${TARGET_NAME} ${POST_BUILD_COMMANDS}) #precompile