Skip to content

Commit ac5bd71

Browse files
authored
[RSDK-10385] Windows build system improvements and rust_utils workarounds (#402)
- Ensure `__cplusplus` is set right on Windows builds. - Don't download or link to `viam_rust_utils` on Windows. - Redirect the `viam_rust_utils` entry points to `abort` on Windows. - Adjust `viamapi` library name on Windows and apply `/WHOLEARCHIVE` - Use `std::invoke_result_t` instead of `result_of` in C++17, where the latter is deprecated - Add windows headers where needed.
1 parent 58e035a commit ac5bd71

File tree

8 files changed

+101
-24
lines changed

8 files changed

+101
-24
lines changed

CMakeLists.txt

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ if (NOT CMAKE_CXX_STANDARD)
173173
set(CMAKE_CXX_STANDARD 14)
174174
endif()
175175
set(CMAKE_CXX_STANDARD_REQUIRED True)
176+
if(MSVC)
177+
# https://discourse.cmake.org/t/set-cmake-cxx-standard-should-set-zc-cplusplus-for-msvc/1876
178+
string(APPEND CMAKE_CXX_FLAGS " /Zc:__cplusplus")
179+
endif()
176180
set(CMAKE_CXX_EXTENSIONS OFF)
177181

178182

@@ -255,7 +259,7 @@ if (viam_rust_utils_files)
255259
${viam_rust_utils_file}
256260
ONLY_IF_DIFFERENT
257261
)
258-
else()
262+
elseif(NOT WIN32) # TODO(RSDK-10366): Currently, rust_utils is not published for windows, so don't even try downloading
259263
set(lvru_system_name ${CMAKE_SYSTEM_NAME})
260264
if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
261265
set(lvru_system_name "macosx")
@@ -275,21 +279,23 @@ else()
275279

276280
endif()
277281

278-
add_library(viam_rust_utils SHARED IMPORTED)
282+
# TODO(RSDK-10366): Currently, rust_utils is not published for windows, so don't even declare the library
283+
if (NOT WIN32)
284+
add_library(viam_rust_utils SHARED IMPORTED)
279285

280-
target_link_directories(viam_rust_utils
281-
INTERFACE
286+
target_link_directories(viam_rust_utils
287+
INTERFACE
282288
"$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>"
283289
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_LIBDIR}>"
284-
)
285-
286-
set_property(TARGET viam_rust_utils PROPERTY IMPORTED_LOCATION ${viam_rust_utils_file})
290+
)
287291

288-
install(
289-
IMPORTED_RUNTIME_ARTIFACTS viam_rust_utils
290-
LIBRARY COMPONENT viam-cpp-sdk_runtime
291-
)
292+
set_property(TARGET viam_rust_utils PROPERTY IMPORTED_LOCATION ${viam_rust_utils_file})
292293

294+
install(
295+
IMPORTED_RUNTIME_ARTIFACTS viam_rust_utils
296+
LIBRARY COMPONENT viam-cpp-sdk_runtime
297+
)
298+
endif()
293299

294300
# Install the license file
295301
install(FILES

conanfile.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,11 @@ def package(self):
8282
CMake(self).install()
8383

8484
def package_info(self):
85-
self.cpp_info.components["viam_rust_utils"].libs = ["viam_rust_utils"]
85+
86+
# TODO(RSDK-10366): Currently, rust_utils is not published for windows
87+
# and the C++ SDK just doesn't include it as a dependency on that platform
88+
if not self.settings.os == "Windows":
89+
self.cpp_info.components["viam_rust_utils"].libs = ["viam_rust_utils"]
8690

8791
self.cpp_info.components["viamsdk"].libs = ["viamsdk"]
8892

@@ -103,10 +107,16 @@ def package_info(self):
103107
else:
104108
lib_folder = os.path.join(self.package_folder, "lib")
105109
lib_fullpath = os.path.join(lib_folder, "libviamapi.a")
110+
if self.settings.os == "Windows":
111+
lib_fullpath = os.path.join(lib_folder, "viamapi.lib")
112+
106113
if is_apple_os(self):
107114
whole_archive = f"-Wl,-force_load,{lib_fullpath}"
115+
elif self.settings.os == "Windows":
116+
whole_archive = f"/WHOLEARCHIVE:{lib_fullpath}"
108117
else:
109118
whole_archive = f"-Wl,--push-state,--whole-archive,{lib_fullpath},--pop-state"
119+
110120
self.cpp_info.components["viamapi"].exelinkflags.append(whole_archive)
111121
self.cpp_info.components["viamapi"].sharedlinkflags.append(whole_archive)
112122

@@ -118,7 +128,13 @@ def package_info(self):
118128
"xtensor::xtensor",
119129

120130
"viamapi",
121-
"viam_rust_utils"
122131
])
123132

