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
23namespace @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
45class @KWSYS_NAMESPACE@_EXPORT DynamicLoader
46{
47public:
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    AllOpenFlags = SearchBesideLibrary
77  };
78
79  /** Load a dynamic library into the current process.
80   * The returned LibraryHandle can be used to access the symbols in the
81   * library. The optional second argument is a set of flags to use when
82   * opening the library. If unrecognized or unsupported flags are specified,
83   * the library is not opened. */
84  static LibraryHandle OpenLibrary(const std::string&);
85  static LibraryHandle OpenLibrary(const std::string&, int);
86
87  /** Attempt to detach a dynamic library from the
88   * process.  A value of true is returned if it is successful. */
89  static int CloseLibrary(LibraryHandle);
90
91  /** Find the address of the symbol in the given library. */
92  static SymbolPointer GetSymbolAddress(LibraryHandle, const std::string&);
93
94  /** Return the default module prefix for the current platform.  */
95  static const char* LibPrefix() { return "@KWSYS_DynamicLoader_PREFIX@"; }
96
97  /** Return the default module suffix for the current platform.  */
98  static const char* LibExtension() { return "@KWSYS_DynamicLoader_SUFFIX@"; }
99
100  /** Return the last error produced from a calls made on this class. */
101  static const char* LastError();
102}; // End Class: DynamicLoader
103
104} // namespace @KWSYS_NAMESPACE@
105
106#endif
107