From 2a22b2830192e3aaefdc5e42ad801a9d5ecb3c83 Mon Sep 17 00:00:00 2001 From: Ziyang Hu Date: Sat, 19 Nov 2022 16:03:00 +0800 Subject: [PATCH] swift lib --- cozo-lib-swift/.gitignore | 3 +++ cozo-lib-swift/Cargo.toml | 19 +++++++++++++++++++ cozo-lib-swift/build-rust.sh | 35 +++++++++++++++++++++++++++++++++++ cozo-lib-swift/build.rs | 13 +++++++++++++ cozo-lib-swift/src/lib.rs | 10 ++++++++++ 5 files changed, 80 insertions(+) create mode 100644 cozo-lib-swift/.gitignore create mode 100644 cozo-lib-swift/Cargo.toml create mode 100755 cozo-lib-swift/build-rust.sh create mode 100644 cozo-lib-swift/build.rs create mode 100644 cozo-lib-swift/src/lib.rs diff --git a/cozo-lib-swift/.gitignore b/cozo-lib-swift/.gitignore new file mode 100644 index 00000000..2314bc5a --- /dev/null +++ b/cozo-lib-swift/.gitignore @@ -0,0 +1,3 @@ +generated +CozoSwiftBridge +.idea \ No newline at end of file diff --git a/cozo-lib-swift/Cargo.toml b/cozo-lib-swift/Cargo.toml new file mode 100644 index 00000000..7f1c66ce --- /dev/null +++ b/cozo-lib-swift/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "cozo-swift" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["staticlib"] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[features] +storage-rocksdb = ["cozo/storage-rocksdb"] + +[build-dependencies] +swift-bridge-build = "0.1.41" + +[dependencies] +swift-bridge = "0.1.41" +cozo = { version = "0.1.7", path = "../cozo-core", default-features = false, features = ["compact"] } diff --git a/cozo-lib-swift/build-rust.sh b/cozo-lib-swift/build-rust.sh new file mode 100755 index 00000000..ef0b3ab5 --- /dev/null +++ b/cozo-lib-swift/build-rust.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash +set -e + +THISDIR=$(dirname $0) +cd $THISDIR + +export SWIFT_BRIDGE_OUT_DIR="$(pwd)/generated" +# Build the project for the desired platforms: +cargo build -p cozo-swift --target x86_64-apple-darwin --release +cargo build -p cozo-swift --target aarch64-apple-darwin --release +mkdir -p ../target/universal-macos/release + +lipo \ + ../target/aarch64-apple-darwin/release/libcozo_swift.a \ + ../target/x86_64-apple-darwin/release/libcozo_swift.a -create -output \ + ../target/universal-macos/release/libcozo_swift.a + +cargo build -p cozo-swift --target aarch64-apple-ios --release + +cargo build -p cozo-swift --target x86_64-apple-ios --release +cargo build -p cozo-swift --target aarch64-apple-ios-sim --release +mkdir -p ../target/universal-ios/release + +lipo \ + ../target/aarch64-apple-ios-sim/release/libcozo_swift.a \ + ../target/x86_64-apple-ios/release/libcozo_swift.a -create -output \ + ../target/universal-ios/release/libcozo_swift.a + +swift-bridge-cli create-package \ + --bridges-dir ./generated \ + --out-dir CozoSwiftBridge \ + --ios ../target/aarch64-apple-ios/release/libcozo_swift.a \ + --simulator ../target/universal-ios/release/libcozo_swift.a \ + --macos ../target/universal-macos/release/libcozo_swift.a \ + --name CozoSwiftBridge \ No newline at end of file diff --git a/cozo-lib-swift/build.rs b/cozo-lib-swift/build.rs new file mode 100644 index 00000000..56ecb926 --- /dev/null +++ b/cozo-lib-swift/build.rs @@ -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")); +} \ No newline at end of file diff --git a/cozo-lib-swift/src/lib.rs b/cozo-lib-swift/src/lib.rs new file mode 100644 index 00000000..0ad74fcb --- /dev/null +++ b/cozo-lib-swift/src/lib.rs @@ -0,0 +1,10 @@ +#[swift_bridge::bridge] +mod ffi { + extern "Rust" { + fn hello_rust(name: &str) -> String; + } +} + +fn hello_rust(name: &str) -> String { + String::from(format!("Hello {} from Rust!", name)) +} \ No newline at end of file