singe/cmake/zig/macos.cmake

99 lines
5.9 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 with zig as the compiler, shared by the aarch64 and x86_64 toolchain files (which set
# SINGE_MACOS_ARCH and CMAKE_SYSTEM_PROCESSOR before including this). 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;
# the default points at the community mirror osxcross users share (github.com/joseluisq/
# macosx-sdks), pinned to one SDK release and its published SHA-256.
# See INSTALL for how to make the tarball from Xcode instead.
include(${CMAKE_CURRENT_LIST_DIR}/zig.cmake)
set(CMAKE_SYSTEM_NAME Darwin)
# Oldest macOS the binary runs on (Ventura: Intel Macs from 2017 and every Apple silicon Mac).
set(SINGE_MACOS_VERSION 13.0)
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 "https://github.com/joseluisq/macosx-sdks/releases/download/15.5/MacOSX15.5.sdk.tar.xz" CACHE STRING "Where to fetch the Apple SDK tarball from; empty to never download")
set(SINGE_MACOS_SDK_SHA256 "c15cf0f3f17d714d1aa5a642da8e118db53d79429eb015771ba816aa7c6c1cbd" CACHE STRING "SHA-256 of that tarball; empty to skip the check")
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()
# No --sysroot: zig would then prefix every -L path with it, the superbuild's own lib directory
# included. The SDK is named outright instead: its usr/include (libDER and other non-libc pieces)
# after zig's own libc headers, its frameworks, and its stub libraries. The SDK's CMTag.h uses two
# macOS 14 types without an availability guard, and FFmpeg's configure makes that an error with
# -Werror=partial-availability; the trailing -Wno-unguarded-availability-new wins over it.
singeZigToolchain(${SINGE_MACOS_ARCH}-macos.${SINGE_MACOS_VERSION} "-idirafter ${SINGE_MACOS_SDK}/usr/include -F${SINGE_MACOS_SDK}/System/Library/Frameworks -L${SINGE_MACOS_SDK}/usr/lib -Wno-unguarded-availability-new")
# SDL3 compiles Objective-C, which the same zig wrappers handle, and CMake's Darwin support insists
# on an install_name_tool even though nothing here links dynamically; llvm's stands in.
set(CMAKE_OBJC_COMPILER ${CMAKE_C_COMPILER} CACHE FILEPATH "" FORCE)
set(CMAKE_OBJCXX_COMPILER ${CMAKE_CXX_COMPILER} CACHE FILEPATH "" FORCE)
find_program(SINGE_INSTALL_NAME_TOOL NAMES llvm-install-name-tool llvm-install-name-tool-21 llvm-install-name-tool-20 llvm-install-name-tool-19 llvm-install-name-tool-18 llvm-install-name-tool-17 llvm-install-name-tool-16 llvm-install-name-tool-15)
if(NOT SINGE_INSTALL_NAME_TOOL)
message(FATAL_ERROR "llvm-install-name-tool is needed for the macOS build (apt-get install llvm).")
endif()
set(CMAKE_INSTALL_NAME_TOOL ${SINGE_INSTALL_NAME_TOOL} CACHE FILEPATH "" FORCE)
set(CMAKE_OSX_SYSROOT "" CACHE PATH "Left empty; the wrappers name the SDK themselves" 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 ${SINGE_MACOS_ARCH})