HP-UX support added

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@1751 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
1999-02-22 19:42:45 +00:00
parent f76f940bce
commit 7b0bfbb256
2 changed files with 259 additions and 179 deletions

View File

@@ -1,32 +1,60 @@
/////////////////////////////////////////////////////////////////////////////
// Name: dynlib.cpp
// Purpose: Dynamic library management
// Author: Guilhem Lavaux
// Modified by:
// Created: 20/07/98
// RCS-ID: $Id$
// Copyright: (c) Guilhem Lavaux
// Licence: wxWindows license
/////////////////////////////////////////////////////////////////////////////
#ifndef _WX_DYNLIB_H__ #ifndef _WX_DYNLIB_H__
#define _WX_DYNLIB_H__ #define _WX_DYNLIB_H__
#ifdef __GNUG__ #ifdef __GNUG__
#pragma interface #pragma interface
#endif #endif
#include <wx/string.h> #include <wx/string.h>
#include <wx/list.h> #include <wx/list.h>
#include <wx/dynarray.h>
#include <wx/hash.h> #include <wx/hash.h>
#if defined(HAVE_DLOPEN)
#include <dlfcn.h>
typedef void *wxDllType;
#elif defined(HAVE_SHLLOAD)
#include <dl.h>
typedef void *wxDllType;
#elif defined(__WINDOWS__)
#include <windows.h>
typedef HMODULE wxDllType;
#elif defined(__WXMAC__)
typedef CFragConnectionID wxDllType;
#else
#error "wxLibrary can't be compiled on this platform, sorry."
#endif // OS
// defined in windows.h
#ifdef LoadLibrary #ifdef LoadLibrary
#undef LoadLibrary #undef LoadLibrary
#endif #endif
// --------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxLibrary // wxLibrary
// ----------------------------------------------------------------------------
class wxLibrary: public wxObject { class wxLibrary : public wxObject
protected: {
void *m_handle; public:
bool m_destroy;
public:
wxHashTable classTable; wxHashTable classTable;
public: public:
wxLibrary(void *handle); wxLibrary(void *handle);
~wxLibrary(void); ~wxLibrary();
// Get a symbol from the dynamic library // Get a symbol from the dynamic library
void *GetSymbol(const wxString& symbname); void *GetSymbol(const wxString& symbname);
@@ -34,35 +62,40 @@ class wxLibrary: public wxObject {
// Create the object whose classname is "name" // Create the object whose classname is "name"
wxObject *CreateObject(const wxString& name); wxObject *CreateObject(const wxString& name);
// Merge the symbols with the main symbols: WARNING! the library will not protected:
// be unloaded.
void MergeWithSystem();
protected:
void PrepareClasses(wxClassInfo *first); void PrepareClasses(wxClassInfo *first);
wxDllType m_handle;
}; };
// --------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxLibraries // wxLibraries
// ----------------------------------------------------------------------------
class wxLibraries { class wxLibraries
protected: {
wxList m_loaded; public:
public: wxLibraries();
wxLibraries(void); ~wxLibraries();
~wxLibraries(void);
// caller is responsible for deleting the returned pointer if !NULL
wxLibrary *LoadLibrary(const wxString& basename);
wxLibrary *LoadLibrary(const wxString& name);
wxObject *CreateObject(const wxString& name); wxObject *CreateObject(const wxString& name);
protected:
wxList m_loaded;
}; };
// --------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Global variables // Global variables
// ----------------------------------------------------------------------------
extern wxLibraries wxTheLibraries; extern wxLibraries wxTheLibraries;
// --------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Interesting defines // Interesting defines
// ----------------------------------------------------------------------------
#define WXDLL_ENTRY_FUNCTION() \ #define WXDLL_ENTRY_FUNCTION() \
extern "C" wxClassInfo *wxGetClassFirst(); \ extern "C" wxClassInfo *wxGetClassFirst(); \
@@ -70,4 +103,4 @@ wxClassInfo *wxGetClassFirst() { \
return wxClassInfo::GetFirst(); \ return wxClassInfo::GetFirst(); \
} }
#endif #endif // _WX_DYNLIB_H__

View File

