Skip to content

Commit

Permalink
Use native assets on iOS and macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
temeddix committed Sep 10, 2024
1 parent c227891 commit c8fcaee
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 32 deletions.
2 changes: 2 additions & 0 deletions flutter_ffi_plugin/example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
*.swp
.DS_Store
.atom/
.build/
.buildlog/
.history
.svn/
.swiftpm/
migrate_working_dir/

# IntelliJ related
Expand Down
2 changes: 1 addition & 1 deletion flutter_ffi_plugin/example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key>
<string>Rust In Flutter</string>
<string>Rinf Example</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
Expand Down
4 changes: 4 additions & 0 deletions flutter_ffi_plugin/example/macos/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ class AppDelegate: FlutterAppDelegate {
override func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
return true
}

override func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub async fn run_debug_tests() -> Result<()> {
// Fetch data from a web API.
let url = "http://jsonplaceholder.typicode.com/todos/1";
let web_response = sample_crate::fetch_from_web_api(url).await?;
debug_print!("Response from a web API: {web_response:?}");
debug_print!("Response from a web API: {web_response}");

// Use a crate that accesses operating system APIs.
let hwid = sample_crate::get_hardward_id()?;
Expand Down
14 changes: 12 additions & 2 deletions flutter_ffi_plugin/hook/build.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@ void main(List<String> args) async {
// Get the crate path.
Directory currentDirectory = Directory.current;
final crateUri = currentDirectory.uri.resolve("native/hub");
final cratePath = crateUri.toFilePath();
var cratePath = crateUri.toFilePath();
if (!cratePath.startsWith("/")) {
// Character `/` makes it act as an absolute path
// even when passed into `Uri.resolve`.
// Not needed on unix-based, but needed on Windows.
cratePath = "/$cratePath";
}

await build(args, (BuildConfig buildConfig, BuildOutput output) async {
if (buildConfig.dryRun) {
return;
}
final builder = RustBuilder(
// The ID of native assets consists of package name and crate name.
package: 'rinf',
cratePath: '/$cratePath', // Character `/` makes it a absolute path.
cratePath: cratePath,
buildConfig: buildConfig,
release: buildConfig.buildMode == BuildMode.release,
);
await builder.run(output: output);
});
Expand Down
13 changes: 0 additions & 13 deletions flutter_ffi_plugin/ios/rinf.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,8 @@ Pod::Spec.new do |s|
s.platform = :ios, '11.0'
s.swift_version = '5.0'

# Include Rust crates in the build process.
s.script_phase = {
:name => 'Build a Rust library',
:script => 'sh ${PODS_TARGET_SRCROOT}/../cargokit/build_pod.sh ${PROJECT_DIR}/../../native/hub hub',
:execution_position=> :before_compile,
:input_files => ['${BUILT_PRODUCTS_DIR}/cargokit_phony'],
# Let XCode know that the static library referenced in -force_load below is
# created by this build step.
:output_files => ["${BUILT_PRODUCTS_DIR}/libhub.a"],
}
s.pod_target_xcconfig = {
'DEFINES_MODULE' => 'YES',
# Flutter framework does not contain a i386 slice. From default Flutter template.
'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386',
# We use `-force_load` instead of `-l` since Xcode strips out unused symbols from static libraries.
'OTHER_LDFLAGS' => '-force_load ${BUILT_PRODUCTS_DIR}/libhub.a -Wl -undefined dynamic_lookup',
}
end
4 changes: 2 additions & 2 deletions flutter_ffi_plugin/lib/src/load_os.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ void loadRustLibrary() {
} else if (io.Platform.isWindows) {
DynamicLibrary.open('hub.dll');
} else if (io.Platform.isIOS) {
DynamicLibrary.open('rinf.framework/rinf');
// DynamicLibrary.open('rinf.framework/rinf');
} else if (io.Platform.isMacOS) {
DynamicLibrary.open('rinf.framework/rinf');
// DynamicLibrary.open('rinf.framework/rinf');
} else {
throw UnsupportedError('This operating system is not supported.');
}
Expand Down
13 changes: 0 additions & 13 deletions flutter_ffi_plugin/macos/rinf.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,8 @@ Pod::Spec.new do |s|
s.platform = :osx, '10.11'
s.swift_version = '5.0'

# Include Rust crates in the build process.
s.script_phase = {
:name => 'Build a Rust library',
:script => 'sh ${PODS_TARGET_SRCROOT}/../cargokit/build_pod.sh ${PROJECT_DIR}/../../native/hub hub',
:execution_position=> :before_compile,
:input_files => ['${BUILT_PRODUCTS_DIR}/cargokit_phony'],
# Let XCode know that the static library referenced in -force_load below is
# created by this build step.
:output_files => ["${BUILT_PRODUCTS_DIR}/libhub.a"],
}
s.pod_target_xcconfig = {
'DEFINES_MODULE' => 'YES',
# Flutter.framework does not contain a i386 slice.
'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'i386',
# We use `-force_load` instead of `-l` since Xcode strips out unused symbols from static libraries.
'OTHER_LDFLAGS' => '-force_load ${BUILT_PRODUCTS_DIR}/libhub.a -Wl -undefined dynamic_lookup',
}
end

0 comments on commit c8fcaee

Please sign in to comment.