cmDynamicLoader.cxx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmDynamicLoader.h"
  14. // This file is actually several different implementations.
  15. // 1. HP machines which uses shl_load
  16. // 2. Apple OSX which uses NSLinkModule
  17. // 3. Windows which uses LoadLibrary
  18. // 4. Most unix systems which use dlopen (default )
  19. // Each part of the ifdef contains a complete implementation for
  20. // the static methods of cmDynamicLoader.
  21. class cmDynamicLoaderCache
  22. {
  23. public:
  24. ~cmDynamicLoaderCache();
  25. void CacheFile(const char* path, const cmLibHandle&);
  26. bool GetCacheFile(const char* path, cmLibHandle&);
  27. bool FlushCache(const char* path);
  28. void FlushCache();
  29. static cmDynamicLoaderCache* GetInstance();
  30. private:
  31. std::map<std::string, cmLibHandle> m_CacheMap;
  32. static cmDynamicLoaderCache* Instance;
  33. };
  34. cmDynamicLoaderCache* cmDynamicLoaderCache::Instance = 0;
  35. cmDynamicLoaderCache::~cmDynamicLoaderCache()
  36. {
  37. }
  38. void cmDynamicLoaderCache::CacheFile(const char* path, const cmLibHandle& p)
  39. {
  40. cmLibHandle h;
  41. if ( this->GetCacheFile(path, h) )
  42. {
  43. this->FlushCache(path);
  44. }
  45. this->m_CacheMap[path] = p;
  46. }
  47. bool cmDynamicLoaderCache::GetCacheFile(const char* path, cmLibHandle& p)
  48. {
  49. std::map<std::string, cmLibHandle>::iterator it = m_CacheMap.find(path);
  50. if ( it != m_CacheMap.end() )
  51. {
  52. p = it->second;
  53. return true;
  54. }
  55. return false;
  56. }
  57. bool cmDynamicLoaderCache::FlushCache(const char* path)
  58. {
  59. std::map<std::string, cmLibHandle>::iterator it = m_CacheMap.find(path);
  60. bool ret = false;
  61. if ( it != m_CacheMap.end() )
  62. {
  63. cmDynamicLoader::CloseLibrary(it->second);
  64. m_CacheMap.erase(it);
  65. ret = true;
  66. }
  67. return ret;
  68. }
  69. void cmDynamicLoaderCache::FlushCache()
  70. {
  71. for ( std::map<std::string, cmLibHandle>::iterator it = m_CacheMap.begin();
  72. it != m_CacheMap.end(); it++ )
  73. {
  74. cmDynamicLoader::CloseLibrary(it->second);
  75. }
  76. delete cmDynamicLoaderCache::Instance;
  77. cmDynamicLoaderCache::Instance = 0;
  78. }
  79. cmDynamicLoaderCache* cmDynamicLoaderCache::GetInstance()
  80. {
  81. if ( !cmDynamicLoaderCache::Instance )
  82. {
  83. cmDynamicLoaderCache::Instance = new cmDynamicLoaderCache;
  84. }
  85. return cmDynamicLoaderCache::Instance;
  86. }
  87. // ---------------------------------------------------------------
  88. // 1. Implementation for HPUX machines
  89. #ifdef __hpux
  90. #define CMDYNAMICLOADER_DEFINED 1
  91. #include <dl.h>
  92. cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
  93. {
  94. cmLibHandle lh;
  95. if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
  96. {
  97. return lh;
  98. }
  99. lh = shl_load(libname, BIND_DEFERRED | DYNAMIC_PATH, 0L);
  100. cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
  101. return lh;
  102. }
  103. int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
  104. {
  105. return shl_unload(lib);
  106. }
  107. cmDynamicLoaderFunction
  108. cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
  109. {
  110. void* addr;
  111. int status;
  112. status = shl_findsym (&lib, sym, TYPE_PROCEDURE, &addr);
  113. void* result = (status < 0) ? (void*)0 : addr;
  114. // Hack to cast pointer-to-data to pointer-to-function.
  115. return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
  116. }
  117. const char* cmDynamicLoader::LastError()
  118. {
  119. return 0;
  120. }
  121. #endif
  122. // ---------------------------------------------------------------
  123. // 2. Implementation for Darwin (including OSX) Machines
  124. #ifdef __APPLE__
  125. #define CMDYNAMICLOADER_DEFINED
  126. #include <mach-o/dyld.h>
  127. cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
  128. {
  129. cmLibHandle lh;
  130. if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
  131. {
  132. return lh;
  133. }
  134. NSObjectFileImageReturnCode rc;
  135. NSObjectFileImage image;
  136. rc = NSCreateObjectFileImageFromFile(libname, &image);
  137. lh = NSLinkModule(image, libname, TRUE);
  138. cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
  139. return lh;
  140. }
  141. int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
  142. {
  143. // we have to use lib because the macro may not...
  144. (void)lib;
  145. NSUnLinkModule(lib, FALSE);
  146. return 0;
  147. }
  148. cmDynamicLoaderFunction
  149. cmDynamicLoader::GetSymbolAddress(cmLibHandle /* lib */, const char* sym)
  150. {
  151. void *result=0;
  152. if(NSIsSymbolNameDefined(sym))
  153. {
  154. NSSymbol symbol= NSLookupAndBindSymbol(sym);
  155. if(symbol)
  156. {
  157. result = NSAddressOfSymbol(symbol);
  158. }
  159. }
  160. // Hack to cast pointer-to-data to pointer-to-function.
  161. return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
  162. }
  163. const char* cmDynamicLoader::LastError()
  164. {
  165. return 0;
  166. }
  167. #endif
  168. // ---------------------------------------------------------------
  169. // 3. Implementation for Windows win32 code
  170. #ifdef _WIN32
  171. #include <windows.h>
  172. #define CMDYNAMICLOADER_DEFINED 1
  173. cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
  174. {
  175. cmLibHandle lh;
  176. if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
  177. {
  178. return lh;
  179. }
  180. #ifdef UNICODE
  181. wchar_t *libn = new wchar_t [mbstowcs(NULL, libname, 32000)];
  182. mbstowcs(libn, libname, 32000);
  183. cmLibHandle ret = LoadLibrary(libn);
  184. delete [] libn;
  185. lh = ret;
  186. #else
  187. lh = LoadLibrary(libname);
  188. #endif
  189. cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
  190. return lh;
  191. }
  192. int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
  193. {
  194. return (int)FreeLibrary(lib);
  195. }
  196. cmDynamicLoaderFunction
  197. cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
  198. {
  199. #ifdef UNICODE
  200. wchar_t *wsym = new wchar_t [mbstowcs(NULL, sym, 32000)];
  201. mbstowcs(wsym, sym, 32000);
  202. void *ret = GetProcAddress(lib, wsym);
  203. delete [] wsym;
  204. void* result = ret;
  205. #else
  206. void* result = (void*)GetProcAddress(lib, sym);
  207. #endif
  208. // Hack to cast pointer-to-data to pointer-to-function.
  209. return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
  210. }
  211. const char* cmDynamicLoader::LastError()
  212. {
  213. LPVOID lpMsgBuf;
  214. FormatMessage(
  215. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  216. NULL,
  217. GetLastError(),
  218. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  219. (LPTSTR) &lpMsgBuf,
  220. 0,
  221. NULL
  222. );
  223. // Free the buffer.
  224. static char* str = 0;
  225. delete [] str;
  226. str = strcpy(new char[strlen((char*)lpMsgBuf)+1], (char*)lpMsgBuf);
  227. LocalFree( lpMsgBuf );
  228. return str;
  229. }
  230. #endif
  231. // ---------------------------------------------------------------
  232. // 4. Implementation for default UNIX machines.
  233. // if nothing has been defined then use this
  234. #ifndef CMDYNAMICLOADER_DEFINED
  235. #define CMDYNAMICLOADER_DEFINED
  236. // Setup for most unix machines
  237. #include <dlfcn.h>
  238. cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
  239. {
  240. cmLibHandle lh;
  241. if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
  242. {
  243. return lh;
  244. }
  245. lh = dlopen(libname, RTLD_LAZY);
  246. cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
  247. return lh;
  248. }
  249. int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
  250. {
  251. return (int)dlclose(lib);
  252. }
  253. cmDynamicLoaderFunction
  254. cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
  255. {
  256. void* result = dlsym(lib, sym);
  257. // Hack to cast pointer-to-data to pointer-to-function.
  258. return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
  259. }
  260. const char* cmDynamicLoader::LastError()
  261. {
  262. return dlerror();
  263. }
  264. #endif
  265. void cmDynamicLoader::FlushCache()
  266. {
  267. cmDynamicLoaderCache::GetInstance()->FlushCache();
  268. }
  269. // Stay consistent with the Modules/Platform directory as
  270. // to what the correct prefix and lib extension
  271. const char* cmDynamicLoader::LibPrefix()
  272. {
  273. return CMAKE_SHARED_MODULE_PREFIX;
  274. }
  275. const char* cmDynamicLoader::LibExtension()
  276. {
  277. return CMAKE_SHARED_LIBRARY_SUFFIX;
  278. }