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

rust-binary-calls-swift-package example doesn't work on a Linux environment #107

Open
yoching opened this issue Aug 11, 2022 · 6 comments · May be fixed by #297
Open

rust-binary-calls-swift-package example doesn't work on a Linux environment #107

yoching opened this issue Aug 11, 2022 · 6 comments · May be fixed by #297

Comments

@yoching
Copy link

yoching commented Aug 11, 2022

Hello.
I'm trying to build a CLI application that is mainly written in Rust and calls some Swift functions, and I'm developing it on a Linux environment.
I tried to run rust-binary-calls-swift-package example, but it didn't run with following errors.
I'm using WSL2 with Ubuntu and swift-5.6.2-RELEASE-ubuntu20.04.

Do you know how I can make it work? Or, are Linux environments not supported?
Thank you in advance!

$ cargo run -p rust-binary-calls-swift-package
   Compiling proc-macro2 v1.0.42
   Compiling unicode-ident v1.0.2
   Compiling quote v1.0.20
   Compiling syn v1.0.98
   Compiling libc v0.2.126
   Compiling fastrand v1.8.0
   Compiling remove_dir_all v0.5.3
   Compiling cfg-if v1.0.0
   Compiling tempfile v3.3.0
   Compiling swift-bridge-ir v0.1.35 (/home/yoshikuni/GitHub/Rust/swift-bridge/crates/swift-bridge-ir)
   Compiling swift-bridge-build v0.1.35 (/home/yoshikuni/GitHub/Rust/swift-bridge/crates/swift-bridge-build)
   Compiling swift-bridge-macro v0.1.35 (/home/yoshikuni/GitHub/Rust/swift-bridge/crates/swift-bridge-macro)
   Compiling rust-binary-calls-swift-package v0.1.0 (/home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package)
   Compiling swift-bridge v0.1.35 (/home/yoshikuni/GitHub/Rust/swift-bridge)
