forked from arrayfire/arrayfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDependencyModule.cpp
More file actions
170 lines (145 loc) · 5.36 KB
/
DependencyModule.cpp
File metadata and controls
170 lines (145 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*******************************************************
* Copyright (c) 2018, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <common/DependencyModule.hpp>
#include <common/Logger.hpp>
#include <common/module_loading.hpp>
#include <algorithm>
#include <string>
#ifdef OS_WIN
#include <Windows.h>
#else
#include <dlfcn.h>
#endif
using common::Version;
using std::make_tuple;
using std::string;
using std::to_string;
using std::vector;
constexpr Version NullVersion{-1, -1, -1};
#ifdef OS_WIN
#include <Windows.h>
static const char* librarySuffix = ".dll";
namespace {
vector<string> libNames(const std::string& name, const string& suffix,
const Version& ver = NullVersion) {
UNUSED(ver); // Windows DLL files are not version suffixed
return {name + suffix + librarySuffix};
}
} // namespace
#elif defined(OS_MAC)
static const char* librarySuffix = ".dylib";
static const char* libraryPrefix = "lib";
namespace {
vector<string> libNames(const std::string& name, const string& suffix,
const Version& ver = NullVersion) {
UNUSED(suffix);
const string noVerName = libraryPrefix + name + librarySuffix;
if (ver != NullVersion) {
const string infix = "." + to_string(std::get<0>(ver)) + ".";
return {libraryPrefix + name + infix + librarySuffix, noVerName};
} else {
return {noVerName};
}
}
} // namespace
#elif defined(OS_LNX)
static const char* librarySuffix = ".so";
static const char* libraryPrefix = "lib";
namespace {
vector<string> libNames(const std::string& name, const string& suffix,
const Version& ver = NullVersion) {
UNUSED(suffix);
const string noVerName = libraryPrefix + name + librarySuffix;
if (ver != NullVersion) {
const string soname("." + to_string(std::get<0>(ver)));
const string vsfx = "." + to_string(std::get<0>(ver)) + "." +
to_string(std::get<1>(ver)) + "." +
to_string(std::get<2>(ver));
return {noVerName + vsfx, noVerName + soname, noVerName};
} else {
return {noVerName};
}
}
} // namespace
#else
#error "Unsupported platform"
#endif
namespace common {
DependencyModule::DependencyModule(const char* plugin_file_name,
const char** paths)
: handle(nullptr), logger(common::loggerFactory("platform")) {
// TODO(umar): Implement handling of non-standard paths
UNUSED(paths);
if (plugin_file_name) {
auto fileNames = libNames(plugin_file_name, "");
AF_TRACE("Attempting to load: {}", fileNames[0]);
handle = loadLibrary(fileNames[0].c_str());
if (handle) {
AF_TRACE("Found: {}", fileNames[0]);
} else {
AF_TRACE("Unable to open {}", plugin_file_name);
}
}
}
DependencyModule::DependencyModule(const vector<string>& plugin_base_file_name,
const vector<string>& suffixes,
const vector<string>& paths,
const size_t verListSize,
const Version* versions)
: handle(nullptr), logger(common::loggerFactory("platform")) {
for (const string& base_name : plugin_base_file_name) {
for (const string& path : paths) {
UNUSED(path);
for (const string& suffix : suffixes) {
#if !defined(OS_WIN)
// For a non-windows OS, i.e. most likely unix, shared library
// names have versions suffix based on the version. Lookup for
// libraries for given versions and proceed to a simple name
// lookup if versioned library is not found.
for (size_t v = 0; v < verListSize; v++) {
auto fileNames = libNames(base_name, suffix, versions[v]);
for (auto& fileName : fileNames) {
AF_TRACE("Attempting to load: {}", fileName);
handle = loadLibrary(fileName.c_str());
if (handle) {
AF_TRACE("Found: {}", fileName);
return;
}
}
}
#endif
auto fileNames = libNames(base_name, suffix);
AF_TRACE("Attempting to load: {}", fileNames[0]);
handle = loadLibrary(fileNames[0].c_str());
if (handle) {
AF_TRACE("Found: {}", fileNames[0]);
return;
}
}
}
}
AF_TRACE("Unable to open {}", plugin_base_file_name[0]);
}
DependencyModule::~DependencyModule() noexcept {
if (handle) { unloadLibrary(handle); }
}
bool DependencyModule::isLoaded() const noexcept {
return static_cast<bool>(handle);
}
bool DependencyModule::symbolsLoaded() const noexcept {
return all_of(begin(functions), end(functions),
[](void* ptr) { return ptr != nullptr; });
}
string DependencyModule::getErrorMessage() noexcept {
return common::getErrorMessage();
}
spdlog::logger* DependencyModule::getLogger() const noexcept {
return logger.get();
}
} // namespace common