HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_DSO.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT library (C++)
7  *
8  * COMMENTS: Dynamic shared object management
9  *
10  */
11 
12 #ifndef __UT_DSO_H__
13 #define __UT_DSO_H__
14 
15 #include "FS_API.h"
16 
17 #include <UT/UT_KnownPath.h>
18 #include <UT/UT_StringHolder.h>
19 #include <SYS/SYS_Hash.h>
20 #include <SYS/SYS_Types.h>
21 #include <utility>
22 
23 #if defined( WIN32 )
24  #define FS_DSO_EXTENSION ".dll"
25 #elif defined( MBSD )
26  #define FS_DSO_EXTENSION ".dylib"
27 #else
28  #define FS_DSO_EXTENSION ".so"
29 #endif
30 
31 namespace UT_Package
32 {
33  class Package;
34 }
35 
36 class dso_file;
37 class UT_String;
38 
39 // This abstract class is only used as a return value from UT_DSO::loadDSO()
40 // and a parameter to UT_DSO::findSymbol().
41 class UT_DSOHandle;
42 
44 {
45 public:
46  // These static functions provide a much simplified and more generic
47  // interface over UT_DSO objects. They also correspond more directly to
48  // the underlying system's dlopen/LoadLibrary, dlsym/GetProcAddress, and
49  // dlclose/FreeLibrary interface than do UT_DSO objects.
50 
51  // loadDSO() will load one particular shared object, using the system's
52  // standard search path. A pointer to an opaque UT_DSOHandle object is
53  // returned on success, and you can use this handle to look up symbols.
54  // on failure, this function returns null.
55  //
56  // Unlike UT_DSO objects, it will not load all the shared objects it can
57  // find in Houdini's search paths. Also, a parameter controls whether or
58  // not the symbols are available to shared objects that are loaded later.
59  // UT_DSO objects instead look for a special function in the shared object
60  // to determine if its symbols should be made available.
61  static UT_DSOHandle *loadDSO(
62  const char *file_name, bool available_to_later_loaded_libraries,
63  UT_String *error = NULL);
64 
65  // get the filename from a DSO handle
66  static UT_StringHolder getFilePath(
67  UT_DSOHandle *handle, UT_StringHolder &error);
68 
69  // Find a symbol (function or global variable) in a loaded shared object.
70  // Unlike the findProcedure() method on UT_DSO objects, you pass in
71  // a dso object that's already been loaded, instead of a file name. This
72  // way you can quickly look up multiple symbols in the same shared library.
73  // This function also doesn't for HoudiniDSOVersion symbols like UT_DSO
74  // objects do.
75  static void *findSymbol(UT_DSOHandle &handle, const char *symbol_name);
76 
77  // Decrement the reference cound on the loaded library, unloading it if
78  // it's no longer used.
79  static int closeDSO(UT_DSOHandle &handle);
80 
81  // Return the UT_DSOHandle for the main program. This lets you look up
82  // symbols in the main program from a shared library that was loaded.
83  static UT_DSOHandle *getExecutable();
84 
85  // Return the UT_DSOHandle for a given loaded library.
86  // Return NULL if the library is not loaded.
87  // Use this function to check whether a particular library has been loaded
88  // or to look up symbols in a library.
89  static UT_DSOHandle *getLoadedLibrary(const char *lib_name);
90 
91  //------------------------------------------------------------------------
92 
93  // Given the name of a shared object, search for the file on disk using the
94  // system's library path resolution mechanism and try to load it. Returns
95  // null if the file could not be found or couldn't be loaded for some
96  // reason.
97  static UT_DSOHandle *loadLibraryInPath(
98  const char *shared_object_name,
99  bool available_to_later_loaded_libraries);
100 
101  //------------------------------------------------------------------------
102 
103  // Load all the DSOs requested by the provided UT_Package.
104  static void loadPackageSharedLibraries(const UT_Package::Package &pkg);
105 
106  //------------------------------------------------------------------------
107 
108  // Load a dynamic object and run the function specified.
109  bool run(const char *function_name, void *data = NULL,
110  bool validate_version = true);
111  bool run(const char *filename, const char *function_name,
112  void *data = NULL, bool validate_version = true,
114 
115  // This will go through the available DSOs in reverse order.
116  // Normally, we run them from $HOME -> $HFS. However, if the
117  // called procedure will override earlier on successive calls (rather
118  // than ignore) the correct order should be $HFS -> $HOME. This is
119  // the case with CMDextendLibrary.
120  bool runReverse(const char *function_name, void *data = NULL,
121  bool validate_version = true);
122 
123  // Rather than running the function, simply load the dynamic object and
124  // return a pointer to the function. This returns a pointer to the
125  // function (or NULL if no function was found).
126  // The actual name of the dynamic object is returned in the fullpath string.
127  void *findProcedure(const char *filename, const char *function_name,
128  UT_StringHolder &fullpath,
129  bool validate_version = true,
130  bool err_on_missing_file = true);
131 
132  // Validate that the given opened shared library has the correct versions.
133  // `filename` is used for HOUDINI_DSO_ERROR messages.
134  static bool checkDSOVersion(UT_DSOHandle *handle, const char *filename);
135 
136  // This will return the file which is currently being loaded (i.e. in the
137  // run() command. If there is no file, then it will return a null ptr.
138  static const char *getRunningFile();
139 
140  // Scans a dso folder and returns a list of library path
141  // files found in the folder. The shared libs can be cached for
142  // further access.
143  // folder_path: folder containing the dso files
144  // add_to_cache: cache the libs found in the folder
145  static UT_StringArray browseDSOFiles(const char* folder_path, bool add_to_cache=false);
146 
147  // Runs func_name from a specific library. If the library
148  // isn't provided, the function runs from the DSO cache.
149  //
150  // func_name: Name of the function to execute from lib_path
151  // or from all the cached libs.
152  // user_data: optional data passed to the function.
153  // lib_path: Library path.
154  static bool runFunction(const char* func_name, void* user_data=nullptr, const char* lib_path=nullptr);
155 
156  // Runs func_name from a specific library (similar to runFunction).
157  // If the library isn't supplied, the function runs from the
158  // DSO cache in reverse order.
159  //
160  // func_name: Name of the function to execute from lib_path
161  // or from all the cached libs.
162  // user_data: optional data passed to the function.
163  // lib_path: Library path.
164  static bool runFunctionReverse(const char* func_name, void* user_data=nullptr, const char* lib_path=nullptr);
165 
166  // Returns the last error message recorded while calling run
167  // methods.
168  static UT_StringHolder lastErrorMsg();
169 
170  /// The minimum compiler version that a DSO should be compiled in
171  /// NOTE: Currently only enforced on Windows
172  static std::pair<unsigned, unsigned> minAllowedCompilerVersion();
173 
174  /// The maximum compiler version that a DSO should be compiled in
175  /// NOTE: Currently only enforced on Windows
176  static std::pair<unsigned, unsigned> maxAllowedCompilerVersion();
177 };
178 
179 #endif
GT_API const UT_StringHolder filename
UT_KnownPath
Definition: UT_KnownPath.h:14
GLsizei const GLchar *const * path
Definition: glcorearb.h:3341
< returns > If no error
Definition: snippets.dox:2
Definition: UT_DSO.h:43
Definition: format.h:1821
#define FS_API
Definition: FS_API.h:10