@@ -9,8 +9,16 @@
// Licence: wxWindows license // Licence: wxWindows license
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
#ifdef __GNUG__ #ifdef __GNUG__
#pragma implementation "dynlib.h" #pragma implementation "dynlib.h"
#endif #endif
#include "wx/wxprec.h" #include "wx/wxprec.h"
@@ -19,29 +27,51 @@
#pragma hdrstop #pragma hdrstop
#endif //__BORLANDC__ #endif //__BORLANDC__
#ifndef WX_PRECOMP // TODO should be done by configure
#endif //WX_PRECOMP #if defined(__UNIX__) && !(defined(HAVE_DLOPEN) || defined(HAVE_SHLLOAD))
#if defined(__LINUX__) || defined(__SOLARIS__) || defined(__SUNOS__)
#ifndef HAVE_DLOPEN
#define HAVE_DLOPEN
#endif
#elif defined(__HPUX__)
#ifndef HAVE_SHLLOAD
#define HAVE_SHLLOAD
#endif
#endif // Unix flavour
#endif // !Unix or already have some HAVE_xxx defined
#include <wx/dynlib.h> #include "wx/dynlib.h"
#include <wx/filefn.h> #include "wx/filefn.h"
#include <wx/list.h> #include "wx/intl.h"
#include <wx/string.h> #include "wx/log.h"
// --------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// System dependent include // conditional compilation
// --------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#if defined(__UNIX__) #if defined(HAVE_DLOPEN)
#include <dlfcn.h> #define wxDllOpen(lib) dlopen(lib, RTLD_LAZY)
#endif #define wxDllGetSymbol(handle, name) dlsym(handle, (char *)name)
#define wxDllClose dlclose
#elif defined(HAVE_SHLLOAD)
#define wxDllOpen(lib) shl_open(lib, BIND_DEFERRED, 0)
#define wxDllClose shl_unload
#ifdef __WINDOWS__ static inline void *wxDllGetSymbol(shl_t *handle, const char *name)
#include <windows.h> {
#endif void *sym;
if ( shl_findsym(handle, name, TYPE_UNDEFINED, &sym) == 0 )
#ifdef LoadLibrary return sym;
#undef LoadLibrary else
#endif return (void *)0;
}
#elif defined(__WINDOWS__)
#define wxDllOpen(lib) ::LoadLibrary(lib)
#define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
#define wxDllClose ::FreeLibrary
#else
#error "Don't know how to load shared libraries on this platform."
#endif // OS
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// Global variables // Global variables
@@ -49,17 +79,39 @@
wxLibraries wxTheLibraries; wxLibraries wxTheLibraries;
// ----------------------------------------------------------------------------
// private functions
// ----------------------------------------------------------------------------
// construct the full name from the base shared object name: adds a .dll
// suffix under Windows or .so under Unix
static wxString ConstructLibraryName(const wxString& basename)
{
wxString fullname(basename);
#if defined(__UNIX__)
fullname << ".so";
#elif defined(__WINDOWS__)
fullname << ".dll";
#endif
return fullname;
}
// ============================================================================
// implementation
// ============================================================================
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
// wxLibrary (one instance per dynamic library // wxLibrary (one instance per dynamic library)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
wxLibrary::wxLibrary(void *handle) wxLibrary::wxLibrary(wxDllType handle)
{ {
typedef wxClassInfo *(*t_get_first)(void); typedef wxClassInfo *(*t_get_first)(void);
t_get_first get_first; t_get_first get_first;
m_handle = handle; m_handle = handle;
m_destroy = TRUE;
// Some system may use a local heap for library. // Some system may use a local heap for library.
get_first = (t_get_first)GetSymbol("wxGetClassFirst"); get_first = (t_get_first)GetSymbol("wxGetClassFirst");
@@ -70,13 +122,9 @@ wxLibrary::wxLibrary(void *handle)
wxLibrary::~wxLibrary() wxLibrary::~wxLibrary()
{ {
if (m_handle && m_destroy) { if ( m_handle )
#if defined(__UNIX__) {
dlclose(m_handle); wxDllClose(m_handle);
#endif
#ifdef __WINDOWS__
FreeLibrary((HMODULE)m_handle);
#endif
} }
} }
@@ -115,11 +163,9 @@ void wxLibrary::PrepareClasses(wxClassInfo *first)
void *wxLibrary::GetSymbol(const wxString& symbname) void *wxLibrary::GetSymbol(const wxString& symbname)
{ {
#if defined(__UNIX__) void *symbol = NULL; // return value
return dlsym(m_handle, WXSTRINGCAST symbname);
#elif defined( __WINDOWS__ ) #if defined( __WXMAC__ )
return GetProcAddress((HINSTANCE) m_handle, WXSTRINGCAST symbname);
#elif defined( __WXMAC__ )
Ptr symAddress ; Ptr symAddress ;
CFragSymbolClass symClass ; CFragSymbolClass symClass ;
Str255 symName ; Str255 symName ;
@@ -127,12 +173,23 @@ void *wxLibrary::GetSymbol(const wxString& symbname)
strcpy( (char*) symName , symbname ) ; strcpy( (char*) symName , symbname ) ;
c2pstr( (char*) symName ) ; c2pstr( (char*) symName ) ;
if ( FindSymbol( (CFragConnectionID) m_handle , symName , &symAddress , &symClass ) == noErr ) if ( FindSymbol( m_handle , symName , &symAddress , &symClass ) == noErr )
{ {
return symAddress ; symbol = (void *)symAddress ;
} }
#else
// VZ: hmm... why is WXSTRINGCAST needed? if it's really modified, we
// should make a copy of it
symbol = wxDllGetSymbol(m_handle, WXSTRINGCAST symbname);
#endif #endif
return NULL;
if ( !symbol )
{
wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
symbname.c_str());
}
return symbol;
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -157,7 +214,6 @@ wxLibraries::~wxLibraries()
wxLibrary *wxLibraries::LoadLibrary(const wxString& name) wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
{ {
wxString lib_name = name;
wxNode *node; wxNode *node;
wxLibrary *lib; wxLibrary *lib;
wxClassInfo *old_sm_first; wxClassInfo *old_sm_first;
@@ -169,35 +225,17 @@ wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
old_sm_first = wxClassInfo::sm_first; old_sm_first = wxClassInfo::sm_first;
wxClassInfo::sm_first = NULL; wxClassInfo::sm_first = NULL;
wxString lib_name = ConstructLibraryName(name);
#if defined(__UNIX__) #if defined(__UNIX__)
lib_name.Prepend("./lib"); // TODO use LD_LIBRARY_PATH!
lib_name += ".so"; lib_name.Prepend("/lib");
#endif // __UNIX__
printf("lib_name = %s\n", WXSTRINGCAST lib_name); wxDllType handle ;
void *handle = dlopen(WXSTRINGCAST lib_name, RTLD_LAZY); #if defined(__WXMAC__)
printf("error = %s\n", dlerror());
if (!handle)
return NULL;
#elif defined(__WINDOWS__)
lib_name += ".dll";
#ifdef UNICODE
HMODULE handle = LoadLibraryW(lib_name);
#else
#ifdef __WIN16__
HMODULE handle = ::LoadLibrary(lib_name);
#else
HMODULE handle = LoadLibraryA(lib_name);
#endif
#endif
if (!handle)
return NULL;
#elif defined(__WXMAC__)
FSSpec myFSSpec ; FSSpec myFSSpec ;
CFragConnectionID handle ;
Ptr myMainAddr ; Ptr myMainAddr ;
Str255 myErrName ; Str255 myErrName ;
@@ -209,15 +247,24 @@ wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
wxASSERT_MSG( 1 , (char*)myErrName ) ; wxASSERT_MSG( 1 , (char*)myErrName ) ;
return NULL ; return NULL ;
} }
#else #else // !Mac
return NULL; handle = wxDllOpen(lib_name);
#endif #endif // OS
lib = new wxLibrary((void *)handle); if ( !handle )
{
wxLogSysError(_("Failed to load shared library '%s'"),
lib_name.c_str());
return NULL;
}
lib = new wxLibrary(handle);
wxClassInfo::sm_first = old_sm_first; wxClassInfo::sm_first = old_sm_first;
m_loaded.Append(name.GetData(), lib); m_loaded.Append(name.GetData(), lib);
return lib; return lib;
} }