add OPENGL_CFLAGS to CPPFLAGS for OpenBSD
[gnash.git] / libbase / sharedlib.cpp
blob06f45b46a6ad171de52d399f2218de7a3eb59cea
1 // sharedlib.cpp: Shared Library support, for Gnash.
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
4 // Foundation, Inc
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #ifdef HAVE_CONFIG_H
23 #include "gnashconfig.h"
24 #endif
26 #include "log.h"
27 #include "sharedlib.h"
29 #include <sys/stat.h>
30 #include <unistd.h>
32 #include <string>
33 #include <cstdlib>
35 #if defined(WIN32) || defined(_WIN32)
36 # define LIBLTDL_DLL_IMPORT 1
37 #endif
38 #ifdef HAVE_DLFCN_H
39 # include <dlfcn.h>
40 #endif
41 #ifdef HAVE_LIBGEN_H
42 # include <libgen.h>
43 #endif
45 #include "ltdl.h"
46 #include <boost/thread/mutex.hpp>
48 #if defined(WIN32) || defined(_WIN32)
49 int lt_dlsetsearchpath (const char *search_path);
50 int lt_dlinit (void);
51 void * lt_dlsym (lt_dlhandle handle, const char *name);
52 const char *lt_dlerror (void);
53 int lt_dlclose (lt_dlhandle handle);
54 int lt_dlmakeresident (lt_dlhandle handle);
55 lt_dlhandle lt_dlopenext (const char *filename);
56 #endif
58 #if defined(_WIN32) || defined(WIN32)
59 # define PLUGINSDIR "./"
60 #endif
62 namespace gnash {
64 SharedLib::SharedLib(const std::string& filespec)
66 _filespec = filespec;
69 SharedLib::SharedLib(const std::string& filespec, const std::string& envvar)
71 _filespec = filespec;
72 scoped_lock lock(_libMutex);
74 // Initialize libtool's dynamic library loader
75 #ifdef HAVE_LTDL
76 int errors = lt_dlinit ();
77 if (errors) {
78 log_error (_("Couldn't initialize ltdl: %s"), lt_dlerror());
80 #else
81 # warning "libltdl not enabled in build".
82 #endif
83 std::string pluginsdir;
84 char *env = std::getenv (envvar.c_str());
85 if (env) {
86 pluginsdir = env;
87 } else {
88 pluginsdir = PLUGINSDIR;
93 SharedLib::~SharedLib()
97 bool
98 SharedLib::closeLib()
100 #ifdef HAVE_LTDL
101 return lt_dlclose(_dlhandle);
102 #else
103 return true;
104 #endif
107 bool
108 SharedLib::openLib()
110 return openLib(_filespec);
113 bool
114 SharedLib::openLib (const std::string& filespec)
117 scoped_lock lock(_libMutex);
119 log_debug ("Trying to open shared library \"%s\"", filespec);
121 #ifdef HAVE_LTDL
122 _dlhandle = lt_dlopenext (filespec.c_str());
124 if (_dlhandle == NULL) {
125 log_error ("%s", lt_dlerror());
126 return false;
129 // Make this module unloadable
130 lt_dlmakeresident(_dlhandle);
131 #endif
133 log_debug (_("Opened dynamic library \"%s\""), filespec);
135 _filespec = filespec;
137 return true;
140 SharedLib::initentry *
141 SharedLib::getInitEntry (const std::string& symbol)
143 // GNASH_REPORT_FUNCTION;
144 lt_ptr run = NULL;
146 scoped_lock lock(_libMutex);
148 #ifdef HAVE_LTDL
149 run = lt_dlsym (_dlhandle, symbol.c_str());
151 if (run == NULL) {
152 log_error (_("Couldn't find symbol: %s"), symbol);
153 return NULL;
154 } else {
155 log_debug (_("Found symbol %s @ %p"), symbol, (void *)run);
157 #endif
159 return (initentry*)(run);
162 SharedLib::entrypoint *
163 SharedLib::getDllSymbol(const std::string& symbol)
165 GNASH_REPORT_FUNCTION;
167 lt_ptr run = NULL;
169 scoped_lock lock(_libMutex);
171 #ifdef HAVE_LTDL
172 run = lt_dlsym (_dlhandle, symbol.c_str());
173 #endif
176 Realistically, we should never get a valid pointer with a value of 0
177 Markus: 'Id est NULL.'
179 if (run == NULL) {
180 log_error (_("Couldn't find symbol: %s"), symbol);
181 return NULL;
182 } else {
183 log_debug (_("Found symbol %s @ %p"), symbol, (void *)run);
186 return (entrypoint*)(run);
189 } // end of gnash namespace