44 snprintf(
error,
capacity,
"Cannot lock ONNX runtime: %s", strerror(locked));
52 const ssize_t length = readlink(
"/proc/self/exe", executable,
sizeof(executable) - 1);
53 if (length <= 0 || (
size_t)length >=
sizeof(executable) - 1) {
54 snprintf(
error,
capacity,
"Cannot resolve executable for ONNX runtime");
58 executable[length] =
'\0';
59 char *separator = strrchr(executable,
'/');
61 snprintf(
error,
capacity,
"Invalid executable path for ONNX runtime");
67 snprintf(parent,
sizeof(parent),
"%s", executable);
68 for (
unsigned level = 0; level < 2; ++level) {
69 char *slash = strrchr(parent,
'/');
70 if (!slash || slash == parent) {
78 const int secondary_length = snprintf(secondary,
sizeof(secondary),
"%s%sbin", parent,
79 parent[strlen(parent) - 1] ==
'/' ?
"" :
"/");
80 if (secondary_length < 0 || (
size_t)secondary_length >=
sizeof(secondary)) {
81 snprintf(
error,
capacity,
"ONNX runtime directory exceeds path capacity");
85 const char *directories[] = {executable, secondary, library_directory};
86 const char *names[] = {
87#if defined(__aarch64__)
89 "libonnxruntime.so-arm64",
"libonnxruntime.so.1-arm64",
"libonnxruntime.so.1.31.0-arm64",
91 "libonnxruntime.so",
"libonnxruntime.so.1",
"libonnxruntime.so.1.31.0"
94 for (
size_t directory = 0; directory <
sizeof(directories) /
sizeof(directories[0]); ++directory) {
95 if (!directories[directory] || !directories[directory][0])
continue;
96 for (
size_t name = 0; name <
sizeof(names) /
sizeof(names[0]); ++name) {
98 const int count = snprintf(filename,
sizeof(filename),
"%s/%s", directories[directory], names[name]);
99 if (
count < 0 || (
size_t)
count >=
sizeof(filename)) {
100 snprintf(
error,
capacity,
"ONNX runtime filename exceeds path capacity");
104 if (stat(filename, &info) != 0) {
105 if (errno == ENOENT || errno == ENOTDIR)
continue;
106 snprintf(
error,
capacity,
"Cannot inspect ONNX Runtime %s: %s", filename, strerror(errno));
109 if (!S_ISREG(info.st_mode)) {
110 snprintf(
error,
capacity,
"ONNX Runtime is not a regular file: %s", filename);
113 void *library = dlopen(filename, RTLD_NOW | RTLD_LOCAL);
116 snprintf(
error,
capacity,
"Cannot load ONNX Runtime %s: %s", filename, dlerror());
121 const OrtApiBase *(*function)(void);
122 } symbol = {.object = dlsym(library,
"OrtGetApiBase")};
124 const OrtApiBase *base = symbol.function ? symbol.function() : NULL;
125 const OrtApi *api = base ? base->GetApi(ORT_API_VERSION) : NULL;
127 snprintf(
error,
capacity,
"Incompatible ONNX Runtime API in %s", filename);
131 OrtEnv *environment = NULL;
132 OrtStatus *status = api->CreateEnv(ORT_LOGGING_LEVEL_WARNING,
"sensoraia", &environment);
134 snprintf(
error,
capacity,
"Cannot initialize ONNX Runtime: %s", api->GetErrorMessage(status));
135 api->ReleaseStatus(status);
148 "ONNX Runtime not found in searched directories: %s, %s, %s. "
149 "Tried libonnxruntime.so, libonnxruntime.so.1, libonnxruntime.so.1.31.0%s. "
150 "Copy a compatible runtime beside the executable or set onnx_lib_path in diplacam_settings.ini.",
151 executable, secondary, library_directory ? library_directory :
"(not configured)",
152#
if defined(__aarch64__)
153 " and their -arm64 variants"
160 const size_t used = strlen(
error);
162 snprintf(
error + used,
capacity - used,
". Copy a compatible %s into %s or set onnx_lib_path in diplacam_settings.ini.",
163 names[0], executable);
bool onnx_runtime_open(const char *library_directory, char *error, size_t capacity)
Initialize one process-lifetime runtime/environment using explicit local search roots.