diff --git a/CMakeLists.txt b/CMakeLists.txt index f69dae252..e005ab987 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -253,8 +253,32 @@ find_package(Threads REQUIRED) # `target_link_directories` call down in src/CMakeLists.txt, as it # will no longer be needed. set(viam_rust_utils_file ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}viam_rust_utils${CMAKE_STATIC_LIBRARY_SUFFIX}) +set(viam_rust_utils_header_file ${CMAKE_CURRENT_BINARY_DIR}/viam_rust_utils.h) file(GLOB viam_rust_utils_files ${PROJECT_SOURCE_DIR}/${CMAKE_SHARED_LIBRARY_PREFIX}viam_rust_utils*${CMAKE_STATIC_LIBRARY_SUFFIX}) +file(GLOB existing_viam_rust_utils_header_file ${PROJECT_SOURCE_DIR}/viam_rust_utils.h) + +if (NOT existing_viam_rust_utils_header_file) + file( + DOWNLOAD + https://github.com/viamrobotics/rust-utils/releases/latest/download/viam_rust_utils.h + ${viam_rust_utils_header_file} + STATUS lvruh_status + ) + list(GET lvruh_status 0 lvruh_status_code) + list(GET lvruh_status 1 lvruh_status_string) + + if(NOT lvruh_status_code EQUAL 0) + message(FATAL_ERROR "No local viam_rust_utils.h found and failed to download: ${lvruh_status_string}") + endif() +#configure_file(${CMAKE_CURRENT_BINARY_DIR}/viam_rust_utils.h src/viam/sdk/rpc/private/viam_rust_utils.h COPYONLY) +else() + FILE(COPY_FILE + ${existing_viam_rust_utils_header_file} + ${viam_rust_utils_header_file} + ONLY_IF_DIFFERENT + ) +endif() if (viam_rust_utils_files) list(LENGTH viam_rust_utils_files num_viam_rust_utils_files) diff --git a/src/viam/sdk/CMakeLists.txt b/src/viam/sdk/CMakeLists.txt index f15e25ae4..2aeff74e6 100644 --- a/src/viam/sdk/CMakeLists.txt +++ b/src/viam/sdk/CMakeLists.txt @@ -28,6 +28,7 @@ else() endif() message(WARNING "api proto label is ${VIAMCPPSDK_API_PROTO_LABEL}") configure_file(common/private/version_metadata.hpp.in common/private/version_metadata.hpp @ONLY) +configure_file(${CMAKE_CURRENT_BINARY_DIR}/../../../viam_rust_utils.h rpc/private/viam_rust_utils.h COPYONLY) # Configure the grpc client forward declarations file configure_file(common/grpc_fwd.hpp.in common/grpc_fwd.hpp) @@ -316,7 +317,7 @@ endif() # TODO several of these dependencies should properly be attached to `viam_rust_utils`, # not `viamsdk`. However, we currently are unable to do so while maintaining compilation if (APPLE) - target_link_libraries(viamsdk PUBLIC "-framework Security") + target_link_libraries(viamsdk PUBLIC "-framework CoreFoundation" "-framework Security") elseif (NOT WIN32) target_link_libraries(viamsdk PRIVATE dl) target_link_libraries(viamsdk PRIVATE rt) diff --git a/src/viam/sdk/rpc/dial.cpp b/src/viam/sdk/rpc/dial.cpp index 5df9b8470..bc027fb59 100644 --- a/src/viam/sdk/rpc/dial.cpp +++ b/src/viam/sdk/rpc/dial.cpp @@ -20,7 +20,8 @@ namespace viam { namespace sdk { struct ViamChannel::impl { - impl(const char* path, void* runtime) : path(path), rust_runtime(runtime) {} + impl(const char* path, void* runtime) + : path(path), rust_runtime(reinterpret_cast(runtime)) {} impl(const impl&) = delete; @@ -38,12 +39,13 @@ struct ViamChannel::impl { } ~impl() { - free_string(path); - free_rust_runtime(rust_runtime); + auto& p = const_cast(path); + viam_free_string(p); + viam_free_rust_runtime(rust_runtime); } const char* path; - void* rust_runtime; + viam_dial_ffi* rust_runtime; }; ViamChannel::ViamChannel(std::shared_ptr channel, const char* path, void* runtime) @@ -158,7 +160,7 @@ ViamChannel ViamChannel::dial_initial(const char* uri, } ViamChannel ViamChannel::dial(const char* uri, const boost::optional& options) { - void* ptr = init_rust_runtime(); + viam_dial_ffi* ptr = init_rust_runtime(); const DialOptions opts = options.get_value_or(DialOptions()); const std::chrono::duration float_timeout = opts.timeout(); const char* type = nullptr; diff --git a/src/viam/sdk/rpc/private/viam_rust_utils.h b/src/viam/sdk/rpc/private/viam_rust_utils.h deleted file mode 100644 index 7542a51f4..000000000 --- a/src/viam/sdk/rpc/private/viam_rust_utils.h +++ /dev/null @@ -1,21 +0,0 @@ -#if defined(__cplusplus) -extern "C" { -#endif - -// Prototypes for the entrypoints from viam_rust_utils -// that we use in the SDK. - -void* init_rust_runtime(); -int free_rust_runtime(void* ptr); -void free_string(const char* s); -char* dial(const char* uri, - const char* entity, - const char* type, - const char* payload, - bool allow_insecure, - float timeout, - void* ptr); - -#if defined(__cplusplus) -} // extern "C" -#endif diff --git a/src/viam/sdk/rpc/private/viam_rust_utils_stubs.cpp b/src/viam/sdk/rpc/private/viam_rust_utils_stubs.cpp index caa78aef2..2b76c6c5a 100644 --- a/src/viam/sdk/rpc/private/viam_rust_utils_stubs.cpp +++ b/src/viam/sdk/rpc/private/viam_rust_utils_stubs.cpp @@ -2,18 +2,18 @@ #include -void* init_rust_runtime() { +viam_dial_ffi* viam_init_rust_runtime() { abort(); } -int free_rust_runtime(void*) { +int viam_free_rust_runtime(viam_dial_ffi*) { abort(); } -void free_string(const char*) { +void viam_free_string(char*) { abort(); } -char* dial(const char*, const char*, const char*, const char*, bool, float, void*) { +char* viam_dial(const char*, const char*, const char*, const char*, bool, float, viam_dial_ffi*) { abort(); }