From ad29033021d98d915bc9396590f8a3535bf752ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carl=20=C3=85stholm?= Date: Tue, 31 Dec 2024 16:43:00 +0100 Subject: [PATCH] zig build: macOS --- README.md | 47 ++++- build.zig | 516 +++++++++++++++++++++++++++++++------------------- build.zig.zon | 2 +- 3 files changed, 370 insertions(+), 195 deletions(-) diff --git a/README.md b/README.md index e121ac90b2cf4..aabafc21fcb08 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,12 @@ SPDX-License-Identifier: MIT # SDL ported to the Zig build system -This is a port of SDL 3.0 to the Zig build system, packaged for the Zig package manager. +This is a port of [SDL](https://libsdl.org/) 3.0 to the Zig build system, packaged for the Zig package manager. ## Usage +Requires Zig `0.12.1`, `0.13.0` or `0.14.0-dev` (master). + ```sh zig fetch --save git+https://github.com/castholm/SDL.git ``` @@ -23,6 +25,47 @@ const sdl_lib = sdl_dep.artifact("SDL3"); const sdl_test_lib = sdl_dep.artifact("SDL3_test"); ``` +Example projects using this SDL package: + +- [castholm/zig-examples/breakout](https://github.com/castholm/zig-examples/tree/master/breakout) +- [castholm/zig-examples/snake](https://github.com/castholm/zig-examples/tree/master/snake) + +## Supported targets + +Target \ Host|Windows|Linux|macOS +-|:-:|:-:|:-: +`x86_64-windows-gnu`|✅|✅|✅ +`x86_64-linux-gnu`|✅|✅|✅ +`x86_64-macos-none`|❌|❌|🉑 +`aarch64-macos-none`|❌|❌|🉑 + +Legend: + +- ✅ Supported +- 🉑 Supported, but requires external SDKs +- ❌ Not supported + +### Windows + +Building for x86-64 Windows from any host system works out of the box. + +### Linux + +Building for x86-64 Linux from any host system works out of the box. The [castholm/SDL_linux_deps](https://github.com/castholm/SDL_linux_deps) package provides supplementary headers and source files requires for compiling for Linux. + +### macOS + +Building for macOS requires Xcode 14.1 or later to be installed on the host system. + +When building for non-native targets (such as for x86-64 from a 64-bit ARM Mac), you must provide a path to the macOS SDK with `--sysroot`, which can be obtained by running `xcrun --sdk macosx --show-sdk-path`: + +```sh +macos_sdk_path=$(xcrun --sdk macosx --show-sdk-path) +zig build -Dtarget=x86_64-macos-none --sysroot "$macos_sdk_path" +``` + +Cross-compiling for macOS from Windows or Linux host systems is not supported because [the Xcode and Apple SDKs Agreement](https://www.apple.com/legal/sla/docs/xcode.pdf) explicitly prohibits using macOS SDK files from non-Apple-branded computers or devices. + ## License This repository is [REUSE-compliant](https://reuse.software/). The effective SPDX license expression for the repository as a whole is: @@ -31,4 +74,6 @@ This repository is [REUSE-compliant](https://reuse.software/). The effective SPD (BSD-3-Clause OR GPL-3.0 OR HIDAPI) AND Apache-2.0 AND BSD-3-Clause AND CC0-1.0 AND HIDAPI AND HPND-sell-variant AND MIT AND SunPro AND Zlib ``` +(This is the same as the upstream SDL repository.) + Copyright notices and license texts have been reproduced in [`LICENSE.txt`](LICENSE.txt), for your convenience. diff --git a/build.zig b/build.zig index b67d35095900c..00b8394c37c08 100644 --- a/build.zig +++ b/build.zig @@ -3,7 +3,10 @@ const std = @import("std"); -const version: std.SemanticVersion = .{ .major = 3, .minor = 1, .patch = 6 }; +pub const version: std.SemanticVersion = .{ .major = 3, .minor = 1, .patch = 6 }; +const formatted_version = std.fmt.comptimePrint("SDL-{}", .{version}); +pub const vendor_info = "https://github.com/castholm/SDL 0.1.0"; +pub const revision = formatted_version ++ " (" ++ vendor_info ++ ")"; pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); @@ -11,16 +14,17 @@ pub fn build(b: *std.Build) void { const preferred_link_mode = b.option( std.builtin.LinkMode, "preferred_link_mode", - "Prefer building SDL as a statically or dynamically linked library", + "Prefer building SDL as a statically or dynamically linked library (default: static)", ) orelse .static; const install_build_config_h = b.option( bool, "install_build_config_h", - "Additionally install 'SDL_build_config.h' when installing SDL (for debugging)", + "Additionally install 'SDL_build_config.h' when installing SDL (default: false)", ) orelse false; var windows = false; var linux = false; + var macos = false; var lazy_linux_deps_dep: ?*std.Build.Dependency = null; var lazy_linux_deps_values: ?LinuxDepsValues = null; switch (target.result.os.tag) { @@ -34,6 +38,9 @@ pub fn build(b: *std.Build) void { lazy_linux_deps_values = LinuxDepsValues.fromBuildZig(build_zig); } }, + .macos => { + macos = true; + }, else => {}, } @@ -47,167 +54,167 @@ pub fn build(b: *std.Build) void { .style = .{ .cmake = b.path("include/build_config/SDL_build_config.h.cmake") }, .include_path = "SDL_build_config.h", }, .{ - .HAVE_GCC_ATOMICS = windows or linux, + .HAVE_GCC_ATOMICS = windows or linux or macos, .HAVE_GCC_SYNC_LOCK_TEST_AND_SET = false, .SDL_DISABLE_ALLOCA = false, - .HAVE_LIBC = windows or linux, - .HAVE_ALLOCA_H = linux, - .HAVE_FLOAT_H = windows or linux, - .HAVE_ICONV_H = linux, - .HAVE_INTTYPES_H = windows or linux, - .HAVE_LIMITS_H = windows or linux, + .HAVE_LIBC = windows or linux or macos, + .HAVE_ALLOCA_H = linux or macos, + .HAVE_FLOAT_H = windows or linux or macos, + .HAVE_ICONV_H = linux or macos, + .HAVE_INTTYPES_H = windows or linux or macos, + .HAVE_LIMITS_H = windows or linux or macos, .HAVE_MALLOC_H = windows or linux, - .HAVE_MATH_H = windows or linux, - .HAVE_MEMORY_H = windows or linux, - .HAVE_SIGNAL_H = windows or linux, - .HAVE_STDARG_H = windows or linux, - .HAVE_STDBOOL_H = windows or linux, - .HAVE_STDDEF_H = windows or linux, - .HAVE_STDINT_H = windows or linux, - .HAVE_STDIO_H = windows or linux, - .HAVE_STDLIB_H = windows or linux, - .HAVE_STRINGS_H = windows or linux, - .HAVE_STRING_H = windows or linux, - .HAVE_SYS_TYPES_H = windows or linux, - .HAVE_WCHAR_H = windows or linux, + .HAVE_MATH_H = windows or linux or macos, + .HAVE_MEMORY_H = windows or linux or macos, + .HAVE_SIGNAL_H = windows or linux or macos, + .HAVE_STDARG_H = windows or linux or macos, + .HAVE_STDBOOL_H = windows or linux or macos, + .HAVE_STDDEF_H = windows or linux or macos, + .HAVE_STDINT_H = windows or linux or macos, + .HAVE_STDIO_H = windows or linux or macos, + .HAVE_STDLIB_H = windows or linux or macos, + .HAVE_STRINGS_H = windows or linux or macos, + .HAVE_STRING_H = windows or linux or macos, + .HAVE_SYS_TYPES_H = windows or linux or macos, + .HAVE_WCHAR_H = windows or linux or macos, .HAVE_PTHREAD_NP_H = false, - .HAVE_DLOPEN = linux, - .HAVE_MALLOC = windows or linux, - .HAVE_CALLOC = windows or linux, - .HAVE_REALLOC = windows or linux, + .HAVE_DLOPEN = linux or macos, + .HAVE_MALLOC = windows or linux or macos, + .HAVE_CALLOC = windows or linux or macos, + .HAVE_REALLOC = windows or linux or macos, .HAVE_FDATASYNC = linux, - .HAVE_FREE = windows or linux, - .HAVE_GETENV = windows or linux, - .HAVE_GETHOSTNAME = linux, - .HAVE_SETENV = linux, - .HAVE_PUTENV = windows or linux, - .HAVE_UNSETENV = linux, - .HAVE_ABS = windows or linux, - .HAVE_BCOPY = linux, - .HAVE_MEMSET = windows or linux, - .HAVE_MEMCPY = windows or linux, - .HAVE_MEMMOVE = windows or linux, - .HAVE_MEMCMP = windows or linux, - .HAVE_WCSLEN = windows or linux, - .HAVE_WCSNLEN = windows or linux, - .HAVE_WCSLCPY = false, - .HAVE_WCSLCAT = false, + .HAVE_FREE = windows or linux or macos, + .HAVE_GETENV = windows or linux or macos, + .HAVE_GETHOSTNAME = linux or macos, + .HAVE_SETENV = linux or macos, + .HAVE_PUTENV = windows or linux or macos, + .HAVE_UNSETENV = linux or macos, + .HAVE_ABS = windows or linux or macos, + .HAVE_BCOPY = linux or macos, + .HAVE_MEMSET = windows or linux or macos, + .HAVE_MEMCPY = windows or linux or macos, + .HAVE_MEMMOVE = windows or linux or macos, + .HAVE_MEMCMP = windows or linux or macos, + .HAVE_WCSLEN = windows or linux or macos, + .HAVE_WCSNLEN = windows or linux or macos, + .HAVE_WCSLCPY = macos, + .HAVE_WCSLCAT = macos, .HAVE__WCSDUP = windows, - .HAVE_WCSDUP = windows or linux, - .HAVE_WCSSTR = windows or linux, - .HAVE_WCSCMP = windows or linux, - .HAVE_WCSNCMP = windows or linux, - .HAVE_WCSTOL = windows or linux, - .HAVE_STRLEN = windows or linux, - .HAVE_STRNLEN = windows or linux, - .HAVE_STRLCPY = false, - .HAVE_STRLCAT = false, - .HAVE_STRPBRK = windows or linux, + .HAVE_WCSDUP = windows or linux or macos, + .HAVE_WCSSTR = windows or linux or macos, + .HAVE_WCSCMP = windows or linux or macos, + .HAVE_WCSNCMP = windows or linux or macos, + .HAVE_WCSTOL = windows or linux or macos, + .HAVE_STRLEN = windows or linux or macos, + .HAVE_STRNLEN = windows or linux or macos, + .HAVE_STRLCPY = macos, + .HAVE_STRLCAT = macos, + .HAVE_STRPBRK = windows or linux or macos, .HAVE__STRREV = windows, .HAVE__STRUPR = false, .HAVE__STRLWR = false, - .HAVE_INDEX = linux, - .HAVE_RINDEX = linux, - .HAVE_STRCHR = windows or linux, - .HAVE_STRRCHR = windows or linux, - .HAVE_STRSTR = windows or linux, - .HAVE_STRNSTR = false, - .HAVE_STRTOK_R = windows or linux, + .HAVE_INDEX = linux or macos, + .HAVE_RINDEX = linux or macos, + .HAVE_STRCHR = windows or linux or macos, + .HAVE_STRRCHR = windows or linux or macos, + .HAVE_STRSTR = windows or linux or macos, + .HAVE_STRNSTR = macos, + .HAVE_STRTOK_R = windows or linux or macos, .HAVE_ITOA = windows, .HAVE__LTOA = windows, .HAVE__UITOA = false, .HAVE__ULTOA = windows, - .HAVE_STRTOL = windows or linux, - .HAVE_STRTOUL = windows or linux, + .HAVE_STRTOL = windows or linux or macos, + .HAVE_STRTOUL = windows or linux or macos, .HAVE__I64TOA = windows, .HAVE__UI64TOA = windows, - .HAVE_STRTOLL = windows or linux, - .HAVE_STRTOULL = windows or linux, - .HAVE_STRTOD = windows or linux, - .HAVE_ATOI = windows or linux, - .HAVE_ATOF = windows or linux, - .HAVE_STRCMP = windows or linux, - .HAVE_STRNCMP = windows or linux, - .HAVE_STRCASESTR = linux, - .HAVE_SSCANF = windows or linux, - .HAVE_VSSCANF = windows or linux, - .HAVE_VSNPRINTF = windows or linux, - .HAVE_ACOS = windows or linux, - .HAVE_ACOSF = windows or linux, - .HAVE_ASIN = windows or linux, - .HAVE_ASINF = windows or linux, - .HAVE_ATAN = windows or linux, - .HAVE_ATANF = windows or linux, - .HAVE_ATAN2 = windows or linux, - .HAVE_ATAN2F = windows or linux, - .HAVE_CEIL = windows or linux, - .HAVE_CEILF = windows or linux, - .HAVE_COPYSIGN = windows or linux, - .HAVE_COPYSIGNF = windows or linux, - .HAVE_COS = windows or linux, - .HAVE_COSF = windows or linux, - .HAVE_EXP = windows or linux, - .HAVE_EXPF = windows or linux, - .HAVE_FABS = windows or linux, - .HAVE_FABSF = windows or linux, - .HAVE_FLOOR = windows or linux, - .HAVE_FLOORF = windows or linux, - .HAVE_FMOD = windows or linux, - .HAVE_FMODF = windows or linux, - .HAVE_ISINF = windows or linux, + .HAVE_STRTOLL = windows or linux or macos, + .HAVE_STRTOULL = windows or linux or macos, + .HAVE_STRTOD = windows or linux or macos, + .HAVE_ATOI = windows or linux or macos, + .HAVE_ATOF = windows or linux or macos, + .HAVE_STRCMP = windows or linux or macos, + .HAVE_STRNCMP = windows or linux or macos, + .HAVE_STRCASESTR = linux or macos, + .HAVE_SSCANF = windows or linux or macos, + .HAVE_VSSCANF = windows or linux or macos, + .HAVE_VSNPRINTF = windows or linux or macos, + .HAVE_ACOS = windows or linux or macos, + .HAVE_ACOSF = windows or linux or macos, + .HAVE_ASIN = windows or linux or macos, + .HAVE_ASINF = windows or linux or macos, + .HAVE_ATAN = windows or linux or macos, + .HAVE_ATANF = windows or linux or macos, + .HAVE_ATAN2 = windows or linux or macos, + .HAVE_ATAN2F = windows or linux or macos, + .HAVE_CEIL = windows or linux or macos, + .HAVE_CEILF = windows or linux or macos, + .HAVE_COPYSIGN = windows or linux or macos, + .HAVE_COPYSIGNF = windows or linux or macos, + .HAVE_COS = windows or linux or macos, + .HAVE_COSF = windows or linux or macos, + .HAVE_EXP = windows or linux or macos, + .HAVE_EXPF = windows or linux or macos, + .HAVE_FABS = windows or linux or macos, + .HAVE_FABSF = windows or linux or macos, + .HAVE_FLOOR = windows or linux or macos, + .HAVE_FLOORF = windows or linux or macos, + .HAVE_FMOD = windows or linux or macos, + .HAVE_FMODF = windows or linux or macos, + .HAVE_ISINF = windows or linux or macos, .HAVE_ISINFF = linux, - .HAVE_ISINF_FLOAT_MACRO = windows or linux, - .HAVE_ISNAN = windows or linux, + .HAVE_ISINF_FLOAT_MACRO = windows or linux or macos, + .HAVE_ISNAN = windows or linux or macos, .HAVE_ISNANF = linux, - .HAVE_ISNAN_FLOAT_MACRO = windows or linux, - .HAVE_LOG = windows or linux, - .HAVE_LOGF = windows or linux, - .HAVE_LOG10 = windows or linux, - .HAVE_LOG10F = windows or linux, - .HAVE_LROUND = windows or linux, - .HAVE_LROUNDF = windows or linux, - .HAVE_MODF = windows or linux, - .HAVE_MODFF = windows or linux, - .HAVE_POW = windows or linux, - .HAVE_POWF = windows or linux, - .HAVE_ROUND = windows or linux, - .HAVE_ROUNDF = windows or linux, - .HAVE_SCALBN = windows or linux, - .HAVE_SCALBNF = windows or linux, - .HAVE_SIN = windows or linux, - .HAVE_SINF = windows or linux, - .HAVE_SQRT = windows or linux, - .HAVE_SQRTF = windows or linux, - .HAVE_TAN = windows or linux, - .HAVE_TANF = windows or linux, - .HAVE_TRUNC = windows or linux, - .HAVE_TRUNCF = windows or linux, + .HAVE_ISNAN_FLOAT_MACRO = windows or linux or macos, + .HAVE_LOG = windows or linux or macos, + .HAVE_LOGF = windows or linux or macos, + .HAVE_LOG10 = windows or linux or macos, + .HAVE_LOG10F = windows or linux or macos, + .HAVE_LROUND = windows or linux or macos, + .HAVE_LROUNDF = windows or linux or macos, + .HAVE_MODF = windows or linux or macos, + .HAVE_MODFF = windows or linux or macos, + .HAVE_POW = windows or linux or macos, + .HAVE_POWF = windows or linux or macos, + .HAVE_ROUND = windows or linux or macos, + .HAVE_ROUNDF = windows or linux or macos, + .HAVE_SCALBN = windows or linux or macos, + .HAVE_SCALBNF = windows or linux or macos, + .HAVE_SIN = windows or linux or macos, + .HAVE_SINF = windows or linux or macos, + .HAVE_SQRT = windows or linux or macos, + .HAVE_SQRTF = windows or linux or macos, + .HAVE_TAN = windows or linux or macos, + .HAVE_TANF = windows or linux or macos, + .HAVE_TRUNC = windows or linux or macos, + .HAVE_TRUNCF = windows or linux or macos, .HAVE_FOPEN64 = windows or linux, - .HAVE_FSEEKO = windows or linux, + .HAVE_FSEEKO = windows or linux or macos, .HAVE_FSEEKO64 = windows or linux, .HAVE_MEMFD_CREATE = linux, .HAVE_POSIX_FALLOCATE = linux, - .HAVE_SIGACTION = linux, - .HAVE_SA_SIGACTION = linux, + .HAVE_SIGACTION = linux or macos, + .HAVE_SA_SIGACTION = linux or macos, .HAVE_ST_MTIM = linux, - .HAVE_SETJMP = linux, - .HAVE_NANOSLEEP = linux, - .HAVE_GMTIME_R = linux, - .HAVE_LOCALTIME_R = linux, - .HAVE_NL_LANGINFO = linux, - .HAVE_SYSCONF = linux, - .HAVE_SYSCTLBYNAME = false, + .HAVE_SETJMP = linux or macos, + .HAVE_NANOSLEEP = linux or macos, + .HAVE_GMTIME_R = linux or macos, + .HAVE_LOCALTIME_R = linux or macos, + .HAVE_NL_LANGINFO = linux or macos, + .HAVE_SYSCONF = linux or macos, + .HAVE_SYSCTLBYNAME = macos, .HAVE_CLOCK_GETTIME = linux, - .HAVE_GETPAGESIZE = linux, + .HAVE_GETPAGESIZE = linux or macos, .HAVE_ICONV = linux, .SDL_USE_LIBICONV = false, - .HAVE_PTHREAD_SETNAME_NP = linux, + .HAVE_PTHREAD_SETNAME_NP = linux or macos, .HAVE_PTHREAD_SET_NAME_NP = false, .HAVE_SEM_TIMEDWAIT = linux, .HAVE_GETAUXVAL = linux, .HAVE_ELF_AUX_INFO = false, - .HAVE_POLL = linux, - .HAVE__EXIT = windows or linux, + .HAVE_POLL = linux or macos, + .HAVE__EXIT = windows or linux or macos, .HAVE_DBUS_DBUS_H = linux, .HAVE_FCITX = linux, .HAVE_IBUS_IBUS_H = linux, @@ -216,7 +223,7 @@ pub fn build(b: *std.Build) void { .HAVE_INOTIFY_INIT1 = linux, .HAVE_INOTIFY = linux, .HAVE_LIBUSB = linux, - .HAVE_O_CLOEXEC = linux, + .HAVE_O_CLOEXEC = linux or macos, .HAVE_LINUX_INPUT_H = linux, .HAVE_LIBUDEV_H = linux, .HAVE_LIBDECOR_H = linux, @@ -253,10 +260,10 @@ pub fn build(b: *std.Build) void { .SDL_AUDIO_DRIVER_ALSA_DYNAMIC = if (lazy_linux_deps_values) |x| b.fmt("\"{s}\"", .{x.alsa_soname}) else "", .SDL_AUDIO_DRIVER_OPENSLES = false, .SDL_AUDIO_DRIVER_AAUDIO = false, - .SDL_AUDIO_DRIVER_COREAUDIO = false, - .SDL_AUDIO_DRIVER_DISK = windows or linux, + .SDL_AUDIO_DRIVER_COREAUDIO = macos, + .SDL_AUDIO_DRIVER_DISK = windows or linux or macos, .SDL_AUDIO_DRIVER_DSOUND = windows, - .SDL_AUDIO_DRIVER_DUMMY = windows or linux, + .SDL_AUDIO_DRIVER_DUMMY = windows or linux or macos, .SDL_AUDIO_DRIVER_EMSCRIPTEN = false, .SDL_AUDIO_DRIVER_HAIKU = false, .SDL_AUDIO_DRIVER_JACK = linux, @@ -286,50 +293,50 @@ pub fn build(b: *std.Build) void { .SDL_JOYSTICK_EMSCRIPTEN = false, .SDL_JOYSTICK_GAMEINPUT = false, .SDL_JOYSTICK_HAIKU = false, - .SDL_JOYSTICK_HIDAPI = windows or linux, - .SDL_JOYSTICK_IOKIT = false, + .SDL_JOYSTICK_HIDAPI = windows or linux or macos, + .SDL_JOYSTICK_IOKIT = macos, .SDL_JOYSTICK_LINUX = linux, - .SDL_JOYSTICK_MFI = false, + .SDL_JOYSTICK_MFI = macos, .SDL_JOYSTICK_N3DS = false, .SDL_JOYSTICK_PS2 = false, .SDL_JOYSTICK_PSP = false, .SDL_JOYSTICK_RAWINPUT = windows, .SDL_JOYSTICK_USBHID = false, - .SDL_JOYSTICK_VIRTUAL = windows or linux, + .SDL_JOYSTICK_VIRTUAL = windows or linux or macos, .SDL_JOYSTICK_VITA = false, .SDL_JOYSTICK_WGI = false, .SDL_JOYSTICK_XINPUT = windows, .SDL_HAPTIC_DUMMY = false, .SDL_HAPTIC_LINUX = linux, - .SDL_HAPTIC_IOKIT = false, + .SDL_HAPTIC_IOKIT = macos, .SDL_HAPTIC_DINPUT = windows, .SDL_HAPTIC_ANDROID = false, .SDL_LIBUSB_DYNAMIC = if (lazy_linux_deps_values) |x| b.fmt("\"{s}\"", .{x.libusb_soname}) else "", .SDL_UDEV_DYNAMIC = if (lazy_linux_deps_values) |x| b.fmt("\"{s}\"", .{x.libudev_soname}) else "", .SDL_PROCESS_DUMMY = false, - .SDL_PROCESS_POSIX = linux, + .SDL_PROCESS_POSIX = linux or macos, .SDL_PROCESS_WINDOWS = windows, .SDL_SENSOR_ANDROID = false, .SDL_SENSOR_COREMOTION = false, .SDL_SENSOR_WINDOWS = windows, - .SDL_SENSOR_DUMMY = linux, + .SDL_SENSOR_DUMMY = linux or macos, .SDL_SENSOR_VITA = false, .SDL_SENSOR_N3DS = false, - .SDL_LOADSO_DLOPEN = linux, + .SDL_LOADSO_DLOPEN = linux or macos, .SDL_LOADSO_DUMMY = false, .SDL_LOADSO_LDG = false, .SDL_LOADSO_WINDOWS = windows, .SDL_THREAD_GENERIC_COND_SUFFIX = windows, .SDL_THREAD_GENERIC_RWLOCK_SUFFIX = windows, - .SDL_THREAD_PTHREAD = linux, - .SDL_THREAD_PTHREAD_RECURSIVE_MUTEX = linux, + .SDL_THREAD_PTHREAD = linux or macos, + .SDL_THREAD_PTHREAD_RECURSIVE_MUTEX = linux or macos, .SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP = false, .SDL_THREAD_WINDOWS = windows, .SDL_THREAD_VITA = false, .SDL_THREAD_PSP = false, .SDL_THREAD_PS2 = false, .SDL_THREAD_N3DS = false, - .SDL_TIME_UNIX = linux, + .SDL_TIME_UNIX = linux or macos, .SDL_TIME_WINDOWS = windows, .SDL_TIME_VITA = false, .SDL_TIME_PSP = false, @@ -337,22 +344,22 @@ pub fn build(b: *std.Build) void { .SDL_TIME_N3DS = false, .SDL_TIMER_HAIKU = false, .SDL_TIMER_DUMMY = false, - .SDL_TIMER_UNIX = linux, + .SDL_TIMER_UNIX = linux or macos, .SDL_TIMER_WINDOWS = windows, .SDL_TIMER_VITA = false, .SDL_TIMER_PSP = false, .SDL_TIMER_PS2 = false, .SDL_TIMER_N3DS = false, .SDL_VIDEO_DRIVER_ANDROID = false, - .SDL_VIDEO_DRIVER_COCOA = false, - .SDL_VIDEO_DRIVER_DUMMY = windows or linux, + .SDL_VIDEO_DRIVER_COCOA = macos, + .SDL_VIDEO_DRIVER_DUMMY = windows or linux or macos, .SDL_VIDEO_DRIVER_EMSCRIPTEN = false, .SDL_VIDEO_DRIVER_HAIKU = false, .SDL_VIDEO_DRIVER_KMSDRM = linux, .SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC = if (lazy_linux_deps_values) |x| b.fmt("\"{s}\"", .{x.drm_soname}) else "", .SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM = if (lazy_linux_deps_values) |x| b.fmt("\"{s}\"", .{x.gbm_soname}) else "", .SDL_VIDEO_DRIVER_N3DS = false, - .SDL_VIDEO_DRIVER_OFFSCREEN = windows or linux, + .SDL_VIDEO_DRIVER_OFFSCREEN = windows or linux or macos, .SDL_VIDEO_DRIVER_PS2 = false, .SDL_VIDEO_DRIVER_PSP = false, .SDL_VIDEO_DRIVER_RISCOS = false, @@ -392,34 +399,34 @@ pub fn build(b: *std.Build) void { .SDL_VIDEO_RENDER_D3D = windows, .SDL_VIDEO_RENDER_D3D11 = windows, .SDL_VIDEO_RENDER_D3D12 = windows, - .SDL_VIDEO_RENDER_GPU = windows or linux, - .SDL_VIDEO_RENDER_METAL = false, - .SDL_VIDEO_RENDER_VULKAN = windows or linux, - .SDL_VIDEO_RENDER_OGL = windows or linux, - .SDL_VIDEO_RENDER_OGL_ES2 = windows or linux, + .SDL_VIDEO_RENDER_GPU = windows or linux or macos, + .SDL_VIDEO_RENDER_METAL = macos, + .SDL_VIDEO_RENDER_VULKAN = windows or linux or macos, + .SDL_VIDEO_RENDER_OGL = windows or linux or macos, + .SDL_VIDEO_RENDER_OGL_ES2 = windows or linux or macos, .SDL_VIDEO_RENDER_PS2 = false, .SDL_VIDEO_RENDER_PSP = false, .SDL_VIDEO_RENDER_VITA_GXM = false, - .SDL_VIDEO_OPENGL = windows or linux, + .SDL_VIDEO_OPENGL = windows or linux or macos, .SDL_VIDEO_OPENGL_ES = linux, - .SDL_VIDEO_OPENGL_ES2 = windows or linux, + .SDL_VIDEO_OPENGL_ES2 = windows or linux or macos, .SDL_VIDEO_OPENGL_BGL = false, - .SDL_VIDEO_OPENGL_CGL = false, + .SDL_VIDEO_OPENGL_CGL = macos, .SDL_VIDEO_OPENGL_GLX = linux, .SDL_VIDEO_OPENGL_WGL = windows, - .SDL_VIDEO_OPENGL_EGL = windows or linux, + .SDL_VIDEO_OPENGL_EGL = windows or linux or macos, .SDL_VIDEO_OPENGL_OSMESA = false, .SDL_VIDEO_OPENGL_OSMESA_DYNAMIC = "", - .SDL_VIDEO_VULKAN = windows or linux, - .SDL_VIDEO_METAL = false, + .SDL_VIDEO_VULKAN = windows or linux or macos, + .SDL_VIDEO_METAL = macos, .SDL_GPU_D3D11 = windows, .SDL_GPU_D3D12 = windows, - .SDL_GPU_VULKAN = windows or linux, - .SDL_GPU_METAL = false, + .SDL_GPU_VULKAN = windows or linux or macos, + .SDL_GPU_METAL = macos, .SDL_POWER_ANDROID = false, .SDL_POWER_LINUX = linux, .SDL_POWER_WINDOWS = windows, - .SDL_POWER_MACOSX = false, + .SDL_POWER_MACOSX = macos, .SDL_POWER_UIKIT = false, .SDL_POWER_HAIKU = false, .SDL_POWER_EMSCRIPTEN = false, @@ -429,7 +436,7 @@ pub fn build(b: *std.Build) void { .SDL_POWER_N3DS = false, .SDL_FILESYSTEM_ANDROID = false, .SDL_FILESYSTEM_HAIKU = false, - .SDL_FILESYSTEM_COCOA = false, + .SDL_FILESYSTEM_COCOA = macos, .SDL_FILESYSTEM_DUMMY = false, .SDL_FILESYSTEM_RISCOS = false, .SDL_FILESYSTEM_UNIX = linux, @@ -439,15 +446,15 @@ pub fn build(b: *std.Build) void { .SDL_FILESYSTEM_PSP = false, .SDL_FILESYSTEM_PS2 = false, .SDL_FILESYSTEM_N3DS = false, - .SDL_STORAGE_GENERIC = windows or linux, - .SDL_STORAGE_STEAM = windows or linux, - .SDL_FSOPS_POSIX = linux, + .SDL_STORAGE_GENERIC = windows or linux or macos, + .SDL_STORAGE_STEAM = windows or linux or macos, + .SDL_FSOPS_POSIX = linux or macos, .SDL_FSOPS_WINDOWS = windows, .SDL_FSOPS_DUMMY = false, - .SDL_CAMERA_DRIVER_DUMMY = windows or linux, + .SDL_CAMERA_DRIVER_DUMMY = windows or linux or macos, .SDL_CAMERA_DRIVER_DISK = false, .SDL_CAMERA_DRIVER_V4L2 = linux, - .SDL_CAMERA_DRIVER_COREMEDIA = false, + .SDL_CAMERA_DRIVER_COREMEDIA = macos, .SDL_CAMERA_DRIVER_ANDROID = false, .SDL_CAMERA_DRIVER_EMSCRIPTEN = false, .SDL_CAMERA_DRIVER_MEDIAFOUNDATION = windows, @@ -458,7 +465,7 @@ pub fn build(b: *std.Build) void { .SDL_MISC_DUMMY = false, .SDL_LOCALE_DUMMY = false, .SDL_ALTIVEC_BLITTERS = false, - .DYNAPI_NEEDS_DLOPEN = linux, + .DYNAPI_NEEDS_DLOPEN = linux or macos, .SDL_USE_IME = linux, .SDL_IPHONE_KEYBOARD = false, .SDL_IPHONE_LAUNCHSCREEN = false, @@ -487,10 +494,20 @@ pub fn build(b: *std.Build) void { .style = .{ .cmake = b.path("include/build_config/SDL_revision.h.cmake") }, .include_path = "SDL3/SDL_revision.h", }, .{ - .SDL_VENDOR_INFO = "https://github.com/castholm/SDL", - .SDL_REVISION = std.fmt.comptimePrint("SDL-{}", .{version}), + .SDL_VENDOR_INFO = vendor_info, + .SDL_REVISION = formatted_version, }); + const common_c_flags = .{ + "-Wall", + "-Wundef", + "-Wfloat-conversion", + "-fno-strict-aliasing", + "-Wshadow", + "-Wno-unused-local-typedefs", + "-Wimplicit-fallthrough", + }; + const sdl_uclibc_lib = b.addStaticLibrary(.{ .name = "SDL_uclib", .target = target, @@ -578,12 +595,35 @@ pub fn build(b: *std.Build) void { } } } + if (macos) { + if (b.sysroot) |sysroot| { + sdl_mod.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{ sysroot, "usr/include" }) }); + sdl_mod.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sysroot, "System/Library/Frameworks" }) }); + sdl_mod.addLibraryPath(.{ .cwd_relative = "/usr/lib" }); // ??? + } else if (!target.query.isNative()) { + std.log.err("'--sysroot' is required when building for non-native macOS targets", .{}); + std.process.exit(1); + } + } - const sdl_c_flags = common_c_flags ++ .{ + var sdl_c_flags = std.BoundedArray([]const u8, 16).fromSlice(&common_c_flags) catch @panic("OOM"); + sdl_c_flags.appendSlice(&.{ "-std=c99", - }; + }) catch @panic("OOM"); + if (linux) { + sdl_c_flags.appendSlice(&.{ + "-pthread", + }) catch @panic("OOM"); + } + if (macos) { + sdl_c_flags.appendSlice(&.{ + "-pthread", + "-fobjc-arc", + }) catch @panic("OOM"); + } + sdl_mod.addCSourceFiles(.{ - .flags = &sdl_c_flags, + .flags = sdl_c_flags.slice(), .files = &.{ "src/SDL.c", "src/SDL_assert.c", @@ -719,7 +759,7 @@ pub fn build(b: *std.Build) void { }); if (windows) { sdl_mod.addCSourceFiles(.{ - .flags = &sdl_c_flags, + .flags = sdl_c_flags.slice(), .files = &.{ "src/audio/dummy/SDL_dummyaudio.c", "src/audio/disk/SDL_diskaudio.c", @@ -822,7 +862,7 @@ pub fn build(b: *std.Build) void { } if (linux) { sdl_mod.addCSourceFiles(.{ - .flags = &sdl_c_flags, + .flags = sdl_c_flags.slice(), .files = &.{ "src/audio/dummy/SDL_dummyaudio.c", "src/audio/disk/SDL_diskaudio.c", @@ -944,12 +984,93 @@ pub fn build(b: *std.Build) void { }); if (lazy_linux_deps_dep) |sysroot_dep| { sdl_mod.addCSourceFiles(.{ - .flags = &sdl_c_flags, + .flags = sdl_c_flags.slice(), .root = sysroot_dep.path("."), .files = lazy_linux_deps_values.?.wayland_c_files, }); } } + if (macos) { + sdl_mod.addCSourceFiles(.{ + .flags = sdl_c_flags.slice(), + .files = &.{ + "src/audio/dummy/SDL_dummyaudio.c", + "src/audio/disk/SDL_diskaudio.c", + "src/camera/dummy/SDL_camera_dummy.c", + "src/loadso/dlopen/SDL_sysloadso.c", + "src/joystick/virtual/SDL_virtualjoystick.c", + "src/video/dummy/SDL_nullevents.c", + "src/video/dummy/SDL_nullframebuffer.c", + "src/video/dummy/SDL_nullvideo.c", + "src/camera/coremedia/SDL_camera_coremedia.m", + "src/misc/macos/SDL_sysurl.m", + "src/audio/coreaudio/SDL_coreaudio.m", + "src/joystick/hidapi/SDL_hidapi_combined.c", + "src/joystick/hidapi/SDL_hidapi_gamecube.c", + "src/joystick/hidapi/SDL_hidapi_luna.c", + "src/joystick/hidapi/SDL_hidapi_ps3.c", + "src/joystick/hidapi/SDL_hidapi_ps4.c", + "src/joystick/hidapi/SDL_hidapi_ps5.c", + "src/joystick/hidapi/SDL_hidapi_rumble.c", + "src/joystick/hidapi/SDL_hidapi_shield.c", + "src/joystick/hidapi/SDL_hidapi_stadia.c", + "src/joystick/hidapi/SDL_hidapi_steam.c", + "src/joystick/hidapi/SDL_hidapi_steam_hori.c", + "src/joystick/hidapi/SDL_hidapi_steamdeck.c", + "src/joystick/hidapi/SDL_hidapi_switch.c", + "src/joystick/hidapi/SDL_hidapi_wii.c", + "src/joystick/hidapi/SDL_hidapi_xbox360.c", + "src/joystick/hidapi/SDL_hidapi_xbox360w.c", + "src/joystick/hidapi/SDL_hidapi_xboxone.c", + "src/joystick/hidapi/SDL_hidapijoystick.c", + "src/joystick/apple/SDL_mfijoystick.m", + "src/joystick/darwin/SDL_iokitjoystick.c", + "src/haptic/darwin/SDL_syshaptic.c", + "src/power/macos/SDL_syspower.c", + "src/locale/macos/SDL_syslocale.m", + "src/time/unix/SDL_systime.c", + "src/timer/unix/SDL_systimer.c", + "src/filesystem/cocoa/SDL_sysfilesystem.m", + "src/storage/generic/SDL_genericstorage.c", + "src/storage/steam/SDL_steamstorage.c", + "src/filesystem/posix/SDL_sysfsops.c", + "src/video/cocoa/SDL_cocoaclipboard.m", + "src/video/cocoa/SDL_cocoaevents.m", + "src/video/cocoa/SDL_cocoakeyboard.m", + "src/video/cocoa/SDL_cocoamessagebox.m", + "src/video/cocoa/SDL_cocoametalview.m", + "src/video/cocoa/SDL_cocoamodes.m", + "src/video/cocoa/SDL_cocoamouse.m", + "src/video/cocoa/SDL_cocoaopengl.m", + "src/video/cocoa/SDL_cocoaopengles.m", + "src/video/cocoa/SDL_cocoapen.m", + "src/video/cocoa/SDL_cocoashape.m", + "src/video/cocoa/SDL_cocoavideo.m", + "src/video/cocoa/SDL_cocoavulkan.m", + "src/video/cocoa/SDL_cocoawindow.m", + "src/render/metal/SDL_render_metal.m", + "src/gpu/metal/SDL_gpu_metal.m", + "src/thread/pthread/SDL_systhread.c", + "src/thread/pthread/SDL_sysmutex.c", + "src/thread/pthread/SDL_syscond.c", + "src/thread/pthread/SDL_sysrwlock.c", + "src/thread/pthread/SDL_systls.c", + "src/thread/pthread/SDL_syssem.c", + "src/dialog/SDL_dialog_utils.c", + "src/dialog/cocoa/SDL_cocoadialog.m", + "src/process/posix/SDL_posixprocess.c", + "src/video/offscreen/SDL_offscreenevents.c", + "src/video/offscreen/SDL_offscreenframebuffer.c", + "src/video/offscreen/SDL_offscreenopengles.c", + "src/video/offscreen/SDL_offscreenvideo.c", + "src/video/offscreen/SDL_offscreenvulkan.c", + "src/video/offscreen/SDL_offscreenwindow.c", + "src/gpu/vulkan/SDL_gpu_vulkan.c", + "src/sensor/dummy/SDL_dummysensor.c", + "src/main/generic/SDL_sysmain_callbacks.c", + }, + }); + } if (sdl_lib.linkage.? == .dynamic) { sdl_lib.setVersionScript(b.path("src/dynapi/SDL_dynapi.sym")); @@ -977,6 +1098,25 @@ pub fn build(b: *std.Build) void { sdl_mod.linkSystemLibrary("m", .{}); sdl_mod.linkSystemLibrary("pthread", .{}); } + if (macos) { + sdl_mod.linkSystemLibrary("m", .{}); + sdl_mod.linkSystemLibrary("pthread", .{}); + sdl_mod.linkFramework("CoreMedia", .{}); + sdl_mod.linkFramework("CoreVideo", .{}); + sdl_mod.linkFramework("Cocoa", .{}); + sdl_mod.linkFramework("IOKit", .{}); + sdl_mod.linkFramework("ForceFeedback", .{}); + sdl_mod.linkFramework("Carbon", .{}); + sdl_mod.linkFramework("CoreAudio", .{}); + sdl_mod.linkFramework("AudioToolbox", .{}); + sdl_mod.linkFramework("AVFoundation", .{}); + sdl_mod.linkFramework("Foundation", .{}); + sdl_mod.linkFramework("CoreHaptics", .{}); + sdl_mod.linkFramework("GameController", .{ .weak = true }); + sdl_mod.linkFramework("Metal", .{ .weak = true }); + sdl_mod.linkFramework("QuartzCore", .{ .weak = true }); + sdl_mod.linkFramework("UniformTypeIdentifiers", .{ .weak = true }); + } sdl_lib.installHeadersDirectory(b.path("include/SDL3"), "SDL3", .{ .exclude_extensions = &.{ @@ -1116,16 +1256,6 @@ const LinuxDepsValues = struct { } }; -const common_c_flags = .{ - "-Wall", - "-Wundef", - "-Wfloat-conversion", - "-fno-strict-aliasing", - "-Wshadow", - "-Wno-unused-local-typedefs", - "-Wimplicit-fallthrough", -}; - fn rootModulePtr(artifact: *std.Build.Step.Compile) *std.Build.Module { const struct_tag = if (@hasField(std.builtin.Type, "Type")) .Struct else .@"struct"; if (@typeInfo(@TypeOf(artifact.root_module)) == struct_tag) { diff --git a/build.zig.zon b/build.zig.zon index a66199fb593f5..cec885a6faf7f 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -3,7 +3,7 @@ .{ .name = "sdl", - .version = "3.1.6", + .version = "0.1.0+SDL-3.1.6", .minimum_zig_version = "0.12.1", .dependencies = .{ .sdl_linux_deps = .{