You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

dlopen_helper.h 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #if defined(_WIN32)
  2. #include <windows.h>
  3. #define RTLD_LAZY 0
  4. static void* dlopen(const char* file, int) {
  5. return static_cast<void*>(LoadLibraryA(file));
  6. }
  7. static void* dlerror() {
  8. const char* errmsg = "dlerror not aviable in windows";
  9. return const_cast<char*>(errmsg);
  10. }
  11. static void* dlsym(void* handle, const char* name) {
  12. FARPROC symbol = GetProcAddress((HMODULE)handle, name);
  13. return reinterpret_cast<void*>(symbol);
  14. }
  15. #else
  16. #include <dlfcn.h>
  17. #include <unistd.h>
  18. #endif
  19. #include <sstream>
  20. #include <string>
  21. #include <vector>
  22. static std::vector<std::string> split_string(const std::string& s, char delim) {
  23. std::vector<std::string> elems;
  24. std::stringstream ss(s);
  25. std::string item;
  26. while (std::getline(ss, item, delim)) {
  27. elems.push_back(item);
  28. }
  29. return elems;
  30. }
  31. static std::vector<std::string> get_env_dir(const char* env_name) {
  32. const char* env_p = std::getenv(env_name);
  33. std::vector<std::string> env_dir;
  34. if (env_p) {
  35. env_dir = split_string(env_p, ':');
  36. }
  37. return env_dir;
  38. }
  39. static void* try_open_handle(std::vector<std::string> dir_vec,
  40. std::string default_so_name) {
  41. void* handle = nullptr;
  42. for (auto& tk_path : dir_vec) {
  43. handle = dlopen((tk_path + "/" + default_so_name).c_str(), RTLD_LAZY);
  44. if (handle) {
  45. break;
  46. }
  47. }
  48. return handle;
  49. }
  50. static void* try_open_handle(const char** so_vec, int nr_so) {
  51. void* handle = nullptr;
  52. for (int i = 0; i < nr_so; ++i) {
  53. handle = dlopen(so_vec[i], RTLD_LAZY);
  54. if (handle) {
  55. break;
  56. }
  57. }
  58. return handle;
  59. }
  60. static void* get_library_handle() {
  61. std::vector<std::string> cuda_tk_dir = get_env_dir("CUDA_TK_PATH");
  62. std::vector<std::string> ld_dir = get_env_dir("LD_LIBRARY_PATH");
  63. void* handle = nullptr;
  64. if (!handle) {
  65. handle = try_open_handle(ld_dir, default_so_name);
  66. }
  67. if (!handle) {
  68. handle = try_open_handle(cuda_tk_dir, default_so_name);
  69. }
  70. if (!handle) {
  71. handle = try_open_handle(default_so_paths,
  72. sizeof(default_so_paths) / sizeof(char*));
  73. }
  74. if (!handle) {
  75. handle = try_open_handle(extra_so_paths,
  76. sizeof(extra_so_paths) / sizeof(char*));
  77. }
  78. if (!handle) {
  79. LOGE("Failed to load %s API library", g_default_api_name);
  80. return nullptr;
  81. }
  82. return handle;
  83. }
  84. static void log_failed_load(int func_idx) {
  85. LOGE("failed to load %s func: %s", g_default_api_name,
  86. g_func_name[func_idx]);
  87. }
  88. static void* resolve_library_func(void* handle, const char* func) {
  89. if (!handle) {
  90. LOGE("%s handle should not be nullptr!", g_default_api_name);
  91. return nullptr;
  92. }
  93. auto ret = dlsym(handle, func);
  94. if (!ret) {
  95. LOGE("failed to load %s func: %s", g_default_api_name, func);
  96. }
  97. return ret;
  98. }