bevy/crates
Mads Marquart e6a6c9fb4c
Link iOS example with rustc, and avoid C trampoline (#14780)
# Objective

On iOS:
- Allow `std` to do its runtime initialization.
- Avoid requiring the user to specify linked libraries and framework in
Xcode.
- Reduce the amount of work that `#[bevy_main]` does
- In the future we may also be able to eliminate the need for it on
Android, cc @daxpedda.

## Solution

We previously:
- Exposed an `extern "C" fn main_rs` entry point.
- Ran Cargo in a separate Xcode target as an external build system.
- Imported that as a dependency of `bevy_mobile_example.app`.
- Compiled a trampoline C file with Xcode that called `main_rs`.
- Linked that via. Xcode.

All of this is unnecessary; `rustc` is well capable of creating iOS
executables, the trick is just to place it at the correct location for
Xcode to understand it, namely `$TARGET_BUILD_DIR/$EXECUTABLE_PATH`
(places it in `bevy_mobile_example.app/bevy_mobile_example`).

Note: We might want to wait with the changes to `#[bevy_main]` until the
problem is resolved on Android too, to make the migration easier.

## Testing

Open the Xcode project, and build for an iOS target.

---

## Migration Guide

**If you have been building your application for iOS:**

Previously, the `#[bevy_main]` attribute created a `main_rs` entry point
that most Xcode templates were using to run your Rust code from C. This
was found to be unnecessary, as you can simply let Rust build your
application as a binary, and run that directly.

You have two options for dealing with this:

If you've added further C code and Xcode customizations, or it makes
sense for your use-case to continue link with Xcode, you can revert to
the old behaviour by adding `#[no_mangle] extern "C" main_rs() { main()
}` to your `main.rs`. Note that the old approach of linking a static
library prevents the Rust standard library from doing runtime
initialization, so certain functionality provided by `std` might be
unavailable (stack overflow handlers, stdout/stderr flushing and other
such functionality provided by the initialization routines).

The other, preferred option is to remove your "compile" and "link" build
phases, and instead replace it with a "run script" phase that invokes
`cargo build --bin ...`, and moves the built binary to the Xcode path
`$TARGET_BUILD_DIR/$EXECUTABLE_PATH`. An example of how to do this can
be viewed at [INSERT LINK TO UPDATED EXAMPLE PROJECT].
To make the debugging experience in Xcode nicer after this, you might
also want to consider either enabling `panic = "abort"` or to set a
breakpoint on the `rust_panic` symbol.

---------

Co-authored-by: François Mockers <mockersf@gmail.com>
Co-authored-by: François Mockers <francois.mockers@vleue.com>
2025-03-17 21:14:07 +00:00
..
bevy_a11y bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00
bevy_animation bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00
bevy_app Small cleanup for ECS error handling (#18280) 2025-03-13 00:13:02 +00:00
bevy_asset bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00
bevy_audio bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00
bevy_color bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00
bevy_core_pipeline bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00
bevy_derive Link iOS example with rustc, and avoid C trampoline (#14780) 2025-03-17 21:14:07 +00:00
bevy_dev_tools Generic system config (#17962) 2025-03-12 00:12:30 +00:00
bevy_diagnostic Add process cpu/memory usage to SystemInformationDiagnosticsPlugin (#18279) 2025-03-16 21:14:46 +00:00
bevy_dylib Upgrade to Rust Edition 2024 (#17967) 2025-02-24 03:54:47 +00:00
bevy_ecs FilteredResource returns a Result instead of a simple Option (#18265) 2025-03-17 18:54:13 +00:00
bevy_encase_derive Internalize BevyManifest logic. Switch to RwLock (#18263) 2025-03-12 00:46:01 +00:00
bevy_gilrs Replace some !Send resources with thread_local! (#17730) 2025-03-04 07:48:02 +00:00
bevy_gizmos bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00
bevy_gltf bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00
bevy_image bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00
bevy_input bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00
bevy_input_focus bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00
bevy_internal don't use bevy_pbr for base bevy_gizmos plugin (#17581) 2025-03-10 21:16:52 +00:00
bevy_log Add print_stdout and print_stderr lints (#17446) (#18233) 2025-03-11 19:35:48 +00:00
bevy_macro_utils Switch to ImDocument in BevyManifest (#18272) 2025-03-12 20:15:39 +00:00
bevy_math bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00
bevy_mesh bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00
bevy_mikktspace Add print_stdout and print_stderr lints (#17446) (#18233) 2025-03-11 19:35:48 +00:00
bevy_pbr bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00
bevy_picking bevy: Replace unnecessary tuple type registrations (#18369) 2025-03-17 20:20:17 +00:00
bevy_platform_support Support for non-browser wasm (#17499) 2025-03-07 21:22:28 +00:00
bevy_ptr moved Debug from derive to impl_ptr in bevy_ptr (#18042) 2025-02-28 02:54:46 +00:00
bevy_reflect Fixed Reflect derive macro on empty enums (#18277) (#18298) 2025-03-17 18:13:55 +00:00
bevy_remote FilteredResource returns a Result instead of a simple Option (#18265) 2025-03-17 18:54:13 +00:00
bevy_render bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00
bevy_scene FilteredResource returns a Result instead of a simple Option (#18265) 2025-03-17 18:54:13 +00:00
bevy_sprite bevy: Replace unnecessary tuple type registrations (#18369) 2025-03-17 20:20:17 +00:00
bevy_state bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00
bevy_tasks Add print_stdout and print_stderr lints (#17446) (#18233) 2025-03-11 19:35:48 +00:00
bevy_text bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00
bevy_time bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00
bevy_transform Revert "Transform Propagation Optimization: Static Subtree Marking (#18094)" (#18363) 2025-03-17 20:01:29 +00:00
bevy_ui Revert "Transform Propagation Optimization: Static Subtree Marking (#18094)" (#18363) 2025-03-17 20:01:29 +00:00
bevy_utils Automatically enable portable-atomic when required (#17570) 2025-02-24 20:52:46 +00:00
bevy_window bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00
bevy_winit bevy_reflect: Add clone registrations project-wide (#18307) 2025-03-17 18:32:35 +00:00