shim/utils.hpp
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <dlfcn.h>
8 #include <sys/utsname.h>
9 #include <stdexcept>
10 #include <string>
11 #include <vector>
12 
13 namespace kvikio {
14 
15 // Macros used for defining symbol visibility.
16 // Since KvikIO declares global default values in headers, we rely on the linker to disambiguate
17 // inline and static methods that have (or return) static references. To do this, the relevant
18 // function/method must have `__attribute__((visibility("default")))`. If not, then if KvikIO is
19 // used in two different DSOs, the function will appear twice, and there will be two static objects.
20 // See <https://gcc.gnu.org/wiki/Visibility> and <https://github.com/rapidsai/kvikio/issues/442>.
21 #if (defined(__GNUC__) || defined(__clang__)) && !defined(__MINGW32__) && !defined(__MINGW64__)
22 #define KVIKIO_EXPORT __attribute__((visibility("default")))
23 #define KVIKIO_HIDDEN __attribute__((visibility("hidden")))
24 #else
25 #define KVIKIO_EXPORT
26 #define KVIKIO_HIDDEN
27 #endif
28 
29 #define KVIKIO_STRINGIFY_DETAIL(x) #x
30 #define KVIKIO_STRINGIFY(x) KVIKIO_STRINGIFY_DETAIL(x)
31 
38 void* load_library(std::string const& name, int mode = RTLD_LAZY | RTLD_LOCAL | RTLD_NODELETE);
39 
48 template <typename T>
49 void get_symbol(T& handle, void* lib, std::string const& name)
50 {
51  ::dlerror(); // Clear old errors
52  // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
53  handle = reinterpret_cast<T>(::dlsym(lib, name.c_str()));
54  char const* err = ::dlerror();
55  if (err != nullptr) { throw std::runtime_error(err); }
56 }
57 
65 [[nodiscard]] bool is_running_in_wsl() noexcept;
66 
76 [[nodiscard]] bool run_udev_readable() noexcept;
77 
78 } // namespace kvikio
KvikIO namespace.
Definition: batch.hpp:16
bool run_udev_readable() noexcept
Check if /run/udev is readable.
void * load_library(std::string const &name, int mode=RTLD_LAZY|RTLD_LOCAL|RTLD_NODELETE)
Load shared library.
void get_symbol(T &handle, void *lib, std::string const &name)
Get symbol using dlsym
Definition: shim/utils.hpp:49
bool is_running_in_wsl() noexcept
Try to detect if running in Windows Subsystem for Linux (WSL)