singe/cmake/zig/aarch64-macos.cmake
2026-09-04 22:36:58 -05:00

81 lines
4.1 KiB
CMake

#
# Singe 3
# Copyright (C) 2006-2026 Scott Duensing <scott@kangaroopunch.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public Licens
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# macOS on Apple silicon with zig as the compiler. zig carries the macOS libc headers and the
# libSystem stub itself; everything else (Cocoa, CoreAudio, VideoToolbox, Metal and the rest of
# the frameworks, libc++, libiconv) comes from an Apple SDK, which Apple does not let a build
# script fetch. The SDK is expected, in this order:
# 1. an unpacked SDK at SINGE_MACOS_SDK (default .builddir/toolchains/MacOSX.sdk);
# 2. a tarball at .builddir/toolchains/MacOSX.sdk.tar.xz, unpacked on first use;
# 3. SINGE_MACOS_SDK_URL (with SINGE_MACOS_SDK_SHA256), downloaded and unpacked on first use.
# See INSTALL for how to make the tarball from Xcode.
include(${CMAKE_CURRENT_LIST_DIR}/zig.cmake)
set(CMAKE_SYSTEM_NAME Darwin)
set(CMAKE_SYSTEM_PROCESSOR arm64)
get_filename_component(sdkToolchains ${CMAKE_CURRENT_LIST_DIR}/../../.builddir/toolchains ABSOLUTE)
set(SINGE_MACOS_SDK ${sdkToolchains}/MacOSX.sdk CACHE PATH "Unpacked Apple SDK for the macOS build")
set(SINGE_MACOS_SDK_URL "" CACHE STRING "Where to fetch the Apple SDK tarball from, if anywhere")
set(SINGE_MACOS_SDK_SHA256 "" CACHE STRING "SHA-256 of that tarball")
set(sdkTarball ${sdkToolchains}/MacOSX.sdk.tar.xz)
if(NOT EXISTS ${SINGE_MACOS_SDK}/System/Library/Frameworks/Cocoa.framework)
if((NOT EXISTS ${sdkTarball}) AND SINGE_MACOS_SDK_URL)
message(STATUS "Downloading the Apple SDK from ${SINGE_MACOS_SDK_URL}")
file(MAKE_DIRECTORY ${sdkToolchains})
if(SINGE_MACOS_SDK_SHA256)
file(DOWNLOAD ${SINGE_MACOS_SDK_URL} ${sdkTarball} EXPECTED_HASH SHA256=${SINGE_MACOS_SDK_SHA256} STATUS status)
else()
file(DOWNLOAD ${SINGE_MACOS_SDK_URL} ${sdkTarball} STATUS status)
endif()
list(GET status 0 code)
if(NOT code EQUAL 0)
message(FATAL_ERROR "Apple SDK download failed: ${status}")
endif()
endif()
if(EXISTS ${sdkTarball})
message(STATUS "Unpacking ${sdkTarball}")
set(sdkUnpack ${sdkToolchains}/MacOSX.sdk.unpack)
file(REMOVE_RECURSE ${sdkUnpack})
file(ARCHIVE_EXTRACT INPUT ${sdkTarball} DESTINATION ${sdkUnpack})
file(GLOB sdkFound LIST_DIRECTORIES true ${sdkUnpack}/*.sdk ${sdkUnpack}/*/*.sdk)
list(LENGTH sdkFound sdkCount)
if(NOT sdkCount EQUAL 1)
message(FATAL_ERROR "${sdkTarball} should hold exactly one MacOSX*.sdk directory; found ${sdkCount}.")
endif()
file(RENAME ${sdkFound} ${SINGE_MACOS_SDK})
file(REMOVE_RECURSE ${sdkUnpack})
endif()
endif()
if(NOT EXISTS ${SINGE_MACOS_SDK}/System/Library/Frameworks/Cocoa.framework)
message(FATAL_ERROR "No Apple SDK at ${SINGE_MACOS_SDK}. Put an SDK tarball at ${sdkTarball} (see INSTALL), or set SINGE_MACOS_SDK to an unpacked SDK, or SINGE_MACOS_SDK_URL to fetch one.")
endif()
# The SDK's headers come after zig's own libc headers; frameworks and stub libraries from the SDK.
singeZigToolchain(aarch64-macos "--sysroot=${SINGE_MACOS_SDK} -idirafter ${SINGE_MACOS_SDK}/usr/include -F${SINGE_MACOS_SDK}/System/Library/Frameworks -L${SINGE_MACOS_SDK}/usr/lib")
set(CMAKE_OSX_SYSROOT ${SINGE_MACOS_SDK} CACHE PATH "Apple SDK" FORCE)
set(CMAKE_OSX_ARCHITECTURES "" CACHE STRING "Left empty; the zig target names the architecture" FORCE)
set(CMAKE_FIND_ROOT_PATH ${SINGE_MACOS_SDK})
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(KANGAROO_OS macos)
set(KANGAROO_ARCH aarch64)