From 9397aa617d0264aa84f1d543d89c76b352cebdbc Mon Sep 17 00:00:00 2001 From: Brendan Allan Date: Fri, 3 Mar 2023 17:22:22 +0800 Subject: [PATCH] Colocate native code + reorganise (#589) colocate native code + all rust in crates/ --- .gitignore | 2 + Cargo.lock | Bin 196091 -> 196270 bytes Cargo.toml | 7 +- apps/desktop/crates/macos/Cargo.toml | 12 ++++ .../native => crates}/macos/Package.resolved | 4 +- .../native => crates}/macos/Package.swift | 4 +- apps/desktop/crates/macos/build.rs | 5 ++ .../macos/src-swift}/webview.swift | 0 .../macos/src-swift}/window.swift | 0 apps/desktop/crates/macos/src/lib.rs | 15 ++++ apps/desktop/src-tauri/Cargo.toml | 6 +- apps/desktop/src-tauri/build.rs | 8 --- .../desktop/src-tauri/native/macos/.gitignore | 9 --- apps/desktop/src-tauri/src/macos/mod.rs | 7 -- apps/desktop/src-tauri/src/macos/native.rs | 3 - apps/desktop/src-tauri/src/macos/webview.rs | 5 -- apps/desktop/src-tauri/src/macos/window.rs | 15 ---- apps/desktop/src-tauri/src/main.rs | 5 +- apps/desktop/src-tauri/src/menu.rs | 4 +- apps/mobile/android/app/build.gradle | 4 +- .../main/java/com/spacedrive/app/SDCore.java | 2 +- .../{rust => crates}/android/Cargo.toml | 4 +- .../{rust => crates}/android/src/lib.rs | 2 +- .../{rust/mobile => crates/core}/Cargo.toml | 2 +- .../{rust/mobile => crates/core}/src/lib.rs | 0 apps/mobile/{rust => crates}/ios/Cargo.toml | 9 ++- apps/mobile/{rust => crates}/ios/src/lib.rs | 2 +- .../ios/Spacedrive.xcodeproj/project.pbxproj | 8 +-- apps/mobile/ios/build-rust.sh | 8 +-- crates/macos/Cargo.toml | 12 ++++ {packages => crates}/macos/Package.resolved | 6 +- {packages => crates}/macos/Package.swift | 14 ++-- crates/macos/build.rs | 7 ++ .../macos/src-swift}/lib.swift | 25 +++---- crates/macos/src/lib.rs | 15 ++++ packages/macos/.gitignore | 7 -- .../xcshareddata/xcschemes/swift-lib.xcscheme | 67 ------------------ packages/macos/README.md | 0 packages/macos/TestPlan.xctestplan | 18 ----- 39 files changed, 120 insertions(+), 203 deletions(-) create mode 100644 apps/desktop/crates/macos/Cargo.toml rename apps/desktop/{src-tauri/native => crates}/macos/Package.resolved (71%) rename apps/desktop/{src-tauri/native => crates}/macos/Package.swift (93%) create mode 100644 apps/desktop/crates/macos/build.rs rename apps/desktop/{src-tauri/native/macos/Sources => crates/macos/src-swift}/webview.swift (100%) rename apps/desktop/{src-tauri/native/macos/Sources => crates/macos/src-swift}/window.swift (100%) create mode 100644 apps/desktop/crates/macos/src/lib.rs delete mode 100644 apps/desktop/src-tauri/native/macos/.gitignore delete mode 100644 apps/desktop/src-tauri/src/macos/mod.rs delete mode 100644 apps/desktop/src-tauri/src/macos/native.rs delete mode 100644 apps/desktop/src-tauri/src/macos/webview.rs delete mode 100644 apps/desktop/src-tauri/src/macos/window.rs rename apps/mobile/{rust => crates}/android/Cargo.toml (74%) rename apps/mobile/{rust => crates}/android/src/lib.rs (99%) rename apps/mobile/{rust/mobile => crates/core}/Cargo.toml (96%) rename apps/mobile/{rust/mobile => crates/core}/src/lib.rs (100%) rename apps/mobile/{rust => crates}/ios/Cargo.toml (87%) rename apps/mobile/{rust => crates}/ios/src/lib.rs (99%) create mode 100644 crates/macos/Cargo.toml rename {packages => crates}/macos/Package.resolved (51%) rename {packages => crates}/macos/Package.swift (76%) create mode 100644 crates/macos/build.rs rename {packages/macos/Sources => crates/macos/src-swift}/lib.swift (93%) create mode 100644 crates/macos/src/lib.rs delete mode 100644 packages/macos/.gitignore delete mode 100644 packages/macos/.swiftpm/xcode/xcshareddata/xcschemes/swift-lib.xcscheme delete mode 100644 packages/macos/README.md delete mode 100644 packages/macos/TestPlan.xctestplan diff --git a/.gitignore b/.gitignore index 652f563a3..d83f99896 100644 --- a/.gitignore +++ b/.gitignore @@ -68,3 +68,5 @@ playwright-report /sdserver_data .spacedrive dev.db-journal +.build/ +.swiftpm diff --git a/Cargo.lock b/Cargo.lock index 4260b405b38ece0f46d6ff53e5c464dcb16139df..4c50f77b8205a63f5e3dedff0aca3663d66d300f 100644 GIT binary patch delta 301 zcmezUn|s|~?uHh|Elk;5+ZB135*c|?Qj4=o@(Xlx6O;2NH`<6!KfuQ%%vxNYnN~7= z;e1Bf?e)A&T8u2Xq&bRHbaV5QGIOTqaxpn^ftjhg$@xXo&$lqCbHZd(rzf&A=}bPb zgMIpZZl*>~sJcvqmD7c|nRM77;*%8w<+m^6V=~g_OVLGk;Piz*nS`0kQj4~Kv1DSE znam>>VxN+1X<(d^oSbA}VU}iYlxCJ{X`EVnPOp*n4FrDXq1>@0+Kc{2a2W| s7#kZT8YiWtCYl+h8cpALn^Anaz;8wl79&GFgUKJK8*hIa#w4~J0JB15iU0rr delta 293 zcmZ4Ym;3i`?uHh|Elk;5-1$YRx`}xyMfsU2(*t>#)TdwKWfI~_1_|fpCuQbLf5*$D z#SZ3Y=1*?45uN^lkI5cc!AE%}ndu4aOl)jW9UJ#Bf>jAmKCpv*decir;pr)SOnKY0 zxR{a{x2y6nsWWa5=V#K_p1$%qBac8?YLSu-mx5Apd1hLPZc*{}RaQ)_GSh1tnFOb= zcVbdWfJx}3XO`F(rIy)R7#pV=StcbK8ylw?rlh4B8yKf1CK?&0q?%hKo1|GJn tauri::Result<()> { #[cfg(target_os = "macos")] { - use macos::*; + use sd_desktop_macos::*; let window = window.ns_window().unwrap(); diff --git a/apps/desktop/src-tauri/src/menu.rs b/apps/desktop/src-tauri/src/menu.rs index 68831467b..54a1f2def 100644 --- a/apps/desktop/src-tauri/src/menu.rs +++ b/apps/desktop/src-tauri/src/menu.rs @@ -108,9 +108,7 @@ pub(crate) fn handle_menu_event(event: WindowMenuEvent) { event .window() .with_webview(|webview| { - use crate::macos::reload_webview; - - unsafe { reload_webview(&(webview.inner() as _)) }; + unsafe { sd_desktop_macos::reload_webview(&(webview.inner() as _)) }; }) .unwrap(); } diff --git a/apps/mobile/android/app/build.gradle b/apps/mobile/android/app/build.gradle index 37a827676..cecfb1815 100644 --- a/apps/mobile/android/app/build.gradle +++ b/apps/mobile/android/app/build.gradle @@ -6,8 +6,8 @@ import org.apache.tools.ant.taskdefs.condition.Os apply plugin: 'org.mozilla.rust-android-gradle.rust-android' cargo { - module = "../../rust/android" - libname = "sd_core_android" + module = "../../crates/android" + libname = "sd_mobile_android" pythonCommand = 'python3' profile = 'release' targets = ["arm", "arm64", "x86", "x86_64"] diff --git a/apps/mobile/android/app/src/main/java/com/spacedrive/app/SDCore.java b/apps/mobile/android/app/src/main/java/com/spacedrive/app/SDCore.java index 9be5d33eb..bc6fe4ef5 100644 --- a/apps/mobile/android/app/src/main/java/com/spacedrive/app/SDCore.java +++ b/apps/mobile/android/app/src/main/java/com/spacedrive/app/SDCore.java @@ -28,7 +28,7 @@ public class SDCore extends ReactContextBaseJavaModule { } static { - System.loadLibrary("sd_core_android"); + System.loadLibrary("sd_mobile_android"); } // is exposed by Rust and is used to register the subscription diff --git a/apps/mobile/rust/android/Cargo.toml b/apps/mobile/crates/android/Cargo.toml similarity index 74% rename from apps/mobile/rust/android/Cargo.toml rename to apps/mobile/crates/android/Cargo.toml index 01c488c76..c39211958 100644 --- a/apps/mobile/rust/android/Cargo.toml +++ b/apps/mobile/crates/android/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "sd-core-android" +name = "sd-mobile-android" version = "0.1.0" edition = "2021" rust-version = "1.64.0" @@ -13,7 +13,7 @@ crate-type = ["cdylib"] jni = "0.19.0" # Core mobile handling stuff -sd-core-mobile = { path = "../mobile", features = ["android"] } +sd-mobile-core = { path = "../core", features = ["android"] } # Other tracing = "0.1.37" diff --git a/apps/mobile/rust/android/src/lib.rs b/apps/mobile/crates/android/src/lib.rs similarity index 99% rename from apps/mobile/rust/android/src/lib.rs rename to apps/mobile/crates/android/src/lib.rs index f337a2bb8..61ae4db12 100644 --- a/apps/mobile/rust/android/src/lib.rs +++ b/apps/mobile/crates/android/src/lib.rs @@ -5,7 +5,7 @@ use jni::{ JNIEnv, }; -use sd_core_mobile::*; +use sd_mobile_core::*; use tracing::error; diff --git a/apps/mobile/rust/mobile/Cargo.toml b/apps/mobile/crates/core/Cargo.toml similarity index 96% rename from apps/mobile/rust/mobile/Cargo.toml rename to apps/mobile/crates/core/Cargo.toml index cf99979f0..16dba9804 100644 --- a/apps/mobile/rust/mobile/Cargo.toml +++ b/apps/mobile/crates/core/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "sd-core-mobile" +name = "sd-mobile-core" version = "0.1.0" edition = "2021" rust-version = "1.64.0" diff --git a/apps/mobile/rust/mobile/src/lib.rs b/apps/mobile/crates/core/src/lib.rs similarity index 100% rename from apps/mobile/rust/mobile/src/lib.rs rename to apps/mobile/crates/core/src/lib.rs diff --git a/apps/mobile/rust/ios/Cargo.toml b/apps/mobile/crates/ios/Cargo.toml similarity index 87% rename from apps/mobile/rust/ios/Cargo.toml rename to apps/mobile/crates/ios/Cargo.toml index 26b034bad..1f67d3cf9 100644 --- a/apps/mobile/rust/ios/Cargo.toml +++ b/apps/mobile/crates/ios/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "sd-core-ios" +name = "sd-mobile-ios" version = "0.1.0" edition = "2021" @@ -11,13 +11,12 @@ rust-version = "1.64.0" # which are only available when linking against the app's ObjC crate-type = ["staticlib"] +[dependencies] +# Core mobile handling stuff +sd-mobile-core = { path = "../core" } [target.'cfg(target_os = "ios")'.dependencies] # FFI objc = "0.2.7" objc_id = "0.1.1" objc-foundation = "0.1.1" - -[dependencies] -# Core mobile handling stuff -sd-core-mobile = { path = "../mobile" } diff --git a/apps/mobile/rust/ios/src/lib.rs b/apps/mobile/crates/ios/src/lib.rs similarity index 99% rename from apps/mobile/rust/ios/src/lib.rs rename to apps/mobile/crates/ios/src/lib.rs index 5fe98fb80..6127afb4f 100644 --- a/apps/mobile/rust/ios/src/lib.rs +++ b/apps/mobile/crates/ios/src/lib.rs @@ -10,7 +10,7 @@ use objc::{msg_send, runtime::Object, sel, sel_impl}; use objc_foundation::{INSString, NSString}; use objc_id::Id; -use sd_core_mobile::*; +use sd_mobile_core::*; extern "C" { fn get_data_directory() -> *const c_char; diff --git a/apps/mobile/ios/Spacedrive.xcodeproj/project.pbxproj b/apps/mobile/ios/Spacedrive.xcodeproj/project.pbxproj index 779bfef57..cfcd470b9 100644 --- a/apps/mobile/ios/Spacedrive.xcodeproj/project.pbxproj +++ b/apps/mobile/ios/Spacedrive.xcodeproj/project.pbxproj @@ -420,13 +420,13 @@ "$(inherited)", "-ObjC", "-lc++", - "-lsd_core_ios-ios", + "-lsd_mobile_ios-ios", ); "OTHER_LDFLAGS[sdk=iphonesimulator*]" = ( "$(inherited)", "-ObjC", "-lc++", - "-lsd_core_ios-iossim", + "-lsd_mobile_ios-iossim", ); OTHER_SWIFT_FLAGS = "$(inherited) -D EXPO_CONFIGURATION_DEBUG"; PRODUCT_BUNDLE_IDENTIFIER = com.spacedrive.app; @@ -515,13 +515,13 @@ "$(inherited)", "-ObjC", "-lc++", - "-lsd_core_ios-ios", + "-lsd_mobile_ios-ios", ); "OTHER_LDFLAGS[sdk=iphonesimulator*]" = ( "$(inherited)", "-ObjC", "-lc++", - "-lsd_core_ios-iossim", + "-lsd_mobile_ios-iossim", ); OTHER_SWIFT_FLAGS = "$(inherited) -D EXPO_CONFIGURATION_RELEASE"; PRODUCT_BUNDLE_IDENTIFIER = com.spacedrive.app; diff --git a/apps/mobile/ios/build-rust.sh b/apps/mobile/ios/build-rust.sh index d1c07c290..31021c885 100755 --- a/apps/mobile/ios/build-rust.sh +++ b/apps/mobile/ios/build-rust.sh @@ -11,9 +11,9 @@ fi if [[ $PLATFORM_NAME = "iphonesimulator" ]] then - cargo build -p sd-core-ios --target aarch64-apple-ios-sim - lipo -create -output $TARGET_DIRECTORY/libsd_core_ios-iossim.a $TARGET_DIRECTORY/aarch64-apple-ios-sim/debug/libsd_core_ios.a + cargo build -p sd-mobile-ios --target aarch64-apple-ios-sim + lipo -create -output $TARGET_DIRECTORY/libsd_mobile_ios-iossim.a $TARGET_DIRECTORY/aarch64-apple-ios-sim/debug/libsd_mobile_ios.a else - cargo build -p sd-core-ios --target aarch64-apple-ios - lipo -create -output $TARGET_DIRECTORY/libsd_core_ios-ios.a $TARGET_DIRECTORY/aarch64-apple-ios/debug/libsd_core_ios.a + cargo build -p sd-mobile-ios --target aarch64-apple-ios + lipo -create -output $TARGET_DIRECTORY/libsd_mobile_ios-ios.a $TARGET_DIRECTORY/aarch64-apple-ios/debug/libsd_mobile_ios.a fi diff --git a/crates/macos/Cargo.toml b/crates/macos/Cargo.toml new file mode 100644 index 000000000..6eca31044 --- /dev/null +++ b/crates/macos/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "sd-macos" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +swift-rs.workspace = true + +[build-dependencies] +swift-rs = { workspace = true, features = ["build"] } diff --git a/packages/macos/Package.resolved b/crates/macos/Package.resolved similarity index 51% rename from packages/macos/Package.resolved rename to crates/macos/Package.resolved index 5b52f2f7e..e331c6992 100644 --- a/packages/macos/Package.resolved +++ b/crates/macos/Package.resolved @@ -3,11 +3,11 @@ "pins": [ { "package": "SwiftRs", - "repositoryURL": "https://github.com/Brendonovich/swift-rs", + "repositoryURL": "https://github.com/brendonovich/swift-rs", "state": { "branch": null, - "revision": "e47a9e8186f6e25be6fae40d3cc3c272abea63a2", - "version": "0.3.0" + "revision": "e30e6f539898248ab9387b0e21b79167729144b6", + "version": "1.0.0" } } ] diff --git a/packages/macos/Package.swift b/crates/macos/Package.swift similarity index 76% rename from packages/macos/Package.swift rename to crates/macos/Package.swift index 2f5d32e7f..72f4cde06 100644 --- a/packages/macos/Package.swift +++ b/crates/macos/Package.swift @@ -4,28 +4,28 @@ import PackageDescription let package = Package( - name: "swift-lib", + name: "sd-macos", platforms: [ .macOS(.v11), ], products: [ // Products define the executables and libraries a package produces, and make them visible to other packages. .library( - name: "swift-lib", + name: "sd-macos", type: .static, - targets: ["swift-lib"]), + targets: ["sd-macos"]), ], dependencies: [ // Dependencies declare other packages that this package depends on. // .package(url: /* package url */, from: "1.0.0"), - .package(url: "https://github.com/brendonovich/swift-rs", revision: "c3003bc0c28a6742d3da341b61887d8e072fda0a"), + .package(url: "https://github.com/brendonovich/swift-rs", from: "1.0.0"), ], targets: [ // Targets are the basic building blocks of a package. A target can define a module or a test suite. // Targets can depend on other targets in this package, and on products in packages this package depends on. .target( - name: "swift-lib", - dependencies: [.product(name: "SwiftRs", package: "SwiftRs")], - path: "Sources") + name: "sd-macos", + dependencies: [.product(name: "SwiftRs", package: "swift-rs")], + path: "src-swift") ] ) diff --git a/crates/macos/build.rs b/crates/macos/build.rs new file mode 100644 index 000000000..d8f64a4ea --- /dev/null +++ b/crates/macos/build.rs @@ -0,0 +1,7 @@ +use swift_rs::SwiftLinker; + +fn main() { + SwiftLinker::new("10.15") + .with_package("sd-macos", "./") + .link() +} diff --git a/packages/macos/Sources/lib.swift b/crates/macos/src-swift/lib.swift similarity index 93% rename from packages/macos/Sources/lib.swift rename to crates/macos/src-swift/lib.swift index b96f8378d..5b425579c 100644 --- a/packages/macos/Sources/lib.swift +++ b/crates/macos/src-swift/lib.swift @@ -3,8 +3,8 @@ import AppKit import SwiftRs @_cdecl("get_file_thumbnail_base64") -public func getFileThumbnailBase64(path: UnsafePointer) -> SRString { - let path = path.to_string(); +public func getFileThumbnailBase64(path: SRString) -> SRString { + let path = path.toString(); let image = NSWorkspace.shared.icon(forFile: path) let bitmap = NSBitmapImageRep(data: image.tiffRepresentation!)!.representation(using: .png, properties: [:])! @@ -13,6 +13,14 @@ public func getFileThumbnailBase64(path: UnsafePointer) -> SRString { } class Volume: NSObject { + var name: SRString + var path: SRString + var total_capacity: Int + var available_capacity: Int + var is_removable: Bool + var is_ejectable: Bool + var is_root_filesystem: Bool + internal init(name: String, path: String, total_capacity: Int, available_capacity: Int, is_removable: Bool, is_ejectable: Bool, is_root_filesystem: Bool) { self.name = SRString(name) self.path = SRString(path) @@ -22,14 +30,6 @@ class Volume: NSObject { self.is_ejectable = is_ejectable self.is_root_filesystem = is_root_filesystem } - - var name: SRString - var path: SRString - var total_capacity: Int - var available_capacity: Int - var is_removable: Bool - var is_ejectable: Bool - var is_root_filesystem: Bool } @_cdecl("get_mounts") @@ -69,8 +69,5 @@ public func getMounts() -> SRObjectArray { } } - return toRust(SRObjectArray(validMounts)) + return SRObjectArray(validMounts) } - - - diff --git a/crates/macos/src/lib.rs b/crates/macos/src/lib.rs new file mode 100644 index 000000000..9de08a515 --- /dev/null +++ b/crates/macos/src/lib.rs @@ -0,0 +1,15 @@ +use swift_rs::*; + +#[repr(C)] +pub struct Volume { + name: SRString, + path: SRString, + total_capacity: Int, + available_capacity: Int, + is_removable: Bool, + is_ejectable: Bool, + is_root_filesystem: Bool, +} + +swift!(pub fn get_file_thumbnail_base64(name: &SRString) -> SRString); +swift!(pub fn get_mounts() -> SRObjectArray); diff --git a/packages/macos/.gitignore b/packages/macos/.gitignore deleted file mode 100644 index bb460e7be..000000000 --- a/packages/macos/.gitignore +++ /dev/null @@ -1,7 +0,0 @@ -.DS_Store -/.build -/Packages -/*.xcodeproj -xcuserdata/ -DerivedData/ -.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata diff --git a/packages/macos/.swiftpm/xcode/xcshareddata/xcschemes/swift-lib.xcscheme b/packages/macos/.swiftpm/xcode/xcshareddata/xcschemes/swift-lib.xcscheme deleted file mode 100644 index ceced7f57..000000000 --- a/packages/macos/.swiftpm/xcode/xcshareddata/xcschemes/swift-lib.xcscheme +++ /dev/null @@ -1,67 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/packages/macos/README.md b/packages/macos/README.md deleted file mode 100644 index e69de29bb..000000000 diff --git a/packages/macos/TestPlan.xctestplan b/packages/macos/TestPlan.xctestplan deleted file mode 100644 index 3cb59c258..000000000 --- a/packages/macos/TestPlan.xctestplan +++ /dev/null @@ -1,18 +0,0 @@ -{ - "configurations" : [ - { - "id" : "217668D6-0AB1-4124-9035-478908965985", - "name" : "Configuration 1", - "options" : { - - } - } - ], - "defaultOptions" : { - "testTimeoutsEnabled" : true - }, - "testTargets" : [ - - ], - "version" : 1 -}