-
Notifications
You must be signed in to change notification settings - Fork 63
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 extern Swift functions public to prevent stripping in Release builds #262
Draft
jmp-0x7C0
wants to merge
3
commits into
chinedufn:master
Choose a base branch
from
jmp-0x7C0:fix-release-builds
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
generated/* | ||
.build/* | ||
target/* | ||
Cargo.lock | ||
TestReleaseFails/* |
20 changes: 20 additions & 0 deletions
20
SwiftRustIntegrationTestRunner/test-release-fails/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[package] | ||
name = "test-release-fails" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = [] | ||
|
||
build = "build.rs" | ||
|
||
[lib] | ||
crate-type = ["staticlib"] | ||
|
||
[build-dependencies] | ||
swift-bridge-build = { git = "https://github.com/chinedufn/swift-bridge.git", branch = "master" } | ||
|
||
[dependencies] | ||
swift-bridge = { git = "https://github.com/chinedufn/swift-bridge.git", branch = "master", features = [ | ||
"async", | ||
] } | ||
|
||
[workspace] |
26 changes: 26 additions & 0 deletions
26
SwiftRustIntegrationTestRunner/test-release-fails/Package.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// swift-tools-version:5.5.0 | ||
import PackageDescription | ||
let package = Package( | ||
name: "TestReleaseFails", | ||
products: [ | ||
.library( | ||
name: "TestReleaseFails", | ||
targets: ["TestReleaseFails"]), | ||
.executable( | ||
name: "TestReleaseFailsRunner", | ||
targets: ["TestReleaseFailsRunner"]), | ||
], | ||
dependencies: [], | ||
targets: [ | ||
.binaryTarget( | ||
name: "RustXcframework", | ||
path: "RustXcframework.xcframework" | ||
), | ||
.target( | ||
name: "TestReleaseFails", | ||
dependencies: ["RustXcframework"]), | ||
.executableTarget( | ||
name: "TestReleaseFailsRunner", | ||
dependencies: ["TestReleaseFails"]) | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Test Release Builds Fail | ||
|
||
This test is a reproduction case for when building for release, | ||
previously as `extern "Swift"` functions were `internal` | ||
the Swift compiler would strip these, as it would infer | ||
that they were dead code, even though these need to be accessible | ||
from the Rust side of the FFI. |
5 changes: 5 additions & 0 deletions
5
SwiftRustIntegrationTestRunner/test-release-fails/SwiftExterns.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Foundation | ||
|
||
func add(lhs: Int32, rhs: Int32) -> Int32 { | ||
lhs + rhs | ||
} |
13 changes: 13 additions & 0 deletions
13
SwiftRustIntegrationTestRunner/test-release-fails/build.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use std::path::PathBuf; | ||
|
||
fn main() { | ||
let out_dir = PathBuf::from("./generated"); | ||
|
||
let bridges = vec!["src/lib.rs"]; | ||
for path in &bridges { | ||
println!("cargo:rerun-if-changed={}", path); | ||
} | ||
|
||
swift_bridge_build::parse_bridges(bridges) | ||
.write_all_concatenated(out_dir, env!("CARGO_PKG_NAME")); | ||
} |
44 changes: 44 additions & 0 deletions
44
SwiftRustIntegrationTestRunner/test-release-fails/build.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/bin/bash | ||
|
||
PACKAGE_NAME=test-release-fails | ||
LIBRARY_NAME=test_release_fails | ||
SWIFT_PACKAGE_NAME=TestReleaseFails | ||
EXECUTABLE_TARGET_NAME="$SWIFT_PACKAGE_NAME"Runner | ||
|
||
set -e | ||
|
||
THISDIR=$(dirname $0) | ||
cd $THISDIR | ||
|
||
echo "Building for macOS..." | ||
rustup target add x86_64-apple-darwin aarch64-apple-darwin | ||
cargo build --target x86_64-apple-darwin --release | ||
cargo build --target aarch64-apple-darwin --release | ||
mkdir -p ./target/universal-macos/release | ||
lipo \ | ||
./target/aarch64-apple-darwin/release/lib$LIBRARY_NAME.a \ | ||
./target/x86_64-apple-darwin/release/lib$LIBRARY_NAME.a -create -output \ | ||
./target/universal-macos/release/lib$LIBRARY_NAME.a | ||
|
||
function create_package { | ||
swift-bridge-cli create-package \ | ||
--bridges-dir ./generated \ | ||
--out-dir $SWIFT_PACKAGE_NAME \ | ||
--ios ./target/universal-macos/release/lib$LIBRARY_NAME.a \ | ||
--name $SWIFT_PACKAGE_NAME | ||
} | ||
|
||
function patch_package { | ||
cp Package.swift $SWIFT_PACKAGE_NAME/Package.swift | ||
mkdir -p $SWIFT_PACKAGE_NAME/Sources/$EXECUTABLE_TARGET_NAME | ||
cp main.swift $SWIFT_PACKAGE_NAME/Sources/$EXECUTABLE_TARGET_NAME/main.swift | ||
cp SwiftExterns.swift $SWIFT_PACKAGE_NAME/Sources/$SWIFT_PACKAGE_NAME/SwiftExterns.swift | ||
} | ||
|
||
echo "Creating Swift package..." | ||
create_package | ||
patch_package | ||
|
||
echo "Building for release..." | ||
cd $SWIFT_PACKAGE_NAME | ||
xcodebuild archive -scheme $EXECUTABLE_TARGET_NAME -archivePath ./build/$EXECUTABLE_TARGET_NAME.xcarchive -destination "platform=macOS" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import Foundation | ||
import TestReleaseFails | ||
|
||
call_swift_add() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe Right now it seems like we're calling a Swift function. |
14 changes: 14 additions & 0 deletions
14
SwiftRustIntegrationTestRunner/test-release-fails/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#[swift_bridge::bridge] | ||
mod ffi { | ||
extern "Rust" { | ||
fn call_swift_add(); | ||
} | ||
|
||
extern "Swift" { | ||
fn add(lhs: i32, rhs: i32) -> i32; | ||
} | ||
} | ||
|
||
fn call_swift_add() { | ||
assert!(ffi::add(1, 1) == 2); | ||
} |
5 changes: 5 additions & 0 deletions
5
SwiftRustIntegrationTestRunner/test-release-succeeds/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
generated/* | ||
.build/* | ||
target/* | ||
Cargo.lock | ||
TestReleaseSucceeds/* |
18 changes: 18 additions & 0 deletions
18
SwiftRustIntegrationTestRunner/test-release-succeeds/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "test-release-succeeds" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = [] | ||
|
||
build = "build.rs" | ||
|
||
[lib] | ||
crate-type = ["staticlib"] | ||
|
||
[build-dependencies] | ||
swift-bridge-build = { path = "../../crates/swift-bridge-build" } | ||
|
||
[dependencies] | ||
swift-bridge = { path = "../../", features = ["async"] } | ||
|
||
[workspace] |
26 changes: 26 additions & 0 deletions
26
SwiftRustIntegrationTestRunner/test-release-succeeds/Package.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// swift-tools-version:5.5.0 | ||
import PackageDescription | ||
let package = Package( | ||
name: "TestReleaseSucceeds", | ||
products: [ | ||
.library( | ||
name: "TestReleaseSucceeds", | ||
targets: ["TestReleaseSucceeds"]), | ||
.executable( | ||
name: "TestReleaseSucceedsRunner", | ||
targets: ["TestReleaseSucceedsRunner"]), | ||
], | ||
dependencies: [], | ||
targets: [ | ||
.binaryTarget( | ||
name: "RustXcframework", | ||
path: "RustXcframework.xcframework" | ||
), | ||
.target( | ||
name: "TestReleaseSucceeds", | ||
dependencies: ["RustXcframework"]), | ||
.executableTarget( | ||
name: "TestReleaseSucceedsRunner", | ||
dependencies: ["TestReleaseSucceeds"]) | ||
] | ||
) |
7 changes: 7 additions & 0 deletions
7
SwiftRustIntegrationTestRunner/test-release-succeeds/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Test Release | ||
|
||
This test makes sure that when building for release, | ||
the Swift compiler doesn't strip functions that need to be accessible | ||
from the Rust side of the FFI. | ||
This test verifies that by making `extern "Swift"` functions `public` | ||
release builds now succeed. |
5 changes: 5 additions & 0 deletions
5
SwiftRustIntegrationTestRunner/test-release-succeeds/SwiftExterns.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import Foundation | ||
|
||
func add(lhs: Int32, rhs: Int32) -> Int32 { | ||
lhs + rhs | ||
} |
13 changes: 13 additions & 0 deletions
13
SwiftRustIntegrationTestRunner/test-release-succeeds/build.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use std::path::PathBuf; | ||
|
||
fn main() { | ||
let out_dir = PathBuf::from("./generated"); | ||
|
||
let bridges = vec!["src/lib.rs"]; | ||
for path in &bridges { | ||
println!("cargo:rerun-if-changed={}", path); | ||
} | ||
|
||
swift_bridge_build::parse_bridges(bridges) | ||
.write_all_concatenated(out_dir, env!("CARGO_PKG_NAME")); | ||
} |
44 changes: 44 additions & 0 deletions
44
SwiftRustIntegrationTestRunner/test-release-succeeds/build.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#!/bin/bash | ||
|
||
PACKAGE_NAME=test-release-succeeds | ||
LIBRARY_NAME=test_release_succeeds | ||
SWIFT_PACKAGE_NAME=TestReleaseSucceeds | ||
EXECUTABLE_TARGET_NAME="$SWIFT_PACKAGE_NAME"Runner | ||
|
||
set -e | ||
|
||
THISDIR=$(dirname $0) | ||
cd $THISDIR | ||
|
||
echo "Building for macOS..." | ||
rustup target add x86_64-apple-darwin aarch64-apple-darwin | ||
cargo build --target x86_64-apple-darwin --release | ||
cargo build --target aarch64-apple-darwin --release | ||
mkdir -p ./target/universal-macos/release | ||
lipo \ | ||
./target/aarch64-apple-darwin/release/lib$LIBRARY_NAME.a \ | ||
./target/x86_64-apple-darwin/release/lib$LIBRARY_NAME.a -create -output \ | ||
./target/universal-macos/release/lib$LIBRARY_NAME.a | ||
|
||
function create_package { | ||
swift-bridge-cli create-package \ | ||
--bridges-dir ./generated \ | ||
--out-dir $SWIFT_PACKAGE_NAME \ | ||
--ios ./target/universal-macos/release/lib$LIBRARY_NAME.a \ | ||
--name $SWIFT_PACKAGE_NAME | ||
} | ||
|
||
function patch_package { | ||
cp Package.swift $SWIFT_PACKAGE_NAME/Package.swift | ||
mkdir -p $SWIFT_PACKAGE_NAME/Sources/$EXECUTABLE_TARGET_NAME | ||
cp main.swift $SWIFT_PACKAGE_NAME/Sources/$EXECUTABLE_TARGET_NAME/main.swift | ||
cp SwiftExterns.swift $SWIFT_PACKAGE_NAME/Sources/$SWIFT_PACKAGE_NAME/SwiftExterns.swift | ||
} | ||
|
||
echo "Creating Swift package..." | ||
create_package | ||
patch_package | ||
|
||
echo "Building for release..." | ||
cd $SWIFT_PACKAGE_NAME | ||
xcodebuild archive -scheme $EXECUTABLE_TARGET_NAME -archivePath ./build/$EXECUTABLE_TARGET_NAME.xcarchive -destination "platform=macOS" |
4 changes: 4 additions & 0 deletions
4
SwiftRustIntegrationTestRunner/test-release-succeeds/main.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import Foundation | ||
import TestReleaseSucceeds | ||
|
||
call_swift_add() |
14 changes: 14 additions & 0 deletions
14
SwiftRustIntegrationTestRunner/test-release-succeeds/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#[swift_bridge::bridge] | ||
mod ffi { | ||
extern "Rust" { | ||
fn call_swift_add(); | ||
} | ||
|
||
extern "Swift" { | ||
fn add(lhs: i32, rhs: i32) -> i32; | ||
} | ||
} | ||
|
||
fn call_swift_add() { | ||
assert!(ffi::add(1, 1) == 2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's document:
Why it would infer that they were dead code (as in, mention
func
vs.public func
)show an example of a problematic
cdecl
function. Explain that we now prependpublic
.How the test works. How are we accomplishing what you've explained here? Maybe explain how the test works step by step.
How to run the test.
Mention that this test gets ran during
test-swift-rust-integration.sh
All of this will help future maintainers understand what's going on and be in a better position to make changes/improvements to it.