2009-02-08 23:56:35 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
2009-02-08 23:56:35 +08:00
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
2003-01-22 00:45:36 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2009-02-08 23:56:35 +08:00
|
|
|
*
|
2003-01-22 00:45:36 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2009-02-08 23:56:35 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2003-01-22 00:45:36 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2006-02-06 05:53:37 +08:00
|
|
|
|
2009-02-08 23:56:35 +08:00
|
|
|
//The dlopen calls were not adding to OS X until 10.3
|
2006-02-06 05:53:37 +08:00
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <AvailabilityMacros.h>
|
|
|
|
#if !defined(MAC_OS_X_VERSION_10_3) || (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_3)
|
|
|
|
#define APPLE_PRE_10_3
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2002-01-04 05:34:57 +08:00
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__)
|
2008-04-11 18:30:00 +08:00
|
|
|
#include <io.h>
|
|
|
|
#include <windows.h>
|
|
|
|
#include <winbase.h>
|
2006-02-06 05:53:37 +08:00
|
|
|
#elif defined(__APPLE__) && defined(APPLE_PRE_10_3)
|
2002-07-17 03:21:31 +08:00
|
|
|
#include <mach-o/dyld.h>
|
|
|
|
#else // all other unix
|
2001-09-20 05:08:56 +08:00
|
|
|
#include <unistd.h>
|
2009-11-18 20:15:29 +08:00
|
|
|
#ifdef __hpux
|
2002-07-23 18:48:22 +08:00
|
|
|
// Although HP-UX has dlopen() it is broken! We therefore need to stick
|
|
|
|
// to shl_load()/shl_unload()/shl_findsym()
|
|
|
|
#include <dl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#else
|
2001-09-20 05:08:56 +08:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
2002-07-23 18:48:22 +08:00
|
|
|
#endif
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-10-22 05:27:40 +08:00
|
|
|
#include <osg/Notify>
|
2009-06-08 18:54:16 +08:00
|
|
|
#include <osg/GLExtensions>
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-10-22 05:27:40 +08:00
|
|
|
#include <osgDB/DynamicLibrary>
|
|
|
|
#include <osgDB/FileUtils>
|
2003-11-06 12:08:53 +08:00
|
|
|
#include <osgDB/FileNameUtils>
|
2010-06-17 19:03:23 +08:00
|
|
|
#include <osgDB/ConvertUTF>
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
using namespace osgDB;
|
|
|
|
|
2009-02-08 23:56:35 +08:00
|
|
|
DynamicLibrary::DynamicLibrary(const std::string& name, HANDLE handle)
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
|
|
|
_name = name;
|
|
|
|
_handle = handle;
|
2010-05-28 23:52:45 +08:00
|
|
|
OSG_INFO<<"Opened DynamicLibrary "<<_name<<std::endl;
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DynamicLibrary::~DynamicLibrary()
|
|
|
|
{
|
|
|
|
if (_handle)
|
|
|
|
{
|
2010-05-28 23:52:45 +08:00
|
|
|
OSG_INFO<<"Closing DynamicLibrary "<<_name<<std::endl;
|
2002-01-04 05:34:57 +08:00
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__)
|
2001-09-20 05:08:56 +08:00
|
|
|
FreeLibrary((HMODULE)_handle);
|
2006-02-06 05:53:37 +08:00
|
|
|
#elif defined(__APPLE__) && defined(APPLE_PRE_10_3)
|
2005-05-06 05:21:14 +08:00
|
|
|
NSUnLinkModule(static_cast<NSModule>(_handle), FALSE);
|
2009-11-18 20:15:29 +08:00
|
|
|
#elif defined(__hpux)
|
2002-07-23 18:48:22 +08:00
|
|
|
// fortunately, shl_t is a pointer
|
|
|
|
shl_unload (static_cast<shl_t>(_handle));
|
2002-07-17 03:21:31 +08:00
|
|
|
#else // other unix
|
2001-09-20 05:08:56 +08:00
|
|
|
dlclose(_handle);
|
2009-02-08 23:56:35 +08:00
|
|
|
#endif
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DynamicLibrary* DynamicLibrary::loadLibrary(const std::string& libraryName)
|
|
|
|
{
|
|
|
|
|
2003-03-10 19:57:17 +08:00
|
|
|
HANDLE handle = NULL;
|
|
|
|
|
|
|
|
std::string fullLibraryName = osgDB::findLibraryFile(libraryName);
|
|
|
|
if (!fullLibraryName.empty()) handle = getLibraryHandle( fullLibraryName ); // try the lib we have found
|
|
|
|
else handle = getLibraryHandle( libraryName ); // havn't found a lib ourselves, see if the OS can find it simply from the library name.
|
2009-02-08 23:56:35 +08:00
|
|
|
|
2002-12-16 21:40:58 +08:00
|
|
|
if (handle) return new DynamicLibrary(libraryName,handle);
|
2003-03-10 19:57:17 +08:00
|
|
|
|
|
|
|
// else no lib found so report errors.
|
2010-05-28 23:52:45 +08:00
|
|
|
OSG_INFO << "DynamicLibrary::failed loading \""<<libraryName<<"\""<<std::endl;
|
2003-03-10 19:57:17 +08:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-12-10 19:40:58 +08:00
|
|
|
DynamicLibrary::HANDLE DynamicLibrary::getLibraryHandle( const std::string& libraryName)
|
2003-03-10 19:57:17 +08:00
|
|
|
{
|
|
|
|
HANDLE handle = NULL;
|
|
|
|
|
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__)
|
2010-06-17 19:03:23 +08:00
|
|
|
#ifdef OSG_USE_UTF8_FILENAME
|
|
|
|
handle = LoadLibraryW( convertUTF8toUTF16(libraryName).c_str() );
|
|
|
|
#else
|
2003-03-10 19:57:17 +08:00
|
|
|
handle = LoadLibrary( libraryName.c_str() );
|
2010-06-17 19:03:23 +08:00
|
|
|
#endif
|
2006-02-06 05:53:37 +08:00
|
|
|
#elif defined(__APPLE__) && defined(APPLE_PRE_10_3)
|
2002-07-17 03:21:31 +08:00
|
|
|
NSObjectFileImage image;
|
|
|
|
// NSModule os_handle = NULL;
|
2003-03-10 19:57:17 +08:00
|
|
|
if (NSCreateObjectFileImageFromFile(libraryName.c_str(), &image) == NSObjectFileImageSuccess) {
|
|
|
|
// os_handle = NSLinkModule(image, libraryName.c_str(), TRUE);
|
|
|
|
handle = NSLinkModule(image, libraryName.c_str(), TRUE);
|
2002-07-17 03:21:31 +08:00
|
|
|
NSDestroyObjectFileImage(image);
|
|
|
|
}
|
2009-11-18 20:15:29 +08:00
|
|
|
#elif defined(__hpux)
|
2002-07-23 18:48:22 +08:00
|
|
|
// BIND_FIRST is neccessary for some reason
|
2003-12-10 19:40:58 +08:00
|
|
|
handle = shl_load ( libraryName.c_str(), BIND_DEFERRED|BIND_FIRST|BIND_VERBOSE, 0);
|
2003-03-10 19:57:17 +08:00
|
|
|
return handle;
|
2002-07-17 03:21:31 +08:00
|
|
|
#else // other unix
|
2003-11-06 12:08:53 +08:00
|
|
|
|
|
|
|
// dlopen will not work with files in the current directory unless
|
|
|
|
// they are prefaced with './' (DB - Nov 5, 2003).
|
|
|
|
std::string localLibraryName;
|
|
|
|
if( libraryName == osgDB::getSimpleFileName( libraryName ) )
|
|
|
|
localLibraryName = "./" + libraryName;
|
|
|
|
else
|
|
|
|
localLibraryName = libraryName;
|
2009-02-08 23:56:35 +08:00
|
|
|
|
2003-11-06 12:08:53 +08:00
|
|
|
handle = dlopen( localLibraryName.c_str(), RTLD_LAZY | RTLD_GLOBAL);
|
2003-11-05 12:56:08 +08:00
|
|
|
if( handle == NULL )
|
2009-02-08 23:56:35 +08:00
|
|
|
{
|
|
|
|
if (fileExists(localLibraryName))
|
|
|
|
{
|
2010-05-28 23:52:45 +08:00
|
|
|
OSG_WARN << "Warning: dynamic library '" << libraryName << "' exists, but an error occurred while trying to open it:" << std::endl;
|
|
|
|
OSG_WARN << dlerror() << std::endl;
|
2009-02-08 23:56:35 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-05-28 23:52:45 +08:00
|
|
|
OSG_INFO << "Warning: dynamic library '" << libraryName << "' does not exist (or isn't readable):" << std::endl;
|
|
|
|
OSG_INFO << dlerror() << std::endl;
|
2009-02-08 23:56:35 +08:00
|
|
|
}
|
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
#endif
|
2003-03-10 19:57:17 +08:00
|
|
|
return handle;
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DynamicLibrary::PROC_ADDRESS DynamicLibrary::getProcAddress(const std::string& procName)
|
|
|
|
{
|
|
|
|
if (_handle==NULL) return NULL;
|
2002-01-04 05:34:57 +08:00
|
|
|
#if defined(WIN32) && !defined(__CYGWIN__)
|
2009-06-08 18:54:16 +08:00
|
|
|
return osg::convertPointerType<DynamicLibrary::PROC_ADDRESS, FARPROC>( GetProcAddress( (HMODULE)_handle, procName.c_str() ) );
|
2006-02-06 05:53:37 +08:00
|
|
|
#elif defined(__APPLE__) && defined(APPLE_PRE_10_3)
|
2002-07-17 03:21:31 +08:00
|
|
|
std::string temp("_");
|
|
|
|
NSSymbol symbol;
|
2005-11-17 21:35:53 +08:00
|
|
|
temp += procName; // Mac OS X prepends an underscore on function names
|
2005-06-24 23:30:11 +08:00
|
|
|
symbol = NSLookupSymbolInModule(static_cast<NSModule>(_handle), temp.c_str());
|
2002-07-17 03:21:31 +08:00
|
|
|
return NSAddressOfSymbol(symbol);
|
2009-11-18 20:15:29 +08:00
|
|
|
#elif defined(__hpux)
|
2002-07-23 18:48:22 +08:00
|
|
|
void* result = NULL;
|
|
|
|
if (shl_findsym (reinterpret_cast<shl_t*>(&_handle), procName.c_str(), TYPE_PROCEDURE, result) == 0)
|
|
|
|
{
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-05-28 23:52:45 +08:00
|
|
|
OSG_WARN << "DynamicLibrary::failed looking up " << procName << std::endl;
|
|
|
|
OSG_WARN << "DynamicLibrary::error " << strerror(errno) << std::endl;
|
2002-07-23 18:48:22 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2002-07-17 03:21:31 +08:00
|
|
|
#else // other unix
|
2002-08-04 02:11:21 +08:00
|
|
|
void* sym = dlsym( _handle, procName.c_str() );
|
|
|
|
if (!sym) {
|
2010-05-28 23:52:45 +08:00
|
|
|
OSG_WARN << "DynamicLibrary::failed looking up " << procName << std::endl;
|
|
|
|
OSG_WARN << "DynamicLibrary::error " << dlerror() << std::endl;
|
2002-08-04 02:11:21 +08:00
|
|
|
}
|
|
|
|
return sym;
|
2001-09-20 05:08:56 +08:00
|
|
|
#endif
|
|
|
|
}
|
2009-02-08 23:56:35 +08:00
|
|
|
|