cmDynamicLoader.cxx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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::LibPrefix()
  118. {
  119. return "lib";
  120. }
  121. const char* cmDynamicLoader::LibExtension()
  122. {
  123. return ".sl";
  124. }
  125. const char* cmDynamicLoader::LastError()
  126. {
  127. return 0;
  128. }
  129. #endif
  130. // ---------------------------------------------------------------
  131. // 2. Implementation for Darwin (including OSX) Machines
  132. #ifdef __APPLE__
  133. #define CMDYNAMICLOADER_DEFINED
  134. #include <mach-o/dyld.h>
  135. cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
  136. {
  137. cmLibHandle lh;
  138. if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
  139. {
  140. return lh;
  141. }
  142. NSObjectFileImageReturnCode rc;
  143. NSObjectFileImage image;
  144. rc = NSCreateObjectFileImageFromFile(libname, &image);
  145. lh = NSLinkModule(image, libname, TRUE);
  146. cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
  147. return lh;
  148. }
  149. int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
  150. {
  151. // we have to use lib because the macro may not...
  152. (void)lib;
  153. NSUnLinkModule(lib, FALSE);
  154. return 0;
  155. }
  156. cmDynamicLoaderFunction
  157. cmDynamicLoader::GetSymbolAddress(cmLibHandle /* lib */, const char* sym)
  158. {
  159. void *result=0;
  160. if(NSIsSymbolNameDefined(sym))
  161. {
  162. NSSymbol symbol= NSLookupAndBindSymbol(sym);
  163. if(symbol)
  164. {
  165. result = NSAddressOfSymbol(symbol);
  166. }
  167. }
  168. // Hack to cast pointer-to-data to pointer-to-function.
  169. return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
  170. }
  171. const char* cmDynamicLoader::LibPrefix()
  172. {
  173. return "lib";
  174. }
  175. const char* cmDynamicLoader::LibExtension()
  176. {
  177. return ".so";
  178. }
  179. const char* cmDynamicLoader::LastError()
  180. {
  181. return 0;
  182. }
  183. #endif
  184. // ---------------------------------------------------------------
  185. // 3. Implementation for Windows win32 code
  186. #ifdef _WIN32
  187. #include <windows.h>
  188. #define CMDYNAMICLOADER_DEFINED 1
  189. cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
  190. {
  191. cmLibHandle lh;
  192. if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
  193. {
  194. return lh;
  195. }
  196. #ifdef UNICODE
  197. wchar_t *libn = new wchar_t [mbstowcs(NULL, libname, 32000)];
  198. mbstowcs(libn, libname, 32000);
  199. cmLibHandle ret = LoadLibrary(libn);
  200. delete [] libn;
  201. lh = ret;
  202. #else
  203. lh = LoadLibrary(libname);
  204. #endif
  205. cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
  206. return lh;
  207. }
  208. int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
  209. {
  210. return (int)FreeLibrary(lib);
  211. }
  212. cmDynamicLoaderFunction
  213. cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
  214. {
  215. #ifdef UNICODE
  216. wchar_t *wsym = new wchar_t [mbstowcs(NULL, sym, 32000)];
  217. mbstowcs(wsym, sym, 32000);
  218. void *ret = GetProcAddress(lib, wsym);
  219. delete [] wsym;
  220. void* result = ret;
  221. #else
  222. void* result = (void*)GetProcAddress(lib, sym);
  223. #endif
  224. // Hack to cast pointer-to-data to pointer-to-function.
  225. return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
  226. }
  227. const char* cmDynamicLoader::LibPrefix()
  228. {
  229. #if defined( __MINGW32__ )
  230. return "lib";
  231. #else
  232. return "";
  233. #endif
  234. }
  235. const char* cmDynamicLoader::LibExtension()
  236. {
  237. return ".dll";
  238. }
  239. const char* cmDynamicLoader::LastError()
  240. {
  241. LPVOID lpMsgBuf;
  242. FormatMessage(
  243. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  244. NULL,
  245. GetLastError(),
  246. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  247. (LPTSTR) &lpMsgBuf,
  248. 0,
  249. NULL
  250. );
  251. // Free the buffer.
  252. static char* str = 0;
  253. delete [] str;
  254. str = strcpy(new char[strlen((char*)lpMsgBuf)+1], (char*)lpMsgBuf);
  255. LocalFree( lpMsgBuf );
  256. return str;
  257. }
  258. #endif
  259. // ---------------------------------------------------------------
  260. // 4. Implementation for default UNIX machines.
  261. // if nothing has been defined then use this
  262. #ifndef CMDYNAMICLOADER_DEFINED
  263. #define CMDYNAMICLOADER_DEFINED
  264. // Setup for most unix machines
  265. #include <dlfcn.h>
  266. cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
  267. {
  268. cmLibHandle lh;
  269. if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
  270. {
  271. return lh;
  272. }
  273. lh = dlopen(libname, RTLD_LAZY);
  274. cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
  275. return lh;
  276. }
  277. int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
  278. {
  279. return (int)dlclose(lib);
  280. }
  281. cmDynamicLoaderFunction
  282. cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
  283. {
  284. void* result = dlsym(lib, sym);
  285. // Hack to cast pointer-to-data to pointer-to-function.
  286. return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
  287. }
  288. const char* cmDynamicLoader::LibPrefix()
  289. {
  290. return "lib";
  291. }
  292. const char* cmDynamicLoader::LibExtension()
  293. {
  294. #ifdef __CYGWIN__
  295. return ".dll";
  296. #else
  297. return ".so";
  298. #endif
  299. }
  300. const char* cmDynamicLoader::LastError()
  301. {
  302. return dlerror();
  303. }
  304. #endif
  305. void cmDynamicLoader::FlushCache()
  306. {
  307. cmDynamicLoaderCache::GetInstance()->FlushCache();
  308. }