• 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 _WIN32
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 {
27     return LoadLibraryA(libPath.c_str());
28 }
29 
LibraryError()30 inline const char *LibraryError()
31 {
32     static char error[256];
33     return error;
34 }
35 
FindSymbol(void * library,const char * name)36 inline void *FindSymbol(void *library, const char *name)
37 {
38     return (void *)GetProcAddress(reinterpret_cast<HMODULE>(library), name);
39 }
40 
LibName(const char * lib)41 inline std::string LibName(const char *lib)
42 {
43     return std::string(lib) + ".dll";
44 }
45 
46 #elif defined(__linux__) || defined(__APPLE__)
47 #include <dlfcn.h>
48 
LoadLibrary(const std::string & libPath)49 inline void *LoadLibrary(const std::string &libPath)
50 {
51     void *handle = dlopen(libPath.c_str(), RTLD_LOCAL | RTLD_NOW);
52     if (handle == nullptr) {
53         return nullptr;
54     }
55     return handle;
56 }
57 
58 // NOLINTNEXTLINE(modernize-redundant-void-arg)
LibraryError(void)59 inline const char *LibraryError(void)
60 {
61     return dlerror();
62 }
63 
64 // CC-OFFNXT(G.NAM.01) false positive
SymbolName(const char * name)65 inline std::string SymbolName(const char *name)
66 {
67     return name;
68 }
69 
FindSymbol(void * library,const char * name)70 inline void *FindSymbol(void *library, const char *name)
71 {
72     return dlsym(library, SymbolName(name).c_str());
73 }
74 
75 // CC-OFFNXT(G.NAM.01) false positive
LibName(const char * lib)76 inline std::string LibName(const char *lib)
77 {
78     std::string result;
79     std::string suffix = ".so";
80     result = "lib" + std::string(lib) + suffix;
81     return result;
82 }
83 
84 #else
85 
86 #include <cstdio>
87 
LoadLibrary(const std::string & libPath)88 inline void *LoadLibrary(const std::string &libPath)
89 {
90     return nullptr;
91 }
92 
LibraryError()93 inline const char *LibraryError()
94 {
95     return nullptr;
96 }
97 
SymbolName(const char * name)98 inline std::string SymbolName(const char *name)
99 {
100     return "";
101 }
102 
FindSymbol(void * library,const char * name)103 inline void *FindSymbol(void *library, const char *name)
104 {
105     return nullptr;
106 }
107 
LibName(const char * lib)108 inline std::string LibName(const char *lib)
109 {
110     return "";
111 }
112 
113 #endif
114 
115 #endif  // _DYNAMIC_LOADER_H