chinedufn / swift-bridge

swift-bridge facilitates Rust and Swift interop.
https://chinedufn.github.io/swift-bridge
Apache License 2.0
815 stars 58 forks source link

Difficulty following the "Xcode + Cargo" example with Xcode 15.0 beta #231

Open ghost opened 1 year ago

ghost commented 1 year ago

I followed it exactly, until the step "Pressing the Run button should now open up the iOS simulator with Xcode's default "Hello World" iOS app." where instead of the iOS simulator there are errors:

Screenshot 2023-06-18 at 20 58 29

This is on a Mac Mini M2 2023 running macOS 14.0 beta Sonoma.

ghost commented 1 year ago

Checked the build log (25916 lines long), noticed advice to run "rustup target add aarch64-apple-ios-sim", now there's only one error: "Sandbox: bash(18727) deny(1) file-read-data /Users/user/Desktop/code/ios-rust-analyzer/IosRustAnalyzer/build-rust.sh"

Copy and pasting the build script into the Build Phases > Run Script box (and changing Shell to /bin/bash), the build log contains the following:

error: failed to run custom build command for `ios-rust-analyzer v0.1.0 (/Users/user/Desktop/code/ios-rust-analyzer)` Caused by: process didn't exit successfully: `/Users/user/Desktop/code/ios-rust-analyzer/target/debug/build/ios-rust-analyzer-d4c0ef4ad7b1cccf/build-script-build` (exit status: 101) --- stdout cargo:rerun-if-changed=src/lib.rs cargo:rerun-if-env-changed=CONFIGURATION --- stderr thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 1, kind: PermissionDenied, message: "Operation not permitted" }', /Users/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/swift-bridge-build-0.1.51/src/lib.rs:78:79 stack backtrace: 0: rust_begin_unwind at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/std/src/panicking.rs:578:5 1: core::panicking::panic_fmt at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/panicking.rs:67:14 2: core::result::unwrap_failed at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/result.rs:1687:5 3: core::result::Result::unwrap at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/result.rs:1089:23 4: swift_bridge_build::GeneratedCode::write_all_concatenated at /Users/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/swift-bridge-build-0.1.51/src/lib.rs:78:9 5: build_script_build::main at ./build.rs:12:5 6: core::ops::function::FnOnce::call_once at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library/core/src/ops/function.rs:250:5 note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. warning: build failed, waiting for other jobs to finish... [ERROR cargo_lipo] Failed to build "ios-rust-analyzer" for "aarch64-apple-ios-sim": Executing ACTION="build"

Here is swift-bridge-build-0.1.51/src/lib.rs line 78 causing the error, in the GeneratedBuild impl:

std::fs::write(out.join(format!("{}.h", crate_name)), concatenated_c).unwrap();

..so somehow need to allowlist the build-swift-build program for writing files I guess?

ghost commented 1 year ago

Removing the Run Script step and running it manually, then trying to build the Xcode project gives the errors:

In /Users/user/Desktop/code/ios-rust-analyzer/target/universal/debug/libios_rust_analyzer.a(ios_rust_analyzer-0ef8df2bcd6643d0.qkrptas7sr7chi3.rcgu.o), building for iOS Simulator, but linking in object file built for iOS, file '/Users/user/Desktop/code/ios-rust-analyzer/target/universal/debug/libios_rust_analyzer.a' for architecture arm64

Linker command failed with exit code 1 (use -v to see invocation)

Changing line 30 of build-rust.sh to

TARGETS="aarch64-apple-ios-sim,x86_64-apple-ios"

and the simulator finally starts!

chinedufn commented 1 year ago

Changing line 30 of build-rust.sh to TARGETS="aarch64-apple-ios-sim,x86_64-apple-ios"

Hmm, isn't this already the case in the guide https://github.com/chinedufn/swift-bridge/blob/ecd3b974754e32fc951b8bc4098ecef84e974821/book/src/building/xcode-and-cargo/README.md?plain=1#L88

I'm not understanding what the problem was and what the fix was?

ghost commented 1 year ago

Oh, right, because I was running the build-rust.sh script manually as otherwise the swift-bridge-build GeneratedBuild code doesn't have write permission for some reason, and was forgetting to set PLATFORM_NAME.

The problem is the Run Script step fails on macOS 14.0 beta Sonoma, possibly because of increased privacy defaults.

ghost commented 1 year ago

Ok, followed the guide to the end now and it works, just wanted to report the build errors with the Run Script portion (included in my second post).

chinedufn commented 1 year ago

Got it. How did you fix the permissions issue?

ghost commented 1 year ago

I didn't, I ran the build-rust.sh script manually. I'm completely new to mac development and since the OS and IDE are both beta versions I put off trying to fix the permissions thing in favour of getting the example to run.

(And now I'm trying to build for rust targets aarch64-apple-ios-macabi and x86_64-apple-ios-macabi so I can compile for Mac Catalyst.)

sax commented 6 months ago

I'm not sure if this is related, but in XCode 15.3 I had to go into my project's Build Settings and configure: Build Option > User Script Sandboxing to No for both debug and release.

willtemperley commented 1 month ago

@sax I think that's the fix, worked for me in Xcode 16 beta 2

willtemperley commented 1 month ago

One other thing related to following this tutorial (which was excellent by the way, I learned a lot) was when copying the lib.rs rust code using the copy button it's wrapped in a fn main {} which isn't what's displayed in the tutorial.