error: linking with `cc` failed: exit status: 1
  |
  = note: "cc" "-m64" "/tmp/rustcBQCunq/symbols.o" "/home/yoshikuni/GitHub/Rust/swift-bridge/target/debug/deps/rust_binary_calls_swift_package-292e71af283f5de2.1w9emz731ogimejr.rcgu.o" "/home/yoshikuni/GitHub/Rust/swift-bridge/target/debug/deps/rust_binary_calls_swift_package-292e71af283f5de2.24kdsva5pt5pnbco.rcgu.o" "/home/yoshikuni/GitHub/Rust/swift-bridge/target/debug/deps/rust_binary_calls_swift_package-292e71af283f5de2.26896tw8fqt0v9fu.rcgu.o" "/home/yoshikuni/GitHub/Rust/swift-bridge/target/debug/deps/rust_binary_calls_swift_package-292e71af283f5de2.2tpci647um6thpo2.rcgu.o" "/home/yoshikuni/GitHub/Rust/swift-bridge/target/debug/deps/rust_binary_calls_swift_package-292e71af283f5de2.4dhdhlqomt6smcj1.rcgu.o" "/home/yoshikuni/GitHub/Rust/swift-bridge/target/debug/deps/rust_binary_calls_swift_package-292e71af283f5de2.4h8n84jaxycwap6v.rcgu.o" "/home/yoshikuni/GitHub/Rust/swift-bridge/target/debug/deps/rust_binary_calls_swift_package-292e71af283f5de2.4hpkoi5vvq6zq02l.rcgu.o" "/home/yoshikuni/GitHub/Rust/swift-bridge/target/debug/deps/rust_binary_calls_swift_package-292e71af283f5de2.abt62j993v3u99r.rcgu.o" "/home/yoshikuni/GitHub/Rust/swift-bridge/target/debug/deps/rust_binary_calls_swift_package-292e71af283f5de2.dj4o1vryudkth0e.rcgu.o" "/home/yoshikuni/GitHub/Rust/swift-bridge/target/debug/deps/rust_binary_calls_swift_package-292e71af283f5de2.zjy3tzpqmre36eq.rcgu.o" "/home/yoshikuni/GitHub/Rust/swift-bridge/target/debug/deps/rust_binary_calls_swift_package-292e71af283f5de2.1zen2gw3lpr0rw58.rcgu.o" "-Wl,--as-needed" "-L" "/home/yoshikuni/GitHub/Rust/swift-bridge/target/debug/deps" "-L" "/home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/.build/debug" "-L" "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/" "-L" "/usr/lib/swift" "-L" "/home/yoshikuni/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "-lswift-library" "/home/yoshikuni/GitHub/Rust/swift-bridge/target/debug/deps/libswift_bridge-882327837dd14bed.rlib" "-Wl,--start-group" "/home/yoshikuni/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-69edc9ac8de4d39c.rlib" "/home/yoshikuni/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-254ac8a4d96ed89e.rlib" "/home/yoshikuni/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-8bed7ede368468ef.rlib" "/home/yoshikuni/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-9da1150e6aeada6b.rlib" "/home/yoshikuni/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-dec39a40a22b358c.rlib" "/home/yoshikuni/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-7a90882341200e38.rlib" "/home/yoshikuni/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-6a5da46a37549d0a.rlib" "/home/yoshikuni/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-6dc3d1df7e408d5b.rlib" "/home/yoshikuni/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-9d89ad14c568d44a.rlib" "/home/yoshikuni/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-ecbdf47025adae10.rlib" "/home/yoshikuni/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-29db3ffec771ef22.rlib" "/home/yoshikuni/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-8b55ad9e1a2080d1.rlib" "/home/yoshikuni/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-f37a55941b8b3bbd.rlib" "/home/yoshikuni/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-c1d45d3d6971ce0d.rlib" "/home/yoshikuni/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-18ac7cbb154ecd62.rlib" "/home/yoshikuni/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-80c5909c82700e85.rlib" "/home/yoshikuni/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-aa7f747e2f6af7d5.rlib" "/home/yoshikuni/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-60d544c9f51ce476.rlib" "-Wl,--end-group" "/home/yoshikuni/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-d1bd89f2a607e488.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/home/yoshikuni/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/home/yoshikuni/GitHub/Rust/swift-bridge/target/debug/deps/rust_binary_calls_swift_package-292e71af283f5de2" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro,-znow" "-nodefaultlibs"
  = note: /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/.build/debug/libswift-library.a(swift_library.swift.o): in function `$s13swift_library0A14_multiply_by_43nums5Int64VAE_tF':
          /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/Sources/swift-library/swift_library.swift:2: undefined reference to `$ss27_allocateUninitializedArrayySayxG_BptBwlFyp_Tg5'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/Sources/swift-library/swift_library.swift:2: undefined reference to `$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/Sources/swift-library/swift_library.swift:2: undefined reference to `$sSSN'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/Sources/swift-library/swift_library.swift:2: undefined reference to `$sypN'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/Sources/swift-library/swift_library.swift:2: undefined reference to `$ss5print_9separator10terminatoryypd_S2StF'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/Sources/swift-library/swift_library.swift:2: undefined reference to `swift_bridgeObjectRelease'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/Sources/swift-library/swift_library.swift:2: undefined reference to `swift_bridgeObjectRelease'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/Sources/swift-library/swift_library.swift:2: undefined reference to `swift_release'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/Sources/swift-library/swift_library.swift:4: undefined reference to `$ss27_allocateUninitializedArrayySayxG_BptBwlFyp_Tg5'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/Sources/swift-library/swift_library.swift:4: undefined reference to `$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/Sources/swift-library/swift_library.swift:4: undefined reference to `$ss5print_9separator10terminatoryypd_S2StF'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/Sources/swift-library/swift_library.swift:4: undefined reference to `swift_bridgeObjectRelease'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/Sources/swift-library/swift_library.swift:4: undefined reference to `swift_bridgeObjectRelease'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/Sources/swift-library/swift_library.swift:4: undefined reference to `swift_release'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/Sources/swift-library/swift_library.swift:8: undefined reference to `$ss27_allocateUninitializedArrayySayxG_BptBwlFyp_Tg5'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/Sources/swift-library/swift_library.swift:8: undefined reference to `$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/Sources/swift-library/swift_library.swift:8: undefined reference to `$ss5print_9separator10terminatoryypd_S2StF'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/Sources/swift-library/swift_library.swift:9: undefined reference to `swift_bridgeObjectRelease'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/Sources/swift-library/swift_library.swift:9: undefined reference to `swift_bridgeObjectRelease'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/Sources/swift-library/swift_library.swift:9: undefined reference to `swift_release'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/.build/debug/libswift-library.a(swift_library.swift.o): in function `$ss27_finalizeUninitializedArrayySayxGABnlF':
          /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/<compiler-generated>:(.text+0x2a7): undefined reference to `$sSaMa'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/<compiler-generated>:(.text+0x2c0): undefined reference to `swift_retain'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/<compiler-generated>:(.text+0x2c9): undefined reference to `swift_release'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/.build/debug/libswift-library.a(swift_library.swift.o): in function `$ss5print_9separator10terminatoryypd_S2StFfA0_':
          /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/<compiler-generated>:(.text+0x2f6): undefined reference to `$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC'
          /usr/bin/ld: /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/.build/debug/libswift-library.a(swift_library.swift.o): in function `$ss5print_9separator10terminatoryypd_S2StFfA1_':
          /home/yoshikuni/GitHub/Rust/swift-bridge/examples/rust-binary-calls-swift-package/swift-library/<compiler-generated>:(.text+0x316): undefined reference to `$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC'
          collect2: error: ld returned 1 exit status
          
  = help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

