Repository: microsoft/onnxruntime
File: onnxruntime/core/platform/posix/env.cc (C++)

Code before:

  // \brief returns a value for the queried variable name (var_name)
  std::string GetEnvironmentVar(const std::string& var_name) const override {
    char* val = getenv(var_name.c_str());
    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");
    // We will find OrtGetApiBase if onnxruntime is loaded as a shared library
    if (dladdr(symbol_from_this_library, &dl_info) && dl_info.dli_fname) {
      return PathString(dl_info.dli_fname) + "/";
    } else {
#if __linux__
      return PathString(std::filesystem::read_symlink(std::filesystem::path("/proc/self/exe")).parent_path()) + "/";
#else
      // TODO: MacOS could use _NSGetExecutablePath, but this needs to be tested!
      return PathString();
#endif
    }
  }

 private:
  Telemetry telemetry_provider_;
#ifdef ORT_USE_CPUINFO
  PosixEnv() {
    cpuinfo_available_ = cpuinfo_initialize();
    if (!cpuinfo_available_) {
      LOGS_DEFAULT(INFO) << "cpuinfo_initialize failed";
    }
  }
  bool cpuinfo_available_{false};
#endif  // ORT_USE_CPUINFO
};

}  // namespace

// REGISTER_FILE_SYSTEM("", PosixFileSystem);
// REGISTER_FILE_SYSTEM("file", LocalPosixFileSystem);
Env& Env::Default() {
  return PosixEnv::Instance();
}

}  // namespace onnxruntime


Review comment (bug):
@edgchen1 This resolves the failing test under Linux. I could alternatively put `std::filesystem::read_symlink(std::filesystem::path("/proc/self/exe")).parent_path()` in the unit test instead and move `PosixEnv::GetRuntimePath` to a separate PR.

I'm not sure whether the Windows behavior to set runtime path to current exe's dir is always desired (but it would be needed to behave the same in tests like on Windows)

Code after:
    }
    // it's possible to get a NULL symbol in our case when Schemas are not custom.
    return common::Status::OK();
  }

  std::string FormatLibraryFileName(const std::string& name, const std::string& version) const override {
    std::string filename;
    if (version.empty()) {
      filename = "lib" + name + ".so";
    } else {
      filename = "lib" + name + ".so" + "." + version;
    }
    return filename;
  }

  // \brief returns a provider that will handle telemetry on the current platform
  const Telemetry& GetTelemetryProvider() const override {
    return telemetry_provider_;
  }

  // \brief returns a value for the queried variable name (var_name)
  std::string GetEnvironmentVar(const std::string& var_name) const override {
    char* val = getenv(var_name.c_str());
    return val == NULL ? std::string() : std::string(val);
  }

 private:
  Telemetry telemetry_provider_;
#ifdef ORT_USE_CPUINFO
  PosixEnv() {
    cpuinfo_available_ = cpuinfo_initialize();
    if (!cpuinfo_available_) {
      LOGS_DEFAULT(INFO) << "cpuinfo_initialize failed";
    }
  }
  bool cpuinfo_available_{false};
#endif  // ORT_USE_CPUINFO
};

}  // namespace

// REGISTER_FILE_SYSTEM("", PosixFileSystem);
// REGISTER_FILE_SYSTEM("file", LocalPosixFileSystem);
Env& Env::Default() {
  return PosixEnv::Instance();
}

}  // namespace onnxruntime