joeylib2/installer/components/sdl3Macos.sh

116 lines
4.2 KiB
Bash

# shellcheck shell=bash
# Part of JoeyLib, Copyright (c) 2026 Scott Duensing, under the MIT License.
# See LICENSE.md for license information.
# SPDX-License-Identifier: MIT
#
# SDL 3 for macOS, static and universal, for the macOS port (make macos):
# the release source the sdl3 component builds for the host (its pin is read
# from sdl3.sh, so there is one), built once for Apple silicon and once for
# Intel with the port's own compiler (make/support/macos/cc.sh, zig and the
# Apple SDK), and the two libraries joined with llvm-lipo into
# toolchains/opt/sdl3Macos.
compDescription="macos: SDL 3 for macOS, static, universal (macos port)"
compGroups="toolchain:macos"
compRequires="host zig macosSdk"
compHostDeps="cmake pkg-config llvm"
sdl3Url="$(compQuery sdl3 compUrl)"
sdl3Sha256="$(compQuery sdl3 compSha256)"
macosCc="$JOEY_REPO/make/support/macos/cc.sh"
# How the compiler is driven is part of what is built, so the wrapper is part
# of the pin.
compPin="$sdl3Sha256:$(compQuery zig compPin):$(compQuery macosSdk compPin):$(sha256sum "$macosCc" | cut -d' ' -f1)"
# The tools the compiler wrapper reads from the environment, as make's
# recipes have them from toolchains/config.mk: zig's and the SDK's.
macosToolsExport() {
local kind
local name
local value
while IFS=$'\t' read -r kind name value; do
if [ "$kind" = var ]; then
export "$name=$value"
fi
done < <(compRun zig compEnv; compRun macosSdk compEnv)
}
compInstall() {
local archive
local src
local arch
local processor
local libs=()
archive="$(fetchFile "$sdl3Url" "$sdl3Sha256")"
src="$(freshBuildDir "$compId")"
extractArchive "$archive" "$src" 1
macosToolsExport
for arch in aarch64 x86_64; do
export MACOS_ARCHS="$arch"
# Apple calls aarch64 arm64.
processor="${arch/aarch64/arm64}"
# Nothing here links dynamically, but CMake's Darwin support wants an
# install_name_tool all the same (LLVM's, beside its lipo); and no
# Xcode sysroot or -arch flags, since the wrapper names the SDK and
# the target itself.
cmake -S "$src" -B "$src/build-$arch" \
-DCMAKE_SYSTEM_NAME=Darwin \
-DCMAKE_SYSTEM_PROCESSOR="$processor" \
-DCMAKE_C_COMPILER="$macosCc" \
-DCMAKE_OBJC_COMPILER="$macosCc" \
-DCMAKE_AR="$(dirname "$MACOS_LIPO")/llvm-ar" \
-DCMAKE_RANLIB="$(dirname "$MACOS_LIPO")/llvm-ranlib" \
-DCMAKE_INSTALL_NAME_TOOL="$(dirname "$MACOS_LIPO")/llvm-install-name-tool" \
-DCMAKE_OSX_SYSROOT= \
-DCMAKE_OSX_ARCHITECTURES= \
-DCMAKE_FIND_ROOT_PATH="$MACOS_SDK" \
-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM=NEVER \
-DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY=ONLY \
-DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE=ONLY \
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=ONLY \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX="$compDir" \
-DCMAKE_INSTALL_LIBDIR=lib \
-DSDL_SHARED=OFF \
-DSDL_STATIC=ON \
-DSDL_TESTS=OFF \
-DSDL_EXAMPLES=OFF
cmake --build "$src/build-$arch" -j"$JOEY_JOBS"
libs+=("$src/build-$arch/libSDL3.a")
done
rm -rf "$compDir"
# The headers, pkg-config file and CMake package are the same for both;
# the library is replaced by the universal one.
cmake --install "$src/build-aarch64"
"$MACOS_LIPO" -create "${libs[@]}" -output "$compDir/lib/libSDL3.a"
dropBuildDir "$compId"
}
compInstalled() {
[ -f "$compDir/lib/libSDL3.a" ] && [ -f "$compDir/lib/pkgconfig/sdl3.pc" ]
}
# Link a program against the library the way make/macos.mk does: it must
# come out universal.
compVerify() {
local tmp
local status=1
macosToolsExport
tmp="$(mktemp -d)"
printf '#include <SDL3/SDL.h>\nint main(void) { return SDL_GetVersion() == 0; }\n' > "$tmp/t.c"
# shellcheck disable=SC2046
if "$macosCc" "$tmp/t.c" -o "$tmp/t" $(PKG_CONFIG_LIBDIR="$compDir/lib/pkgconfig" pkg-config --cflags --static --libs sdl3) \
&& "$MACOS_LIPO" "$tmp/t" -verify_arch arm64 x86_64; then
status=0
fi
rm -rf "$tmp"
return $status
}
compEnv() {
envVar SDL3_MACOS_DIR "$compDir"
}