diff options
author | Andy Cedilnik <andy.cedilnik@kitware.com> | 2006-03-16 16:01:05 (GMT) |
---|---|---|
committer | Andy Cedilnik <andy.cedilnik@kitware.com> | 2006-03-16 16:01:05 (GMT) |
commit | f483e48d2a2f3361dcf3c60096bbfe9975f90ddf (patch) | |
tree | 2909a53a3efb79bf633ba0be2fa93d7c7b68181f /Source/kwsys | |
parent | 4102949bf95eee1ce88544fafdf80439aece85b2 (diff) | |
download | CMake-f483e48d2a2f3361dcf3c60096bbfe9975f90ddf.zip CMake-f483e48d2a2f3361dcf3c60096bbfe9975f90ddf.tar.gz CMake-f483e48d2a2f3361dcf3c60096bbfe9975f90ddf.tar.bz2 |
ENH: Cleanup DynamicLoader so that the symbols have more consistent names, start using dynamic loader from kwsys in CMake
Diffstat (limited to 'Source/kwsys')
-rw-r--r-- | Source/kwsys/DynamicLoader.cxx | 45 | ||||
-rw-r--r-- | Source/kwsys/DynamicLoader.hxx.in | 81 | ||||
-rw-r--r-- | Source/kwsys/testDynamicLoader.cxx | 10 | ||||
-rw-r--r-- | Source/kwsys/testDynload.c | 2 |
4 files changed, 64 insertions, 74 deletions
diff --git a/Source/kwsys/DynamicLoader.cxx b/Source/kwsys/DynamicLoader.cxx index fed90ea..486d5b3 100644 --- a/Source/kwsys/DynamicLoader.cxx +++ b/Source/kwsys/DynamicLoader.cxx @@ -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); } //---------------------------------------------------------------------------- diff --git a/Source/kwsys/DynamicLoader.hxx.in b/Source/kwsys/DynamicLoader.hxx.in index ee6eebe..29d213e 100644 --- a/Source/kwsys/DynamicLoader.hxx.in +++ b/Source/kwsys/DynamicLoader.hxx.in @@ -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(); diff --git a/Source/kwsys/testDynamicLoader.cxx b/Source/kwsys/testDynamicLoader.cxx index efd84d6..444f6d9 100644 --- a/Source/kwsys/testDynamicLoader.cxx +++ b/Source/kwsys/testDynamicLoader.cxx @@ -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); diff --git a/Source/kwsys/testDynload.c b/Source/kwsys/testDynload.c index 7308530..67c33c3 100644 --- a/Source/kwsys/testDynload.c +++ b/Source/kwsys/testDynload.c @@ -6,6 +6,6 @@ DL_EXPORT int TestDynamicLoaderData = 0; -DL_EXPORT void TestDynamicLoaderFunction() +DL_EXPORT void TestDynamicLoaderSymbolPointer() { } |