1 | // Copyright 2009-2021 Intel Corporation |
2 | // SPDX-License-Identifier: Apache-2.0 |
3 | |
4 | #include "library.h" |
5 | #include "sysinfo.h" |
6 | #include "filename.h" |
7 | |
8 | //////////////////////////////////////////////////////////////////////////////// |
9 | /// Windows Platform |
10 | //////////////////////////////////////////////////////////////////////////////// |
11 | |
12 | #if defined(__WIN32__) |
13 | |
14 | #define WIN32_LEAN_AND_MEAN |
15 | #include <windows.h> |
16 | |
17 | namespace embree |
18 | { |
19 | /* opens a shared library */ |
20 | lib_t openLibrary(const std::string& file) |
21 | { |
22 | std::string fullName = file+".dll" ; |
23 | FileName executable = getExecutableFileName(); |
24 | HANDLE handle = LoadLibrary((executable.path() + fullName).c_str()); |
25 | return lib_t(handle); |
26 | } |
27 | |
28 | /* returns address of a symbol from the library */ |
29 | void* getSymbol(lib_t lib, const std::string& sym) { |
30 | return (void*)GetProcAddress(HMODULE(lib),sym.c_str()); |
31 | } |
32 | |
33 | /* closes the shared library */ |
34 | void closeLibrary(lib_t lib) { |
35 | FreeLibrary(HMODULE(lib)); |
36 | } |
37 | } |
38 | #endif |
39 | |
40 | //////////////////////////////////////////////////////////////////////////////// |
41 | /// Unix Platform |
42 | //////////////////////////////////////////////////////////////////////////////// |
43 | |
44 | #if defined(__UNIX__) |
45 | |
46 | #include <dlfcn.h> |
47 | |
48 | namespace embree |
49 | { |
50 | /* opens a shared library */ |
51 | lib_t openLibrary(const std::string& file) |
52 | { |
53 | #if defined(__MACOSX__) |
54 | std::string fullName = "lib" +file+".dylib" ; |
55 | #else |
56 | std::string fullName = "lib" +file+".so" ; |
57 | #endif |
58 | void* lib = dlopen(fullName.c_str(), RTLD_NOW); |
59 | if (lib) return lib_t(lib); |
60 | FileName executable = getExecutableFileName(); |
61 | lib = dlopen((executable.path() + fullName).c_str(),RTLD_NOW); |
62 | if (lib == nullptr) { |
63 | const char* error = dlerror(); |
64 | if (error) { |
65 | THROW_RUNTIME_ERROR(error); |
66 | } else { |
67 | THROW_RUNTIME_ERROR("could not load library " +executable.str()); |
68 | } |
69 | } |
70 | return lib_t(lib); |
71 | } |
72 | |
73 | /* returns address of a symbol from the library */ |
74 | void* getSymbol(lib_t lib, const std::string& sym) { |
75 | return dlsym(lib,sym.c_str()); |
76 | } |
77 | |
78 | /* closes the shared library */ |
79 | void closeLibrary(lib_t lib) { |
80 | dlclose(lib); |
81 | } |
82 | } |
83 | #endif |
84 | |