ENH: Cleanup DynamicLoader so that the symbols have more consistent names, start using dynamic loader from kwsys in CMake

This commit is contained in:
Andy Cedilnik 2006-03-16 11:01:05 -05:00
parent 4102949bf9
commit f483e48d2a
12 changed files with 134 additions and 375 deletions

View File

@ -9,34 +9,26 @@
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information. PURPOSE. See the above copyright notices for more information.
=========================================================================*/ =========================================================================*/
#include "cmDynamicLoader.h" #include "cmDynamicLoader.h"
// This file is actually several different implementations. class cmDynamicLoaderCache
// 1. HP machines which uses shl_load
// 2. Mac OS X 10.2.x and earlier which uses NSLinkModule
// 3. Windows which uses LoadLibrary
// 4. Most unix systems (including Mac OS X 10.3 and later) which use dlopen (default)
// Each part of the ifdef contains a complete implementation for
// the static methods of cmDynamicLoader.
class cmDynamicLoaderCache
{ {
public: public:
~cmDynamicLoaderCache(); ~cmDynamicLoaderCache();
void CacheFile(const char* path, const cmLibHandle&); void CacheFile(const char* path,
bool GetCacheFile(const char* path, cmLibHandle&); const cmsys::DynamicLoader::LibraryHandle&);
bool GetCacheFile(const char* path, cmsys::DynamicLoader::LibraryHandle&);
bool FlushCache(const char* path); bool FlushCache(const char* path);
void FlushCache(); void FlushCache();
static cmDynamicLoaderCache* GetInstance(); static cmDynamicLoaderCache* GetInstance();
private: private:
std::map<cmStdString, cmLibHandle> CacheMap; std::map<cmStdString, cmsys::DynamicLoader::LibraryHandle> CacheMap;
static cmDynamicLoaderCache* Instance; static cmDynamicLoaderCache* Instance;
}; };
@ -46,9 +38,10 @@ cmDynamicLoaderCache::~cmDynamicLoaderCache()
{ {
} }
void cmDynamicLoaderCache::CacheFile(const char* path, const cmLibHandle& p) void cmDynamicLoaderCache::CacheFile(const char* path,
const cmsys::DynamicLoader::LibraryHandle& p)
{ {
cmLibHandle h; cmsys::DynamicLoader::LibraryHandle h;
if ( this->GetCacheFile(path, h) ) if ( this->GetCacheFile(path, h) )
{ {
this->FlushCache(path); this->FlushCache(path);
@ -56,9 +49,11 @@ void cmDynamicLoaderCache::CacheFile(const char* path, const cmLibHandle& p)
this->CacheMap[path] = p; this->CacheMap[path] = p;
} }
bool cmDynamicLoaderCache::GetCacheFile(const char* path, cmLibHandle& p) bool cmDynamicLoaderCache::GetCacheFile(const char* path,
cmsys::DynamicLoader::LibraryHandle& p)
{ {
std::map<cmStdString, cmLibHandle>::iterator it = this->CacheMap.find(path); std::map<cmStdString, cmsys::DynamicLoader::LibraryHandle>::iterator it
= this->CacheMap.find(path);
if ( it != this->CacheMap.end() ) if ( it != this->CacheMap.end() )
{ {
p = it->second; p = it->second;
@ -69,11 +64,12 @@ bool cmDynamicLoaderCache::GetCacheFile(const char* path, cmLibHandle& p)
bool cmDynamicLoaderCache::FlushCache(const char* path) bool cmDynamicLoaderCache::FlushCache(const char* path)
{ {
std::map<cmStdString, cmLibHandle>::iterator it = this->CacheMap.find(path); std::map<cmStdString, cmsys::DynamicLoader::LibraryHandle>::iterator it
= this->CacheMap.find(path);
bool ret = false; bool ret = false;
if ( it != this->CacheMap.end() ) if ( it != this->CacheMap.end() )
{ {
cmDynamicLoader::CloseLibrary(it->second); cmsys::DynamicLoader::CloseLibrary(it->second);
this->CacheMap.erase(it); this->CacheMap.erase(it);
ret = true; ret = true;
} }
@ -82,10 +78,12 @@ bool cmDynamicLoaderCache::FlushCache(const char* path)
void cmDynamicLoaderCache::FlushCache() void cmDynamicLoaderCache::FlushCache()
{ {
for ( std::map<cmStdString, cmLibHandle>::iterator it = this->CacheMap.begin(); for ( std::map<cmStdString,
cmsys::DynamicLoader::LibraryHandle>::iterator it
= this->CacheMap.begin();
it != this->CacheMap.end(); it++ ) it != this->CacheMap.end(); it++ )
{ {
cmDynamicLoader::CloseLibrary(it->second); cmsys::DynamicLoader::CloseLibrary(it->second);
} }
delete cmDynamicLoaderCache::Instance; delete cmDynamicLoaderCache::Instance;
cmDynamicLoaderCache::Instance = 0; cmDynamicLoaderCache::Instance = 0;
@ -100,231 +98,19 @@ cmDynamicLoaderCache* cmDynamicLoaderCache::GetInstance()
return cmDynamicLoaderCache::Instance; return cmDynamicLoaderCache::Instance;
} }
// --------------------------------------------------------------- cmsys::DynamicLoader::LibraryHandle cmDynamicLoader::OpenLibrary(
// 1. Implementation for HPUX machines const char* libname )
#ifdef __hpux
#define CMDYNAMICLOADER_DEFINED 1
#include <dl.h>
cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
{ {
cmLibHandle lh; cmsys::DynamicLoader::LibraryHandle lh;
if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) ) if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
{ {
return lh; return lh;
} }
lh = cmsys::DynamicLoader::OpenLibrary(libname);
lh = shl_load(libname, BIND_DEFERRED | DYNAMIC_PATH, 0L);
cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh); cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
return lh; return lh;
} }
int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
{
return !shl_unload(lib);
}
cmDynamicLoaderFunction
cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
{
void* addr;
int status;
status = shl_findsym (&lib, sym, TYPE_PROCEDURE, &addr);
void* result = (status < 0) ? (void*)0 : addr;
// Hack to cast pointer-to-data to pointer-to-function.
return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
}
const char* cmDynamicLoader::LastError()
{
return 0;
}
#endif
// ---------------------------------------------------------------
// 2. Implementation for Mac OS X 10.2.x and earlier
#ifdef __APPLE__
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1030
#define CMDYNAMICLOADER_DEFINED 1
#include <mach-o/dyld.h>
cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
{
cmLibHandle lh;
if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
{
return lh;
}
NSObjectFileImageReturnCode rc;
NSObjectFileImage image = 0;
rc = NSCreateObjectFileImageFromFile(libname, &image);
if(!image)
{
return 0;
}
lh = NSLinkModule(image, libname, NSLINKMODULE_OPTION_BINDNOW);
if(lh)
{
cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
}
return lh;
}
int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
{
NSUnLinkModule((NSModule)lib, NSUNLINKMODULE_OPTION_NONE);
return 1;
}
cmDynamicLoaderFunction
cmDynamicLoader::GetSymbolAddress(cmLibHandle /* lib */, const char* sym)
{
void *result=0;
if(NSIsSymbolNameDefined(sym))
{
NSSymbol symbol= NSLookupAndBindSymbol(sym);
if(symbol)
{
result = NSAddressOfSymbol(symbol);
}
}
// Hack to cast pointer-to-data to pointer-to-function.
return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
}
const char* cmDynamicLoader::LastError()
{
return 0;
}
#endif
#endif
// ---------------------------------------------------------------
// 3. Implementation for Windows win32 code
#ifdef _WIN32
#include <windows.h>
#define CMDYNAMICLOADER_DEFINED 1
cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
{
cmLibHandle lh;
if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
{
return lh;
}
#ifdef UNICODE
wchar_t *libn = new wchar_t [mbstowcs(NULL, libname, 32000)];
mbstowcs(libn, libname, 32000);
cmLibHandle ret = LoadLibrary(libn);
delete [] libn;
lh = ret;
#else
lh = LoadLibrary(libname);
#endif
cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
return lh;
}
int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
{
return (int)FreeLibrary(lib);
}
cmDynamicLoaderFunction
cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
{
#ifdef UNICODE
wchar_t *wsym = new wchar_t [mbstowcs(NULL, sym, 32000)];
mbstowcs(wsym, sym, 32000);
void *ret = GetProcAddress(lib, wsym);
delete [] wsym;
void* result = ret;
#else
void* result = (void*)GetProcAddress(lib, sym);
#endif
// Hack to cast pointer-to-data to pointer-to-function.
return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
}
const char* cmDynamicLoader::LastError()
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
// Free the buffer.
static char* str = 0;
delete [] str;
str = strcpy(new char[strlen((char*)lpMsgBuf)+1], (char*)lpMsgBuf);
LocalFree( lpMsgBuf );
return str;
}
#endif
// ---------------------------------------------------------------
// 4. Implementation for default UNIX machines.
// if nothing has been defined then use this
#ifndef CMDYNAMICLOADER_DEFINED
#define CMDYNAMICLOADER_DEFINED 1
// Setup for most unix machines
#include <dlfcn.h>
cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
{
cmLibHandle lh;
if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
{
return lh;
}
lh = dlopen(libname, RTLD_LAZY);
cmDynamicLoaderCache::GetInstance()->CacheFile(libname, lh);
return lh;
}
int cmDynamicLoader::CloseLibrary(cmLibHandle lib)
{
return !(int)dlclose(lib);
}
cmDynamicLoaderFunction
cmDynamicLoader::GetSymbolAddress(cmLibHandle lib, const char* sym)
{
void* result = dlsym(lib, sym);
// Hack to cast pointer-to-data to pointer-to-function.
return *reinterpret_cast<cmDynamicLoaderFunction*>(&result);
}
const char* cmDynamicLoader::LastError()
{
return dlerror();
}
#endif
void cmDynamicLoader::FlushCache() void cmDynamicLoader::FlushCache()
{ {
cmDynamicLoaderCache::GetInstance()->FlushCache(); cmDynamicLoaderCache::GetInstance()->FlushCache();
@ -333,7 +119,7 @@ void cmDynamicLoader::FlushCache()
// Stay consistent with the Modules/Platform directory as // Stay consistent with the Modules/Platform directory as
// to what the correct prefix and lib extension // to what the correct prefix and lib extension
const char* cmDynamicLoader::LibPrefix() const char* cmDynamicLoader::LibPrefix()
{ {
return CMAKE_SHARED_MODULE_PREFIX; return CMAKE_SHARED_MODULE_PREFIX;
} }

View File

@ -9,15 +9,15 @@
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information. PURPOSE. See the above copyright notices for more information.
=========================================================================*/ =========================================================================*/
// .NAME cmDynamicLoader - class interface to system dynamic libraries // .NAME cmDynamicLoader - class interface to system dynamic libraries
// .SECTION Description // .SECTION Description
// cmDynamicLoader provides a portable interface to loading dynamic // cmDynamicLoader provides a portable interface to loading dynamic
// libraries into a process. // libraries into a process.
#ifndef __cmDynamicLoader_h #ifndef __cmDynamicLoader_h
@ -25,46 +25,16 @@
#include "cmStandardIncludes.h" #include "cmStandardIncludes.h"
// Ugly stuff for library handles #include <cmsys/DynamicLoader.hxx>
// They are different on several different OS's
#if defined(__hpux)
#include <dl.h>
typedef shl_t cmLibHandle;
#elif defined(_WIN32)
#include <windows.h>
typedef HMODULE cmLibHandle;
#elif defined(__APPLE__)
#include <AvailabilityMacros.h>
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1030
#include <mach-o/dyld.h>
typedef NSModule cmLibHandle;
#else
typedef void* cmLibHandle;
#endif
#else
typedef void* cmLibHandle;
#endif
// Return type from cmDynamicLoader::GetSymbolAddress.
typedef void (*cmDynamicLoaderFunction)();
class cmDynamicLoader class cmDynamicLoader
{ {
public: public:
// Description: // Description:
// Load a dynamic library into the current process. // Load a dynamic library into the current process.
// The returned cmLibHandle can be used to access the symbols in the // The returned cmsys::DynamicLoader::LibraryHandle can be used to access
// library. // the symbols in the library.
static cmLibHandle OpenLibrary(const char*); static cmsys::DynamicLoader::LibraryHandle OpenLibrary(const char*);
// Description:
// Attempt to detach a dynamic library from the
// process. A value of true is returned if it is successful.
static int CloseLibrary(cmLibHandle);
// Description:
// Find the address of the symbol in the given library
static cmDynamicLoaderFunction GetSymbolAddress(cmLibHandle, const char*);
// Description: // Description:
// Return the library prefix for the given architecture // Return the library prefix for the given architecture
@ -74,19 +44,14 @@ public:
// Return the library extension for the given architecture // Return the library extension for the given architecture
static const char* LibExtension(); static const char* LibExtension();
// Description:
// Return the last error produced from a calls made on this class.
static const char* LastError();
// Description: // Description:
// Flush the cache of dynamic loader. // Flush the cache of dynamic loader.
static void FlushCache(); static void FlushCache();
protected: protected:
cmDynamicLoader() {}; cmDynamicLoader() {};
~cmDynamicLoader() {}; ~cmDynamicLoader() {};
private: private:
cmDynamicLoader(const cmDynamicLoader&); // Not implemented. cmDynamicLoader(const cmDynamicLoader&); // Not implemented.
void operator=(const cmDynamicLoader&); // Not implemented. void operator=(const cmDynamicLoader&); // Not implemented.

View File

@ -18,6 +18,9 @@
#include "cmCPluginAPI.h" #include "cmCPluginAPI.h"
#include "cmCPluginAPI.cxx" #include "cmCPluginAPI.cxx"
#include "cmDynamicLoader.h" #include "cmDynamicLoader.h"
#include <cmsys/DynamicLoader.hxx>
#include <signal.h> #include <signal.h>
extern "C" void TrapsForSignalsCFunction(int sig); extern "C" void TrapsForSignalsCFunction(int sig);
@ -254,12 +257,13 @@ bool cmLoadCommandCommand::InitialPass(std::vector<std::string> const& args)
} }
// try loading the shared library / dll // try loading the shared library / dll
cmLibHandle lib = cmDynamicLoader::OpenLibrary(fullPath.c_str()); cmsys::DynamicLoader::LibraryHandle lib
= cmDynamicLoader::OpenLibrary(fullPath.c_str());
if(!lib) if(!lib)
{ {
std::string err = "Attempt to load the library "; std::string err = "Attempt to load the library ";
err += fullPath + " failed."; err += fullPath + " failed.";
const char* error = cmDynamicLoader::LastError(); const char* error = cmsys::DynamicLoader::LastError();
if ( error ) if ( error )
{ {
err += " Additional error info is:\n"; err += " Additional error info is:\n";
@ -276,14 +280,14 @@ bool cmLoadCommandCommand::InitialPass(std::vector<std::string> const& args)
std::string initFuncName = args[0] + "Init"; std::string initFuncName = args[0] + "Init";
CM_INIT_FUNCTION initFunction CM_INIT_FUNCTION initFunction
= (CM_INIT_FUNCTION) = (CM_INIT_FUNCTION)
cmDynamicLoader::GetSymbolAddress(lib, initFuncName.c_str()); cmsys::DynamicLoader::GetSymbolAddress(lib, initFuncName.c_str());
if ( !initFunction ) if ( !initFunction )
{ {
initFuncName = "_"; initFuncName = "_";
initFuncName += args[0]; initFuncName += args[0];
initFuncName += "Init"; initFuncName += "Init";
initFunction = (CM_INIT_FUNCTION)( initFunction = (CM_INIT_FUNCTION)(
cmDynamicLoader::GetSymbolAddress(lib, initFuncName.c_str())); cmsys::DynamicLoader::GetSymbolAddress(lib, initFuncName.c_str()));
} }
// if the symbol is found call it to set the name on the // if the symbol is found call it to set the name on the
// function blocker // function blocker

View File

@ -1501,6 +1501,10 @@ bool cmSystemTools::CreateTar(const char* outFileName,
} }
return true; return true;
#else #else
(void)outFileName;
(void)files;
(void)gzip;
(void)verbose;
return false; return false;
#endif #endif
} }
@ -1553,6 +1557,9 @@ bool cmSystemTools::ExtractTar(const char* outFileName,
} }
return true; return true;
#else #else
(void)outFileName;
(void)gzip;
(void)verbose;
return false; return false;
#endif #endif
} }
@ -1623,6 +1630,10 @@ bool cmSystemTools::ListTar(const char* outFileName,
} }
return true; return true;
#else #else
(void)outFileName;
(void)files;
(void)gzip;
(void)verbose;
return false; return false;
#endif #endif
} }

View File

@ -17,10 +17,10 @@
#include "cmakewizard.h" #include "cmakewizard.h"
#include "cmake.h" #include "cmake.h"
#include "cmCacheManager.h" #include "cmCacheManager.h"
#include "cmDynamicLoader.h"
#include "cmListFileCache.h" #include "cmListFileCache.h"
#ifdef CMAKE_BUILD_WITH_CMAKE #ifdef CMAKE_BUILD_WITH_CMAKE
#include "cmDynamicLoader.h"
#include "cmDocumentation.h" #include "cmDocumentation.h"
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------

View File

@ -37,8 +37,8 @@
// 1. HP machines which uses shl_load // 1. HP machines which uses shl_load
// 2. Mac OS X 10.2.x and earlier which uses NSLinkModule // 2. Mac OS X 10.2.x and earlier which uses NSLinkModule
// 3. Windows which uses LoadLibrary // 3. Windows which uses LoadLibrary
// 4. Most unix systems (including Mac OS X 10.3 and later) which use dlopen (default) // 4. Most unix systems (including Mac OS X 10.3 and later) which use dlopen
// Each part of the ifdef contains a complete implementation for // (default) Each part of the ifdef contains a complete implementation for
// the static methods of DynamicLoader. // the static methods of DynamicLoader.
namespace KWSYS_NAMESPACE namespace KWSYS_NAMESPACE
@ -66,25 +66,25 @@ namespace KWSYS_NAMESPACE
{ {
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
LibHandle DynamicLoader::OpenLibrary(const char* libname ) DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const char* libname )
{ {
return shl_load(libname, BIND_DEFERRED | DYNAMIC_PATH, 0L); return shl_load(libname, BIND_DEFERRED | DYNAMIC_PATH, 0L);
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
int DynamicLoader::CloseLibrary(LibHandle lib) int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib)
{ {
return !shl_unload(lib); return !shl_unload(lib);
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
DynamicLoaderFunction DynamicLoader::SymbolPointer
DynamicLoader::GetSymbolAddress(LibHandle lib, const char* sym) DynamicLoader::GetSymbolAddress(DynamicLoader::LibraryHandle lib, const char* sym)
{ {
void* addr; void* addr;
int status; int status;
/* TYPE_PROCEDURE Look for a function or procedure. /* TYPE_PROCEDURE Look for a function or procedure. (This used to be default)
* TYPE_DATA Look for a symbol in the data segment (for example, variables). * TYPE_DATA Look for a symbol in the data segment (for example, variables).
* TYPE_UNDEFINED Look for any symbol. * TYPE_UNDEFINED Look for any symbol.
*/ */
@ -92,7 +92,7 @@ DynamicLoader::GetSymbolAddress(LibHandle lib, const char* sym)
void* result = (status < 0) ? (void*)0 : addr; void* result = (status < 0) ? (void*)0 : addr;
// Hack to cast pointer-to-data to pointer-to-function. // Hack to cast pointer-to-data to pointer-to-function.
return *reinterpret_cast<DynamicLoaderFunction*>(&result); return *reinterpret_cast<DynamicLoader::SymbolPointer*>(&result);
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -149,7 +149,7 @@ namespace KWSYS_NAMESPACE
{ {
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
LibHandle DynamicLoader::OpenLibrary(const char* libname ) DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const char* libname )
{ {
NSObjectFileImageReturnCode rc; NSObjectFileImageReturnCode rc;
NSObjectFileImage image = 0; NSObjectFileImage image = 0;
@ -167,7 +167,7 @@ LibHandle DynamicLoader::OpenLibrary(const char* libname )
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
int DynamicLoader::CloseLibrary( LibHandle lib) int DynamicLoader::CloseLibrary( DynamicLoader::LibraryHandle lib)
{ {
// NSUNLINKMODULE_OPTION_KEEP_MEMORY_MAPPED // NSUNLINKMODULE_OPTION_KEEP_MEMORY_MAPPED
// With this option the memory for the module is not deallocated // With this option the memory for the module is not deallocated
@ -179,7 +179,8 @@ int DynamicLoader::CloseLibrary( LibHandle lib)
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
DynamicLoaderFunction DynamicLoader::GetSymbolAddress(LibHandle lib, const char* sym) DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress(
DynamicLoader::LibraryHandle lib, const char* sym)
{ {
void *result=0; void *result=0;
// Need to prepend symbols with '_' on Apple-gcc compilers // Need to prepend symbols with '_' on Apple-gcc compilers
@ -196,7 +197,7 @@ DynamicLoaderFunction DynamicLoader::GetSymbolAddress(LibHandle lib, const char*
delete[] rsym; delete[] rsym;
// Hack to cast pointer-to-data to pointer-to-function. // Hack to cast pointer-to-data to pointer-to-function.
return *reinterpret_cast<DynamicLoaderFunction*>(&result); return *reinterpret_cast<DynamicLoader::SymbolPointer*>(&result);
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -235,9 +236,9 @@ namespace KWSYS_NAMESPACE
{ {
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
LibHandle DynamicLoader::OpenLibrary(const char* libname) DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const char* libname)
{ {
LibHandle lh; DynamicLoader::LibraryHandle lh;
#ifdef UNICODE #ifdef UNICODE
wchar_t libn[MB_CUR_MAX]; wchar_t libn[MB_CUR_MAX];
mbstowcs(libn, libname, MB_CUR_MAX); mbstowcs(libn, libname, MB_CUR_MAX);
@ -249,13 +250,14 @@ LibHandle DynamicLoader::OpenLibrary(const char* libname)
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
int DynamicLoader::CloseLibrary(LibHandle lib) int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib)
{ {
return (int)FreeLibrary(lib); return (int)FreeLibrary(lib);
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
DynamicLoaderFunction DynamicLoader::GetSymbolAddress(LibHandle lib, const char* sym) DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress(
DynamicLoader::LibraryHandle lib, const char* sym)
{ {
void *result; void *result;
#ifdef __BORLANDC__ #ifdef __BORLANDC__
@ -278,7 +280,7 @@ DynamicLoaderFunction DynamicLoader::GetSymbolAddress(LibHandle lib, const char*
delete[] rsym; delete[] rsym;
#endif #endif
// Hack to cast pointer-to-data to pointer-to-function. // Hack to cast pointer-to-data to pointer-to-function.
return *reinterpret_cast<DynamicLoaderFunction*>(&result); return *reinterpret_cast<DynamicLoader::SymbolPointer*>(&result);
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@ -336,13 +338,13 @@ namespace KWSYS_NAMESPACE
{ {
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
LibHandle DynamicLoader::OpenLibrary(const char* libname ) DynamicLoader::LibraryHandle DynamicLoader::OpenLibrary(const char* libname )
{ {
return dlopen(libname, RTLD_LAZY); return dlopen(libname, RTLD_LAZY);
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
int DynamicLoader::CloseLibrary(LibHandle lib) int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib)
{ {
if (lib) if (lib)
{ {
@ -354,12 +356,13 @@ int DynamicLoader::CloseLibrary(LibHandle lib)
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
DynamicLoaderFunction DynamicLoader::GetSymbolAddress(LibHandle lib, const char* sym) DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress(
DynamicLoader::LibraryHandle lib, const char* sym)
{ {
void* result = dlsym(lib, sym); void* result = dlsym(lib, sym);
// Hack to cast pointer-to-data to pointer-to-function. // Hack to cast pointer-to-data to pointer-to-function.
return *reinterpret_cast<DynamicLoaderFunction*>(&result); return *reinterpret_cast<DynamicLoader::SymbolPointer*>(&result);
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------

View File

@ -16,46 +16,8 @@
#include <@KWSYS_NAMESPACE@/Configure.h> #include <@KWSYS_NAMESPACE@/Configure.h>
// Ugly stuff for library handles
// They are different on several different OS's
#if defined(__hpux)
#include <dl.h>
namespace @KWSYS_NAMESPACE@ namespace @KWSYS_NAMESPACE@
{ {
typedef shl_t LibHandle;
} // namespace @KWSYS_NAMESPACE@
#elif defined(_WIN32)
#include <windows.h>
namespace @KWSYS_NAMESPACE@
{
typedef HMODULE LibHandle;
} // namespace @KWSYS_NAMESPACE@
#elif defined(__APPLE__)
#include <AvailabilityMacros.h>
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1030
#include <mach-o/dyld.h>
namespace @KWSYS_NAMESPACE@
{
typedef NSModule LibHandle;
} // namespace @KWSYS_NAMESPACE@
#else
namespace @KWSYS_NAMESPACE@
{
typedef void* LibHandle;
} // namespace @KWSYS_NAMESPACE@
#endif
#else
namespace @KWSYS_NAMESPACE@
{
typedef void* LibHandle;
} // namespace @KWSYS_NAMESPACE@
#endif
namespace @KWSYS_NAMESPACE@
{
// Return type from DynamicLoader::GetSymbolAddress.
typedef void (*DynamicLoaderFunction)();
/** \class DynamicLoader /** \class DynamicLoader
* \brief Portable loading of dynamic libraries or dll's. * \brief Portable loading of dynamic libraries or dll's.
* *
@ -66,31 +28,54 @@ typedef void (*DynamicLoaderFunction)();
* operating systems * operating systems
* *
* \warning dlopen on *nix system works the following way: * \warning dlopen on *nix system works the following way:
* If filename contains a slash ("/"), then it is interpreted as a (relative or absolute) * If filename contains a slash ("/"), then it is interpreted as a (relative
* pathname. Otherwise, the dynamic linker searches for the library as follows : * or absolute) pathname. Otherwise, the dynamic linker searches for the
* see ld.so(8) for further details): * library as follows : see ld.so(8) for further details):
* Whereas this distinction does not exist on Win32. Therefore ideally you should be doing * Whereas this distinction does not exist on Win32. Therefore ideally you
* full path to garantee to have a consistent way of dealing with dynamic loading of shared * should be doing full path to garantee to have a consistent way of dealing
* library. * with dynamic loading of shared library.
*/ */
class @KWSYS_NAMESPACE@_EXPORT DynamicLoader class @KWSYS_NAMESPACE@_EXPORT DynamicLoader
{ {
public: public:
// Ugly stuff for library handles
// They are different on several different OS's
#if defined(__hpux)
#include <dl.h>
typedef shl_t LibraryHandle;
#elif defined(_WIN32)
#include <windows.h>
typedef HMODULE LibraryHandle;
#elif defined(__APPLE__)
#include <AvailabilityMacros.h>
#if MAC_OS_X_VERSION_MIN_REQUIRED < 1030
#include <mach-o/dyld.h>
typedef NSModule LibraryHandle;
#else
typedef void* LibraryHandle;
#endif
#else
typedef void* LibraryHandle;
#endif
// Return type from DynamicLoader::GetSymbolAddress.
typedef void (*SymbolPointer)();
DynamicLoader(); DynamicLoader();
~DynamicLoader(); ~DynamicLoader();
/** Load a dynamic library into the current process. /** Load a dynamic library into the current process.
* The returned LibHandle can be used to access the symbols in the * The returned LibraryHandle can be used to access the symbols in the
* library. */ * library. */
static LibHandle OpenLibrary(const char*); static LibraryHandle OpenLibrary(const char*);
/** Attempt to detach a dynamic library from the /** Attempt to detach a dynamic library from the
* process. A value of true is returned if it is sucessful. */ * process. A value of true is returned if it is sucessful. */
static int CloseLibrary(LibHandle); static int CloseLibrary(LibraryHandle);
/** Find the address of the symbol in the given library. */ /** Find the address of the symbol in the given library. */
static DynamicLoaderFunction GetSymbolAddress(LibHandle, const char*); static SymbolPointer GetSymbolAddress(LibraryHandle, const char*);
/** Return the library prefix for the given architecture */ /** Return the library prefix for the given architecture */
static const char* LibPrefix(); static const char* LibPrefix();

View File

@ -53,7 +53,8 @@ kwsys_stl::string GetLibName(const char* lname)
int TestDynamicLoader(const char* libname, const char* symbol, int r1, int r2, int r3) int TestDynamicLoader(const char* libname, const char* symbol, int r1, int r2, int r3)
{ {
kwsys_ios::cerr << "Testing: " << libname << kwsys_ios::endl; kwsys_ios::cerr << "Testing: " << libname << kwsys_ios::endl;
kwsys::LibHandle l = kwsys::DynamicLoader::OpenLibrary(libname); kwsys::DynamicLoader::LibraryHandle l
= kwsys::DynamicLoader::OpenLibrary(libname);
// If result is incompatible with expectation just fails (xor): // If result is incompatible with expectation just fails (xor):
if( (r1 && !l) || (!r1 && l) ) if( (r1 && !l) || (!r1 && l) )
{ {
@ -61,7 +62,8 @@ int TestDynamicLoader(const char* libname, const char* symbol, int r1, int r2, i
<< kwsys::DynamicLoader::LastError() << kwsys_ios::endl; << kwsys::DynamicLoader::LastError() << kwsys_ios::endl;
return 1; return 1;
} }
kwsys::DynamicLoaderFunction f = kwsys::DynamicLoader::GetSymbolAddress(l, symbol); kwsys::DynamicLoader::SymbolPointer f
= kwsys::DynamicLoader::GetSymbolAddress(l, symbol);
if( (r2 && !f) || (!r2 && f) ) if( (r2 && !f) || (!r2 && f) )
{ {
kwsys_ios::cerr kwsys_ios::cerr
@ -104,8 +106,8 @@ int main(int argc, char *argv[])
// Now try on the generated library // Now try on the generated library
kwsys_stl::string libname = GetLibName("testDynload"); kwsys_stl::string libname = GetLibName("testDynload");
res += TestDynamicLoader(libname.c_str(), "dummy",1,0,1); res += TestDynamicLoader(libname.c_str(), "dummy",1,0,1);
res += TestDynamicLoader(libname.c_str(), "TestDynamicLoaderFunction",1,1,1); res += TestDynamicLoader(libname.c_str(), "TestDynamicLoaderSymbolPointer",1,1,1);
res += TestDynamicLoader(libname.c_str(), "_TestDynamicLoaderFunction",1,0,1); res += TestDynamicLoader(libname.c_str(), "_TestDynamicLoaderSymbolPointer",1,0,1);
res += TestDynamicLoader(libname.c_str(), "TestDynamicLoaderData",1,1,1); res += TestDynamicLoader(libname.c_str(), "TestDynamicLoaderData",1,1,1);
res += TestDynamicLoader(libname.c_str(), "_TestDynamicLoaderData",1,0,1); res += TestDynamicLoader(libname.c_str(), "_TestDynamicLoaderData",1,0,1);

View File

@ -6,6 +6,6 @@
DL_EXPORT int TestDynamicLoaderData = 0; DL_EXPORT int TestDynamicLoaderData = 0;
DL_EXPORT void TestDynamicLoaderFunction() DL_EXPORT void TestDynamicLoaderSymbolPointer()
{ {
} }

View File

@ -15,6 +15,7 @@ extern "C" {
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cmOrderLinkDirectories.h" #include "cmOrderLinkDirectories.h"
#include "cmGeneratedFileStream.h" #include "cmGeneratedFileStream.h"
#include <cmsys/DynamicLoader.hxx>
#else #else
#include <vector> #include <vector>
#include <string> #include <string>
@ -327,7 +328,7 @@ int main()
lib += cmDynamicLoader::LibPrefix(); lib += cmDynamicLoader::LibPrefix();
lib += "CMakeTestModule"; lib += "CMakeTestModule";
lib += cmDynamicLoader::LibExtension(); lib += cmDynamicLoader::LibExtension();
cmLibHandle handle = cmDynamicLoader::OpenLibrary(lib.c_str()); cmsys::DynamicLoader::LibraryHandle handle = cmDynamicLoader::OpenLibrary(lib.c_str());
if(!handle) if(!handle)
{ {
std::string err = "Can not open CMakeTestModule:\n"; std::string err = "Can not open CMakeTestModule:\n";
@ -336,11 +337,11 @@ int main()
} }
else else
{ {
cmDynamicLoaderFunction fun = cmsys::DynamicLoader::SymbolPointer fun =
cmDynamicLoader::GetSymbolAddress(handle, "ModuleFunction"); cmsys::DynamicLoader::GetSymbolAddress(handle, "ModuleFunction");
if(!fun) if(!fun)
{ {
fun = cmDynamicLoader::GetSymbolAddress(handle, "_ModuleFunction"); fun = cmsys::DynamicLoader::GetSymbolAddress(handle, "_ModuleFunction");
} }
typedef int (*TEST_FUNCTION)(); typedef int (*TEST_FUNCTION)();
TEST_FUNCTION testFun = (TEST_FUNCTION)fun; TEST_FUNCTION testFun = (TEST_FUNCTION)fun;

View File

@ -15,6 +15,7 @@ extern "C" {
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cmOrderLinkDirectories.h" #include "cmOrderLinkDirectories.h"
#include "cmGeneratedFileStream.h" #include "cmGeneratedFileStream.h"
#include <cmsys/DynamicLoader.hxx>
#else #else
#include <vector> #include <vector>
#include <string> #include <string>
@ -327,7 +328,7 @@ int main()
lib += cmDynamicLoader::LibPrefix(); lib += cmDynamicLoader::LibPrefix();
lib += "CMakeTestModule"; lib += "CMakeTestModule";
lib += cmDynamicLoader::LibExtension(); lib += cmDynamicLoader::LibExtension();
cmLibHandle handle = cmDynamicLoader::OpenLibrary(lib.c_str()); cmsys::DynamicLoader::LibraryHandle handle = cmDynamicLoader::OpenLibrary(lib.c_str());
if(!handle) if(!handle)
{ {
std::string err = "Can not open CMakeTestModule:\n"; std::string err = "Can not open CMakeTestModule:\n";
@ -336,11 +337,11 @@ int main()
} }
else else
{ {
cmDynamicLoaderFunction fun = cmsys::DynamicLoader::SymbolPointer fun =
cmDynamicLoader::GetSymbolAddress(handle, "ModuleFunction"); cmsys::DynamicLoader::GetSymbolAddress(handle, "ModuleFunction");
if(!fun) if(!fun)
{ {
fun = cmDynamicLoader::GetSymbolAddress(handle, "_ModuleFunction"); fun = cmsys::DynamicLoader::GetSymbolAddress(handle, "_ModuleFunction");
} }
typedef int (*TEST_FUNCTION)(); typedef int (*TEST_FUNCTION)();
TEST_FUNCTION testFun = (TEST_FUNCTION)fun; TEST_FUNCTION testFun = (TEST_FUNCTION)fun;

View File

@ -15,6 +15,7 @@ extern "C" {
#include "cmSystemTools.h" #include "cmSystemTools.h"
#include "cmOrderLinkDirectories.h" #include "cmOrderLinkDirectories.h"
#include "cmGeneratedFileStream.h" #include "cmGeneratedFileStream.h"
#include <cmsys/DynamicLoader.hxx>
#else #else
#include <vector> #include <vector>
#include <string> #include <string>
@ -327,7 +328,7 @@ int main()
lib += cmDynamicLoader::LibPrefix(); lib += cmDynamicLoader::LibPrefix();
lib += "CMakeTestModule"; lib += "CMakeTestModule";
lib += cmDynamicLoader::LibExtension(); lib += cmDynamicLoader::LibExtension();
cmLibHandle handle = cmDynamicLoader::OpenLibrary(lib.c_str()); cmsys::DynamicLoader::LibraryHandle handle = cmDynamicLoader::OpenLibrary(lib.c_str());
if(!handle) if(!handle)
{ {
std::string err = "Can not open CMakeTestModule:\n"; std::string err = "Can not open CMakeTestModule:\n";
@ -336,11 +337,11 @@ int main()
} }
else else
{ {
cmDynamicLoaderFunction fun = cmsys::DynamicLoader::SymbolPointer fun =
cmDynamicLoader::GetSymbolAddress(handle, "ModuleFunction"); cmsys::DynamicLoader::GetSymbolAddress(handle, "ModuleFunction");
if(!fun) if(!fun)
{ {
fun = cmDynamicLoader::GetSymbolAddress(handle, "_ModuleFunction"); fun = cmsys::DynamicLoader::GetSymbolAddress(handle, "_ModuleFunction");
} }
typedef int (*TEST_FUNCTION)(); typedef int (*TEST_FUNCTION)();
TEST_FUNCTION testFun = (TEST_FUNCTION)fun; TEST_FUNCTION testFun = (TEST_FUNCTION)fun;