Skip to content

Commit 0bf0c6b

Browse files
committed
[CORE]: Implement PosixEnv::GetRuntimePath
We try to follow the implementation of WindowsEnv which resolves the runtime directory as the directory of the current DLL (onnxruntime) or executable for static linkage.
1 parent 7d1c75f commit 0bf0c6b

File tree

1 file changed

+20
-0
lines changed
  • onnxruntime/core/platform/posix

1 file changed

+20
-0
lines changed

onnxruntime/core/platform/posix/env.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ limitations under the License.
2626
#include <stdlib.h>
2727
#include <string.h>
2828
#include <sys/mman.h>
29+
#include <filesystem>
2930
#if !defined(_AIX)
3031
#include <sys/syscall.h>
3132
#endif
@@ -592,6 +593,25 @@ class PosixEnv : public Env {
592593
return val == NULL ? std::string() : std::string(val);
593594
}
594595

596+
// Return the path of the executable/shared library for the current running code. This is to make it
597+
// possible to load other shared libraries installed next to our core runtime code.
598+
PathString GetRuntimePath() const override {
599+
Dl_info dl_info{};
600+
// Must be one of the symbols exported in libonnxruntime.{so,dynlib}.
601+
void* symbol_from_this_library = dlsym(RTLD_DEFAULT, "OrtGetApiBase");
602+
// We will find OrtGetApiBase if onnxruntime is loaded as a shared library
603+
if (dladdr(symbol_from_this_library, &dl_info) && dl_info.dli_fname) {
604+
return PathString(dl_info.dli_fname) + "/";
605+
} else {
606+
#if __linux__
607+
return PathString(std::filesystem::read_symlink(std::filesystem::path("/proc/self/exe")).parent_path()) + "/";
608+
#else
609+
// TODO: MacOS could use _NSGetExecutablePath, but this needs to be tested!
610+
return PathString();
611+
#endif
612+
}
613+
}
614+
595615
private:
596616
Telemetry telemetry_provider_;
597617
#ifdef ORT_USE_CPUINFO

0 commit comments

Comments
 (0)