diff --git a/cmake/onnxruntime_unittests.cmake b/cmake/onnxruntime_unittests.cmake index 460736ff8506e..e3bcb9bcaa592 100644 --- a/cmake/onnxruntime_unittests.cmake +++ b/cmake/onnxruntime_unittests.cmake @@ -1979,7 +1979,7 @@ endif() # Build library that can be used with RegisterExecutionProviderLibrary and automatic EP selection # We need a shared lib build to use that as a dependency for the test library # Currently we only have device discovery on Windows so no point building the test app on other platforms. -if (WIN32 AND onnxruntime_BUILD_SHARED_LIB AND +if (onnxruntime_BUILD_SHARED_LIB AND NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten" AND NOT onnxruntime_MINIMAL_BUILD) # example_plugin_ep diff --git a/onnxruntime/core/platform/posix/env.cc b/onnxruntime/core/platform/posix/env.cc index fed697ea962bc..e10f3f3ab30ff 100644 --- a/onnxruntime/core/platform/posix/env.cc +++ b/onnxruntime/core/platform/posix/env.cc @@ -26,6 +26,7 @@ limitations under the License. #include #include #include +#include #if !defined(_AIX) #include #endif @@ -592,6 +593,31 @@ class PosixEnv : public Env { return val == NULL ? std::string() : std::string(val); } + // Return the path of the executable/shared library for the current running code. This is to make it + // possible to load other shared libraries installed next to our core runtime code. + PathString GetRuntimePath() const override { + Dl_info dl_info{}; + // Must be one of the symbols exported in libonnxruntime.{so,dynlib}. + void* symbol_from_this_library = dlsym(RTLD_DEFAULT, "OrtGetApiBase"); + PathString runtime_path; + // We will find OrtGetApiBase if onnxruntime is loaded as a shared library + if (dladdr(symbol_from_this_library, &dl_info) && dl_info.dli_fname) { + // Converting to absolute path since running a program with and without gdb attached can make a difference whether + // dli_fname will be absolute or relative. Converting to absolute for consistent result + runtime_path = PathString(std::filesystem::absolute(std::filesystem::path(dl_info.dli_fname).parent_path().string())) + "/"; + } else { + // else use path of current executable to mirror Windows behavior +#if __linux__ + runtime_path = PathString(std::filesystem::absolute(std::filesystem::read_symlink(std::filesystem::path("/proc/self/exe")).parent_path())) + "/"; +#else + // TODO: MacOS could use _NSGetExecutablePath, but this needs to be tested! + runtime_path = PathString(); +#endif + } + LOGS_DEFAULT(VERBOSE) << "Determined PosixEnv runtime path as \"" << runtime_path << "\""; + return runtime_path; + } + private: Telemetry telemetry_provider_; #ifdef ORT_USE_CPUINFO diff --git a/onnxruntime/core/providers/cuda/version_script.lds b/onnxruntime/core/providers/cuda/version_script.lds index c02a8e4bcf724..7ce96e3e611c0 100644 --- a/onnxruntime/core/providers/cuda/version_script.lds +++ b/onnxruntime/core/providers/cuda/version_script.lds @@ -2,6 +2,8 @@ VERS_1.0 { global: GetProvider; + CreateEpFactories; + ReleaseEpFactory; _binary_*; # Hide everything else. diff --git a/onnxruntime/test/autoep/test_selection.cc b/onnxruntime/test/autoep/test_selection.cc index 72f39be917f90..60506a1d0c887 100644 --- a/onnxruntime/test/autoep/test_selection.cc +++ b/onnxruntime/test/autoep/test_selection.cc @@ -1,8 +1,6 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -// registration/selection is only supported on windows as there's no device discovery on other platforms -#ifdef _WIN32 #include // #include @@ -171,7 +169,11 @@ TEST(AutoEpSelection, CpuEP) { TEST(AutoEpSelection, CudaEP) { Ort::KeyValuePairs provider_options; provider_options.Add("prefer_nhwc", "1"); - RunBasicTest(kCudaExecutionProvider, "onnxruntime_providers_cuda", provider_options); +#if __linux__ + RunBasicTest(kCudaExecutionProvider, "libonnxruntime_providers_cuda.so", provider_options); +#else + RunBasicTest(kCudaExecutionProvider, "onnxruntime_providers_cuda.dll", provider_options); +#endif } #endif @@ -502,5 +504,3 @@ TEST(AutoEpSelection, PolicyDelegateReturnsError) { } // namespace test } // namespace onnxruntime - -#endif // _WIN32