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 #if __MINGW32__ 43 #define PREFIX "lib" 44 #else 45 #define PREFIX 46 #endif 47 #define EXT ".dll" 48 #else 49 #define PREFIX "lib" 50 #if __APPLE__ 51 #define EXT ".dylib" 52 // Android SDK build system does not support .so file name versioning 53 #elif __FreeBSD__ || __NetBSD__ || __sun || _AIX || __ANDROID__ 54 #define EXT ".so" 55 #elif __unix__ // Order of these elif's matters! 56 #define EXT __TBB_STRING(.so.2) 57 #else 58 #error Unknown OS 59 #endif 60 #endif 61 62 // Form the names of the TBB memory allocator binaries. 63 #define MALLOCLIB_NAME1 PREFIX "tbbmalloc" SUFFIX1 EXT 64 #define MALLOCLIB_NAME2 PREFIX "tbbmalloc" SUFFIX2 EXT 65 66 #if _WIN32 || _WIN64 67 using LIBRARY_HANDLE = HMODULE; 68 #else 69 using LIBRARY_HANDLE = void*; 70 #endif 71 72 #if _WIN32 || _WIN64 73 #define TEST_LIBRARY_NAME(base) PREFIX base SUFFIX1 ".dll" 74 #elif __APPLE__ 75 #define TEST_LIBRARY_NAME(base) PREFIX base SUFFIX1 ".dylib" 76 #else 77 #define TEST_LIBRARY_NAME(base) PREFIX base SUFFIX1 ".so" 78 #endif 79 80 LIBRARY_HANDLE OpenLibrary(const char *name) 81 { 82 #if _WIN32 || _WIN64 83 #if __TBB_WIN8UI_SUPPORT 84 TCHAR wlibrary[MAX_PATH]; 85 if (MultiByteToWideChar(CP_UTF8, 0, name, -1, wlibrary, MAX_PATH) == 0) return false; 86 return ::LoadPackagedLibrary(wlibrary, 0); 87 #else 88 return ::LoadLibrary(name); 89 #endif 90 #else 91 return dlopen(name, RTLD_NOW|RTLD_GLOBAL); 92 #endif 93 } 94 95 void CloseLibrary(LIBRARY_HANDLE lib) 96 { 97 #if _WIN32 || _WIN64 98 BOOL ret = FreeLibrary(lib); 99 REQUIRE_MESSAGE(ret, "FreeLibrary must be successful"); 100 #else 101 int ret = dlclose(lib); 102 REQUIRE_MESSAGE(ret == 0, "dlclose must be successful"); 103 #endif 104 } 105 106 typedef void (*FunctionAddress)(); 107 108 template <typename FunctionPointer> 109 void GetAddress(utils::LIBRARY_HANDLE lib, const char *name, FunctionPointer& func) 110 { 111 #if _WIN32 || _WIN64 112 func = (FunctionPointer)(void*)GetProcAddress(lib, name); 113 #else 114 func = (FunctionPointer)dlsym(lib, name); 115 #endif 116 REQUIRE_MESSAGE(func, "Can't find required symbol in dynamic library"); 117 } 118 119 FunctionAddress GetAddress(utils::LIBRARY_HANDLE lib, const char *name) 120 { 121 FunctionAddress func; 122 GetAddress(lib, name, func); 123 return func; 124 } 125 126 } // namespace utils 127 128 #endif // __TBB_DYNAMIC_LOAD_ENABLED 129 #endif // __TBB_test_common_utils_dynamic_libs_H_ 130