From ba818509e0dafe0c9b6827afcff829ed1b20a6ce Mon Sep 17 00:00:00 2001 From: Charlie Windolf Date: Sun, 2 Aug 2026 09:44:50 -0400 Subject: [PATCH 1/8] mac: build complaining about extra defines --- src/macos.zig | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/macos.zig b/src/macos.zig index 5b0f11f4a..8cd103467 100644 --- a/src/macos.zig +++ b/src/macos.zig @@ -73,9 +73,9 @@ pub fn build( // C library functions .HAVE_DLOPEN = 1, .HAVE_MALLOC = 1, - .HAVE_CALLOC = 1, - .HAVE_REALLOC = 1, - .HAVE_FREE = 1, + // .HAVE_CALLOC = 1, + // .HAVE_REALLOC = 1, + // .HAVE_FREE = 1, .HAVE_GETENV = 1, .HAVE_SETENV = 1, .HAVE_PUTENV = 1, @@ -169,10 +169,10 @@ pub fn build( .HAVE_SYSCONF = 1, .HAVE_CLOCK_GETTIME = 1, .HAVE_GETPAGESIZE = 1, - .HAVE_MPROTECT = 1, + // .HAVE_MPROTECT = 1, .HAVE_PTHREAD_SETNAME_NP = 1, .HAVE_SEM_TIMEDWAIT = 1, - .HAVE_SYSCTL = 1, + // .HAVE_SYSCTL = 1, .HAVE_SYSCTLBYNAME = 1, .HAVE_O_CLOEXEC = 1, .USE_POSIX_SPAWN = 1, From 1cd7a14e759a0ce40565330ad0662ba87de4aac7 Mon Sep 17 00:00:00 2001 From: Charlie Windolf Date: Sun, 2 Aug 2026 09:45:00 -0400 Subject: [PATCH 2/8] include SDL_main --- src/sdl.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/sdl.h b/src/sdl.h index a897cf086..895b265f0 100644 --- a/src/sdl.h +++ b/src/sdl.h @@ -51,3 +51,6 @@ #include #include #include + +// I want to use callbacks +#include From 781ef70d63296eb31456da61392b36105e061c49 Mon Sep 17 00:00:00 2001 From: Charlie Windolf Date: Mon, 3 Aug 2026 14:57:49 -0400 Subject: [PATCH 3/8] sdl: callbacks working on mac and at least compiling for ios simulator --- build.zig | 56 +++++++++- src/ios.zig | 271 +++++++++++++++++++++++++++++++++++++++++++++++++ src/macos.zig | 4 +- src/sdl.h | 3 - src/sdl.zon | 30 ++++++ src/sdl_main.c | 3 + 6 files changed, 360 insertions(+), 7 deletions(-) create mode 100644 src/ios.zig create mode 100644 src/sdl_main.c diff --git a/build.zig b/build.zig index 7c4c5541f..7792fef2d 100644 --- a/build.zig +++ b/build.zig @@ -2,6 +2,7 @@ const std = @import("std"); const linux = @import("src/linux.zig"); const windows = @import("src/windows.zig"); const macos = @import("src/macos.zig"); +const ios = @import("src/ios.zig"); const build_zon = @import("build.zig.zon"); const Translator = @import("translate_c").Translator; @@ -36,6 +37,11 @@ pub fn build(b: *std.Build) !void { , ) orelse .static; + const main_callbacks = b.option(bool, "main_callbacks", + \\build SDL3_main to enter the app through SDL_AppInit/Iterate/Event/Quit rather + \\than a classic SDL_main, defaults to false + ) orelse false; + // Create the library const lib = b.addLibrary(.{ .name = "SDL3", @@ -45,8 +51,7 @@ pub fn build(b: *std.Build) !void { .link_libc = true, }), .linkage = linkage, - .version = comptime std.SemanticVersion.parse(build_zon.dependencies.sdl.so_version) - catch unreachable, + .version = comptime std.SemanticVersion.parse(build_zon.dependencies.sdl.so_version) catch unreachable, }); switch (linkage) { .dynamic => { @@ -75,6 +80,23 @@ pub fn build(b: *std.Build) !void { .flags = flags, }); + const main_lib = b.addLibrary(.{ + .name = "SDL3_main", + .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + .link_libc = true, + }), + .linkage = .static, + }); + main_lib.root_module.addIncludePath(upstream.path("include")); + if (main_callbacks) { + main_lib.root_module.addCMacro("SDL_MAIN_USE_CALLBACKS", "1"); + } + main_lib.root_module.addCSourceFile(.{ .file = b.path("src/sdl_main.c") }); + addAppleSdkPathsToModule(b, target.result, main_lib.root_module); + b.installArtifact(main_lib); + if (default_target_config) { const build_config_h = b.addConfigHeader(.{ .style = .{ .cmake = upstream.path("include/build_config/SDL_build_config.h.cmake") }, @@ -104,6 +126,7 @@ pub fn build(b: *std.Build) !void { .linux => linux.build(b, target.result, lib, build_config_h), .windows => windows.build(b, target.result, lib, build_config_h), .macos => macos.build(b, target.result, lib, build_config_h), + .ios => ios.build(b, target.result, lib, build_config_h), else => @panic("target has no default config"), } } @@ -125,11 +148,16 @@ pub fn build(b: *std.Build) !void { else => optimize, }, }); + translator.defineCMacro("USING_GENERATED_CONFIG_H", "1"); translator.addIncludePath(upstream.path("include")); translator.addIncludePath(upstream.path("src/video/khronos")); translator.mod.linkLibrary(lib); try b.modules.putNoClobber(b.graph.arena, "sdl3", translator.mod); + addAppleSdkPathsToTranslator(b, target.result, translator); + if (target.result.os.tag == .ios) { + translator.defineCMacro("TARGET_OS_IPHONE", "1"); + } // Add the example const example = b.addExecutable(.{ @@ -149,3 +177,27 @@ pub fn build(b: *std.Build) !void { const run_step = b.step("run-example", "Run the example app"); run_step.dependOn(&run_example.step); } + +pub fn addAppleSdkPathsToTranslator(b: *std.Build, target: std.Target, translator: Translator) void { + if (!target.os.tag.isDarwin()) return; + + const sdk = std.zig.system.darwin.getSdk(b.allocator, b.graph.io, &target) orelse + @panic("Couldn't detect Apple SDK"); + + translator.addIncludePath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "usr/include" }) }); + translator.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "System/Library/Frameworks" }) }); + translator.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "System/Library/SubFrameworks" }) }); + translator.addIncludePath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "usr/lib" }) }); +} + +pub fn addAppleSdkPathsToModule(b: *std.Build, target: std.Target, module: *std.Build.Module) void { + if (!target.os.tag.isDarwin()) return; + + const sdk = std.zig.system.darwin.getSdk(b.allocator, b.graph.io, &target) orelse + @panic("Couldn't detect Apple SDK"); + + module.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "usr/include" }) }); + module.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "System/Library/Frameworks" }) }); + module.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "System/Library/SubFrameworks" }) }); + module.addLibraryPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "usr/lib" }) }); +} diff --git a/src/ios.zig b/src/ios.zig new file mode 100644 index 000000000..228ce629e --- /dev/null +++ b/src/ios.zig @@ -0,0 +1,271 @@ +const std = @import("std"); +const build_zon = @import("../build.zig.zon"); +const sources = @import("sdl.zon"); +const root = @import("../build.zig"); +const Subsystems = root.Subsystems; +const AllDrivers = root.Drivers; + +pub fn build( + b: *std.Build, + target: std.Target, + lib: *std.Build.Step.Compile, + build_config_h: *std.Build.Step.ConfigHeader, +) void { + const upstream = b.dependency("sdl", .{}); + // _ = target; + lib.root_module.addIncludePath(b.dependency("egl", .{}).path("api")); + lib.root_module.addIncludePath(b.dependency("opengl", .{}).path("api")); + + root.addAppleSdkPathsToModule(b, target, lib.root_module); + + // Link with the platform specific system frameworks + lib.root_module.linkFramework("UIKit", .{}); + lib.root_module.linkFramework("UIUtilities", .{}); + lib.root_module.linkFramework("CoreMotion", .{}); + lib.root_module.linkFramework("CoreVideo", .{}); + lib.root_module.linkFramework("CoreAudio", .{}); + lib.root_module.linkFramework("CoreHaptics", .{}); + lib.root_module.linkFramework("CoreFoundation", .{}); + lib.root_module.linkFramework("CoreMedia", .{}); + lib.root_module.linkFramework("CoreGraphics", .{}); + lib.root_module.linkFramework("Metal", .{}); + lib.root_module.linkFramework("QuartzCore", .{}); + lib.root_module.linkFramework("AudioToolbox", .{}); + lib.root_module.linkFramework("AVFoundation", .{}); + lib.root_module.linkFramework("Foundation", .{}); + lib.root_module.linkFramework("GameController", .{}); + lib.root_module.linkFramework("CoreBluetooth", .{}); + lib.root_module.linkFramework("UniformTypeIdentifiers", .{}); + lib.root_module.linkFramework("OpenGLES", .{}); + // lib.root_module.linkSystemLibrary("iconv", .{}); + + // Add the platform specific sources + const objc_flags = root.flags.* ++ [_][]const u8{"-fobjc-arc"}; + lib.root_module.addCSourceFiles(.{ + .files = &(sources.uikit ++ sources.darwin ++ sources.ios ++ sources.unix ++ sources.pthread), + .root = upstream.path("src"), + .flags = &objc_flags, + }); + + // Set the platform specific build config + build_config_h.addValues(.{ + .HAVE_GCC_ATOMICS = 1, + + .HAVE_ALLOCA_H = 1, + .HAVE_FLOAT_H = 1, + .HAVE_INTTYPES_H = 1, + .HAVE_LIMITS_H = 1, + .HAVE_MATH_H = 1, + .HAVE_SIGNAL_H = 1, + .HAVE_STDARG_H = 1, + .HAVE_STDDEF_H = 1, + .HAVE_STDINT_H = 1, + .HAVE_STDIO_H = 1, + .HAVE_STDLIB_H = 1, + .HAVE_STRING_H = 1, + .HAVE_SYS_TYPES_H = 1, + .HAVE_WCHAR_H = 1, + + // C library functions + .HAVE_LIBC = 1, + .HAVE_DLOPEN = 1, + .HAVE_MALLOC = 1, + .HAVE_GETENV = 1, + .HAVE_GETHOSTNAME = 1, + .HAVE_PUTENV = 1, + .HAVE_SETENV = 1, + .HAVE_UNSETENV = 1, + .HAVE_ABS = 1, + .HAVE_BCOPY = 1, + .HAVE_MEMSET = 1, + .HAVE_MEMCPY = 1, + .HAVE_MEMMOVE = 1, + .HAVE_MEMCMP = 1, + .HAVE_STRLEN = 1, + .HAVE_STRLCPY = 1, + .HAVE_STRLCAT = 1, + .HAVE_STRCHR = 1, + .HAVE_STRRCHR = 1, + .HAVE_STRSTR = 1, + .HAVE_STRTOK_R = 1, + .HAVE_STRTOL = 1, + .HAVE_STRTOUL = 1, + .HAVE_STRTOLL = 1, + .HAVE_STRTOULL = 1, + .HAVE_STRTOD = 1, + .HAVE_ATOI = 1, + .HAVE_ATOF = 1, + .HAVE_STRCMP = 1, + .HAVE_STRNCMP = 1, + .HAVE_STRPBRK = 1, + .HAVE_VSSCANF = 1, + .HAVE_VSNPRINTF = 1, + .HAVE_ACOS = 1, + .HAVE_ACOSF = 1, + .HAVE_ASIN = 1, + .HAVE_ASINF = 1, + .HAVE_ATAN = 1, + .HAVE_ATANF = 1, + .HAVE_ATAN2 = 1, + .HAVE_ATAN2F = 1, + .HAVE_CEIL = 1, + .HAVE_CEILF = 1, + .HAVE_COPYSIGN = 1, + .HAVE_COPYSIGNF = 1, + .HAVE_COS = 1, + .HAVE_COSF = 1, + .HAVE_EXP = 1, + .HAVE_EXPF = 1, + .HAVE_FABS = 1, + .HAVE_FABSF = 1, + .HAVE_FLOOR = 1, + .HAVE_FLOORF = 1, + .HAVE_FMOD = 1, + .HAVE_FMODF = 1, + .HAVE_ISINF = 1, + .HAVE_ISINF_FLOAT_MACRO = 1, + .HAVE_ISNAN = 1, + .HAVE_ISNAN_FLOAT_MACRO = 1, + .HAVE_LOG = 1, + .HAVE_LOGF = 1, + .HAVE_LOG10 = 1, + .HAVE_LOG10F = 1, + .HAVE_LROUND = 1, + .HAVE_LROUNDF = 1, + .HAVE_MODF = 1, + .HAVE_MODFF = 1, + .HAVE_POW = 1, + .HAVE_POWF = 1, + .HAVE_ROUND = 1, + .HAVE_ROUNDF = 1, + .HAVE_SCALBN = 1, + .HAVE_SCALBNF = 1, + .HAVE_SIN = 1, + .HAVE_SINF = 1, + .HAVE_SQRT = 1, + .HAVE_SQRTF = 1, + .HAVE_TAN = 1, + .HAVE_TANF = 1, + .HAVE_TRUNC = 1, + .HAVE_TRUNCF = 1, + .HAVE_SIGACTION = 1, + .HAVE_SETJMP = 1, + .HAVE_NANOSLEEP = 1, + .HAVE_GMTIME_R = 1, + .HAVE_LOCALTIME_R = 1, + .HAVE_NL_LANGINFO = 1, + .HAVE_SYSCONF = 1, + .HAVE_SYSCTLBYNAME = 1, + .HAVE_O_CLOEXEC = 1, + + // enable iPhone version of Core Audio driver + .SDL_AUDIO_DRIVER_COREAUDIO = 1, + // Enable the dummy audio driver (src/audio/dummy/\*.c) + .SDL_AUDIO_DRIVER_DUMMY = 1, + + // Enable the stub haptic driver (src/haptic/dummy/\*.c) + .SDL_HAPTIC_DUMMY = 1, + + // Enable joystick support + // Only enable HIDAPI support if you want to support Steam Controllers on iOS and tvOS + .SDL_JOYSTICK_MFI = 1, + .SDL_JOYSTICK_VIRTUAL = 1, + + // Enable various process implementations + .SDL_PROCESS_DUMMY = 1, + + // Enable the CoreMotion sensor driver + .SDL_SENSOR_COREMOTION = 1, + + // Enable Unix style SO loading + .SDL_LOADSO_DLOPEN = 1, + + // Enable various threading systems + .SDL_THREAD_PTHREAD = 1, + .SDL_THREAD_PTHREAD_RECURSIVE_MUTEX = 1, + + // Enable various RTC system + .SDL_TIME_UNIX = 1, + + // Enable various timer systems + .SDL_TIMER_UNIX = 1, + + // Supported video drivers + .SDL_VIDEO_DRIVER_UIKIT = 1, + .SDL_VIDEO_DRIVER_DUMMY = 1, + + // .SDL_PLATFORM_SUPPORTS_METAL = 1, + .SDL_VIDEO_OPENGL_ES2 = 1, + .SDL_VIDEO_OPENGL_ES = 1, + .SDL_VIDEO_RENDER_OGL_ES2 = 1, + + .SDL_VIDEO_METAL = 1, + .SDL_VIDEO_VULKAN = 1, + .SDL_GPU_METAL = 1, + .SDL_GPU_VULKAN = 1, + .SDL_VIDEO_RENDER_METAL = 1, + .SDL_VIDEO_RENDER_GPU = 1, + + // Enable system power support + .SDL_POWER_UIKIT = 1, + + // enable iPhone keyboard support + .SDL_IPHONE_KEYBOARD = 1, + + // enable iOS extended launch screen + .SDL_IPHONE_LAUNCHSCREEN = 1, + + // enable filesystem support + .SDL_FILESYSTEM_COCOA = 1, + .SDL_FSOPS_POSIX = 1, + + .SDL_CAMERA_DRIVER_DUMMY = 1, + + // Enable dialog subsystem + .SDL_DIALOG_DUMMY = 1, + + // Enable tray subsystem + .SDL_TRAY_DUMMY = 1, + + // Disable ARM SVE2 intrinsics until we confirm they're available on all Apple mobile and TV hardware + // .SDL_DISABLE_SVE2 = 1, + + // Enable steam storage + .SDL_STORAGE_STEAM = 1, + + // Unused + .SDL_AUDIO_DRIVER_ALSA_DYNAMIC = "", + .SDL_AUDIO_DRIVER_JACK_DYNAMIC = "", + .SDL_AUDIO_DRIVER_PIPEWIRE_DYNAMIC = "", + .SDL_AUDIO_DRIVER_PULSEAUDIO_DYNAMIC = "", + .SDL_AUDIO_DRIVER_SNDIO_DYNAMIC = "", + .SDL_LIBUSB_DYNAMIC = "", + .SDL_UDEV_DYNAMIC = "", + .SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC = "", + .SDL_VIDEO_DRIVER_KMSDRM_DYNAMIC_GBM = "", + .SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC = "", + .SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_CURSOR = "", + .SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_EGL = "", + .SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_LIBDECOR = "", + .SDL_VIDEO_DRIVER_WAYLAND_DYNAMIC_XKBCOMMON = "", + .SDL_VIDEO_DRIVER_X11_DYNAMIC = "", + .SDL_VIDEO_DRIVER_X11_DYNAMIC_XCURSOR = "", + .SDL_VIDEO_DRIVER_X11_DYNAMIC_XEXT = "", + .SDL_VIDEO_DRIVER_X11_DYNAMIC_XFIXES = "", + .SDL_VIDEO_DRIVER_X11_DYNAMIC_XINPUT2 = "", + .SDL_VIDEO_DRIVER_X11_DYNAMIC_XRANDR = "", + .SDL_VIDEO_DRIVER_X11_DYNAMIC_XSS = "", + .SDL_CAMERA_DRIVER_PIPEWIRE_DYNAMIC = "", + .SDL_LIBDECOR_VERSION_MAJOR = "", + .SDL_LIBDECOR_VERSION_MINOR = "", + .SDL_LIBDECOR_VERSION_PATCH = "", + .SDL_FRIBIDI_DYNAMIC = "", + .SDL_LIBTHAI_DYNAMIC = "", + .SDL_VIDEO_DRIVER_X11_DYNAMIC_XTEST = "", + .SDL_VIDEO_DRIVER_X11_XINPUT2_SUPPORTS_GESTURE = "", + .SDL_XKBCOMMON_VERSION_MAJOR = "", + .SDL_XKBCOMMON_VERSION_MINOR = "", + .SDL_XKBCOMMON_VERSION_PATCH = "", + .SDL_EMSCRIPTEN_PERSISTENT_PATH_STRING = "", + }); +} diff --git a/src/macos.zig b/src/macos.zig index 8cd103467..217a47923 100644 --- a/src/macos.zig +++ b/src/macos.zig @@ -11,14 +11,14 @@ pub fn build( lib: *std.Build.Step.Compile, build_config_h: *std.Build.Step.ConfigHeader, ) void { - _ = target; - const upstream = b.dependency("sdl", .{}); // Add the platform specific dependency include paths lib.root_module.addIncludePath(b.dependency("egl", .{}).path("api")); lib.root_module.addIncludePath(b.dependency("opengl", .{}).path("api")); + root.addAppleSdkPathsToModule(b, target, lib.root_module); + // Link with the platform specific system frameworks lib.root_module.linkFramework("Cocoa", .{}); lib.root_module.linkFramework("IOKit", .{}); diff --git a/src/sdl.h b/src/sdl.h index 895b265f0..a897cf086 100644 --- a/src/sdl.h +++ b/src/sdl.h @@ -51,6 +51,3 @@ #include #include #include - -// I want to use callbacks -#include diff --git a/src/sdl.zon b/src/sdl.zon index 390507fd3..e61fdbb38 100644 --- a/src/sdl.zon +++ b/src/sdl.zon @@ -476,6 +476,33 @@ "joystick/darwin/SDL_iokitjoystick.c", "joystick/apple/SDL_mfijoystick.m", }, + .uikit = .{ + "audio/coreaudio/SDL_coreaudio.m", + "camera/coremedia/SDL_camera_coremedia.m", + "render/metal/SDL_render_metal.m", + "gpu/metal/SDL_gpu_metal.m", + "joystick/apple/SDL_mfijoystick.m", + "filesystem/cocoa/SDL_sysfilesystem.m", + "locale/macos/SDL_syslocale.m", + "misc/ios/SDL_sysurl.m", + "video/uikit/SDL_uikitappdelegate.m", + "video/uikit/SDL_uikitclipboard.m", + "video/uikit/SDL_uikitevents.m", + "video/uikit/SDL_uikitmessagebox.m", + "video/uikit/SDL_uikitmetalview.m", + "video/uikit/SDL_uikitmodes.m", + "video/uikit/SDL_uikitopengles.m", + "video/uikit/SDL_uikitopenglview.m", + "video/uikit/SDL_uikitpen.m", + "video/uikit/SDL_uikitview.m", + "video/uikit/SDL_uikitviewcontroller.m", + "video/uikit/SDL_uikitvideo.m", + "video/uikit/SDL_uikitvulkan.m", + "video/uikit/SDL_uikitwindow.m", + "power/uikit/SDL_syspower.m", + "sensor/coremotion/SDL_coremotionsensor.m", + "main/ios/SDL_sysmain_callbacks.m", + }, .emscripten = .{ "filesystem/emscripten/SDL_sysfilesystem.c", "joystick/emscripten/SDL_sysjoystick.c", @@ -501,6 +528,9 @@ .mac = .{ "hidapi/mac/hid.c", }, + .ios = .{ + "hidapi/ios/hid.m", + }, .n3ds = .{ "io/n3ds/SDL_iostreamromfs.c", "joystick/n3ds/SDL_sysjoystick.c", diff --git a/src/sdl_main.c b/src/sdl_main.c new file mode 100644 index 000000000..35e5e659d --- /dev/null +++ b/src/sdl_main.c @@ -0,0 +1,3 @@ +#ifdef SDL_MAIN_USE_CALLBACKS +#include +#endif From d7bbda82a2b55f5d7b117d2a10df706edcca42e9 Mon Sep 17 00:00:00 2001 From: Charlie Windolf Date: Mon, 3 Aug 2026 15:55:09 -0400 Subject: [PATCH 4/8] get ios simulator running --- build.zig | 4 ++++ src/ios.zig | 1 - 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/build.zig b/build.zig index 7792fef2d..258792906 100644 --- a/build.zig +++ b/build.zig @@ -42,6 +42,8 @@ pub fn build(b: *std.Build) !void { \\than a classic SDL_main, defaults to false ) orelse false; + const can_san = target.result.os.tag != .ios; + // Create the library const lib = b.addLibrary(.{ .name = "SDL3", @@ -49,6 +51,7 @@ pub fn build(b: *std.Build) !void { .target = target, .optimize = optimize, .link_libc = true, + .sanitize_c = if (can_san) null else .off, }), .linkage = linkage, .version = comptime std.SemanticVersion.parse(build_zon.dependencies.sdl.so_version) catch unreachable, @@ -86,6 +89,7 @@ pub fn build(b: *std.Build) !void { .target = target, .optimize = optimize, .link_libc = true, + .sanitize_c = if (can_san) null else .off, }), .linkage = .static, }); diff --git a/src/ios.zig b/src/ios.zig index 228ce629e..272d0a1a2 100644 --- a/src/ios.zig +++ b/src/ios.zig @@ -20,7 +20,6 @@ pub fn build( // Link with the platform specific system frameworks lib.root_module.linkFramework("UIKit", .{}); - lib.root_module.linkFramework("UIUtilities", .{}); lib.root_module.linkFramework("CoreMotion", .{}); lib.root_module.linkFramework("CoreVideo", .{}); lib.root_module.linkFramework("CoreAudio", .{}); From d9969ab222118edee9beeef8f84bec89d7bd8236 Mon Sep 17 00:00:00 2001 From: Charlie Windolf Date: Sun, 25 Oct 2026 11:10:37 -0400 Subject: [PATCH 5/8] refactor to better support -Dinclude_path etc rather than calling xcrun always, and also unify SDK logic in one place --- build.zig | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/build.zig b/build.zig index 1426000ee..f76d21d7a 100644 --- a/build.zig +++ b/build.zig @@ -127,7 +127,7 @@ pub fn build(b: *std.Build) !void { main_lib.root_module.addCMacro("SDL_MAIN_USE_CALLBACKS", "1"); } main_lib.root_module.addCSourceFile(.{ .file = b.path("src/sdl_main.c") }); - addAppleSdkPathsToModule(b, target.result, main_lib.root_module); + addSystemPathsToModule(b, main_lib.root_module, system_paths); b.installArtifact(main_lib); if (default_target_config) { @@ -179,7 +179,7 @@ pub fn build(b: *std.Build) !void { translate_c.defineCMacro("USING_GENERATED_CONFIG_H", "1"); translate_c.addIncludePath(upstream.path("include")); translate_c.addIncludePath(upstream.path("src/video/khronos")); - addAppleSdkPathsToTranslateC(b, target.result, translate_c); + addSystemPathsToTranslateC(b, translate_c, system_paths); const module = translate_c.addModule("sdl3"); module.linkLibrary(lib); @@ -249,15 +249,21 @@ pub fn withAppleSdkFallback(b: *std.Build, target: std.Target, paths: SystemPath }; } -pub fn addAppleSdkPathsToTranslateC(b: *std.Build, target: std.Target, translate_c: *std.Build.Step.TranslateC) void { - if (!target.os.tag.isDarwin()) return; - - const sdk = std.zig.system.darwin.getSdk(b.allocator, b.graph.io, &target) orelse - @panic("Couldn't detect Apple SDK"); +fn addSystemPathsToModule(b: *std.Build, module: *std.Build.Module, paths: SystemPaths) void { + if (paths.include) |p| module.addSystemIncludePath(p); + if (paths.framework) |p| { + module.addSystemFrameworkPath(p); + module.addSystemFrameworkPath(p.dirname().path(b, "SubFrameworks")); + } + if (paths.library) |p| module.addLibraryPath(p); +} - translate_c.addIncludePath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "usr/include" }) }); - translate_c.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "System/Library/Frameworks" }) }); - translate_c.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "System/Library/SubFrameworks" }) }); +fn addSystemPathsToTranslateC(b: *std.Build, translate_c: *std.Build.Step.TranslateC, paths: SystemPaths) void { + if (paths.include) |p| translate_c.addSystemIncludePath(p); + if (paths.framework) |p| { + translate_c.addSystemFrameworkPath(p); + translate_c.addSystemFrameworkPath(p.dirname().path(b, "SubFrameworks")); + } } pub fn addAppleSdkPathsToModule(b: *std.Build, target: std.Target, module: *std.Build.Module) void { From f458a5705f8afe024ba3aa6af0afb58f87cd1262 Mon Sep 17 00:00:00 2001 From: Charlie Windolf Date: Sat, 7 Nov 2026 10:33:06 -0500 Subject: [PATCH 6/8] ios ubsan problem: track down and disable ubsan as narrowly as I can There seem to be two issues here. First, there is something up with an Apple function dispatch_once which has a fishy __builtin_assume that I don't fully follow and neither does UBSan. It seems like that issue exists in apple code because they use an older clang which wouldn't sanitize this? Second, zig's UBSan seems not to handle the case of a bad __builtin_assume, so it crashed rather than making an error message. --- src/ios.zig | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ios.zig b/src/ios.zig index 4144047d5..cdc8176ff 100644 --- a/src/ios.zig +++ b/src/ios.zig @@ -44,7 +44,10 @@ pub fn build( lib.root_module.linkFramework("CoreHaptics", .{ .weak = true }); // Add the platform specific sources - const objc_flags = root.flags.* ++ [_][]const u8{"-fobjc-arc"}; + const objc_flags = root.flags.* ++ [_][]const u8{ + "-fobjc-arc", + "-fno-sanitize=builtin", + }; lib.root_module.addCSourceFiles(.{ .files = &(sources.uikit ++ sources.apple ++ sources.pthread), .root = upstream.path("src"), From b7f27a817301ae00702511bf1b2826dfdb72c533 Mon Sep 17 00:00:00 2001 From: Charlie Windolf Date: Sat, 7 Nov 2026 11:17:34 -0500 Subject: [PATCH 7/8] callbacks: only include the SDL3_main library when using callbacks rather than otherwise including an empty library --- build.zig | 30 +++++++++++++++--------------- src/sdl_main.c | 2 -- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/build.zig b/build.zig index 8be2b7291..5ccb47e3b 100644 --- a/build.zig +++ b/build.zig @@ -105,23 +105,23 @@ pub fn build(b: *std.Build) !void { .flags = flags, }); - const main_lib = b.addLibrary(.{ - .name = "SDL3_main", - .root_module = b.createModule(.{ - .target = target, - .optimize = optimize, - .link_libc = true, - }), - .linkage = .static, - }); - main_lib.root_module.addIncludePath(upstream.path("include")); if (main_callbacks) { - main_lib.root_module.addCMacro("SDL_MAIN_USE_CALLBACKS", "1"); + const sdl_main_lib = b.addLibrary(.{ + .name = "SDL3_main", + .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + .link_libc = true, + }), + .linkage = .static, + }); + sdl_main_lib.root_module.addIncludePath(upstream.path("include")); + sdl_main_lib.root_module.addCMacro("SDL_MAIN_USE_CALLBACKS", "1"); + sdl_main_lib.root_module.addCSourceFile(.{ .file = b.path("src/sdl_main.c") }); + if (system_paths.include) |p| sdl_main_lib.root_module.addSystemIncludePath(p); + if (system_paths.framework) |p| sdl_main_lib.root_module.addSystemFrameworkPath(p); + b.installArtifact(sdl_main_lib); } - main_lib.root_module.addCSourceFile(.{ .file = b.path("src/sdl_main.c") }); - if (system_paths.include) |p| main_lib.root_module.addSystemIncludePath(p); - if (system_paths.framework) |p| main_lib.root_module.addSystemFrameworkPath(p); - b.installArtifact(main_lib); if (default_target_config) { const build_config_h = b.addConfigHeader(.{ diff --git a/src/sdl_main.c b/src/sdl_main.c index 35e5e659d..6e1dd53c6 100644 --- a/src/sdl_main.c +++ b/src/sdl_main.c @@ -1,3 +1 @@ -#ifdef SDL_MAIN_USE_CALLBACKS #include -#endif From 41a3149af6f0db1aa2dfb24c9e1702f6b0ad4ec2 Mon Sep 17 00:00:00 2001 From: Charlie Windolf Date: Sat, 7 Nov 2026 11:41:05 -0500 Subject: [PATCH 8/8] callbacks: port the example to callbacks and add run-callbacks-example --- build.zig | 26 ++++++++++++++ src/callbacks_example.zig | 71 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 src/callbacks_example.zig diff --git a/build.zig b/build.zig index 5ccb47e3b..4a2aea70e 100644 --- a/build.zig +++ b/build.zig @@ -105,6 +105,7 @@ pub fn build(b: *std.Build) !void { .flags = flags, }); + var maybe_sdl_main_lib: ?*std.Build.Step.Compile = null; if (main_callbacks) { const sdl_main_lib = b.addLibrary(.{ .name = "SDL3_main", @@ -121,6 +122,7 @@ pub fn build(b: *std.Build) !void { if (system_paths.include) |p| sdl_main_lib.root_module.addSystemIncludePath(p); if (system_paths.framework) |p| sdl_main_lib.root_module.addSystemFrameworkPath(p); b.installArtifact(sdl_main_lib); + maybe_sdl_main_lib = sdl_main_lib; } if (default_target_config) { @@ -197,6 +199,30 @@ pub fn build(b: *std.Build) !void { const run_example = b.addRunArtifact(example); const run_step = b.step("run-example", "Run the example app"); run_step.dependOn(&run_example.step); + + // Add a callbacks example. Note, it will fail if no -Dmain_callbacks. + const build_callbacks_example_step = b.step("callbacks-example", "Build the callbacks example app (requires -Dmain_callbacks)"); + const run_callbacks_step = b.step("run-callbacks-example", "Run the callbacks example app (requires -Dmain_callbacks)"); + if (maybe_sdl_main_lib) |sdl_main_lib| { + const callbacks_example = b.addExecutable(.{ + .name = "callbacks_example", + .root_module = b.createModule(.{ + .root_source_file = b.path("src/callbacks_example.zig"), + .target = target, + .optimize = optimize, + }), + }); + callbacks_example.root_module.addImport("sdl3", module); + callbacks_example.root_module.linkLibrary(sdl_main_lib); + build_callbacks_example_step.dependOn(&callbacks_example.step); + + const run_callbacks_example = b.addRunArtifact(callbacks_example); + run_callbacks_step.dependOn(&run_callbacks_example.step); + } else { + const fail = b.addFail("The callbacks example requires -Dmain_callbacks"); + build_callbacks_example_step.dependOn(&fail.step); + run_callbacks_step.dependOn(&fail.step); + } } /// Fetches a lazy dependency, unless provided via `-fsys`. diff --git a/src/callbacks_example.zig b/src/callbacks_example.zig new file mode 100644 index 000000000..fda69d2de --- /dev/null +++ b/src/callbacks_example.zig @@ -0,0 +1,71 @@ +const std = @import("std"); +const sdl = @import("sdl3"); + +// main is provided by SDL_main.h, which calls the SDL_App* callbacks below +pub extern fn main(argc: c_int, argv: [*c][*c]u8) c_int; + +var window: ?*sdl.SDL_Window = null; +var renderer: ?*sdl.SDL_Renderer = null; + +export fn SDL_AppInit(appstate: ?*?*anyopaque, argc: c_int, argv: [*c][*c]u8) sdl.SDL_AppResult { + _ = appstate; + _ = argc; + _ = argv; + + // Initialize SDL + if (!sdl.SDL_Init(sdl.SDL_INIT_VIDEO)) { + std.debug.panic("{s}", .{sdl.SDL_GetError()}); + } + + std.debug.print("video driver: {s}\n", .{sdl.SDL_GetCurrentVideoDriver() orelse @as([*c]const u8, "null")}); + + // Create a window and renderer + if (!sdl.SDL_CreateWindowAndRenderer( + "example", + 960, + 540, + sdl.SDL_WINDOW_RESIZABLE | sdl.SDL_WINDOW_HIGH_PIXEL_DENSITY, + &window, + &renderer, + )) { + std.debug.panic("{s}", .{sdl.SDL_GetError()}); + } + + return sdl.SDL_APP_CONTINUE; +} + +export fn SDL_AppEvent(appstate: ?*anyopaque, event: ?*sdl.SDL_Event) sdl.SDL_AppResult { + _ = appstate; + + if (event.?.type == sdl.SDL_EVENT_QUIT) { + return sdl.SDL_APP_SUCCESS; + } + return sdl.SDL_APP_CONTINUE; +} + +export fn SDL_AppIterate(appstate: ?*anyopaque) sdl.SDL_AppResult { + _ = appstate; + + // Update the background color + const now = @as(f64, @floatFromInt(sdl.SDL_GetTicks())) / 1000.0; + const r: f32 = @floatCast(0.5 + 0.5 * @sin(now)); + const g: f32 = @floatCast(0.5 + 0.5 * @sin(now + std.math.pi * 2 / 3.0)); + const b: f32 = @floatCast(0.5 + 0.5 * @sin(now + std.math.pi * 4 / 3.0)); + + if (!sdl.SDL_SetRenderDrawColorFloat(renderer, r, g, b, sdl.SDL_ALPHA_OPAQUE_FLOAT)) { + std.debug.panic("{s}", .{sdl.SDL_GetError()}); + } + if (!sdl.SDL_RenderClear(renderer)) { + std.debug.panic("{s}", .{sdl.SDL_GetError()}); + } + if (!sdl.SDL_RenderPresent(renderer)) { + std.debug.panic("{s}", .{sdl.SDL_GetError()}); + } + + return sdl.SDL_APP_CONTINUE; +} + +export fn SDL_AppQuit(appstate: ?*anyopaque, result: sdl.SDL_AppResult) void { + _ = appstate; + _ = result; +}