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

Make rust-binary-calls-swift-package example support Linux #297

Conversation

terraputix
Copy link
Contributor

Closes #107.

}

fn compile_swift() {
let swift_package_dir = manifest_dir().join("swift-library");

let mut cmd = Command::new("swift");

cmd.current_dir(swift_package_dir)
.arg("build")
.args(&["-Xswiftc", "-static"])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed this because the Package.swift already defines this as a static library.

Comment on lines +20 to +21
// This fix is for macOS only
#[cfg(target_os = "macos")]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this is also required for other Apple platforms?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, but the line "cargo:rustc-link-search={}/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/ suggests that it's macos only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, maybe it is problematic in cross-compilation scenarios?

Copy link
Owner

@chinedufn chinedufn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Left some minor feedback.

There have been multiple issues/PRs lately about Linux support.
There's at least some level of demand for swift-bridge to support Swift+Linux.
This PR would be helpful.

Hopefully you're still able to complete it after such a long delay. I can review your fixes quickly.

Thanks for working on this.

Comment on lines +20 to +21
// This fix is for macOS only
#[cfg(target_os = "macos")]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, but the line "cargo:rustc-link-search={}/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/macosx/ suggests that it's macos only.

examples/rust-binary-calls-swift-package/build.rs Outdated Show resolved Hide resolved
}
}

fn get_swift_lib_path() -> Result<String, String> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a default path for the Swift library?

If so, instead of spawning a which command can we just use the default path, and if it doesn't exist we can panic and tell the user to set a SWIFT_LIBRARY_PATH=... environment variable that points to "explanation of what to point to here".

This way:

  • we avoid spawning an extra command
  • the example more clearly explains its dependencies

Right now this is a bit magical, whereas an example should be more explicit and help the user understand exactly what's happening.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I adapted the code to use /usr/lib/swift/linux as a default path, if not otherwise configured by a user via the SWIFT_LIBRARY_PATH environment variable.

examples/rust-binary-calls-swift-package/build.rs Outdated Show resolved Hide resolved
// This fix is for Linux only
#[cfg(target_os = "linux")]
{
// Without this there will be a lot of missing symbols when linking to the Swift library.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give an example of one or a few of the errors?

This helps someone who has run into that error find this example when they search online.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added an example.

@chinedufn chinedufn changed the title fix: rust-binary-calls-swift-package build on Linux Make rust-binary-calls-swift-package example run on Linux Jan 30, 2025
@chinedufn chinedufn changed the title Make rust-binary-calls-swift-package example run on Linux Make rust-binary-calls-swift-package example support Linux Jan 30, 2025
@chinedufn chinedufn added the linux-support Supporting using `swift-bridge` for Linux targets label Jan 30, 2025
@terraputix
Copy link
Contributor Author

I had another look at this PR and got it working on a newer Swift version (most likely I still used 5.9 or 5.10 when submitting this PR).
This fix gets the job done on my machine, but I fear it might not be the most generic fix.

I was also quickly trying to get this built correctly using the static-linux-sdk, but I had no luck with that.

I guess the real issue is that Swift Package Manager does not correctly support static linking for libraries, but only for executables, as this recent issue suggests: swiftlang/swift-package-manager#8198

@chinedufn
Copy link
Owner

Got it. Well, before this PR a user couldn't run on Linux at all, so this is an improvement.

If people have problems with these instructions they can open issues and we can try to continue to make the example to work on more Swift versions.

Thanks for landing your first contribution. People will appreciate this example.

@chinedufn chinedufn merged commit 0a0b44e into chinedufn:master Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
linux-support Supporting using `swift-bridge` for Linux targets
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rust-binary-calls-swift-package example doesn't work on a Linux environment
2 participants