]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynlib.cpp
Remove all extensions before inport, not just .py
[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
55d99c7a 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
BP
32#if defined(__WINDOWS__)
33 #include "wx/msw/private.h"
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 {
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",
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 int dyld_result;
103 NSObjectFileImage ofile;
104 NSModule handle = NULL;
105
106 dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
107 if (dyld_result != NSObjectFileImageSuccess)
7b0bfbb2 108 {
1948bb32 109 TranslateError(path, dyld_result);
7b0bfbb2 110 }
1948bb32
VS
111 else
112 {
113 // NSLinkModule will cause the run to abort on any link error's
114 // not very friendly but the error recovery functionality is limited.
115 handle = NSLinkModule(ofile, path, NSLINKMODULE_OPTION_BINDNOW);
116 }
117
118 return handle;
7a4b9130
GL
119}
120
1948bb32 121int dlclose(void *handle)
7a4b9130 122{
1948bb32
VS
123 NSUnLinkModule( handle, NSUNLINKMODULE_OPTION_NONE);
124 return 0;
f4a8c29f
GL
125}
126
1948bb32 127void *dlsym(void *handle, const char *symbol)
f4a8c29f 128{
1948bb32
VS
129 void *addr;
130
131 NSSymbol nsSymbol = NSLookupSymbolInModule( handle , symbol ) ;
132
133 if ( nsSymbol)
7b0bfbb2 134 {
1948bb32 135 addr = NSAddressOfSymbol(nsSymbol);
7b0bfbb2 136 }
1948bb32 137 else
7b0bfbb2 138 {
1948bb32 139 addr = NULL;
7b0bfbb2 140 }
1948bb32 141 return addr;
7a4b9130
GL
142}
143
1948bb32
VS
144#endif // defined(__DARWIN__)
145
7b0bfbb2 146
a585ca5c 147// ---------------------------------------------------------------------------
1948bb32 148// wxDynamicLibrary
a585ca5c 149// ---------------------------------------------------------------------------
7b0bfbb2 150
1948bb32
VS
151//FIXME: This class isn't really common at all, it should be moved into
152// platform dependent files.
f6bcfd97
BP
153
154#if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
1948bb32 155 const wxChar *wxDynamicLibrary::ms_dllext = _T(".dll");
87ec9b8f
GD
156#elif defined(__WXMAC__) && !defined(__DARWIN__)
157 const wxChar *wxDynamicLibrary::ms_dllext = _T("");
f6bcfd97 158#elif defined(__UNIX__)
1948bb32
VS
159 #if defined(__HPUX__)
160 const wxChar *wxDynamicLibrary::ms_dllext = _T(".sl");
87ec9b8f
GD
161 #elif defined(__DARWIN__)
162 const wxChar *wxDynamicLibrary::ms_dllext = _T(".dylib");
1948bb32
VS
163 #else
164 const wxChar *wxDynamicLibrary::ms_dllext = _T(".so");
165 #endif
f6bcfd97 166#endif
f6bcfd97 167
1948bb32 168wxDllType wxDynamicLibrary::GetProgramHandle()
0868079c 169{
1a787c5d 170#if defined( HAVE_DLOPEN ) && !defined(__EMX__)
1948bb32 171 return dlopen(0, RTLD_LAZY);
7742efff 172#elif defined (HAVE_SHL_LOAD)
1948bb32 173 return PROG_HANDLE;
0868079c 174#else
58c837a4 175 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
7742efff 176 return 0;
7cc98b3e 177#endif
0868079c
KB
178}
179
1948bb32 180bool wxDynamicLibrary::Load(wxString libname, int flags)
a585ca5c 181{
1948bb32
VS
182 wxASSERT_MSG(m_handle == 0, _T("Library already loaded."));
183
184 // add the proper extension for the DLL ourselves unless told not to
185 if ( !(flags & wxDL_VERBATIM) )
186 {
187 // and also check that the libname doesn't already have it
188 wxString ext;
189 wxFileName::SplitPath(libname, NULL, NULL, &ext);
190 if ( ext.empty() )
191 {
192 libname += GetDllExt();
193 }
194 }
7b0bfbb2 195
1948bb32
VS
196 // different ways to load a shared library
197 //
198 // FIXME: should go to the platform-specific files!
199#if defined(__WXMAC__) && !defined(__DARWIN__)
0b9ab0bd
RL
200 FSSpec myFSSpec;
201 Ptr myMainAddr;
202 Str255 myErrName;
203
204 wxMacFilename2FSSpec( libname , &myFSSpec );
205
206 if( GetDiskFragment( &myFSSpec,
207 0,
208 kCFragGoesToEOF,
209 "\p",
210 kPrivateCFragCopy,
1948bb32 211 &m_handle,
0b9ab0bd
RL
212 &myMainAddr,
213 myErrName ) != noErr )
7cc98b3e 214 {
0b9ab0bd
RL
215 p2cstr( myErrName );
216 wxLogSysError( _("Failed to load shared library '%s' Error '%s'"),
217 libname.c_str(),
218 (char*)myErrName );
1948bb32 219 m_handle = 0;
7cc98b3e 220 }
0b9ab0bd 221
1a787c5d 222#elif defined(__WXPM__) || defined(__EMX__)
1948bb32
VS
223 char err[256] = "";
224 DosLoadModule(err, sizeof(err), libname.c_str(), &m_handle);
225
226#elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
0b9ab0bd 227
1948bb32 228#if defined(__VMS) || defined(__DARWIN__)
5f3f0f17 229 m_handle = dlopen(libname.fn_str(), 0); // The second parameter is ignored
1948bb32
VS
230#else // !__VMS && !__DARWIN__
231 int rtldFlags = 0;
232
233 if ( flags & wxDL_LAZY )
234 {
235 wxASSERT_MSG( (flags & wxDL_NOW) == 0,
236 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
237#ifdef RTLD_LAZY
238 rtldFlags |= RTLD_LAZY;
239#else
240 wxLogDebug(_T("wxDL_LAZY is not supported on this platform"));
241#endif
242 }
243 else if ( flags & wxDL_NOW )
244 {
245#ifdef RTLD_NOW
246 rtldFlags |= RTLD_NOW;
0b9ab0bd 247#else
1948bb32
VS
248 wxLogDebug(_T("wxDL_NOW is not supported on this platform"));
249#endif
250 }
0b9ab0bd 251
1948bb32
VS
252 if ( flags & wxDL_GLOBAL )
253 {
254#ifdef RTLD_GLOBAL
255 rtldFlags |= RTLD_GLOBAL;
256#else
257 wxLogDebug(_T("RTLD_GLOBAL is not supported on this platform."));
0b9ab0bd 258#endif
1948bb32
VS
259 }
260
261 m_handle = dlopen(libname.fn_str(), rtldFlags);
262#endif // __VMS || __DARWIN__ ?
263
264#elif defined(HAVE_SHL_LOAD)
265 int shlFlags = 0;
a585ca5c 266
1948bb32
VS
267 if( flags & wxDL_LAZY )
268 {
269 wxASSERT_MSG( (flags & wxDL_NOW) == 0,
270 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
271 shlFlags |= BIND_DEFERRED;
272 }
273 else if( flags & wxDL_NOW )
274 {
275 shlFlags |= BIND_IMMEDIATE;
276 }
277 m_handle = shl_load(libname.fn_str(), BIND_DEFERRED, 0);
278
279#elif defined(__WINDOWS__)
280 m_handle = ::LoadLibrary(libname.c_str());
281#else
282 #error "runtime shared lib support not implemented on this platform"
283#endif
284
285 if ( m_handle == 0 )
7cc98b3e 286 {
f6bcfd97 287 wxString msg(_("Failed to load shared library '%s'"));
1948bb32 288#if defined(HAVE_DLERROR) && !defined(__EMX__)
f6bcfd97 289
1948bb32
VS
290#if wxUSE_UNICODE
291 wxWCharBuffer buffer = wxConvLocal.cMB2WC( dlerror() );
292 const wxChar *err = buffer;
293#else
d0166913 294 const wxChar *err = dlerror();
1948bb32
VS
295#endif
296
0b9ab0bd 297 if( err )
0b9ab0bd 298 wxLogError( msg, err );
0b9ab0bd 299#else
0b9ab0bd
RL
300 wxLogSysError( msg, libname.c_str() );
301#endif
7cc98b3e
VZ
302 }
303
1948bb32 304 return IsLoaded();
a585ca5c
KB
305}
306
9d033af9
VZ
307/* static */
308void wxDynamicLibrary::Unload(wxDllType handle)
752c7d6b 309{
1948bb32 310#if defined(__WXPM__) || defined(__EMX__)
ff793cab 311 DosFreeModule( handle );
1948bb32 312#elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
ff793cab 313 dlclose( handle );
1948bb32 314#elif defined(HAVE_SHL_LOAD)
ff793cab 315 shl_unload( handle );
1948bb32 316#elif defined(__WINDOWS__)
ff793cab 317 ::FreeLibrary( handle );
1948bb32 318#elif defined(__WXMAC__) && !defined(__DARWIN__)
ff793cab 319 CloseConnection( (CFragConnectionID*) &handle );
1948bb32 320#else
9d033af9 321 #error "runtime shared lib support not implemented"
1948bb32 322#endif
752c7d6b
KB
323}
324
1948bb32 325void *wxDynamicLibrary::GetSymbol(const wxString &name, bool *success) const
a585ca5c 326{
1948bb32
VS
327 wxCHECK_MSG( IsLoaded(), NULL,
328 _T("Can't load symbol from unloaded library") );
329
330 bool failed = FALSE;
0b9ab0bd 331 void *symbol = 0;
a585ca5c 332
999836aa 333 wxUnusedVar(symbol);
1948bb32 334#if defined(__WXMAC__) && !defined(__DARWIN__)
0b9ab0bd
RL
335 Ptr symAddress;
336 CFragSymbolClass symClass;
337 Str255 symName;
1948bb32 338#if TARGET_CARBON
f73bc315 339 c2pstrcpy( (StringPtr) symName, name.fn_str() );
1948bb32 340#else
f73bc315 341 strcpy( (char *)symName, name.fn_str() );
1948bb32
VS
342 c2pstr( (char *)symName );
343#endif
609533d5 344 if( FindSymbol( m_handle, symName, &symAddress, &symClass ) == noErr )
0b9ab0bd 345 symbol = (void *)symAddress;
7cc98b3e 346
0b9ab0bd 347#elif defined(__WXPM__) || defined(__EMX__)
1948bb32 348 DosQueryProcAddr( m_handle, 1L, name.c_str(), (PFN*)symbol );
0b9ab0bd 349
1948bb32
VS
350#elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
351 symbol = dlsym( m_handle, name.fn_str() );
352
353#elif defined(HAVE_SHL_LOAD)
354 // use local variable since shl_findsym modifies the handle argument
355 // to indicate where the symbol was found (GD)
356 wxDllType the_handle = m_handle;
357 if( shl_findsym( &the_handle, name.fn_str(), TYPE_UNDEFINED, &symbol ) != 0 )
358 symbol = 0;
359
360#elif defined(__WINDOWS__)
ffcb4ee4
JS
361#ifdef __WXWINCE__
362 symbol = (void*) ::GetProcAddress( m_handle, name );
363#else
1948bb32 364 symbol = (void*) ::GetProcAddress( m_handle, name.mb_str() );
ffcb4ee4 365#endif
b7b35e50 366
1c193821 367#else
1948bb32 368#error "runtime shared lib support not implemented"
1c193821 369#endif
0b9ab0bd 370
7b0bfbb2
VZ
371 if ( !symbol )
372 {
1948bb32
VS
373#if defined(HAVE_DLERROR) && !defined(__EMX__)
374
375#if wxUSE_UNICODE
376 wxWCharBuffer buffer = wxConvLocal.cMB2WC( dlerror() );
377 const wxChar *err = buffer;
378#else
d0166913 379 const wxChar *err = dlerror();
1948bb32
VS
380#endif
381
0b9ab0bd
RL
382 if( err )
383 {
ee682a94 384 wxLogError(wxT("%s"), err);
0b9ab0bd
RL
385 }
386#else
387 failed = TRUE;
7cc98b3e
VZ
388 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
389 name.c_str());
0b9ab0bd 390#endif
7b0bfbb2 391 }
0b9ab0bd
RL
392 if( success )
393 *success = !failed;
394
7b0bfbb2 395 return symbol;
7a4b9130 396}
1948bb32 397
7a4b9130 398
1948bb32 399/*static*/
350fffae
VZ
400wxString
401wxDynamicLibrary::CanonicalizeName(const wxString& name,
402 wxDynamicLibraryCategory
403#ifdef __UNIX__
404 cat
405#else // !__UNIX__
406 WXUNUSED(cat)
407#endif // __UNIX__/!__UNIX__
408 )
7a4b9130 409{
350fffae
VZ
410 wxString nameCanonic;
411
87ec9b8f 412 // under Unix the library names usually start with "lib" prefix, add it
1948bb32 413#ifdef __UNIX__
350fffae
VZ
414 switch ( cat )
415 {
416 default:
417 wxFAIL_MSG( _T("unknown wxDynamicLibraryCategory value") );
418 // fall through
419
420 case wxDL_MODULE:
421 // don't do anything for modules, their names are arbitrary
422 break;
423
424 case wxDL_LIBRARY:
425 // library names should start with "lib" under Unix
426 nameCanonic = _T("lib");
427 break;
428 }
429#endif // __UNIX__
430
431 nameCanonic << name << GetDllExt();
432 return nameCanonic;
7a4b9130 433}
8a0d4cf6 434
1948bb32
VS
435/*static*/
436wxString wxDynamicLibrary::CanonicalizePluginName(const wxString& name,
437 wxPluginCategory cat)
f11bdd03 438{
1948bb32
VS
439 wxString suffix;
440 if ( cat == wxDL_PLUGIN_GUI )
f11bdd03 441 {
a8eaaeb2
VS
442 wxAppTraits *traits = wxAppConsole::GetInstance() ?
443 wxAppConsole::GetInstance()->GetTraits() : NULL;
444 wxASSERT_MSG( traits,
445 _("can't query for GUI plugins name in console applications") );
324899f6 446 suffix = traits->GetToolkitInfo().shortName;
f11bdd03 447 }
1948bb32
VS
448#if wxUSE_UNICODE
449 suffix << _T('u');
450#endif
451#ifdef __WXDEBUG__
452 suffix << _T('d');
453#endif
f11bdd03 454
1948bb32
VS
455 if ( !suffix.empty() )
456 suffix = wxString(_T("_")) + suffix;
f11bdd03 457
3546ffae 458#define WXSTRINGIZE(x) #x
1948bb32
VS
459#ifdef __UNIX__
460 #if (wxMINOR_VERSION % 2) == 0
3546ffae 461 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y)
1948bb32 462 #else
3546ffae 463 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y) "." WXSTRINGIZE(z)
1948bb32
VS
464 #endif
465#else
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#endif
350fffae 472
1948bb32
VS
473 suffix << wxString::FromAscii(wxDLLVER(wxMAJOR_VERSION, wxMINOR_VERSION,
474 wxRELEASE_NUMBER));
475#undef wxDLLVER
3546ffae 476#undef WXSTRINGIZE
f11bdd03 477
2c02ec05
VS
478#ifdef __WINDOWS__
479 // Add compiler identification:
480 #if defined(__GNUG__)
481 suffix << _T("_gcc");
482 #elif defined(__VISUALC__)
483 suffix << _T("_vc");
484 #elif defined(__WATCOMC__)
485 suffix << _T("_wat");
486 #elif defined(__BORLANDC__)
487 suffix << _T("_bcc");
488 #endif
489#endif
490
1948bb32 491 return CanonicalizeName(name + suffix, wxDL_MODULE);
f11bdd03 492}
1948bb32
VS
493
494/*static*/
495wxString wxDynamicLibrary::GetPluginsDirectory()
f11bdd03 496{
1948bb32
VS
497#ifdef __UNIX__
498 wxString format = wxGetInstallPrefix();
499 format << wxFILE_SEP_PATH
500 << wxT("lib") << wxFILE_SEP_PATH
501 << wxT("wx") << wxFILE_SEP_PATH
502 << wxT("%i.%i");
503 wxString dir;
504 dir.Printf(format.c_str(), wxMAJOR_VERSION, wxMINOR_VERSION);
505 return dir;
506#else
507 return wxEmptyString;
508#endif
f11bdd03
GD
509}
510
f11bdd03 511
1948bb32 512#endif // wxUSE_DYNLIB_CLASS