| 1 |
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
| 2 |
file Copyright.txt or https://cmake.org/licensing#kwsys for details. */ |
| 3 |
#ifndef @KWSYS_NAMESPACE@_DynamicLoader_hxx |
| 4 |
#define @KWSYS_NAMESPACE@_DynamicLoader_hxx |
| 5 |
|
| 6 |
#include <@KWSYS_NAMESPACE@/Configure.hxx> |
| 7 |
|
| 8 |
#include <string> |
| 9 |
|
| 10 |
#if defined(__hpux) |
| 11 |
# include <dl.h> |
| 12 |
#elif defined(_WIN32) && !defined(__CYGWIN__) |
| 13 |
# include <windows.h> |
| 14 |
#elif defined(__APPLE__) |
| 15 |
# include <AvailabilityMacros.h> |
| 16 |
# if MAC_OS_X_VERSION_MAX_ALLOWED < 1030 |
| 17 |
# include <mach-o/dyld.h> |
| 18 |
# endif |
| 19 |
#elif defined(__BEOS__) |
| 20 |
# include <be/kernel/image.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
namespace @KWSYS_NAMESPACE@ { |
| 24 |
/** \class DynamicLoader |
| 25 |
* \brief Portable loading of dynamic libraries or dll's. |
| 26 |
* |
| 27 |
* DynamicLoader provides a portable interface to loading dynamic |
| 28 |
* libraries or dll's into a process. |
| 29 |
* |
| 30 |
* Directory currently works with Windows, Apple, HP-UX and Unix (POSIX) |
| 31 |
* operating systems |
| 32 |
* |
| 33 |
* \warning dlopen on *nix system works the following way: |
| 34 |
* If filename contains a slash ("/"), then it is interpreted as a (relative |
| 35 |
* or absolute) pathname. Otherwise, the dynamic linker searches for the |
| 36 |
* library as follows : see ld.so(8) for further details): |
| 37 |
* Whereas this distinction does not exist on Win32. Therefore ideally you |
| 38 |
* should be doing full path to guarantee to have a consistent way of dealing |
| 39 |
* with dynamic loading of shared library. |
| 40 |
* |
| 41 |
* \warning the Cygwin implementation do not use the Win32 HMODULE. Put extra |
| 42 |
* condition so that we can include the correct declaration (POSIX) |
| 43 |
*/ |
| 44 |
|
| 45 |
class @KWSYS_NAMESPACE@_EXPORT DynamicLoader |
| 46 |
{ |
| 47 |
public: |
| 48 |
// Ugly stuff for library handles |
| 49 |
// They are different on several different OS's |
| 50 |
#if defined(__hpux) |
| 51 |
typedef shl_t LibraryHandle; |
| 52 |
#elif defined(_WIN32) && !defined(__CYGWIN__) |
| 53 |
typedef HMODULE LibraryHandle; |
| 54 |
#elif defined(__APPLE__) |
| 55 |
# if MAC_OS_X_VERSION_MAX_ALLOWED < 1030 |
| 56 |
typedef NSModule LibraryHandle; |
| 57 |
# else |
| 58 |
typedef void* LibraryHandle; |
| 59 |
# endif |
| 60 |
#elif defined(__BEOS__) |
| 61 |
typedef image_id LibraryHandle; |
| 62 |
#else // POSIX |
| 63 |
typedef void* LibraryHandle; |
| 64 |
#endif |
| 65 |
|
| 66 |
// Return type from DynamicLoader::GetSymbolAddress. |
| 67 |
typedef void (*SymbolPointer)(); |
| 68 |
|
| 69 |
enum OpenFlags |
| 70 |
{ |
| 71 |
// Search for dependent libraries beside the library being loaded. |
| 72 |
// |
| 73 |
// This is currently only supported on Windows. |
| 74 |
SearchBesideLibrary = 0x00000001, |
| 75 |
|
| 76 |
// Make loaded symbols visible globally |
| 77 |
// |
| 78 |
// This is currently only supported on *nix systems. |
| 79 |
RTLDGlobal = 0x00000002, |
| 80 |
|
| 81 |
AllOpenFlags = SearchBesideLibrary | RTLDGlobal |
| 82 |
}; |
| 83 |
|
| 84 |
/** Load a dynamic library into the current process. |
| 85 |
* The returned LibraryHandle can be used to access the symbols in the |
| 86 |
* library. The optional second argument is a set of flags to use when |
| 87 |
* opening the library. If unrecognized or unsupported flags are specified, |
| 88 |
* the library is not opened. */ |
| 89 |
static LibraryHandle OpenLibrary(const std::string&); |
| 90 |
static LibraryHandle OpenLibrary(const std::string&, int); |
| 91 |
|
| 92 |
/** Attempt to detach a dynamic library from the |
| 93 |
* process. A value of true is returned if it is successful. */ |
| 94 |
static int CloseLibrary(LibraryHandle); |
| 95 |
|
| 96 |
/** Find the address of the symbol in the given library. */ |
| 97 |
static SymbolPointer GetSymbolAddress(LibraryHandle, const std::string&); |
| 98 |
|
| 99 |
/** Return the default module prefix for the current platform. */ |
| 100 |
static const char* LibPrefix() { return "@KWSYS_DynamicLoader_PREFIX@"; } |
| 101 |
|
| 102 |
/** Return the default module suffix for the current platform. */ |
| 103 |
static const char* LibExtension() { return "@KWSYS_DynamicLoader_SUFFIX@"; } |
| 104 |
|
| 105 |
/** Return the last error produced from a calls made on this class. */ |
| 106 |
static const char* LastError(); |
| 107 |
}; // End Class: DynamicLoader |
| 108 |
|
| 109 |
} // namespace @KWSYS_NAMESPACE@ |
| 110 |
|
| 111 |
#endif |