133+
# TODO(RSDK-10366): Currently, rust_utils is not published for windows
134+
# and the C++ SDK just doesn't include it as a dependency on that platform
135+
if self.settings.os != "Windows":
136+
self.cpp_info.components["viamsdk"].requires.extend([
137+
"viam_rust_utils"
138+
])
139+
124140
self.cpp_info.components["viamsdk"].frameworks = ["Security"]

src/viam/sdk/CMakeLists.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,10 +267,19 @@ target_link_libraries(viamsdk
267267
PRIVATE ${VIAMCPPSDK_GRPCXX_LIBRARIES}
268268
PRIVATE ${VIAMCPPSDK_GRPC_LIBRARIES}
269269
PRIVATE ${VIAMCPPSDK_PROTOBUF_LIBRARIES}
270-
PRIVATE viam_rust_utils
271270
PRIVATE Threads::Threads
272271
)
273272

273+
# TODO(RSDK-10366): Currently, rust_utils is not published for
274+
# windows, so don't link to it. Instead, link a stub implementation
275+
# that just calls `abort`.
276+
if (NOT WIN32)
277+
target_link_libraries(viamsdk
278+
PRIVATE viam_rust_utils
279+
)
280+
else()
281+
target_sources(viamsdk PRIVATE rpc/private/viam_rust_utils_stubs.cpp)
282+
endif()
274283

275284
if (APPLE)
276285
target_link_libraries(viamsdk PUBLIC "-framework Security")

src/viam/sdk/common/private/service_helper.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,13 @@ class ServiceHelper : public ServiceHelperBase {
5555
}
5656

5757
private:
58+
#if __cplusplus >= 201703L
59+
template <typename Callable, typename... Args>
60+
using is_void_result = std::is_void<std::invoke_result_t<Callable, Args...>>;
61+
#else
5862
template <typename Callable, typename... Args>
5963
using is_void_result = std::is_void<std::result_of_t<Callable(Args...)>>;
64+
#endif
6065

6166
// Implementation of `invoke_` for a Callable returning non-void,
6267
// presumably an error return, which we return as a

src/viam/sdk/module/service.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
#ifdef _WIN32
2+
#define NOMINMAX
3+
// clang-format off
4+
// Otherwise clang-format tries to alphabetize these headers,
5+
// but `winsock2.h` should definitely precede `windows.h`.
6+
#include <winsock2.h>
7+
#include <windows.h>
8+
// clang-format on
9+
#endif
10+
111
#include <viam/sdk/module/service.hpp>
212

313
#include <exception>

src/viam/sdk/rpc/dial.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,8 @@
1313
#include <viam/api/robot/v1/robot.pb.h>
1414
#include <viam/sdk/common/exception.hpp>
1515
#include <viam/sdk/rpc/private/viam_grpc_channel.hpp>
16+
#include <viam/sdk/rpc/private/viam_rust_utils.h>
1617

17-
extern "C" void* init_rust_runtime();
18-
extern "C" int free_rust_runtime(void* ptr);
19-
extern "C" void free_string(const char* s);
20-
extern "C" char* dial(const char* uri,
21-
const char* entity,
22-
const char* type,
23-
const char* payload,
24-
bool allow_insecure,
25-
float timeout,
26-
void* ptr);
2718
namespace viam {
2819
namespace sdk {
2920

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#if defined(__cplusplus)
2+
extern "C" {
3+
#endif
4+
5+
// Prototypes for the entrypoints from viam_rust_utils
6+
// that we use in the SDK.
7+
8+
void* init_rust_runtime();
9+
int free_rust_runtime(void* ptr);
10+
void free_string(const char* s);
11+
char* dial(const char* uri,
12+
const char* entity,
13+
const char* type,
14+
const char* payload,
15+
bool allow_insecure,
16+
float timeout,
17+
void* ptr);
18+
19+
#if defined(__cplusplus)
20+
} // extern "C"
21+
#endif
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <viam/sdk/rpc/private/viam_rust_utils.h>
2+
3+
#include <cstdlib>
4+
5+
void* init_rust_runtime() {
6+
abort();
7+
}
8+
9+
int free_rust_runtime(void*) {
10+
abort();
11+
}
12+
13+
void free_string(const char*) {
14+
abort();
15+
}
16+
17+
char* dial(const char*, const char*, const char*, const char*, bool, float, void*) {
18+
abort();
19+
}

0 commit comments

Comments
 (0)