From d59a3ddd61d01b426568073a3590074730578d5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois?= Date: Mon, 29 Nov 2021 23:25:22 +0000 Subject: [PATCH] iOS: replace cargo-lipo, and update for new macOS (#3109) # Objective - Remove `cargo-lipo` as [it's deprecated](https://github.com/TimNN/cargo-lipo#maintenance-status) and doesn't work on new Apple processors - Fix CI that will fail as soon as GitHub update the worker used by Bevy to macOS 11 ## Solution - Replace `cargo-lipo` with building with the correct target - Setup the correct path to libraries by using `xcrun --show-sdk-path` - Also try and fix path to cmake in case it's not found but available through homebrew --- examples/README.md | 18 +++--- examples/ios/Cargo.toml | 17 +++++- examples/ios/Makefile | 4 +- .../project.pbxproj | 26 +++++++-- examples/ios/build_rust_deps.sh | 54 ++++++++++++++++-- examples/ios/src/lib.rs | 55 ++++++++++++++++++- 6 files changed, 146 insertions(+), 28 deletions(-) diff --git a/examples/README.md b/examples/README.md index 6d8a563bd8fb6..8329c0908cc46 100644 --- a/examples/README.md +++ b/examples/README.md @@ -322,9 +322,14 @@ Example | File | Description ### Setup +You need to install the correct rust targets: + +- `aarch64-apple-ios`: iOS devices +- `x86_64-apple-ios`: iOS simulator on x86 processors +- `aarch64-apple-ios-sim`: iOS simulator on Apple processors + ```sh -rustup target add aarch64-apple-ios x86_64-apple-ios -cargo install cargo-lipo +rustup target add aarch64-apple-ios x86_64-apple-ios aarch64-apple-ios-sim ``` ### Build & Run @@ -353,15 +358,6 @@ open bevy_ios_example.xcodeproj/ which will open xcode. You then must push the zoom zoom play button and wait for the magic. -The Xcode build GUI will by default build the rust library for both -`x86_64-apple-ios`, and `aarch64-apple-ios` which may take a while. If you'd -like speed this up, you update the `IOS_TARGETS` User-Defined environment -variable in the "`cargo_ios` target" to be either `x86_64-apple-ios` or -`aarch64-apple-ios` depending on your goal. - -Note: if you update this variable in Xcode, it will also change the default -used for the `Makefile`. - Example | File | Description --- | --- | --- `ios` | [`ios/src/lib.rs`](./ios/src/lib.rs) | The `3d/3d_scene.rs` example for iOS diff --git a/examples/ios/Cargo.toml b/examples/ios/Cargo.toml index 81253d5edcb02..6f9c3e5bdb055 100644 --- a/examples/ios/Cargo.toml +++ b/examples/ios/Cargo.toml @@ -11,4 +11,19 @@ name = "bevy_ios_example" crate-type = ["staticlib"] [dependencies] -bevy = { path = "../../", features = [ "bevy_gilrs", "bevy_gltf", "bevy_wgpu", "bevy_winit", "render", "png", "hdr", "bevy_audio", "mp3"], default-features = false} +bevy = { path = "../../", features = [ + "bevy_audio", + "bevy_core_pipeline", + "bevy_gltf2", + "bevy_wgpu", + "bevy_sprite2", + "bevy_render2", + "bevy_pbr2", + "bevy_winit", + "render", + "png", + "hdr", + "mp3", + "x11", + "filesystem_watcher" +], default-features = false} diff --git a/examples/ios/Makefile b/examples/ios/Makefile index f16ec7a27d0bf..6d13bdcccaea0 100644 --- a/examples/ios/Makefile +++ b/examples/ios/Makefile @@ -15,10 +15,10 @@ install: xcodebuild-simulator boot-sim xcrun simctl install $(DEVICE) build/Build/Products/Debug-iphonesimulator/bevy_ios_example.app xcodebuild-simulator: - IOS_TARGETS=x86_64-apple-ios xcodebuild -scheme bevy_ios_example -configuration Debug -derivedDataPath build -destination "id=$(DEVICE)" + IOS_TARGETS=x86_64-apple-ios xcodebuild -scheme bevy_ios_example -configuration Debug -derivedDataPath build -destination "id=$(DEVICE)" xcodebuild-iphone: - IOS_TARGETS=aarch64-apple-ios xcodebuild -scheme bevy_ios_example -configuration Debug -derivedDataPath build -arch arm64 + IOS_TARGETS=aarch64-apple-ios xcodebuild -scheme bevy_ios_example -configuration Debug -derivedDataPath build -arch arm64 clean: rm -r build diff --git a/examples/ios/bevy_ios_example.xcodeproj/project.pbxproj b/examples/ios/bevy_ios_example.xcodeproj/project.pbxproj index 7b28d9a9e7500..790312c38e20a 100644 --- a/examples/ios/bevy_ios_example.xcodeproj/project.pbxproj +++ b/examples/ios/bevy_ios_example.xcodeproj/project.pbxproj @@ -276,9 +276,17 @@ "$(inherited)", "@executable_path/Frameworks", ); - LIBRARY_SEARCH_PATHS = ( + "LIBRARY_SEARCH_PATHS[sdk=iphoneos*][arch=arm64]" = ( "$(inherited)", - ../../target/universal/release, + "../../target/aarch64-apple-ios/release", + ); + "LIBRARY_SEARCH_PATHS[sdk=iphonesimulator*][arch=arm64]" = ( + "$(inherited)", + "../../target/aarch64-apple-ios-sim/release", + ); + "LIBRARY_SEARCH_PATHS[sdk=iphonesimulator*][arch=x86_64]" = ( + "$(inherited)", + "../../target/x86_64-apple-ios/release", ); OTHER_LDFLAGS = ( "$(inherited)", @@ -346,7 +354,6 @@ 8265913A25816D964A847F1B /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { - IOS_TARGETS = ""; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -376,9 +383,17 @@ "$(inherited)", "@executable_path/Frameworks", ); - LIBRARY_SEARCH_PATHS = ( + "LIBRARY_SEARCH_PATHS[sdk=iphoneos*][arch=arm64]" = ( + "$(inherited)", + "../../target/aarch64-apple-ios/debug", + ); + "LIBRARY_SEARCH_PATHS[sdk=iphonesimulator*][arch=arm64]" = ( + "$(inherited)", + "../../target/aarch64-apple-ios-sim/debug", + ); + "LIBRARY_SEARCH_PATHS[sdk=iphonesimulator*][arch=x86_64]" = ( "$(inherited)", - ../../target/universal/debug, + "../../target/x86_64-apple-ios/debug", ); OTHER_LDFLAGS = ( "$(inherited)", @@ -394,7 +409,6 @@ FEA9B18D9236F9F6DC6DF799 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { - IOS_TARGETS = ""; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", diff --git a/examples/ios/build_rust_deps.sh b/examples/ios/build_rust_deps.sh index aa5ee3134e98e..ec39adf372daf 100755 --- a/examples/ios/build_rust_deps.sh +++ b/examples/ios/build_rust_deps.sh @@ -1,12 +1,54 @@ -#!/bin/sh +#!/usr/bin/env bash -set -e +# based on https://github.com/mozilla/glean/blob/main/build-scripts/xc-universal-binary.sh + +set -eux PATH=$PATH:$HOME/.cargo/bin -# If you want your build to run faster, add a "--targets x86_64-apple-ios" for just using the ios simulator. -if [ -n ${IOS_TARGETS} ]; then - cargo lipo --targets ${IOS_TARGETS} +RELFLAG= +if [[ "$CONFIGURATION" != "Debug" ]]; then + RELFLAG=--release +fi + +set -euvx + +# add path to the system SDK, needed since macOS 11 +if [ -z ${LIBRARY_PATH+x} ]; then + export LIBRARY_PATH="$(xcrun --show-sdk-path)/usr/lib" else - cargo lipo + export LIBRARY_PATH="$LIBRARY_PATH:$(xcrun --show-sdk-path)/usr/lib" +fi + +# add homebrew bin path, as it's the most commonly used package manager on macOS +# this is needed for cmake on apple arm processors as it's not available by default +export PATH="$PATH:/opt/homebrew/bin" + +IS_SIMULATOR=0 +if [ "${LLVM_TARGET_TRIPLE_SUFFIX-}" = "-simulator" ]; then + IS_SIMULATOR=1 fi + +for arch in $ARCHS; do + case "$arch" in + x86_64) + if [ $IS_SIMULATOR -eq 0 ]; then + echo "Building for x86_64, but not a simulator build. What's going on?" >&2 + exit 2 + fi + + # Intel iOS simulator + export CFLAGS_x86_64_apple_ios="-target x86_64-apple-ios" + cargo build --lib $RELFLAG --target x86_64-apple-ios + ;; + + arm64) + if [ $IS_SIMULATOR -eq 0 ]; then + # Hardware iOS targets + cargo build --lib $RELFLAG --target aarch64-apple-ios + else + # M1 iOS simulator -- currently in Nightly only and requires to build `libstd` + cargo build --lib $RELFLAG --target aarch64-apple-ios-sim + fi + esac +done diff --git a/examples/ios/src/lib.rs b/examples/ios/src/lib.rs index 978390d1a77a8..4136955749e27 100644 --- a/examples/ios/src/lib.rs +++ b/examples/ios/src/lib.rs @@ -1,4 +1,21 @@ -use bevy::{prelude::*, window::WindowMode}; +use bevy::{ + audio::{Audio, AudioPlugin}, + input::touch::TouchPhase, + math::{Vec2, Vec3}, + pbr2::{PbrBundle, PointLight, PointLightBundle, StandardMaterial}, + prelude::{ + bevy_main, App, AssetServer, Assets, Commands, EventReader, Local, Query, Res, ResMut, + TouchInput, Transform, With, + }, + render2::{ + camera::{Camera, PerspectiveCameraBundle}, + color::Color, + mesh::{shape, Mesh}, + view::Msaa, + }, + window::{WindowDescriptor, WindowMode, Windows}, + PipelinedDefaultPlugins, +}; // the `bevy_main` proc_macro generates the required ios boilerplate #[bevy_main] @@ -11,11 +28,40 @@ fn main() { ..Default::default() }) .insert_resource(Msaa { samples: 4 }) - .add_plugins(DefaultPlugins) + .add_plugins(PipelinedDefaultPlugins) + .add_plugin(AudioPlugin) .add_startup_system(setup_scene) .add_startup_system(setup_music) + .add_system(touch_camera) .run(); } + +fn touch_camera( + windows: ResMut, + mut touches: EventReader, + mut camera: Query<&mut Transform, With>, + mut last_position: Local>, +) { + for touch in touches.iter() { + if touch.phase == TouchPhase::Started { + *last_position = None; + } + if let Some(last_position) = *last_position { + let window = windows.get_primary().unwrap(); + let mut transform = camera.single_mut(); + *transform = Transform::from_xyz( + transform.translation.x + + (touch.position.x - last_position.x) / window.width() * 5.0, + transform.translation.y, + transform.translation.z + + (touch.position.y - last_position.y) / window.height() * 5.0, + ) + .looking_at(Vec3::ZERO, Vec3::Y); + } + *last_position = Some(touch.position); + } +} + /// set up a simple 3D scene fn setup_scene( mut commands: Commands, @@ -48,6 +94,11 @@ fn setup_scene( // light commands.spawn_bundle(PointLightBundle { transform: Transform::from_xyz(4.0, 8.0, 4.0), + point_light: PointLight { + intensity: 5000.0, + shadows_enabled: true, + ..Default::default() + }, ..Default::default() }); // camera