Skip to content

Commit

Permalink
feat(static): static link library works now
Browse files Browse the repository at this point in the history
  • Loading branch information
jgeluk committed Feb 18, 2023
1 parent dd681da commit 598e83d
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 38 deletions.
44 changes: 32 additions & 12 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved.
// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved.
//---------------------------------------------------------------
//! build.rs
#![feature(absolute_path)]
Expand Down Expand Up @@ -32,8 +32,17 @@ const BLOCKLIST_ITEMS: &[&str] = &[
"std::remove_const_type",
"std::remove_volatile_type",
"^std::value$",
"__va_list_tag",
"__builtin_va_list",
"__darwin_va_list",
"va_list",
"vasprintf",
"vdprintf",
"__darwin_pthread_handler_rec",
"^_Tp$",
];
const ALLOWLIST_ITEMS: &[&str] = &["C.*"];

const RUSTFMT_CONFIG: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/.rustfmt.toml");

lazy_static! {
Expand Down Expand Up @@ -298,7 +307,7 @@ fn main() {
))
.rust_target(RustTarget::Nightly)
.generate_comments(true)
.opaque_type("void")
// .opaque_type("void")
.default_enum_style(bindgen::EnumVariation::Rust {
non_exhaustive: true,
})
Expand All @@ -308,33 +317,44 @@ fn main() {
.clang_arg(format!("-I{}", rdfox_header_dir().to_str().unwrap()))
.clang_arg("-v")
// .clang_arg(r"-Wl,--whole-archive RDFox-static -Wl,--no-whole-archive")
.emit_builtins()
// .emit_builtins()
.layout_tests(true)
.enable_function_attribute_detection()
// .enable_function_attribute_detection()
.no_copy(".*CCursor.*")
.no_copy(".*COutputStream.*")
.no_copy(".*CException.*")
.no_copy(".*CInputStream.*")
.no_copy(".*CParameters.*")
.no_copy(".*CPrefixes.*")
.no_copy(".*CServerConnection.*")
.no_copy(".*CDataStoreConnection.*")
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
// .parse_callbacks(Box::new(bindgen::CargoCallbacks))
.rustfmt_configuration_file(Some(PathBuf::from(RUSTFMT_CONFIG)))
.enable_cxx_namespaces()
.merge_extern_blocks(true)
.wrap_unsafe_ops(true)
// .merge_extern_blocks(true)
// .wrap_unsafe_ops(true)
.array_pointers_in_arguments(true)
.dynamic_link_require_all(true)
// .dynamic_link_require_all(true)
.size_t_is_usize(false)
.explicit_padding(true)
// .explicit_padding(true)
.sort_semantically(true)
.respect_cxx_access_specs(true)
.vtable_generation(false)
.enable_function_attribute_detection()
.generate_inline_functions(true);

// .vtable_generation(false)
// .enable_function_attribute_detection()
// .generate_inline_functions(true);
;
for item in BLOCKLIST_ITEMS {
builder = builder.blocklist_type(item);
builder = builder.blocklist_item(item);
builder = builder.blocklist_function(item);
}
for item in ALLOWLIST_ITEMS {
builder = builder.allowlist_type(item);
builder = builder.allowlist_var(item);
builder = builder.allowlist_function(item);
}

// let command_line_flags = builder.command_line_flags();
// for flag in &command_line_flags {
Expand Down
2 changes: 1 addition & 1 deletion src/cursor/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Cursor {
connection.inner,
c_query.as_ptr(),
c_query_len,
parameters.inner,
parameters.inner.as_ref().cast_const(),
&mut c_cursor,
)
)?;
Expand Down
2 changes: 1 addition & 1 deletion src/data_store_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ impl DataStoreConnection {
self.inner,
statement_text.as_ptr(),
statement_text_len,
parameters.inner,
parameters.inner.as_ref().cast_const(),
&mut statement_result,
)
)?;
Expand Down
62 changes: 55 additions & 7 deletions src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@ use {
root::{
CParameters,
CParameters_destroy,
CParameters_getString,
CParameters_newEmptyParameters,
CParameters_setString,
},
},
alloc::ffi::CString,
rdf_store_rs::{consts::LOG_TARGET_DATABASE, RDFStoreError},
std::{
ffi::CStr,
fmt::{Display, Formatter},
os::raw::c_char,
path::Path,
ptr,
sync::Arc,
},
};

Expand All @@ -44,9 +48,9 @@ impl Display for PersistenceMode {
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Parameters {
pub(crate) inner: *mut CParameters,
pub(crate) inner: Arc<*mut CParameters>,
}

unsafe impl Sync for Parameters {}
Expand All @@ -67,7 +71,7 @@ impl Drop for Parameters {
"Parameters-object was already dropped"
);
unsafe {
CParameters_destroy(self.inner);
CParameters_destroy(self.inner.cast());
tracing::trace!(target: LOG_TARGET_DATABASE, "Destroyed params");
}
}
Expand All @@ -80,7 +84,7 @@ impl Parameters {
"Allocating parameters",
CParameters_newEmptyParameters(&mut parameters)
)?;
Ok(Parameters { inner: parameters })
Ok(Parameters { inner: Arc::new(parameters) })
}

pub fn set_string(&self, key: &str, value: &str) -> Result<(), RDFStoreError> {
Expand All @@ -93,10 +97,32 @@ impl Parameters {
);
database_call!(
msg.as_str(),
CParameters_setString(self.inner, c_key.as_ptr(), c_value.as_ptr())
CParameters_setString(*self.inner, c_key.as_ptr(), c_value.as_ptr())
)
}

