Skip to content

Commit b4a38fc

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 b4a38fc

File tree

1 file changed

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

1 file changed

+41
-0
lines changed

onnxruntime/core/platform/posix/env.cc

Lines changed: 41 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
@@ -591,6 +592,46 @@ class PosixEnv : public Env {
591592
char* val = getenv(var_name.c_str());
592593
return val == NULL ? std::string() : std::string(val);
593594
}
595+
// Return the path of the executable/shared library for the current running code. This is to make it
596+
// possible to load other shared libraries installed next to our core runtime code.
597+
PathString GetRuntimePath() const override {
598+
Dl_info dl_info{};
599+
// Must be one of the symbols exported in libonnxruntime.{so,dynlib}.
600+
void* symbol_from_this_library = dlsym(RTLD_DEFAULT, "OrtGetApiBase");
601+
// We will find OrtGetApiBase if onnxruntime is loaded as a shared library
602+
if (dladdr(symbol_from_this_library, &dl_info) && dl_info.dli_fname) {
603+
return PathString(dl_info.dli_fname) + "/";
604+
} else {
605+
// else use path of current executable to mirror Windows behavior
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+
615+
616+
// Return the path of the executable/shared library for the current running code. This is to make it
617+
// possible to load other shared libraries installed next to our core runtime code.
618+
PathString GetRuntimePath() const override {
619+
Dl_info dl_info{};
620+
// Must be one of the symbols exported in libonnxruntime.{so,dynlib}.
621+
void* symbol_from_this_library = dlsym(RTLD_DEFAULT, "OrtGetApiBase");
622+
// We will find OrtGetApiBase if onnxruntime is loaded as a shared library
623+
if (dladdr(symbol_from_this_library, &dl_info) && dl_info.dli_fname) {
624+
return PathString(dl_info.dli_fname) + "/";
625+
} else {
626+
// else use path of current executable to mirror Windows behavior
627+
#if __linux__
628+
return PathString(std::filesystem::read_symlink(std::filesystem::path("/proc/self/exe")).parent_path()) + "/";
629+
#else
630+
// TODO: MacOS could use _NSGetExecutablePath, but this needs to be tested!
631+
return PathString();
632+
#endif
633+
}
634+
}
594635

595636
private:
596637
Telemetry telemetry_provider_;

0 commit comments

Comments
 (0)