diff --git a/CMakeLists.txt b/CMakeLists.txt index 2eb6942eb2a..7e8fb2ff750 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -439,6 +439,12 @@ add_library(local_install INTERFACE) target_include_directories(local_install INTERFACE ${TILEDB_LOCALINSTALL_INCLUDE}) target_include_directories(local_install INTERFACE ${CMAKE_SOURCE_DIR}) +################## +# Add Rust Library +################## + +add_subdirectory("rust") + ############################################################ # Enable testing and add subdirectories ############################################################ diff --git a/rust/CMakeLists.txt b/rust/CMakeLists.txt new file mode 100644 index 00000000000..7574e8cc98b --- /dev/null +++ b/rust/CMakeLists.txt @@ -0,0 +1,36 @@ +if (CMAKE_BUILD_TYPE STREQUAL "Debug") + set(CARGO_CMD cargo build -p tiledb-rust-api) + set(TARGET_DIR "debug") +else () + set(CARGO_CMD cargo build --release -p tiledb-rust-api) + set(TARGET_DIR "release") +endif () + +set(TILEDB_RUST_API_H "${CMAKE_CURRENT_BINARY_DIR}/tiledb_rust_api.h") +set(TILEDB_RUST_API_CXX "${CMAKE_CURRENT_BINARY_DIR}/tiledb_rust_api.cpp") +set(TILEDB_RUST_API_LIB "${CMAKE_CURRENT_BINARY_DIR}/${TARGET_DIR}/libtiledb_rust_api.a") + +add_custom_command( + OUTPUT ${TILEDB_RUST_API_H} ${TILEDB_RUST_API_CXX} + COMMAND MACOSX_DEPLOYMENT_TARGET=${CMAKE_OSX_DEPLOYMENT_TARGET} CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR} RUSTFLAGS="${RUST_FLAGS}" ${CARGO_CMD} + COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/cxxbridge/tiledb-rust-api/src/lib.rs.h ${TILEDB_RUST_API_H} + COMMAND cp ${CMAKE_CURRENT_BINARY_DIR}/cxxbridge/tiledb-rust-api/src/lib.rs.cc ${TILEDB_RUST_API_CXX} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +) + +add_custom_target(tiledb_rust_generated + DEPENDS + ${TILEDB_RUST_API_H} + ${TILEDB_RUST_API_CXX} +) + +add_library(tiledb_rust_bridge STATIC ${TILEDB_RUST_API_CXX}) +add_dependencies(tiledb_rust_bridge tiledb_rust_generated) +target_include_directories(tiledb_rust_bridge PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/..") +target_link_libraries(tiledb_rust_bridge pthread dl ${TILEDB_RUST_API_LIB}) + +add_test( + NAME tiledb_rust + COMMAND CARGO_TARGET_DIR=${CMAKE_CURRENT_BINARY_DIR} cargo test + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +) diff --git a/rust/Cargo.toml b/rust/Cargo.toml new file mode 100644 index 00000000000..ee674ae7d22 --- /dev/null +++ b/rust/Cargo.toml @@ -0,0 +1,17 @@ +[workspace] +resolver = "2" +members = [ + "crates/api", + "crates/utils", +] + +[workspace.package] +edition = "2021" +rust-version = "1.80" +version = "0.1.0" + +[workspace.dependencies] +cxx = "1.0" +cxx-build = "1.0" + +tiledb-utils.path = "crates/utils" diff --git a/rust/crates/api/Cargo.toml b/rust/crates/api/Cargo.toml new file mode 100644 index 00000000000..fa2ffd0cd5c --- /dev/null +++ b/rust/crates/api/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "tiledb-rust-api" +edition.workspace = true +rust-version.workspace = true +version.workspace = true + +[dependencies] +cxx.workspace = true +tiledb-utils.workspace = true + +[build-dependencies] +cxx-build.workspace = true + +[lib] +crate-type = ["staticlib"] diff --git a/rust/crates/api/build.rs b/rust/crates/api/build.rs new file mode 100644 index 00000000000..7b1aa53b5fb --- /dev/null +++ b/rust/crates/api/build.rs @@ -0,0 +1,4 @@ +fn main() { + let _ = cxx_build::bridge("src/lib.rs"); + println!("cargo:rerun-if-changed=src/lib.rs"); +} diff --git a/rust/crates/api/src/lib.rs b/rust/crates/api/src/lib.rs new file mode 100644 index 00000000000..9ea8da431e5 --- /dev/null +++ b/rust/crates/api/src/lib.rs @@ -0,0 +1,13 @@ + +#[cxx::bridge] +mod ffi { + #[namespace = "tiledb::rust"] + extern "Rust" { + fn timestamp_now_ms() -> u64; + } +} + + +pub fn timestamp_now_ms() -> u64 { + tiledb_utils::timestamp_now_ms() +} diff --git a/rust/crates/utils/Cargo.toml b/rust/crates/utils/Cargo.toml new file mode 100644 index 00000000000..a69d586cee8 --- /dev/null +++ b/rust/crates/utils/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "tiledb-utils" +edition.workspace = true +rust-version.workspace = true +version.workspace = true + +[dependencies] diff --git a/rust/crates/utils/src/lib.rs b/rust/crates/utils/src/lib.rs new file mode 100644 index 00000000000..0b1bc7d783b --- /dev/null +++ b/rust/crates/utils/src/lib.rs @@ -0,0 +1,8 @@ +use std::time::SystemTime; + +pub fn timestamp_now_ms() -> u64 { + match SystemTime::now().duration_since(SystemTime::UNIX_EPOCH) { + Ok(time) => time.as_millis() as u64, + Err(_) => 0 + } +} diff --git a/tiledb/CMakeLists.txt b/tiledb/CMakeLists.txt index 4ecaf58feae..e839d42f393 100644 --- a/tiledb/CMakeLists.txt +++ b/tiledb/CMakeLists.txt @@ -445,7 +445,7 @@ target_include_directories(TILEDB_CORE_OBJECTS "${TILEDB_CORE_INCLUDE_DIR}" "${TILEDB_CORE_INCLUDE_DIR}/tiledb/sm/c_api" "${TILEDB_EXTERNALS_INCLUDE_DIRS}" - # to pickup /tiledb for capnp gen'd files + # To pickup /tiledb for capnp gen'd files "${CMAKE_CURRENT_BINARY_DIR}/.." ) diff --git a/tiledb/common/CMakeLists.txt b/tiledb/common/CMakeLists.txt index 032e2030482..852b248d8b0 100644 --- a/tiledb/common/CMakeLists.txt +++ b/tiledb/common/CMakeLists.txt @@ -73,6 +73,7 @@ commence(object_library baseline) find_package(spdlog REQUIRED) target_link_libraries(baseline PUBLIC spdlog::spdlog) target_link_libraries(baseline PUBLIC common) + target_link_libraries(baseline PUBLIC tiledb_rust_bridge) conclude(object_library) # diff --git a/tiledb/sm/misc/CMakeLists.txt b/tiledb/sm/misc/CMakeLists.txt index d6848f8f1a9..f4d5dad4e8c 100644 --- a/tiledb/sm/misc/CMakeLists.txt +++ b/tiledb/sm/misc/CMakeLists.txt @@ -106,6 +106,7 @@ conclude(object_library) # commence(object_library time) this_target_sources(tdb_time.cc) + this_target_link_libraries(tiledb_rust_bridge) conclude(object_library) add_test_subdirectory() diff --git a/tiledb/sm/misc/tdb_time.cc b/tiledb/sm/misc/tdb_time.cc index e720a6509d3..8c6c61dbfda 100644 --- a/tiledb/sm/misc/tdb_time.cc +++ b/tiledb/sm/misc/tdb_time.cc @@ -31,29 +31,12 @@ * This file defines a timestamp function */ -#include "tdb_time.h" -#ifdef _WIN32 -#include -#include -#else -#include -#endif -#include +#include "rust/tiledb_rust_api.h" namespace tiledb::sm::utils::time { uint64_t timestamp_now_ms() { -#ifdef _WIN32 - struct __timeb64 tb; - memset(&tb, 0, sizeof(tb)); - _ftime64_s(&tb); - return static_cast(tb.time * 1000L + tb.millitm); -#else - struct timeval tp; - memset(&tp, 0, sizeof(struct timeval)); - gettimeofday(&tp, nullptr); - return static_cast(tp.tv_sec * 1000L + tp.tv_usec / 1000); -#endif + return tiledb::rust::timestamp_now_ms(); } } // namespace tiledb::sm::utils::time