]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynlib.cpp
old makefiles removed
[wxWidgets.git] / src / common / dynlib.cpp
CommitLineData
7a4b9130
GL
1/////////////////////////////////////////////////////////////////////////////
2// Name: dynlib.cpp
3// Purpose: Dynamic library management
4// Author: Guilhem Lavaux
5// Modified by:
6// Created: 20/07/98
7// RCS-ID: $Id$
8// Copyright: (c) Guilhem Lavaux
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
7b0bfbb2
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
7a4b9130 20#ifdef __GNUG__
7b0bfbb2 21 #pragma implementation "dynlib.h"
7a4b9130
GL
22#endif
23
0c32066b
JS
24#include "wx/wxprec.h"
25
26#ifdef __BORLANDC__
27 #pragma hdrstop
28#endif //__BORLANDC__
29
8a0d4cf6
VZ
30#if wxUSE_DYNLIB_CLASS
31
7b0bfbb2
VZ
32#include "wx/dynlib.h"
33#include "wx/filefn.h"
34#include "wx/intl.h"
35#include "wx/log.h"
8a0d4cf6 36#include "wx/tokenzr.h"
7b0bfbb2
VZ
37
38// ----------------------------------------------------------------------------
39// conditional compilation
40// ----------------------------------------------------------------------------
41
42#if defined(HAVE_DLOPEN)
50920146
OK
43 #define wxDllOpen(lib) dlopen(lib.fn_str(), RTLD_LAZY)
44 #define wxDllGetSymbol(handle, name) dlsym(handle, name.mb_str())
7b0bfbb2 45 #define wxDllClose dlclose
8a0d4cf6 46#elif defined(HAVE_SHL_LOAD)
50920146 47 #define wxDllOpen(lib) shl_load(lib.fn_str(), BIND_DEFERRED, 0)
7b0bfbb2
VZ
48 #define wxDllClose shl_unload
49
50920146 50 static inline void *wxDllGetSymbol(shl_t handle, const wxString& name)
7b0bfbb2
VZ
51 {
52 void *sym;
50920146 53 if ( shl_findsym(&handle, name.mb_str(), TYPE_UNDEFINED, &sym) == 0 )
7b0bfbb2
VZ
54 return sym;
55 else
56 return (void *)0;
57 }
58#elif defined(__WINDOWS__)
90022ddc
VZ
59 #include <windows.h>
60
61 // using LoadLibraryEx under Win32 to avoid name clash with LoadLibrary
62 #ifdef __WIN32__
63 #define wxDllOpen(lib) ::LoadLibraryEx(lib, 0, 0)
64 #else // Win16
65 #define wxDllOpen(lib) ::LoadLibrary(lib)
66 #endif // Win32/16
7b0bfbb2
VZ
67 #define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
68 #define wxDllClose ::FreeLibrary
69#else
70 #error "Don't know how to load shared libraries on this platform."
71#endif // OS
7a4b9130
GL
72
73// ---------------------------------------------------------------------------
7b0bfbb2 74// Global variables
7a4b9130
GL
75// ---------------------------------------------------------------------------
76
7b0bfbb2 77wxLibraries wxTheLibraries;
7a4b9130 78
7b0bfbb2
VZ
79// ----------------------------------------------------------------------------
80// private functions
81// ----------------------------------------------------------------------------
123a7fdd 82
7b0bfbb2
VZ
83// construct the full name from the base shared object name: adds a .dll
84// suffix under Windows or .so under Unix
85static wxString ConstructLibraryName(const wxString& basename)
86{
87 wxString fullname(basename);
88
89#if defined(__UNIX__)
8a0d4cf6
VZ
90 #if defined(__HPUX__)
91 fullname << ".sl";
92 #else //__HPUX__
93 fullname << ".so";
94 #endif //__HPUX__
7b0bfbb2
VZ
95#elif defined(__WINDOWS__)
96 fullname << ".dll";
27529614
JS
97#endif
98
7b0bfbb2
VZ
99 return fullname;
100}
7a4b9130 101
7b0bfbb2
VZ
102// ============================================================================
103// implementation
104// ============================================================================
7a4b9130
GL
105
106// ---------------------------------------------------------------------------
7b0bfbb2 107// wxLibrary (one instance per dynamic library)
7a4b9130
GL
108// ---------------------------------------------------------------------------
109
7b0bfbb2 110wxLibrary::wxLibrary(wxDllType handle)
7a4b9130 111{
7b0bfbb2
VZ
112 typedef wxClassInfo *(*t_get_first)(void);
113 t_get_first get_first;
7a4b9130 114
7b0bfbb2 115 m_handle = handle;
7a4b9130 116
7b0bfbb2
VZ
117 // Some system may use a local heap for library.
118 get_first = (t_get_first)GetSymbol("wxGetClassFirst");
119 // It is a wxWindows DLL.
120 if (get_first)
121 PrepareClasses(get_first());
7a4b9130
GL
122}
123
124wxLibrary::~wxLibrary()
125{
7b0bfbb2
VZ
126 if ( m_handle )
127 {
128 wxDllClose(m_handle);
129 }
7a4b9130
GL
130}
131
132wxObject *wxLibrary::CreateObject(const wxString& name)
133{
7b0bfbb2 134 wxClassInfo *info = (wxClassInfo *)classTable.Get(name);
f4a8c29f 135
7b0bfbb2
VZ
136 if (!info)
137 return NULL;
f4a8c29f 138
7b0bfbb2 139 return info->CreateObject();
f4a8c29f
GL
140}
141
856d2e52 142void wxLibrary::PrepareClasses(wxClassInfo *first)
f4a8c29f 143{
7b0bfbb2
VZ
144 // Index all class infos by their class name
145 wxClassInfo *info = first;
146 while (info)
147 {
148 if (info->m_className)
149 classTable.Put(info->m_className, (wxObject *)info);
150 info = info->GetNext();
151 }
152
153 // Set base pointers for each wxClassInfo
154 info = first;
155 while (info)
156 {
157 if (info->GetBaseClassName1())
158 info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1());
159 if (info->GetBaseClassName2())
160 info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2());
161 info = info->m_next;
162 }
7a4b9130
GL
163}
164
165void *wxLibrary::GetSymbol(const wxString& symbname)
166{
7b0bfbb2
VZ
167 void *symbol = NULL; // return value
168
169#if defined( __WXMAC__ )
170 Ptr symAddress ;
171 CFragSymbolClass symClass ;
172 Str255 symName ;
173
174 strcpy( (char*) symName , symbname ) ;
175 c2pstr( (char*) symName ) ;
176
177 if ( FindSymbol( m_handle , symName , &symAddress , &symClass ) == noErr )
178 {
179 symbol = (void *)symAddress ;
180 }
181#else
50920146 182 symbol = wxDllGetSymbol(m_handle, symbname);
123a7fdd 183#endif
7b0bfbb2
VZ
184
185 if ( !symbol )
186 {
187 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
188 symbname.c_str());
189 }
190
191 return symbol;
7a4b9130
GL
192}
193
194// ---------------------------------------------------------------------------
195// wxLibraries (only one instance should normally exist)
196// ---------------------------------------------------------------------------
197
198wxLibraries::wxLibraries()
199{
200}
201
202wxLibraries::~wxLibraries()
203{
7b0bfbb2 204 wxNode *node = m_loaded.First();
7a4b9130 205
7b0bfbb2
VZ
206 while (node) {
207 wxLibrary *lib = (wxLibrary *)node->Data();
208 delete lib;
7a4b9130 209
7b0bfbb2
VZ
210 node = node->Next();
211 }
7a4b9130
GL
212}
213
214wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
215{
7b0bfbb2
VZ
216 wxNode *node;
217 wxLibrary *lib;
218 wxClassInfo *old_sm_first;
7a4b9130 219
7b0bfbb2
VZ
220 if ( (node = m_loaded.Find(name.GetData())) )
221 return ((wxLibrary *)node->Data());
7a4b9130 222
7b0bfbb2
VZ
223 // If DLL shares data, this is necessary.
224 old_sm_first = wxClassInfo::sm_first;
225 wxClassInfo::sm_first = NULL;
226
227 wxString lib_name = ConstructLibraryName(name);
856d2e52 228
9e4b2f1c 229#if defined(__UNIX__)
8a0d4cf6
VZ
230 // found the first file in LD_LIBRARY_PATH with this name
231 wxString libPath("/lib:/usr/lib"); // system path first
232 const char *envLibPath = getenv("LD_LIBRARY_PATH");
233 if ( envLibPath )
234 libPath << ':' << envLibPath;
50920146 235 wxStringTokenizer tokenizer(libPath, _T(':'));
8a0d4cf6
VZ
236 while ( tokenizer.HasMoreToken() )
237 {
238 wxString fullname(tokenizer.NextToken());
239
240 fullname << '/' << lib_name;
241 if ( wxFileExists(fullname) )
242 {
243 lib_name = fullname;
244
245 // found the library
246 break;
247 }
248 }
249 //else: not found in the path, leave the name as is (secutiry risk?)
250
7b0bfbb2 251#endif // __UNIX__
7a4b9130 252
8a0d4cf6 253 wxDllType handle;
7a4b9130 254
7b0bfbb2
VZ
255#if defined(__WXMAC__)
256 FSSpec myFSSpec ;
257 Ptr myMainAddr ;
258 Str255 myErrName ;
7a4b9130 259
7b0bfbb2
VZ
260 wxMacPathToFSSpec( lib_name , &myFSSpec ) ;
261 if (GetDiskFragment( &myFSSpec , 0 , kCFragGoesToEOF , "\p" , kPrivateCFragCopy , &handle , &myMainAddr ,
262 myErrName ) != noErr )
263 {
264 p2cstr( myErrName ) ;
265 wxASSERT_MSG( 1 , (char*)myErrName ) ;
266 return NULL ;
267 }
268#else // !Mac
269 handle = wxDllOpen(lib_name);
270#endif // OS
856d2e52 271
7b0bfbb2
VZ
272 if ( !handle )
273 {
274 wxLogSysError(_("Failed to load shared library '%s'"),
275 lib_name.c_str());
7a4b9130 276
7b0bfbb2
VZ
277 return NULL;
278 }
f4a8c29f 279
7b0bfbb2 280 lib = new wxLibrary(handle);
7a4b9130 281
7b0bfbb2 282 wxClassInfo::sm_first = old_sm_first;
856d2e52 283
7b0bfbb2
VZ
284 m_loaded.Append(name.GetData(), lib);
285
286 return lib;
7a4b9130
GL
287}
288
289wxObject *wxLibraries::CreateObject(const wxString& path)
290{
7b0bfbb2
VZ
291 wxNode *node = m_loaded.First();
292 wxObject *obj;
7a4b9130 293
7b0bfbb2
VZ
294 while (node) {
295 obj = ((wxLibrary *)node->Data())->CreateObject(path);
296 if (obj)
297 return obj;
7a4b9130 298
7b0bfbb2
VZ
299 node = node->Next();
300 }
301 return NULL;
7a4b9130 302}
8a0d4cf6
VZ
303
304#endif // wxUSE_DYNLIB_CLASS