error: could not compile `rust-binary-calls-swift-package` due to previous error
@chinedufn
Copy link
Owner

chinedufn commented Aug 16, 2022

I haven't tried swift-bridge on linux so I don't have a lot of experience here. But I'm happy to help get to the bottom of this.

Right now we build this example in CI on macOS, so it works there

- name: Build Rust binary calls Swift Package examaple
run: cargo build -p rust-binary-calls-swift-package
.

In your error message I see things like undefined reference to$ss5print_9separator10terminatoryypd_S2StF'`.

Seems like a bunch of symbols that have nothing to do with swift-bridge are missing.

Can you try adding import Foundation to the top of the file and seeing if that does anything?


Also, are you able to run any of the other examples?

@yoching
Copy link
Author

yoching commented Aug 22, 2022

Thank you for your reply. These are my new trials.

  • rust-binary-calls-swift-package
    I added import Foundation at the top of swift_library.swift, but the result was the same.
  • async-functions
    The build didn't succeed. I pasted the log on gist here, as it's very long.
  • codegen-visualizer
    Since Xcode doesn't exist on Linux, this example doesn't run on linux.

@chinedufn
Copy link
Owner

rust-binary-calls-swift-package

Hmm this one requires more research.. Not sure to what extent this is possible on Linux. Maybe it is with the right flags, I don't know yet.


The build didn't succeed. I pasted the log on gist

Hmmm something to do with openssl, which is probably being pulled in by the reqwest dependency.

Really the error is clang-13: error: linker command failed with exit code 1 (use -v to see invocation)..
We'll need to research how to tell the linker to link in openssl.

@terraputix
Copy link

I also just ran into this issue on Fedora Linux when I wanted to test whether it is feasible (in a future) to use cross platform binary artifacts via SPM (compare swiftlang/swift-package-manager#7035, the referenced PR and this POC implementation using binary library artifacts). cargo test --all will report missing symbols for rust-binary-calls-swift-package.

I think, at least part of the problem is due to the bridging-header.h and the compiler flag -import-objc-header which is not supported outside of Apple platforms.

I am wondering if this bridging-header.h is even really needed? Instead, I guess there could be a C-Target that provides the required external definitions instead and separate Swift targets that hold generated code. I adapted the rust-binary-calls-swift-package example this way so you can see what I mean: https://github.com/terraputix/swift-bridge-rust-calls-swift/tree/main/Sources.

@chinedufn
Copy link
Owner

Seems promising.

What are the trade-offs on Apple platforms when using your example approach vs. -import-objc-header?

This will help us understand whether we should guide users towards having platform dependent build process such as using -import-objc-header on Apple platforms and something else on Linux.

If there aren't any meaningful trade-offs then we can consider recommending one cross-platform friendly approach.

@terraputix
Copy link

Let me correct myself: -import-objc-header does work outside of Apple platforms (only the name and that it is not listed in swiftc --help was confusing me), but you can only use it for a normal C header on non Darwin platforms (which you are doing), compare this discussion: https://forums.swift.org/t/using-c-in-swift-away-from-xcode/64784/15.

As far as I can tell from this thread, it is mostly about preferences whether to use a dedicated C target or a bridging header when working with SPM packages. I am not sure how that behaves when working directly with swiftc though. So I guess what you are currently doing is fine.

I eventually found additional linker flags to that enabled me to compile the rust-binary-calls-swift-package example and run it locally on Fedora. Will try to prepare a PR with a fix.

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 a pull request may close this issue.

3 participants