pub fn get_string(&self, key: &str, default: &str) -> Result<String, RDFStoreError> {
let c_key = CString::new(key).unwrap();
let c_default = CString::new(default).unwrap();
let mut c_value: *const c_char = ptr::null();
let msg = format!(
"Getting parameter {} with default value {}",
c_key.to_str().unwrap(),
c_default.to_str().unwrap()
);
database_call!(
msg.as_str(),
CParameters_getString(
*self.inner,
c_key.as_ptr(),
c_default.as_ptr(),
&mut c_value as *mut *const c_char
)
)?;
let c_version = unsafe { CStr::from_ptr(c_value) };
Ok(c_version.to_str().unwrap().to_owned())
}

pub fn fact_domain(self, fact_domain: FactDomain) -> Result<Self, RDFStoreError> {
match fact_domain {
FactDomain::ASSERTED => self.set_string("fact-domain", "explicit")?,
Expand Down Expand Up @@ -162,7 +188,29 @@ impl Parameters {
/// Specifies the directory into which API logs will be written.
/// Default is directory api-log within the configured server directory.
pub fn api_log_directory(self, dir: &Path) -> Result<Self, RDFStoreError> {
self.set_string("api-log.directory", dir.to_str().unwrap())?;
Ok(self)
if dir.exists() {
let x = self.api_log(true)?;
x.set_string("api-log.directory", dir.to_str().unwrap())?;
Ok(x)
} else {
tracing::error!(
"Could not enable logging since directory does not exist: {}",
dir.to_str().unwrap()
);
Ok(self)
}
}
}

#[cfg(test)]
mod tests {
use crate::Parameters;

#[test_log::test]
fn test_set_param() {
let params = Parameters::empty().unwrap();
params.set_string("key1", "value1").unwrap();
let value = params.get_string("key1", "whatever").unwrap();
assert_eq!(value, "value1");
}
}
20 changes: 14 additions & 6 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,25 @@ impl Server {
pub fn is_running(&self) -> bool { self.running.load(Ordering::Relaxed) }

pub fn start(role_creds: RoleCreds) -> Result<Arc<Self>, RDFStoreError> {
Self::start_with_parameters(role_creds, &Parameters::empty()?)
Self::start_with_parameters(role_creds, None)
}

pub fn start_with_parameters(
role_creds: RoleCreds,
params: &Parameters,
params: Option<Parameters>,
) -> Result<Arc<Self>, RDFStoreError> {
database_call!(
"Starting a local RDFFox server",
CServer_startLocalServer(params.inner)
)?;
if let Some(params) = params {
database_call!(
"Starting a local RDFFox server",
CServer_startLocalServer(params.inner.cast_const())
)?;
} else {
let params = Parameters::empty()?;
database_call!(
"Starting a local RDFFox server",
CServer_startLocalServer(params.inner.cast_const())
)?;
};
let server = Server {
default_role_creds: role_creds,
running: AtomicBool::new(true),
Expand Down
4 changes: 1 addition & 3 deletions src/server_connection/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ impl ServerConnection {
}

pub fn get_number_of_threads(&self) -> Result<u32, RDFStoreError> {
tracing::trace!("get_number_of_threads");
let mut number_of_threads = 0_u64;
database_call!(
format!(
Expand Down Expand Up @@ -116,7 +115,6 @@ impl ServerConnection {
}

pub fn get_memory_use(&self) -> Result<(u64, u64), RDFStoreError> {
tracing::trace!("get_memory_use");
let mut max_used_bytes = 0_u64;
let mut available_bytes = 0_u64;
database_call!(CServerConnection_getMemoryUse(
Expand Down Expand Up @@ -149,7 +147,7 @@ impl ServerConnection {
CServerConnection_createDataStore(
self.inner,
c_name.as_ptr(),
data_store.parameters.inner,
data_store.parameters.inner.cast_const(),
)
)?;
tracing::debug!(
Expand Down
2 changes: 1 addition & 1 deletion src/streamer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl<'a, W: 'a + Write> Streamer<'a, W> {
connection_ptr,
statement_text.as_ptr(),
statement_text_len,
parameters.inner,
parameters.inner.cast_const(),
stream_raw_ptr as *const COutputStream,
query_answer_format_name.as_ptr(),
&mut number_of_solutions,
Expand Down
15 changes: 8 additions & 7 deletions tests/load.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018-2022, agnos.ai UK Ltd, all rights reserved.
// Copyright (c) 2018-2023, agnos.ai UK Ltd, all rights reserved.
//---------------------------------------------------------------
/// We're using `#[test_log::test]` tests in this file which allows
/// you to see the log in your test runner if you set the environment
Expand All @@ -7,8 +7,6 @@
/// See https://crates.io/crates/test-log.
///
/// TODO: Add test for "import axioms" (add test ontology)
use std::path::Path;

use {
indoc::formatdoc,
iref::Iri,
Expand All @@ -27,6 +25,7 @@ use {
Statement,
Transaction,
},
// std::path::Path,
std::{ops::Deref, sync::Arc, thread::sleep, time::Duration},
};

Expand All @@ -40,12 +39,14 @@ fn test_define_data_store() -> Result<Arc<DataStore>, RDFStoreError> {

fn test_create_server() -> Result<Arc<Server>, RDFStoreError> {
tracing::info!("test_create_server");
let server_params = &Parameters::empty()?
.api_log(true)?
.api_log_directory(Path::new("./tests"))?
let server_params = Parameters::empty()?
.persist_datastore(PersistenceMode::Off)?
.persist_roles(PersistenceMode::Off)?;
Server::start_with_parameters(RoleCreds::default(), server_params)

// TODO: The line below causes a SIGSEGV error when using the static link
// library .api_log_directory(Path::new("./tests"))?;

Server::start_with_parameters(RoleCreds::default(), Some(server_params))
}

fn test_create_server_connection(
Expand Down

0 comments on commit 598e83d

Please sign in to comment.