Brendonovich / swift-rs

Call Swift functions from Rust with ease!
Apache License 2.0
246 stars 28 forks source link

Unable to load standard library with Tauri #42

Closed lok52 closed 1 year ago

lok52 commented 1 year ago

When building a Tauri project I get the following error:

Caused by:
  process didn't exit successfully: `/Users/leonidtyurin/projects/tauri-bencher/src-tauri/target/debug/build/tauri-1c55146931e6e5c3/build-script-build` (exit status: 101)
  --- stdout
  cargo:rustc-cfg=dev
  cargo:rustc-cfg=mobile
  cargo:rustc-cfg=shell_open
  cargo:rustc-cfg=shell_any
  cargo:rustc-cfg=shell_scope
  cargo:rustc-link-search=native=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphonesimulator
  cargo:rustc-link-search=native=/usr/lib/swift
  cargo:rustc-link-lib=clang_rt.ios
  cargo:rustc-link-search=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/14.0.0/lib/darwin
  [0/1] Planning build
  Building for debugging...
  warning: inferring simulator environment for target 'x86_64-apple-ios13.0'; use '-target x86_64-apple-ios13.0-simulator' insteaderror: emit-module command failed with exit code 1 (use -v to see invocation)[1/3] Emitting module SwiftRs
  <unknown>:0: error: unable to load standard library for target 'x86_64-apple-ios13.0-simulator'
  [2/3] Compiling SwiftRs lib.swift
  <unknown>:0: error: unable to load standard library for target 'x86_64-apple-ios13.0-simulator'

  --- stderr
  thread 'main' panicked at 'Failed to compile swift package Tauri', /Users/leonidtyurin/.cargo/registry/src/github.com-1ecc6299db9ec823/swift-rs-1.0.3/src-rs/build.rs:270:17
  stack backtrace:
     0: rust_begin_unwind
               at /rustc/8460ca823e8367a30dda430efda790588b8c84d3/library/std/src/panicking.rs:575:5
     1: core::panicking::panic_fmt
               at /rustc/8460ca823e8367a30dda430efda790588b8c84d3/library/core/src/panicking.rs:64:14
     2: swift_rs::build::SwiftLinker::link
               at /Users/leonidtyurin/.cargo/registry/src/github.com-1ecc6299db9ec823/swift-rs-1.0.3/src-rs/build.rs:270:17
     3: tauri_build::mobile::link_swift_library
               at /Users/leonidtyurin/.cargo/registry/src/github.com-1ecc6299db9ec823/tauri-build-2.0.0-alpha.2/src/mobile.rs:106:3
     4: build_script_build::main
               at /Users/leonidtyurin/.cargo/registry/src/github.com-1ecc6299db9ec823/tauri-2.0.0-alpha.4/build.rs:186:7
     5: core::ops::function::FnOnce::call_once
               at /rustc/8460ca823e8367a30dda430efda790588b8c84d3/library/core/src/ops/function.rs:250:5
  note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
       Error [tauri_cli_node] Failed to run `cargo build`: Command "cargo build --package tauri-bencher --manifest-path /Users/leonidtyurin/projects/tauri-bencher/src-tauri/Cargo.toml --target x86_64-apple-ios --no-default-features --color always" didn't complete successfully, exiting with code 101.

Versions:

Xcode: 14.2

Environment
  › OS: Mac OS 13.2.1 X64
  › Node.js: 18.12.1
  › npm: 8.19.2
  › pnpm: Not installed!
  › yarn: 1.22.19
  › rustup: 1.25.2
  › rustc: 1.68.1
  › cargo: 1.68.1
  › Rust toolchain: stable-x86_64-apple-darwin

Packages
  › @tauri-apps/cli [NPM]: 2.0.0-alpha.4
  › @tauri-apps/api [NPM]: 2.0.0-alpha.1
  › tauri [RUST]: 2.0.0-alpha.4,
  › tauri-build [RUST]: 2.0.0-alpha.2,
  › tao [RUST]: 0.18.3,
  › wry [RUST]: 0.27.1,

Looks like something is wrong with environment setup and probably completely related to Tauri itself. But, maybe someone knows how to solve it?

Brendonovich commented 1 year ago

Can you make a reproduction repo? I'm not quite sure what's going on here.

lok52 commented 1 year ago

I followed the official guide using create-tauri-app with default template. You can reproduce these commands locally:

projects > yarn create tauri-app --alpha
yarn create v1.22.19
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 🔨  Building fresh packages...
success Installed "create-tauri-app@3.2.1" with binaries:
      - create-tauri-app
✔ Project name · tauri-test
✔ Choose which language to use for your frontend · TypeScript / JavaScript - (pnpm, yarn, npm)
✔ Choose your package manager · yarn
✔ Choose your UI template · Svelte - (https://svelte.dev/)
✔ Choose your UI flavor · TypeScript
✔ Would you like to setup the project for mobile as well? · yes

Template created! To get started run:
  cd tauri-test
  yarn
  yarn tauri android init
  yarn tauri ios init

For Desktop development, run:
  yarn tauri dev

For Android development, run:
  yarn tauri android dev

For iOS development, run:
  yarn tauri ios dev

✨  Done in 19.74s.

Then just:

  cd tauri-test
  yarn
  yarn tauri ios init
  yarn tauri ios dev

And the error appears

Brendonovich commented 1 year ago

It works fine for me - probably because my Rust target is aarch64-apple-ios-sim. Gonna talk about this with the Tauri team.

lok52 commented 1 year ago

Did you use a machine with M1 chip?

Brendonovich commented 1 year ago

Yeah M1 pro

lok52 commented 1 year ago

Looks like I found the problem. The following command invoked by swift-rs produced the error:

ios-api > swift build -c debug --scratch-path /Users/leonidtyurin/projects/tauri-test/src-tauri/target/x86_64-apple-ios/debug/build/tauri-b8a991d93d8a93f2/out/swift-rs/Tauri -Xswiftc -sdk -Xswiftc /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS16.2.sdk -Xswiftc -target -Xswiftc x86_64-apple-ios16.0
Building for debugging...
<unknown>:0: error: unable to load standard library for target 'x86_64-apple-ios16.0-simulator'
<unknown>:0: error: unable to load standard library for target 'x86_64-apple-ios16.0-simulator'

But, if I change sdk path variable (/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS16.2.sdk -> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator16.2.sdk) everything works fine:

ios-api > swift build -c debug --scratch-path /Users/leonidtyurin/projects/tauri-test/src-tauri/target/x86_64-apple-ios/debug/build/tauri-b8a991d93d8a93f2/out/swift-rs/Tauri -Xswiftc -sdk -Xswiftc /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator16.2.sdk -Xswiftc -target -Xswiftc x86_64-apple-ios16.0
Building for debugging...
[11/11] Archiving libTauri.a
Build complete! (8.52s)

So, I guess problem is somewhere here in the build.rs file.

lok52 commented 1 year ago

So, it incorrectly infers SDK path for simulator

lok52 commented 1 year ago

I think this PR fixes the issue, but the Tauri does not include it, yet :)

Brendonovich commented 1 year ago

Looks like this is rectified over on Tauri's end :)