Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add minimal support to Zig toolchain to support building iOS binaries #9532

Merged
merged 7 commits into from Aug 11, 2021

Conversation

kubkon
Copy link
Member

@kubkon kubkon commented Aug 6, 2021

With this change, it is possible to successfully target iOS and iPhone Simulator platforms. See kubkon/zig-ios-example for a complete project example how.

Major changes include:

  • ensure we correctly transfer -iwithsysroot and -iframeworkwithsysroot flags with values from build.zig and that they are correctly transferred forward to zig cc
  • move std.zig.system.macos into std.zig.system.darwin.macos since we now special-case all Apple platforms when building from another Apple platform (usually macOS) - std.zig.system.darwin.getSDKPath now takes two args as input: allocator and Target, and based on the target it will invoke xcrun --sdk <sdk_name> --show-sdk-path with the appropriate sdk_name such as iphonesimulator or iphoneos, etc. With this change, we make Zig auto-invoke getSDKPath when targeting an Apple platform from an Apple platform, meaning that we no longer constrain ourselves to running natively but also when targeting ios from macos, etc.
  • try to look for libSystem.tbd in the provided syslibroot
  • take into account .simulator ABI when targeting iOS - .simulator ABI signals that the binary is meant to be run on Apple iPhone Simulator rather than on a real device and therefore it should be linked against a completely standalone set of libraries and frameworks known as iphonesimulator SDK
  • swap out VERSION_MIN load command for BUILD_VERSION - turns out, the latter is required to make the app get properly launched on the iPhone Simulator, while presence of the former generates a sigkill-equivalent for some reason

I could use some input here on the validity of treating std.build.addSystemIncludeDir and std.build.addFrameworkDir differently based on the presence of the sysroot on the command line - if there is no sysroot present, we convert them into -isystem and -iframework; if the sysroot is present however, we change that to -iwithsysroot and -iframeworkwithsysroot. This can trigger a weird error if we are auto-detecting sysroots by Zig (when targeting iOS for instance) and the user adds explicit system search paths to their build.zig as then we get conflicting search dirs for the headers which can lead to a compile error.


UPDATE: turns out that when linking against simulator ABI such as aarch64-ios-simulator, several libc functions such as fstat, etc., are provided by the host (aarch64-macos), and thus, it is necessary to take that into account when linking against tbd stubs. This has now been added in 83f1203.

* ensure we correctly transfer `-iwithsysroot` and
  `-iframeworkwithsysroot` flags with values from `build.zig` and that
  they are correctly transferred forward to `zig cc`
* try to look for `libSystem.tbd` in the provided syslibroot - one
  caveat that the user will have to specify library search paths too
this makes the app successfully run on the iOS simluator!
For example, in order to run a binary on an iPhone Simulator,
you need to specify that explicitly as part of the target as
`aarch64-ios-simulator` rather than `aarch64-ios-gnu` or
`aarch64-ios` for short.
This is now no longer limited to targeting macOS natively but also
tries to detect the sysroot when targeting different Apple platforms
from macOS; for instance targeting iPhone Simulator from macOS. In
this case, Zig will try detecting the SDK path by invoking
`xcrun --sdk iphonesimulator --show-sdk-path`, and if the command
fails because the SDK doesn't exist (case when having CLT installed only)
or not having either Xcode or CLT installed, we simply return null
signaling that the user has to provide the sysroot themselves.
otherwise, linking may fail as some libc functions are provided by
the host when simulating a different OS such iPhoneOS.
@kubkon kubkon merged commit 60a5552 into master Aug 11, 2021
@kubkon kubkon deleted the basic-ios-support branch August 11, 2021 17:36
@jedisct1
Copy link
Contributor

\o/

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.

None yet

2 participants