This repository has been archived by the owner on Feb 21, 2019. It is now read-only.
/
module.h
299 lines (276 loc) · 9.94 KB
/
module.h
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* #%L
* OME-COMMON C++ library for C++ compatibility/portability
* %%
* Copyright © 2006 - 2016 Open Microscopy Environment:
* - Massachusetts Institute of Technology
* - National Institutes of Health
* - University of Dundee
* - Board of Regents of the University of Wisconsin-Madison
* - Glencoe Software, Inc.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of any organization.
* #L%
*/
/**
* @file ome/common/module.h Module loading and introspection.
*
* This header defines functions for introspecting the runtime
* location of modules.
*/
#ifndef OME_COMMON_MODULE_H
#define OME_COMMON_MODULE_H
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <ome/common/config.h>
#include <ome/common/filesystem.h>
/**
* Open Microscopy Environment C++.
*/
namespace ome
{
namespace common
{
/**
* Get the runtime installation prefix path for a module.
*
* This is intended primarily for internal use, to allow discovery
* of the location of datafiles, loadable modules, etc. However,
* it may be freely used by additional components, both OME and
* third-party, to register paths.
*
* @param dtype the directory type to query.
* @returns the installation prefix path.
* @throws a @c std::runtime_error if the path could not be
* determined.
*/
const boost::filesystem::path&
module_runtime_path(const std::string& dtype);
/**
* Register OME-Common module paths.
*
* This function forces path registration.
*
* @note This is a hack to allow static linking to work on
* Windows; without this, the module object is omitted and the
* paths aren't automatically registered. This will no longer be
* required once it is built as a DLL. Its only purpose is to
* force object inclusion when static linking, and ensure that the
* registration happens independently of object static
* construction order to allow use prior to main() entry. You
* should not use this.
*/
void
register_module_paths();
/**
* A run-time path for a given module.
*
* This is used to find the location of in installation path at
* runtime, handling relocatable installs by introspecting the
* installation prefix and also by allowing overriding of the path
* by an environment variable.
*/
struct Module
{
/// Name of the path, e.g. "bin" or "ome-xml-schema".
std::string name;
/// Name of the environment variable used to override the
/// autodetected path.
std::string envvar;
/// Name of the environment variable used to specify the
/// module installation root.
std::string module_envvar;
/// Name of the environment variable used to specify the
/// installation root.
std::string root_envvar;
/// Absolute path (used when configured to use an absolute
/// install path).
boost::filesystem::path abspath;
/// Relative path (used for relocatable installs).
boost::filesystem::path relpath;
/// Absolute installation path (used for non-relocatable installs).
boost::filesystem::path install_prefix;
/// Shared library path (used for relocatable installs).
boost::filesystem::path shlibpath;
/// The detected path (used to cache search result).
boost::filesystem::path realpath;
/// Function to obtain the absolute path of the module providing
/// the path (from the shared library or DLL); this won't work
/// when static libraries are in use. Used to introspect the
/// installation path.
boost::filesystem::path (*module_path)();
/**
* Constructor.
*
* @param name the name of the module path.
* @param envvar the environment variable to override the path.
* @param module_envvar the environment variable to override the
* module installation path.
* @param root_envvar the environment variable to override the
* root installation path.
* @param abspath the absolute path.
* @param relpath the relative path.
* @param install_prefix the absolute installation path.
* @param shlibpath the absolute path to the shared library.
* @param module_path a function pointer to provide the module
* installation path, or null to skip introspection.
*/
Module(const std::string& name,
const std::string& envvar,
const std::string& module_envvar,
const std::string& root_envvar,
const boost::filesystem::path& abspath,
const boost::filesystem::path& relpath,
const boost::filesystem::path& install_prefix,
const boost::filesystem::path& shlibpath,
boost::filesystem::path (*module_path)());
};
/**
* Register a module to make it available to module_runtime_path().
*
* The arguments are used to construct a Module object and insert
* it into a map for module lookups.
*/
struct RegisterModule
{
/// Name of the path, e.g. "bin" or "ome-xml-schema".
std::string name;
/// Is the path registered in the path map?
bool registered;
/**
* Constructor.
*
* Register the named module.
*
* @param name the name of the module path.
* @param envvar the environment variable to override the path.
* @param module_envvar the environment variable to override the
* module installation path.
* @param root_envvar the environment variable to override the
* root path.
* @param abspath the absolute path.
* @param relpath the relative path.
* @param install_prefix the absolute installation path.
* @param shlibpath the absolute path to the shared library.
* @param module_path a function pointer to provide the module
* installation path.
*/
RegisterModule(const std::string& name,
const std::string& envvar,
const std::string& module_envvar,
const std::string& root_envvar,
const boost::filesystem::path& abspath,
const boost::filesystem::path& relpath,
const boost::filesystem::path& install_prefix,
const boost::filesystem::path& shlibpath,
boost::filesystem::path (*module_path)());
/**
* Destructor.
*
* Unregister the named module.
*/
~RegisterModule();
};
}
}
// Set to include introspection functionality (used for registering
// paths; not for normal use).
#ifdef OME_COMMON_MODULE_INTROSPECTION
#ifdef OME_HAVE_DLADDR
#ifndef _GNU_SOURCE
# define _GNU_SOURCE 1
#endif
#include <dlfcn.h>
#endif // OME_HAVE_DLADDR
#ifdef _MSC_VER
# include <windows.h>
#endif
namespace
{
#ifdef OME_HAVE_DLADDR
Dl_info this_module;
__attribute__((constructor))
void
find_module(void)
{
if(!dladdr(reinterpret_cast<void *>(find_module), &this_module))
{
this_module.dli_fname = 0;
}
}
boost::filesystem::path
module_path()
{
if (this_module.dli_fname)
return canonical(boost::filesystem::path(this_module.dli_fname));
return boost::filesystem::path();
}
#elif _MSC_VER
HMODULE
find_module(void)
{
static bool found_module = false;
static HMODULE this_module;
if (!found_module)
{
if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
reinterpret_cast<LPCWSTR>(&find_module),
&this_module))
{
this_module = 0;
}
found_module = true;
}
return this_module;
}
boost::filesystem::path
module_path()
{
HMODULE this_module = find_module();
if (this_module)
{
WCHAR win_wide_path[MAX_PATH];
GetModuleFileNameW(this_module, win_wide_path, sizeof(win_wide_path));
return boost::filesystem::path(win_wide_path);
}
return boost::filesystem::path();
}
#else // No introspection available
boost::filesystem::path
module_path()
{
return boost::filesystem::path();
}
#endif // _MSC_VER
}
#endif // OME_COMMON_MODULE_INTROSPECTION
#endif // OME_COMMON_MODULE_H
/*
* Local Variables:
* mode:C++
* End:
*/