Skip to content

SDL callbacks and iOS simulator support - #51

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

cwindolf wants to merge 6 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
\\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.

Comment thread build.zig
.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?

Comment thread build.zig
// 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.

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.

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!
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