feat: init ffi

This commit is contained in:
leeeon233 2023-01-28 19:59:37 +08:00 committed by Leonzhao
parent b5880f0b66
commit a26d4b0122
5 changed files with 121 additions and 0 deletions

50
Cargo.lock generated
View file

@ -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"

View file

@ -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"

35
crates/loro-ffi/build.rs Normal file
View file

@ -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")
}
}

View file

@ -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"

View file

@ -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));
}
}