]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynlib.cpp
Removed Pango homemade implementation and
[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
65571936 9// Licence: wxWindows licence
7a4b9130
GL
10/////////////////////////////////////////////////////////////////////////////
11
7b0bfbb2
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
a585ca5c 21# pragma implementation "dynlib.h"
7a4b9130
GL
22#endif
23
0c32066b
JS
24#include "wx/wxprec.h"
25
2df7be7f
RR
26#ifdef __BORLANDC__
27 #pragma hdrstop
28#endif
f6bcfd97 29
1948bb32 30#if wxUSE_DYNLIB_CLASS
8a0d4cf6 31
f6bcfd97 32#if defined(__WINDOWS__)
5fccb5b4 33 #include "wx/msw/wrapwin.h"
f6bcfd97
BP
34#endif
35
7b0bfbb2
VZ
36#include "wx/dynlib.h"
37#include "wx/filefn.h"
38#include "wx/intl.h"
39#include "wx/log.h"
1948bb32
VS
40#include "wx/utils.h"
41#include "wx/filename.h" // for SplitPath()
a8eaaeb2
VS
42#include "wx/app.h"
43#include "wx/apptrait.h"
7b0bfbb2 44
76a5e5d2
SC
45#if defined(__WXMAC__)
46 #include "wx/mac/private.h"
47#endif
48
7b0bfbb2 49
1948bb32
VS
50// ============================================================================
51// implementation
52// ============================================================================
846e1424 53
1948bb32
VS
54#if defined(__DARWIN__)
55// ---------------------------------------------------------------------------
56// For Darwin/Mac OS X
57// supply the sun style dlopen functions in terms of Darwin NS*
58// ---------------------------------------------------------------------------
0b9ab0bd 59
f11bdd03
GD
60/* Porting notes:
61 * The dlopen port is a port from dl_next.xs by Anno Siegel.
62 * dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess.
63 * The method used here is just to supply the sun style dlopen etc.
64 * functions in terms of Darwin NS*.
65 */
7a4b9130 66
1948bb32
VS
67#include <stdio.h>
68#include <mach-o/dyld.h>
7a4b9130 69
1948bb32 70static char dl_last_error[1024];
123a7fdd 71
1948bb32
VS
72static
73void TranslateError(const char *path, int number)
7b0bfbb2 74{
1948bb32
VS
75 unsigned int index;
76 static char *OFIErrorStrings[] =
77 {
5fccb5b4
VZ
78 "%s(%d): Object Image Load Failure\n",
79 "%s(%d): Object Image Load Success\n",
80 "%s(%d): Not an recognisable object file\n",
81 "%s(%d): No valid architecture\n",
82 "%s(%d): Object image has an invalid format\n",
83 "%s(%d): Invalid access (permissions?)\n",
84 "%s(%d): Unknown error code from NSCreateObjectFileImageFromFile\n",
1948bb32
VS
85 };
86#define NUM_OFI_ERRORS (sizeof(OFIErrorStrings) / sizeof(OFIErrorStrings[0]))
27529614 87
1948bb32
VS
88 index = number;
89 if (index > NUM_OFI_ERRORS - 1) {
90 index = NUM_OFI_ERRORS - 1;
91 }
92 sprintf(dl_last_error, OFIErrorStrings[index], path, number);
7b0bfbb2 93}
7a4b9130 94
1948bb32 95const char *dlerror()
7a4b9130 96{
1948bb32 97 return dl_last_error;
7a4b9130
GL
98}
99
1948bb32 100void *dlopen(const char *path, int WXUNUSED(mode) /* mode is ignored */)
7a4b9130 101{
1948bb32
VS
102 NSObjectFileImage ofile;
103 NSModule handle = NULL;
104
48a611cf
VZ
105 int dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
106 if ( dyld_result != NSObjectFileImageSuccess )
7b0bfbb2 107 {
48a611cf 108 handle = NULL;
7b0bfbb2 109 }
1948bb32
VS
110 else
111 {
48a611cf
VZ
112 handle = NSLinkModule
113 (
114 ofile,
115 path,
116 NSLINKMODULE_OPTION_BINDNOW |
117 NSLINKMODULE_OPTION_RETURN_ON_ERROR
118 );
1948bb32
VS
119 }
120
48a611cf
VZ
121 if ( !handle )
122 TranslateError(path, dyld_result);
123
1948bb32 124 return handle;
7a4b9130
GL
125}
126
1948bb32 127int dlclose(void *handle)
7a4b9130 128{
1948bb32
VS
129 NSUnLinkModule( handle, NSUNLINKMODULE_OPTION_NONE);
130 return 0;
f4a8c29f
GL
131}
132
1948bb32 133void *dlsym(void *handle, const char *symbol)
f4a8c29f 134{
42a24523
VZ
135 // as on many other systems, C symbols have prepended underscores under
136 // Darwin but unlike the normal dlopen(), NSLookupSymbolInModule() is not
137 // aware of this
3640d0da
VZ
138 wxCharBuffer buf(strlen(symbol) + 1);
139 char *p = buf.data();
140 p[0] = '_';
141 strcpy(p + 1, symbol);
142
143 NSSymbol nsSymbol = NSLookupSymbolInModule( handle, p );
42a24523 144 return nsSymbol ? NSAddressOfSymbol(nsSymbol) : NULL;
7a4b9130
GL
145}
146
1948bb32
VS
147#endif // defined(__DARWIN__)
148
7b0bfbb2 149
a585ca5c 150// ---------------------------------------------------------------------------
1948bb32 151// wxDynamicLibrary
a585ca5c 152// ---------------------------------------------------------------------------
7b0bfbb2 153
1948bb32
VS
154//FIXME: This class isn't really common at all, it should be moved into
155// platform dependent files.
f6bcfd97
BP
156
157#if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
1948bb32 158 const wxChar *wxDynamicLibrary::ms_dllext = _T(".dll");
87ec9b8f
GD
159#elif defined(__WXMAC__) && !defined(__DARWIN__)
160 const wxChar *wxDynamicLibrary::ms_dllext = _T("");
f6bcfd97 161#elif defined(__UNIX__)
1948bb32
VS
162 #if defined(__HPUX__)
163 const wxChar *wxDynamicLibrary::ms_dllext = _T(".sl");
87ec9b8f 164 #elif defined(__DARWIN__)
42a24523 165 const wxChar *wxDynamicLibrary::ms_dllext = _T(".bundle");
1948bb32
VS
166 #else
167 const wxChar *wxDynamicLibrary::ms_dllext = _T(".so");
168 #endif
f6bcfd97 169#endif
f6bcfd97 170
1948bb32 171wxDllType wxDynamicLibrary::GetProgramHandle()
0868079c 172{
1a787c5d 173#if defined( HAVE_DLOPEN ) && !defined(__EMX__)
1948bb32 174 return dlopen(0, RTLD_LAZY);
7742efff 175#elif defined (HAVE_SHL_LOAD)
1948bb32 176 return PROG_HANDLE;
0868079c 177#else
58c837a4 178 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
7742efff 179 return 0;
7cc98b3e 180#endif
0868079c
KB
181}
182
1948bb32 183bool wxDynamicLibrary::Load(wxString libname, int flags)
a585ca5c 184{
1948bb32
VS
185 wxASSERT_MSG(m_handle == 0, _T("Library already loaded."));
186
187 // add the proper extension for the DLL ourselves unless told not to
188 if ( !(flags & wxDL_VERBATIM) )
189 {
190 // and also check that the libname doesn't already have it
191 wxString ext;
192 wxFileName::SplitPath(libname, NULL, NULL, &ext);
193 if ( ext.empty() )
194 {
195 libname += GetDllExt();
196 }
197 }
7b0bfbb2 198
1948bb32
VS
199 // different ways to load a shared library
200 //
201 // FIXME: should go to the platform-specific files!
202#if defined(__WXMAC__) && !defined(__DARWIN__)
0b9ab0bd
RL
203 FSSpec myFSSpec;
204 Ptr myMainAddr;
205 Str255 myErrName;
206
207 wxMacFilename2FSSpec( libname , &myFSSpec );
208
209 if( GetDiskFragment( &myFSSpec,
210 0,
211 kCFragGoesToEOF,
212 "\p",
213 kPrivateCFragCopy,
1948bb32 214 &m_handle,
0b9ab0bd
RL
215 &myMainAddr,
216 myErrName ) != noErr )
7cc98b3e 217 {
0b9ab0bd
RL
218 wxLogSysError( _("Failed to load shared library '%s' Error '%s'"),
219 libname.c_str(),
fb44fc34 220 wxMacMakeStringFromPascal( myErrName ).c_str() );
1948bb32 221 m_handle = 0;
7cc98b3e 222 }
0b9ab0bd 223
1a787c5d 224#elif defined(__WXPM__) || defined(__EMX__)
1948bb32
VS
225 char err[256] = "";
226 DosLoadModule(err, sizeof(err), libname.c_str(), &m_handle);
227
228#elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
0b9ab0bd 229
1948bb32 230#if defined(__VMS) || defined(__DARWIN__)
5f3f0f17 231 m_handle = dlopen(libname.fn_str(), 0); // The second parameter is ignored
1948bb32
VS
232#else // !__VMS && !__DARWIN__
233 int rtldFlags = 0;
234
235 if ( flags & wxDL_LAZY )
236 {
237 wxASSERT_MSG( (flags & wxDL_NOW) == 0,
238 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
239#ifdef RTLD_LAZY
240 rtldFlags |= RTLD_LAZY;
241#else
242 wxLogDebug(_T("wxDL_LAZY is not supported on this platform"));
243#endif
244 }
245 else if ( flags & wxDL_NOW )
246 {
247#ifdef RTLD_NOW
248 rtldFlags |= RTLD_NOW;
0b9ab0bd 249#else
1948bb32
VS
250 wxLogDebug(_T("wxDL_NOW is not supported on this platform"));
251#endif
252 }
0b9ab0bd 253
1948bb32
VS
254 if ( flags & wxDL_GLOBAL )
255 {
256#ifdef RTLD_GLOBAL
257 rtldFlags |= RTLD_GLOBAL;
258#else
259 wxLogDebug(_T("RTLD_GLOBAL is not supported on this platform."));
0b9ab0bd 260#endif
1948bb32
VS
261 }
262
263 m_handle = dlopen(libname.fn_str(), rtldFlags);
264#endif // __VMS || __DARWIN__ ?
265
266#elif defined(HAVE_SHL_LOAD)
267 int shlFlags = 0;
a585ca5c 268
1948bb32
VS
269 if( flags & wxDL_LAZY )
270 {
271 wxASSERT_MSG( (flags & wxDL_NOW) == 0,
272 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
273 shlFlags |= BIND_DEFERRED;
274 }
275 else if( flags & wxDL_NOW )
276 {
277 shlFlags |= BIND_IMMEDIATE;
278 }
279 m_handle = shl_load(libname.fn_str(), BIND_DEFERRED, 0);
280
281#elif defined(__WINDOWS__)
282 m_handle = ::LoadLibrary(libname.c_str());
283#else
284 #error "runtime shared lib support not implemented on this platform"
285#endif
286
287 if ( m_handle == 0 )
7cc98b3e 288 {
f6bcfd97 289 wxString msg(_("Failed to load shared library '%s'"));
1948bb32 290#if defined(HAVE_DLERROR) && !defined(__EMX__)
f6bcfd97 291
1948bb32
VS
292#if wxUSE_UNICODE
293 wxWCharBuffer buffer = wxConvLocal.cMB2WC( dlerror() );
294 const wxChar *err = buffer;
295#else
d0166913 296 const wxChar *err = dlerror();
1948bb32
VS
297#endif
298
0b9ab0bd 299 if( err )
0b9ab0bd 300 wxLogError( msg, err );
0b9ab0bd 301#else
0b9ab0bd
RL
302 wxLogSysError( msg, libname.c_str() );
303#endif
7cc98b3e
VZ
304 }
305
1948bb32 306 return IsLoaded();
a585ca5c
KB
307}
308
9d033af9
VZ
309/* static */
310void wxDynamicLibrary::Unload(wxDllType handle)
752c7d6b 311{
1948bb32 312#if defined(__WXPM__) || defined(__EMX__)
ff793cab 313 DosFreeModule( handle );
1948bb32 314#elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
ff793cab 315 dlclose( handle );
1948bb32 316#elif defined(HAVE_SHL_LOAD)
ff793cab 317 shl_unload( handle );
1948bb32 318#elif defined(__WINDOWS__)
ff793cab 319 ::FreeLibrary( handle );
1948bb32 320#elif defined(__WXMAC__) && !defined(__DARWIN__)
ff793cab 321 CloseConnection( (CFragConnectionID*) &handle );
1948bb32 322#else
9d033af9 323 #error "runtime shared lib support not implemented"
1948bb32 324#endif
752c7d6b
KB
325}
326
a018a119 327void *wxDynamicLibrary::DoGetSymbol(const wxString &name, bool *success) const
a585ca5c 328{
1948bb32
VS
329 wxCHECK_MSG( IsLoaded(), NULL,
330 _T("Can't load symbol from unloaded library") );
331
0b9ab0bd 332 void *symbol = 0;
a585ca5c 333
999836aa 334 wxUnusedVar(symbol);
1948bb32 335#if defined(__WXMAC__) && !defined(__DARWIN__)
0b9ab0bd
RL
336 Ptr symAddress;
337 CFragSymbolClass symClass;
338 Str255 symName;
1948bb32 339#if TARGET_CARBON
f73bc315 340 c2pstrcpy( (StringPtr) symName, name.fn_str() );
1948bb32 341#else
f73bc315 342 strcpy( (char *)symName, name.fn_str() );
1948bb32
VS
343 c2pstr( (char *)symName );
344#endif
609533d5 345 if( FindSymbol( m_handle, symName, &symAddress, &symClass ) == noErr )
0b9ab0bd 346 symbol = (void *)symAddress;
7cc98b3e 347
0b9ab0bd 348#elif defined(__WXPM__) || defined(__EMX__)
1948bb32 349 DosQueryProcAddr( m_handle, 1L, name.c_str(), (PFN*)symbol );
0b9ab0bd 350
1948bb32
VS
351#elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
352 symbol = dlsym( m_handle, name.fn_str() );
353
354#elif defined(HAVE_SHL_LOAD)
355 // use local variable since shl_findsym modifies the handle argument
356 // to indicate where the symbol was found (GD)
357 wxDllType the_handle = m_handle;
358 if( shl_findsym( &the_handle, name.fn_str(), TYPE_UNDEFINED, &symbol ) != 0 )
359 symbol = 0;
360
361#elif defined(__WINDOWS__)
ffcb4ee4
JS
362#ifdef __WXWINCE__
363 symbol = (void*) ::GetProcAddress( m_handle, name );
364#else
1948bb32 365 symbol = (void*) ::GetProcAddress( m_handle, name.mb_str() );
ffcb4ee4 366#endif
b7b35e50 367
1c193821 368#else
1948bb32 369#error "runtime shared lib support not implemented"
1c193821 370#endif
0b9ab0bd 371
a018a119
VZ
372 if ( success )
373 *success = symbol != NULL;
374
375 return symbol;
376}
377
378void *wxDynamicLibrary::GetSymbol(const wxString& name, bool *success) const
379{
380 void *symbol = DoGetSymbol(name, success);
7b0bfbb2
VZ
381 if ( !symbol )
382 {
1948bb32
VS
383#if defined(HAVE_DLERROR) && !defined(__EMX__)
384
385#if wxUSE_UNICODE
386 wxWCharBuffer buffer = wxConvLocal.cMB2WC( dlerror() );
387 const wxChar *err = buffer;
388#else
d0166913 389 const wxChar *err = dlerror();
1948bb32
VS
390#endif
391
0b9ab0bd
RL
392 if( err )
393 {
ee682a94 394 wxLogError(wxT("%s"), err);
0b9ab0bd
RL
395 }
396#else
7cc98b3e
VZ
397 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
398 name.c_str());
0b9ab0bd 399#endif
7b0bfbb2 400 }
0b9ab0bd 401
7b0bfbb2 402 return symbol;
7a4b9130 403}
5fccb5b4 404
1948bb32 405/*static*/
350fffae
VZ
406wxString
407wxDynamicLibrary::CanonicalizeName(const wxString& name,
408 wxDynamicLibraryCategory
409#ifdef __UNIX__
410 cat
411#else // !__UNIX__
412 WXUNUSED(cat)
413#endif // __UNIX__/!__UNIX__
414 )
7a4b9130 415{
350fffae
VZ
416 wxString nameCanonic;
417
87ec9b8f 418 // under Unix the library names usually start with "lib" prefix, add it
1948bb32 419#ifdef __UNIX__
350fffae
VZ
420 switch ( cat )
421 {
422 default:
423 wxFAIL_MSG( _T("unknown wxDynamicLibraryCategory value") );
424 // fall through
425
426 case wxDL_MODULE:
427 // don't do anything for modules, their names are arbitrary
428 break;
429
430 case wxDL_LIBRARY:
431 // library names should start with "lib" under Unix
432 nameCanonic = _T("lib");
433 break;
434 }
435#endif // __UNIX__
436
437 nameCanonic << name << GetDllExt();
438 return nameCanonic;
7a4b9130 439}
8a0d4cf6 440
1948bb32
VS
441/*static*/
442wxString wxDynamicLibrary::CanonicalizePluginName(const wxString& name,
443 wxPluginCategory cat)
f11bdd03 444{
1948bb32
VS
445 wxString suffix;
446 if ( cat == wxDL_PLUGIN_GUI )
f11bdd03 447 {
a8eaaeb2
VS
448 wxAppTraits *traits = wxAppConsole::GetInstance() ?
449 wxAppConsole::GetInstance()->GetTraits() : NULL;
5fccb5b4 450 wxASSERT_MSG( traits,
a8eaaeb2 451 _("can't query for GUI plugins name in console applications") );
324899f6 452 suffix = traits->GetToolkitInfo().shortName;
f11bdd03 453 }
1948bb32
VS
454#if wxUSE_UNICODE
455 suffix << _T('u');
456#endif
457#ifdef __WXDEBUG__
458 suffix << _T('d');
459#endif
f11bdd03 460
1948bb32
VS
461 if ( !suffix.empty() )
462 suffix = wxString(_T("_")) + suffix;
f11bdd03 463
3546ffae 464#define WXSTRINGIZE(x) #x
1948bb32
VS
465#ifdef __UNIX__
466 #if (wxMINOR_VERSION % 2) == 0
3546ffae 467 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y)
1948bb32 468 #else
3546ffae 469 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y) "." WXSTRINGIZE(z)
1948bb32
VS
470 #endif
471#else
472 #if (wxMINOR_VERSION % 2) == 0
3546ffae 473 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y)
1948bb32 474 #else
3546ffae 475 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y) WXSTRINGIZE(z)
1948bb32
VS
476 #endif
477#endif
350fffae 478
1948bb32
VS
479 suffix << wxString::FromAscii(wxDLLVER(wxMAJOR_VERSION, wxMINOR_VERSION,
480 wxRELEASE_NUMBER));
481#undef wxDLLVER
3546ffae 482#undef WXSTRINGIZE
f11bdd03 483
2c02ec05
VS
484#ifdef __WINDOWS__
485 // Add compiler identification:
486 #if defined(__GNUG__)
487 suffix << _T("_gcc");
488 #elif defined(__VISUALC__)
489 suffix << _T("_vc");
490 #elif defined(__WATCOMC__)
491 suffix << _T("_wat");
492 #elif defined(__BORLANDC__)
493 suffix << _T("_bcc");
494 #endif
495#endif
496
1948bb32 497 return CanonicalizeName(name + suffix, wxDL_MODULE);
f11bdd03 498}
5fccb5b4 499
1948bb32
VS
500/*static*/
501wxString wxDynamicLibrary::GetPluginsDirectory()
f11bdd03 502{
1948bb32
VS
503#ifdef __UNIX__
504 wxString format = wxGetInstallPrefix();
cb979fac 505 wxString dir;
1948bb32
VS
506 format << wxFILE_SEP_PATH
507 << wxT("lib") << wxFILE_SEP_PATH
508 << wxT("wx") << wxFILE_SEP_PATH
cb979fac 509#if (wxMINOR_VERSION % 2) == 0
1948bb32 510 << wxT("%i.%i");
1948bb32 511 dir.Printf(format.c_str(), wxMAJOR_VERSION, wxMINOR_VERSION);
1948bb32 512#else
cb979fac
VS
513 << wxT("%i.%i.%i");
514 dir.Printf(format.c_str(),
515 wxMAJOR_VERSION, wxMINOR_VERSION, wxRELEASE_NUMBER);
516#endif
517 return dir;
518
519#else // ! __UNIX__
1948bb32
VS
520 return wxEmptyString;
521#endif
f11bdd03
GD
522}
523
f11bdd03 524
1948bb32 525#endif // wxUSE_DYNLIB_CLASS