xref: /oneTBB/test/common/utils_dynamic_libs.h (revision 68e075cb)
1 /*
2     Copyright (c) 2005-2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 #ifndef __TBB_test_common_utils_dynamic_libs_H_
18 #define __TBB_test_common_utils_dynamic_libs_H_
19 
20 #include "common/test.h"
21 #include "oneapi/tbb/version.h"
22 
23 #if __TBB_DYNAMIC_LOAD_ENABLED
24 
25 #if _WIN32 || _WIN64
26 #include <Windows.h>
27 #else
28 #include <dlfcn.h>
29 #endif
30 
31 namespace utils {
32 
33 #if TBB_USE_DEBUG
34 #define SUFFIX1 "_debug"
35 #define SUFFIX2
36 #else
37 #define SUFFIX1
38 #define SUFFIX2 "_debug"
39 #endif /* TBB_USE_DEBUG */
40 
41 #if _WIN32||_WIN64
42 #define PREFIX
43 #define EXT ".dll"
44 #else
45 #define PREFIX "lib"
46 #if __APPLE__
47 #define EXT ".dylib"
48 // Android SDK build system does not support .so file name versioning
49 #elif __FreeBSD__ || __NetBSD__ || __sun || _AIX || __ANDROID__
50 #define EXT ".so"
51 #elif __unix__  // Order of these elif's matters!
52 #define EXT __TBB_STRING(.so.2)
53 #else
54 #error Unknown OS
55 #endif
56 #endif
57 
58 // Form the names of the TBB memory allocator binaries.
59 #define MALLOCLIB_NAME1 PREFIX "tbbmalloc" SUFFIX1 EXT
60 #define MALLOCLIB_NAME2 PREFIX "tbbmalloc" SUFFIX2 EXT
61 
62 #if _WIN32 || _WIN64
63 using LIBRARY_HANDLE = HMODULE;
64 #else
65 using LIBRARY_HANDLE = void*;
66 #endif
67 
68 #if _WIN32 || _WIN64
69 #define TEST_LIBRARY_NAME(base) PREFIX base SUFFIX1 ".dll"
70 #elif __APPLE__
71 #define TEST_LIBRARY_NAME(base) PREFIX base SUFFIX1 ".dylib"
72 #else
73 #define TEST_LIBRARY_NAME(base) PREFIX base SUFFIX1 ".so"
74 #endif
75 
76 LIBRARY_HANDLE OpenLibrary(const char *name)
77 {
78 #if _WIN32 || _WIN64
79 #if __TBB_WIN8UI_SUPPORT
80     TCHAR wlibrary[MAX_PATH];
81     if (MultiByteToWideChar(CP_UTF8, 0, name, -1, wlibrary, MAX_PATH) == 0) return false;
82     return ::LoadPackagedLibrary(wlibrary, 0);
83 #else
84     return ::LoadLibrary(name);
85 #endif
86 #else
87     return dlopen(name, RTLD_NOW|RTLD_GLOBAL);
88 #endif
89 }
90 
91 void CloseLibrary(LIBRARY_HANDLE lib)
92 {
93 #if _WIN32 || _WIN64
94     BOOL ret = FreeLibrary(lib);
95     REQUIRE_MESSAGE(ret, "FreeLibrary must be successful");
96 #else
97     int ret = dlclose(lib);
98     REQUIRE_MESSAGE(ret == 0, "dlclose must be successful");
99 #endif
100 }
101 
102 typedef void (*FunctionAddress)();
103 
104 template <typename FunctionPointer>
105 void GetAddress(utils::LIBRARY_HANDLE lib, const char *name, FunctionPointer& func)
106 {
107 #if _WIN32 || _WIN64
108     func = (FunctionPointer)(void*)GetProcAddress(lib, name);
109 #else
110     func = (FunctionPointer)dlsym(lib, name);
111 #endif
112     REQUIRE_MESSAGE(func, "Can't find required symbol in dynamic library");
113 }
114 
115 FunctionAddress GetAddress(utils::LIBRARY_HANDLE lib, const char *name)
116 {
117     FunctionAddress func;
118     GetAddress(lib, name, func);
119     return func;
120 }
121 
122 }  // namespace utils
123 
124 #endif // __TBB_DYNAMIC_LOAD_ENABLED
125 #endif // __TBB_test_common_utils_dynamic_libs_H_
126