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

27
Cargo.lock generated
View file

@ -591,6 +591,25 @@ dependencies = [
"url",
]
[[package]]
name = "cbindgen"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51e3973b165dc0f435831a9e426de67e894de532754ff7a3f307c03ee5dec7dc"
dependencies = [
"clap",
"heck",
"indexmap",
"log",
"proc-macro2",
"quote",
"serde",
"serde_json",
"syn",
"tempfile",
"toml",
]
[[package]]
name = "cc"
version = "1.0.68"
@ -3008,6 +3027,14 @@ dependencies = [
"toml_edit",
]
[[package]]
name = "taskchampion-lib"
version = "0.1.0"
dependencies = [
"cbindgen",
"taskchampion",
]
[[package]]
name = "taskchampion-sync-server"
version = "0.4.1"

View file

@ -4,5 +4,6 @@ members = [
"taskchampion",
"cli",
"sync-server",
"replica-server-tests"
"replica-server-tests",
"lib"
]

2
binding-tests/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
*.o
doctest

19
binding-tests/Makefile Normal file
View file

@ -0,0 +1,19 @@
CXX=g++
INC=-I ../lib
LIB=-L ../target/debug
RPATH=-Wl,-rpath,../target/debug
TESTS = uuid.cpp
.PHONY: all test
all: test
test: doctest
@./doctest --no-version --no-intro
%.o: %.cpp ../lib/taskchampion.h
$(CXX) $(INC) -c $< -o $@
doctest: doctest.o $(subst .cpp,.o,$(TESTS))
$(CXX) $(LIB) $(RPATH) $< $(subst .cpp,.o,$(TESTS)) -ltaskchampion -o $@

View file

@ -0,0 +1,2 @@
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest.h"

6816
binding-tests/doctest.h Normal file

File diff suppressed because it is too large Load diff

7
binding-tests/uuid.cpp Normal file
View file

@ -0,0 +1,7 @@
#include "doctest.h"
#include "taskchampion.h"
TEST_CASE("creating a UUID") {
StoragePtr *storage = storage_new_in_memory();
storage_free(storage);
}

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"