Skip to content

Commit

Permalink
Calling Rust from C++
Browse files Browse the repository at this point in the history
  • Loading branch information
davisp committed Nov 22, 2024
1 parent f6ee4c4 commit ca9eca1
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 20 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
############################################################
Expand Down
36 changes: 36 additions & 0 deletions rust/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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}
)
17 changes: 17 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
15 changes: 15 additions & 0 deletions rust/crates/api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"]
4 changes: 4 additions & 0 deletions rust/crates/api/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
let _ = cxx_build::bridge("src/lib.rs");
println!("cargo:rerun-if-changed=src/lib.rs");
}
13 changes: 13 additions & 0 deletions rust/crates/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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()
}
7 changes: 7 additions & 0 deletions rust/crates/utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "tiledb-utils"
edition.workspace = true
rust-version.workspace = true
version.workspace = true

[dependencies]
8 changes: 8 additions & 0 deletions rust/crates/utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
2 changes: 1 addition & 1 deletion tiledb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <build_dir>/tiledb for capnp gen'd files
# To pickup <build_dir>/tiledb for capnp gen'd files
"${CMAKE_CURRENT_BINARY_DIR}/.."
)

Expand Down
1 change: 1 addition & 0 deletions tiledb/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

#
Expand Down
1 change: 1 addition & 0 deletions tiledb/sm/misc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
21 changes: 2 additions & 19 deletions tiledb/sm/misc/tdb_time.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,12 @@
* This file defines a timestamp function
*/

#include "tdb_time.h"
#ifdef _WIN32
#include <sys/timeb.h>
#include <sys/types.h>
#else
#include <sys/time.h>
#endif
#include <cstring>
#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<uint64_t>(tb.time * 1000L + tb.millitm);
#else
struct timeval tp;
memset(&tp, 0, sizeof(struct timeval));
gettimeofday(&tp, nullptr);
return static_cast<uint64_t>(tp.tv_sec * 1000L + tp.tv_usec / 1000);
#endif
return tiledb::rust::timestamp_now_ms();
}

} // namespace tiledb::sm::utils::time

0 comments on commit ca9eca1

Please sign in to comment.