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

@ -16,27 +16,19 @@
=========================================================================*/
#include "cmDynamicLoader.h"
// This file is actually several different implementations.
// 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:
~cmDynamicLoaderCache();
void CacheFile(const char* path, const cmLibHandle&);
bool GetCacheFile(const char* path, cmLibHandle&);
void CacheFile(const char* path,
const cmsys::DynamicLoader::LibraryHandle&);
bool GetCacheFile(const char* path, cmsys::DynamicLoader::LibraryHandle&);
bool FlushCache(const char* path);
void FlushCache();
static cmDynamicLoaderCache* GetInstance();
private:
std::map<cmStdString, cmLibHandle> CacheMap;
std::map<cmStdString, cmsys::DynamicLoader::LibraryHandle> CacheMap;
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) )
{
this->FlushCache(path);
@ -56,9 +49,11 @@ void cmDynamicLoaderCache::CacheFile(const char* path, const cmLibHandle& 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() )
{
p = it->second;
@ -69,11 +64,12 @@ bool cmDynamicLoaderCache::GetCacheFile(const char* path, cmLibHandle& p)
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;
if ( it != this->CacheMap.end() )
{
cmDynamicLoader::CloseLibrary(it->second);
cmsys::DynamicLoader::CloseLibrary(it->second);
this->CacheMap.erase(it);
ret = true;
}
@ -82,10 +78,12 @@ bool cmDynamicLoaderCache::FlushCache(const char* path)
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++ )
{
cmDynamicLoader::CloseLibrary(it->second);
cmsys::DynamicLoader::CloseLibrary(it->second);
}
delete cmDynamicLoaderCache::Instance;
cmDynamicLoaderCache::Instance = 0;
@ -100,231 +98,19 @@ cmDynamicLoaderCache* cmDynamicLoaderCache::GetInstance()
return cmDynamicLoaderCache::Instance;
}
// ---------------------------------------------------------------
// 1. Implementation for HPUX machines
#ifdef __hpux
#define CMDYNAMICLOADER_DEFINED 1
#include <dl.h>
cmLibHandle cmDynamicLoader::OpenLibrary(const char* libname )
cmsys::DynamicLoader::LibraryHandle cmDynamicLoader::OpenLibrary(
const char* libname )
{
cmLibHandle lh;
cmsys::DynamicLoader::LibraryHandle lh;
if ( cmDynamicLoaderCache::GetInstance()->GetCacheFile(libname, lh) )
{
return lh;
}
lh = shl_load(libname, BIND_DEFERRED | DYNAMIC_PATH, 0L);
lh = cmsys::DynamicLoader::OpenLibrary(libname);
cmDynamicLoaderCache::GetInstance()->CacheFile(libname, 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()
{
cmDynamicLoaderCache::GetInstance()->FlushCache();

View File

@ -25,46 +25,16 @@
#include "cmStandardIncludes.h"
// Ugly stuff for library handles
// 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)();
#include <cmsys/DynamicLoader.hxx>
class cmDynamicLoader
{
public:
// Description:
// Load a dynamic library into the current process.
// The returned cmLibHandle can be used to access the symbols in the
// library.
static cmLibHandle 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*);
// The returned cmsys::DynamicLoader::LibraryHandle can be used to access
// the symbols in the library.
static cmsys::DynamicLoader::LibraryHandle OpenLibrary(const char*);
// Description:
// Return the library prefix for the given architecture
@ -74,10 +44,6 @@ public:
// Return the library extension for the given architecture
static const char* LibExtension();
// Description:
// Return the last error produced from a calls made on this class.
static const char* LastError();
// Description:
// Flush the cache of dynamic loader.
static void FlushCache();
@ -86,7 +52,6 @@ protected:
cmDynamicLoader() {};
~cmDynamicLoader() {};
private:
cmDynamicLoader(const cmDynamicLoader&); // Not implemented.
void operator=(const cmDynamicLoader&); // Not implemented.

View File

@ -18,6 +18,9 @@
#include "cmCPluginAPI.h"
#include "cmCPluginAPI.cxx"
#include "cmDynamicLoader.h"
#include <cmsys/DynamicLoader.hxx>
#include <signal.h>
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
cmLibHandle lib = cmDynamicLoader::OpenLibrary(fullPath.c_str());
cmsys::DynamicLoader::LibraryHandle lib
= cmDynamicLoader::OpenLibrary(fullPath.c_str());
if(!lib)
{
std::string err = "Attempt to load the library ";
err += fullPath + " failed.";
const char* error = cmDynamicLoader::LastError();
const char* error = cmsys::DynamicLoader::LastError();
if ( error )
{
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";
CM_INIT_FUNCTION initFunction
= (CM_INIT_FUNCTION)
cmDynamicLoader::GetSymbolAddress(lib, initFuncName.c_str());
cmsys::DynamicLoader::GetSymbolAddress(lib, initFuncName.c_str());
if ( !initFunction )
{
initFuncName = "_";
initFuncName += args[0];
initFuncName += "Init";
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
// function blocker

View File

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

View File

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

View File

@ -37,8 +37,8 @@
// 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
// 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 DynamicLoader.
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);
}
//----------------------------------------------------------------------------
int DynamicLoader::CloseLibrary(LibHandle lib)
int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle lib)
{
return !shl_unload(lib);
}
//----------------------------------------------------------------------------
DynamicLoaderFunction
DynamicLoader::GetSymbolAddress(LibHandle lib, const char* sym)
DynamicLoader::SymbolPointer
DynamicLoader::GetSymbolAddress(DynamicLoader::LibraryHandle lib, const char* sym)
{
void* addr;
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_UNDEFINED Look for any symbol.
*/
@ -92,7 +92,7 @@ DynamicLoader::GetSymbolAddress(LibHandle lib, const char* sym)
void* result = (status < 0) ? (void*)0 : addr;
// 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;
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
// 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;
// Need to prepend symbols with '_' on Apple-gcc compilers
@ -196,7 +197,7 @@ DynamicLoaderFunction DynamicLoader::GetSymbolAddress(LibHandle lib, const char*
delete[] rsym;
// 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
wchar_t libn[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);
}
//----------------------------------------------------------------------------
DynamicLoaderFunction DynamicLoader::GetSymbolAddress(LibHandle lib, const char* sym)
DynamicLoader::SymbolPointer DynamicLoader::GetSymbolAddress(
DynamicLoader::LibraryHandle lib, const char* sym)
{
void *result;
#ifdef __BORLANDC__
@ -278,7 +280,7 @@ DynamicLoaderFunction DynamicLoader::GetSymbolAddress(LibHandle lib, const char*
delete[] rsym;
#endif
// 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);
}
//----------------------------------------------------------------------------
int DynamicLoader::CloseLibrary(LibHandle lib)
int DynamicLoader::CloseLibrary(DynamicLoader::LibraryHandle 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);
// 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>
// Ugly stuff for library handles
// They are different on several different OS's
#if defined(__hpux)
#include <dl.h>
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
* \brief Portable loading of dynamic libraries or dll's.
*
@ -66,31 +28,54 @@ typedef void (*DynamicLoaderFunction)();
* operating systems
*
* \warning dlopen on *nix system works the following way:
* If filename contains a slash ("/"), then it is interpreted as a (relative or absolute)
* pathname. Otherwise, the dynamic linker searches for the library as follows :
* see ld.so(8) for further details):
* Whereas this distinction does not exist on Win32. Therefore ideally you should be doing
* full path to garantee to have a consistent way of dealing with dynamic loading of shared
* library.
* If filename contains a slash ("/"), then it is interpreted as a (relative
* or absolute) pathname. Otherwise, the dynamic linker searches for the
* library as follows : see ld.so(8) for further details):
* Whereas this distinction does not exist on Win32. Therefore ideally you
* should be doing full path to garantee to have a consistent way of dealing
* with dynamic loading of shared library.
*/
class @KWSYS_NAMESPACE@_EXPORT DynamicLoader
{
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();
/** 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. */
static LibHandle OpenLibrary(const char*);
static LibraryHandle OpenLibrary(const char*);
/** Attempt to detach a dynamic library from the
* 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. */
static DynamicLoaderFunction GetSymbolAddress(LibHandle, const char*);
static SymbolPointer GetSymbolAddress(LibraryHandle, const char*);
/** Return the library prefix for the given architecture */
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)
{
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( (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;
return 1;
}
kwsys::DynamicLoaderFunction f = kwsys::DynamicLoader::GetSymbolAddress(l, symbol);
kwsys::DynamicLoader::SymbolPointer f
= kwsys::DynamicLoader::GetSymbolAddress(l, symbol);
if( (r2 && !f) || (!r2 && f) )
{
kwsys_ios::cerr
@ -104,8 +106,8 @@ int main(int argc, char *argv[])
// Now try on the generated library
kwsys_stl::string libname = GetLibName("testDynload");
res += TestDynamicLoader(libname.c_str(), "dummy",1,0,1);
res += TestDynamicLoader(libname.c_str(), "TestDynamicLoaderFunction",1,1,1);
res += TestDynamicLoader(libname.c_str(), "_TestDynamicLoaderFunction",1,0,1);
res += TestDynamicLoader(libname.c_str(), "TestDynamicLoaderSymbolPointer",1,1,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,0,1);

View File

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

View File

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

View File

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