• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef _DYNAMIC_LOADER_H
17 #define _DYNAMIC_LOADER_H
18 
19 #include <string>
20 
21 #ifdef KOALA_WINDOWS
22 #include <windows.h>
23 // Here we need to find module where GetArkUINodeAPI()
24 // function is implemented.
loadLibrary(const std::string & libPath)25 inline void* loadLibrary(const std::string& libPath) {
26     return LoadLibraryA(libPath.c_str());
27 }
28 
libraryError()29 inline const char* libraryError() {
30     static char error[256];
31     snprintf(error, sizeof error, "error %lu", GetLastError());
32     return error;
33 }
34 
findSymbol(void * library,const char * name)35 inline void* findSymbol(void* library, const char* name) {
36     return (void*)GetProcAddress(reinterpret_cast<HMODULE>(library), name);
37 }
38 
libName(const char * lib)39 inline std::string libName(const char* lib) {
40     return std::string(lib) + ".dll";
41 }
42 
43 #elif defined(KOALA_LINUX) || defined(KOALA_MACOS) || defined(KOALA_OHOS)
44 #include <dlfcn.h>
45 
loadLibrary(const std::string & libPath)46 inline void* loadLibrary(const std::string& libPath) {
47     void* handle = dlopen(libPath.c_str(), RTLD_LOCAL | RTLD_NOW);
48     if (!handle) {
49         return nullptr;
50     }
51     return handle;
52 }
53 
libraryError()54 inline const char* libraryError() {
55     return dlerror();
56 }
57 
symbolName(const char * name)58 inline std::string symbolName(const char* name) {
59     return name;
60 }
61 
findSymbol(void * library,const char * name)62 inline void* findSymbol(void* library, const char* name) {
63     return dlsym(library, symbolName(name).c_str());
64 }
65 
libName(const char * lib)66 inline std::string libName(const char* lib) {
67     std::string result;
68     std::string suffix =
69 #ifdef KOALA_MACOS
70     ".dylib"
71 #else
72     ".so"
73 #endif
74     ;
75     result = "lib" + std::string(lib) + suffix;
76     return result;
77 }
78 
79 #else
80 
81 #include <cstdio>
82 
loadLibrary(const std::string & libPath)83 inline void* loadLibrary(const std::string& libPath) {
84     fprintf(stderr, "No loadLibrary() on this platform\n");
85     return nullptr;
86 }
87 
libraryError()88 inline const char* libraryError() {
89     fprintf(stderr, "No libraryError() on this platform\n");
90     return nullptr;
91 }
92 
symbolName(const char * name)93 inline std::string symbolName(const char* name) {
94     fprintf(stderr, "No symbolName() on this platform\n");
95     return "";
96 }
97 
findSymbol(void * library,const char * name)98 inline void* findSymbol(void* library, const char* name) {
99     fprintf(stderr, "No findSymbol() on this platform\n");
100     return nullptr;
101 }
102 
libName(const char * lib)103 inline std::string libName(const char* lib) {
104     fprintf(stderr, "No libName() on this platform\n");
105     return "";
106 }
107 
108 #endif
109 
110 #endif  // _DYNAMIC_LOADER_H