first bits of a dynamc lib

This commit is contained in:
Dustin J. Mitchell 2022-01-22 22:48:40 +00:00
parent 8576e7ffa7
commit 33f5f056b1
13 changed files with 6942 additions and 1 deletions

15
lib/Cargo.toml Normal file
View file

@ -0,0 +1,15 @@
[package]
name = "taskchampion-lib"
version = "0.1.0"
edition = "2021"
build = "build.rs"
[lib]
name = "taskchampion"
crate-type = ["cdylib"]
[dependencies]
taskchampion = { path = "../taskchampion" }
[build-dependencies]
cbindgen = "0.20.0"

2
lib/Makefile Normal file
View file

@ -0,0 +1,2 @@
taskchampion.h: cbindgen.toml ../target/debug/libtaskchampion.so
cbindgen --config cbindgen.toml --crate taskchampion-lib --output $@

18
lib/build.rs Normal file
View file

@ -0,0 +1,18 @@
use cbindgen::*;
use std::env;
fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
Builder::new()
.with_crate(crate_dir)
.with_language(Language::C)
.with_config(Config {
cpp_compat: true,
..Default::default()
})
.generate()
.expect("Unable to generate bindings")
.write_to_file("taskchampion.h");
}

1
lib/src/lib.rs Normal file
View file

@ -0,0 +1 @@
pub mod storage;

16
lib/src/storage.rs Normal file
View file

@ -0,0 +1,16 @@
use taskchampion::{storage::Storage, StorageConfig};
pub struct StoragePtr(Box<dyn Storage>);
#[no_mangle]
pub extern "C" fn storage_new_in_memory() -> *mut StoragePtr {
// TODO: this is a box containing a fat pointer
Box::into_raw(Box::new(StoragePtr(
StorageConfig::InMemory.into_storage().unwrap(),
)))
}
#[no_mangle]
pub extern "C" fn storage_free(storage: *mut StoragePtr) {
drop(unsafe { Box::from_raw(storage) });
}

15
lib/taskchampion.h Normal file
View file

@ -0,0 +1,15 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>
struct StoragePtr;
extern "C" {
StoragePtr *storage_new_in_memory();
void storage_free(StoragePtr *storage);
} // extern "C"