1
0

cmDynamicLoader.cxx 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 = 0;
  136. rc = NSCreateObjectFileImageFromFile(libname, &image);
  137. if(!image)
  138. {
  139. return 0;
  140. }
  141. lh = NSLinkModule(image, libname, TRUE);
  142. if(lh)
  143. {
  144. cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
  145. }
  146. return lh;
  147. }
  148. int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
  149. {
  150. // we have to use lib because the macro may not...
  151. (void)lib;
  152. NSUnLinkModule(lib, FALSE);
  153. return 1;
  154. }
  155. cmDynamicLoaderFunction
  156. cmDynamicLoader::GetSymbolAddress(cmLibHandle /* lib */, const char* sym)
  157. {
  158. void *result=0;
  159. if(NSIsSymbolNameDefined(sym))
  160. {
  161. NSSymbol symbol= NSLookupAndBindSymbol(sym);
  162. if(symbol)
  163. {
  164. result = NSAddressOfSymbol(symbol);
  165. }
  166. }
  167. // Hack to cast pointer-to-data to pointer-to-function.
  168. return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
  169. }
  170. const char* cmDynamicLoader::LastError()
  171. {
  172. return 0;
  173. }
  174. #endif
  175. // ---------------------------------------------------------------
  176. // 3. Implementation for Windows win32 code
  177. #ifdef _WIN32
  178. #include <windows.h>
  179. #define CMDYNAMICLOADER_DEFINED 1
  180. cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
  181. {
  182. cmLibHandle lh;
  183. if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
  184. {
  185. return lh;
  186. }
  187. #ifdef UNICODE
  188. wchar_t *libn = new wchar_t [mbstowcs(NULL, libname, 32000)];
  189. mbstowcs(libn, libname, 32000);
  190. cmLibHandle ret = LoadLibrary(libn);
  191. delete [] libn;
  192. lh = ret;
  193. #else
  194. lh = LoadLibrary(libname);
  195. #endif
  196. cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
  197. return lh;
  198. }
  199. int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
  200. {
  201. return (int)FreeLibrary(lib);
  202. }
  203. cmDynamicLoaderFunction
  204. cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
  205. {
  206. #ifdef UNICODE
  207. wchar_t *wsym = new wchar_t [mbstowcs(NULL, sym, 32000)];
  208. mbstowcs(wsym, sym, 32000);
  209. void *ret = GetProcAddress(lib, wsym);
  210. delete [] wsym;
  211. void* result = ret;
  212. #else
  213. void* result = (void*)GetProcAddress(lib, sym);
  214. #endif
  215. // Hack to cast pointer-to-data to pointer-to-function.
  216. return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
  217. }
  218. const char* cmDynamicLoader::LastError()
  219. {
  220. LPVOID lpMsgBuf;
  221. FormatMessage(
  222. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  223. NULL,
  224. GetLastError(),
  225. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  226. (LPTSTR) &lpMsgBuf,
  227. 0,
  228. NULL
  229. );
  230. // Free the buffer.
  231. static char* str = 0;
  232. delete [] str;
  233. str = strcpy(new char[strlen((char*)lpMsgBuf)+1], (char*)lpMsgBuf);
  234. LocalFree( lpMsgBuf );
  235. return str;
  236. }
  237. #endif
  238. // ---------------------------------------------------------------
  239. // 4. Implementation for default UNIX machines.
  240. // if nothing has been defined then use this
  241. #ifndef CMDYNAMICLOADER_DEFINED
  242. #define CMDYNAMICLOADER_DEFINED
  243. // Setup for most unix machines
  244. #include <dlfcn.h>
  245. cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
  246. {
  247. cmLibHandle lh;
  248. if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
  249. {
  250. return lh;
  251. }
  252. lh = dlopen(libname, RTLD_LAZY);
  253. cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
  254. return lh;
  255. }
  256. int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
  257. {
  258. return !(int)dlclose(lib);
  259. }
  260. cmDynamicLoaderFunction
  261. cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
  262. {
  263. void* result = dlsym(lib, sym);
  264. // Hack to cast pointer-to-data to pointer-to-function.
  265. return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
  266. }
  267. const char* cmDynamicLoader::LastError()
  268. {
  269. return dlerror();
  270. }
  271. #endif
  272. void cmDynamicLoader::FlushCache()
  273. {
  274. cmDynamicLoaderCache::GetInstance()->FlushCache();
  275. }
  276. // Stay consistent with the Modules/Platform directory as
  277. // to what the correct prefix and lib extension
  278. const char* cmDynamicLoader::LibPrefix()
  279. {
  280. return CMAKE_SHARED_MODULE_PREFIX;
  281. }
  282. const char* cmDynamicLoader::LibExtension()
  283. {
  284. return CMAKE_SHARED_MODULE_SUFFIX;
  285. }