From 3be8c4714a7ff67e3a7a22d210caded3eec9c60f Mon Sep 17 00:00:00 2001 From: zhaodezan Date: Fri, 7 May 2021 21:45:05 +0800 Subject: [PATCH] windows dlopen --- mindspore/lite/src/common/loader_util.cc | 60 ++++++++++++++----- mindspore/lite/src/common/loader_util.h | 8 +-- .../lite/test/ut/src/loader_util_test.cc | 8 +-- 3 files changed, 50 insertions(+), 26 deletions(-) diff --git a/mindspore/lite/src/common/loader_util.cc b/mindspore/lite/src/common/loader_util.cc index 582723ef3c..d2d24b3f08 100644 --- a/mindspore/lite/src/common/loader_util.cc +++ b/mindspore/lite/src/common/loader_util.cc @@ -18,43 +18,73 @@ #include #include #include "include/errorcode.h" -#include "src/common/log_util.h" - #ifndef _WIN32 +#include +#else +#include +#endif + +#define LOG_ERROR(content, args...) \ + { printf("[ERROR] %s|%d|%s: " #content "\r\n", __FILE__, __LINE__, __func__, ##args); } namespace mindspore { namespace lite { -int SoLoader::Open(const char *so_path, int mode) { - if ((strlen(so_path)) >= PATH_MAX) { - MS_LOG(ERROR) << "path is too long"; +int DynamicLibraryLoader::Open(const char *lib_path) { + if ((strlen(lib_path)) >= PATH_MAX) { + LOG_ERROR("path is too long"); return RET_ERROR; } char resolved_path[PATH_MAX]; - auto resolve_res = realpath(so_path, resolved_path); - if (resolve_res == nullptr) { - MS_LOG(ERROR) << "path not exist"; + +#ifndef _WIN32 + char *real_path = realpath(lib_path, resolved_path); +#else + char *real_path = _fullpath(resolved_path, lib_path, 1024); +#endif + + if (real_path == nullptr) { + LOG_ERROR("path not exist"); return RET_ERROR; } - handler_ = dlopen(so_path, mode); + +#ifndef _WIN32 + handler_ = dlopen(lib_path, RTLD_LAZY); +#else + handler_ = LoadLibrary(lib_path); +#endif + if (handler_ == nullptr) { - MS_LOG(ERROR) << "open path failed"; + LOG_ERROR("open path failed"); return RET_ERROR; } return RET_OK; } -void *SoLoader::GetFunc(const char *func_name) { return dlsym(handler_, func_name); } +void *DynamicLibraryLoader::GetFunc(const char *func_name) { +#ifndef _WIN32 + return dlsym(handler_, func_name); +#else + auto func = GetProcAddress(reinterpret_cast(handler_), func_name); + return reinterpret_cast(func); +#endif +} -int SoLoader::Close() { +int DynamicLibraryLoader::Close() { +#ifndef _WIN32 auto close_res = dlclose(handler_); if (close_res != 0) { - MS_LOG(ERROR) << "can not close handler"; + LOG_ERROR("can not close handler"); return RET_ERROR; } +#else + auto close_res = FreeLibrary(reinterpret_cast(handler_)); + if (close_res == 0) { + LOG_ERROR("can not close handler"); + return RET_ERROR; + } +#endif return RET_OK; } } // namespace lite } // namespace mindspore - -#endif diff --git a/mindspore/lite/src/common/loader_util.h b/mindspore/lite/src/common/loader_util.h index 2c01424d41..ac2458ea4f 100644 --- a/mindspore/lite/src/common/loader_util.h +++ b/mindspore/lite/src/common/loader_util.h @@ -17,15 +17,12 @@ #ifndef MINDSPORE_LITE_SRC_COMMON_LOADER_UTIL_H_ #define MINDSPORE_LITE_SRC_COMMON_LOADER_UTIL_H_ -#ifndef _WIN32 -#include - namespace mindspore { namespace lite { -class SoLoader { +class DynamicLibraryLoader { public: - int Open(const char *so_path, int mode = RTLD_LAZY); + int Open(const char *lib_path); void *GetFunc(const char *func_name); int Close(); @@ -37,4 +34,3 @@ class SoLoader { } // namespace mindspore #endif -#endif diff --git a/mindspore/lite/test/ut/src/loader_util_test.cc b/mindspore/lite/test/ut/src/loader_util_test.cc index 13c3d95531..8112652cfa 100644 --- a/mindspore/lite/test/ut/src/loader_util_test.cc +++ b/mindspore/lite/test/ut/src/loader_util_test.cc @@ -24,7 +24,7 @@ class LoaderUtilTest : public mindspore::CommonTest { }; /* - in file add.cc, the code is: + in file add.c, the code is: int add(int a, int b) {return a + b;} use this command to generate so file: gcc add.cc -fPIC -shared -o libadd.so @@ -32,14 +32,12 @@ class LoaderUtilTest : public mindspore::CommonTest { nm -D libadd.so */ TEST_F(LoaderUtilTest, TestAdd) { -#ifndef _WIN32 - lite::SoLoader loader; + lite::DynamicLibraryLoader loader; loader.Open("./libadd.so"); int (*add)(int a, int b); - add = (int (*)(int, int))loader.GetFunc("_Z3addii"); + add = (int (*)(int, int))loader.GetFunc("add"); int res = add(7, 8); loader.Close(); ASSERT_EQ(15, res); -#endif } } // namespace mindspore