Skip to content

SDL callbacks and iOS simulator support - #51

Open
cwindolf wants to merge 10 commits into
allyourcodebase:mainfrom
cwindolf:cw-dev
Open

cwindolf wants to merge 10 commits into
allyourcodebase:mainfrom
cwindolf:cw-dev

Conversation

@cwindolf

@cwindolf cwindolf commented Oct 25, 2026 •

Copy link
Copy Markdown

Hi all,

This PR does a couple of things to get SDL working on iOS with SDL's callbacks.

  • Passes Apple SDK paths to translate-c
  • Disables the sanitizer on iOS, where it crashes at runtime (not build time)
  • Adds a new -Dmain_callbacks: when passed, src/sdl_main.c includes SDL's SDL_main.h
  • Also adds this build helper addAppleSdkPathsToModule which users can call in their builds to link UIKit etc as needed. I'm not sure if this part would be controversial and happy to remove it. It's used in my demo (below) so I could move it there, but I think that any iOS-targeting user would need something similar.

I haven't updated the demo in this repo since I didn't want to force it to use callbacks, but I reworked it here:
https://github.com/cwindolf/zig-sdl3-callbacks-example. In that repository, you can zig build ios-run -Dtarget=aarch64-ios-simulator and see the rainbow appear on the iPhone of your choice.

I wrote this in a vacuum so I apologize if it conflicts with other PRs or handles existing issues in a non-optimal way. I tried to get a handle on those questions, here are some notes:

  • Re Add Emscripten support #43 (Emscripten): I think my changes here may cover their -Dinclude_path logic as a special case, for instance. They also change this repo's demo app to use callbacks, but in a way which wouldn't work on iOS because iOS needs to run via SDL_RunApp.
  • Re macOS cross-compilation: iconv linked unconditionally #47 (mac cross-compilation): I didn't handle this, but my code could probably affect the implementation. Not super sure.

Thank you for taking a look at this. Let me know if it's useful, and if so if there are any changes you'd suggest! For now, I haven't touched the README or the example. I wanted to check in about the approach first, but I could at least mention -Dmain_callbacks in the README if that's of interest. Not sure what to do about the example, I'm open to any options.

… and other apple

related stuff)

retains my stuff: SDL3_main/-Dmain_callbacks; helpers for finding and linking Apple
SDK's for iOS; and this thing where sanitize_c is not working on iOS (seems that Zig's
UBSan does not understand something Apple-related)
…un always, and also

unify SDK logic in one place
@MasonRemaley

Copy link
Copy Markdown
Contributor

Nice thanks for the PR! I'm going to have access to a mac to test with next week, at which point I'll review all the Apple related PRs. No worries about conflicts with other open PRs I can sort all that out. :)

@cwindolf

Copy link
Copy Markdown
Author

Great, thank you! Happy to break this into pieces or remove things, whatever seems best.

@MasonRemaley MasonRemaley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Left some comments, feel free to push back on anything you think that I'm misunderstanding.

Comment thread build.zig Outdated
\\than a classic SDL_main, defaults to false
) orelse false;

const can_san = target.result.os.tag != .ios;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What triggers the sanitizer to fail? Instead of turning it off, maybe we can patch SDL to fix the UB. I've landed other UBSAN related fixes in SDL and they've been happy to merge them.

@cwindolf cwindolf Nov 7, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looked into this. It is triggered by some Apple libdispatch code which has a bad __builtin_assume (the function is dispatch_once, which is called from SDL's SDL_SetiOSEventPump). It could be something they are doing deliberately, kind of over my head...

I added a commit to stop disabling the sanitizer globally and just do -fno-sanitize=builtin on the iOS platform sources, wondering if that feels acceptable?

As an aside, this also hit a UBSan warning type that Zig did not have implemented, so I opened an issue on Zig about that: https://codeberg.org/ziglang/zig/issues/37575

Comment thread build.zig Outdated
.flags = flags,
});

const main_lib = b.addLibrary(.{

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be a separate static library, or could we instead configure the existing SDL library differently depending on whether or not the callbacks feature is enabled?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is simplest to have this as a separate static library. SDL has this as a header with platform branches and stuff, and I think it is expected that applications would include it since they "own main".

However, I changed it so that the library is only included when callbacks are turned on rather than always including an empty library all the time in non-callback builds.

Comment thread build.zig Outdated
// SDK's headers/frameworks/libs passed in explicitly. Scoped to the SDL library only, unlike
// `--sysroot` this does not re-roots paths across the whole build graph.
const system_paths: SystemPaths = .{
// On Apple, if paths are not passed explicitly then fall back to the SDK found via `xcrun`.

@MasonRemaley MasonRemaley Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should have to bake in platform specific paths into this file. I would expect dependencies to be found one of three ways:
1. Provided by the build system (e.g. via build.zig.zon)
2. Provided by the system (e.g. -fsys=... and linkSystemLibrary/such)
3. Provided by the user (e.g. --search-prefix)

I think this might be on me for not reviewing some of the previous macOS PRs carefully enough. I have access to a mac for a few weeks, so unless you feel motivated to take on the task of cleaning that up feel free to just move this part of the changes into your example and I can work on that separately.

You can leave the translate C change here for now though since IIUC that's necessary for this to work at all.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I ported my game to macOS, I understand why we're taking these paths in as build options. However, we shouldn't need to do any fixups on the paths here.

If you're cross compiling then the caller needs to provide these paths, if you're not cross compiling they can all be left off. I suspect what's happening on your end is you're technically cross compiling to mac from mac because you set -Dtarget explicitly, and this is causing you to need to find the paths yourself, but I could be mistaken.

If that's not the case let me know and I can help you figure out why it's not working for you without these changes. Good call on the translate C changes though, I just fixed those in master because I was blocked on them and credited you in the commit message.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is helpful for me (not much experience with this kind of thing, probably overcomplicating it). I'll work on understanding what's going on better and see if I can remove my SDK path stuff.

@cwindolf cwindolf Nov 7, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, so I have updated this path handling stuff a bit! I tracked your changes on main, but I had to also point translateC itself (not the SDL module) to the system paths to get the ios build working. It finds them automatically when building for mac from mac, but not when cross compiling to ios. I decided to match the style of keeping these inline instead of in helpers the way I had it before.

Comment thread build.zig
,
) orelse .static;

const main_callbacks = b.option(bool, "main_callbacks",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be reasonable to duplicate the example program and rename it to callbacks_example, and make use of this feature there.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, this is added! (Runs with zig build run-callbacks-example -Dmain_callbacks.) I decided to have it error out if no -Dmain_callbacks.

MasonRemaley added a commit that referenced this pull request Nov 3, 2026
Thanks to @cwindolf for pointing out that we need to add the system
paths to translate C in #51!
Gets rid of my apple helpers which were put in directly in macos.zig and in the main
build for the translateC part.

Also remove the sanitizer disabling thing for now to see about tracking it down.
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.
…ther than otherwise

including an empty library
@cwindolf

cwindolf commented Nov 7, 2026

Copy link
Copy Markdown
Author

Thank you for the review! Learned some things, very helpful. I have made attempts to address all of your comments now, let me know how they feel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants