From a26d4b0122f30b958b417ed92d43f823fa89adb8 Mon Sep 17 00:00:00 2001 From: leeeon233 Date: Sat, 28 Jan 2023 19:59:37 +0800 Subject: [PATCH] feat: init ffi --- Cargo.lock | 50 +++++++++++++++++++++++++++++++++++ crates/loro-ffi/Cargo.toml | 18 +++++++++++++ crates/loro-ffi/build.rs | 35 ++++++++++++++++++++++++ crates/loro-ffi/cbindgen.toml | 3 +++ crates/loro-ffi/src/lib.rs | 15 +++++++++++ 5 files changed, 121 insertions(+) create mode 100644 crates/loro-ffi/Cargo.toml create mode 100644 crates/loro-ffi/build.rs create mode 100644 crates/loro-ffi/cbindgen.toml create mode 100644 crates/loro-ffi/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 0c87ca02..a98b90f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -171,6 +171,25 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" +[[package]] +name = "cbindgen" +version = "0.24.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6358dedf60f4d9b8db43ad187391afe959746101346fe51bb978126bec61dfb" +dependencies = [ + "clap", + "heck", + "indexmap", + "log", + "proc-macro2 1.0.47", + "quote 1.0.21", + "serde", + "serde_json", + "syn 1.0.105", + "tempfile", + "toml", +] + [[package]] name = "cc" version = "1.0.77" @@ -216,9 +235,12 @@ version = "3.2.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71655c45cb9845d3270c9d6df84ebe72b4dad3c2ba3f7023ad47c144e4e473a5" dependencies = [ + "atty", "bitflags", "clap_lex", "indexmap", + "strsim", + "termcolor", "textwrap", ] @@ -735,6 +757,25 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "loro-ffi" +version = "0.1.0" +dependencies = [ + "cbindgen", + "loro-core", +] + +[[package]] +name = "loro-framework" +version = "0.1.0" +dependencies = [ + "fxhash", + "loro-core", + "ring", + "rle", + "sha2", +] + [[package]] name = "loro-wasm" version = "0.1.0" @@ -1629,6 +1670,15 @@ dependencies = [ "serde_json", ] +[[package]] +name = "toml" +version = "0.5.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1333c76748e868a4d9d1017b5ab53171dfd095f70c712fdb4653a406547f598f" +dependencies = [ + "serde", +] + [[package]] name = "tracing" version = "0.1.37" diff --git a/crates/loro-ffi/Cargo.toml b/crates/loro-ffi/Cargo.toml new file mode 100644 index 00000000..ec547e66 --- /dev/null +++ b/crates/loro-ffi/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "loro-ffi" +version = "0.1.0" +edition = "2021" +build = "build.rs" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +loro-core = { path = "../loro-core" } + +[lib] +crate-type = ["staticlib"] +name = "loro" + + +[build-dependencies] +cbindgen = "0.24.3" diff --git a/crates/loro-ffi/build.rs b/crates/loro-ffi/build.rs new file mode 100644 index 00000000..03b51f64 --- /dev/null +++ b/crates/loro-ffi/build.rs @@ -0,0 +1,35 @@ +extern crate cbindgen; + +use cbindgen::Config; +use std::env; +use std::path::PathBuf; + +fn main() { + let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); + + let package_name = env::var("CARGO_PKG_NAME").unwrap(); + let output_file = target_dir() + .join(format!("{}.hpp", package_name)) + .display() + .to_string(); + + let config = Config { + namespace: Some(String::from("loro_ffi")), + ..Default::default() + }; + + cbindgen::generate_with_config(&crate_dir, config) + .unwrap() + .write_to_file(&output_file); +} + +/// Find the location of the `target/` directory. Note that this may be +/// overridden by `cmake`, so we also need to check the `CARGO_TARGET_DIR` +/// variable. +fn target_dir() -> PathBuf { + if let Ok(target) = env::var("CARGO_TARGET_DIR") { + PathBuf::from(target) + } else { + PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("target") + } +} diff --git a/crates/loro-ffi/cbindgen.toml b/crates/loro-ffi/cbindgen.toml new file mode 100644 index 00000000..98188b59 --- /dev/null +++ b/crates/loro-ffi/cbindgen.toml @@ -0,0 +1,3 @@ +include_guard = "libloro_ffi_h" +autogen_warning = "/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */" +language = "C" diff --git a/crates/loro-ffi/src/lib.rs b/crates/loro-ffi/src/lib.rs new file mode 100644 index 00000000..b2b7c9a2 --- /dev/null +++ b/crates/loro-ffi/src/lib.rs @@ -0,0 +1,15 @@ +use loro_core::LoroCore; + +/// create Loro with a random unique client id +#[no_mangle] +pub extern "C" fn loro_new() -> *mut LoroCore { + Box::into_raw(Box::new(LoroCore::default())) +} + +/// Release all memory of Loro +#[no_mangle] +pub unsafe extern "C" fn loro_free(loro: *mut LoroCore) { + if !loro.is_null() { + drop(Box::from_raw(loro)); + } +}