2006-03-31 10:32:02 +00:00
|
|
|
/*****************************************************************************
|
2012-02-02 01:53:35 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2015-06-16 10:49:16 +00:00
|
|
|
* Copyright (C) 2015 Artem Pavlenko
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
2005-06-14 15:06:59 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 10:32:02 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-06-14 15:06:59 +00:00
|
|
|
*
|
2006-03-31 10:32:02 +00:00
|
|
|
*****************************************************************************/
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2006-10-04 11:22:18 +00:00
|
|
|
#include <mapnik/plugin.hpp>
|
2013-04-10 02:37:02 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
#ifdef _WINDOWS
|
2013-11-06 23:34:53 +00:00
|
|
|
#define NOMINMAX
|
2013-04-10 02:37:02 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#define handle HMODULE
|
|
|
|
#define dlsym GetProcAddress
|
|
|
|
#define dlclose FreeLibrary
|
|
|
|
#define dlerror GetLastError
|
2014-12-19 05:13:47 +00:00
|
|
|
#define MAPNIK_SUPPORTS_DLOPEN
|
2013-04-10 02:37:02 +00:00
|
|
|
#else
|
2014-12-16 21:03:51 +00:00
|
|
|
#ifdef MAPNIK_HAS_DLCFN
|
|
|
|
#include <dlfcn.h>
|
2014-12-19 05:13:47 +00:00
|
|
|
#define MAPNIK_SUPPORTS_DLOPEN
|
2014-12-16 21:03:51 +00:00
|
|
|
#endif
|
2013-04-10 02:37:02 +00:00
|
|
|
#define handle void *
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// TODO - handle/report dlerror
|
2005-06-14 15:06:59 +00:00
|
|
|
|
|
|
|
namespace mapnik
|
|
|
|
{
|
|
|
|
|
2013-04-10 02:37:02 +00:00
|
|
|
struct _mapnik_lib_t {
|
|
|
|
handle dl;
|
|
|
|
};
|
|
|
|
|
|
|
|
PluginInfo::PluginInfo(std::string const& filename,
|
|
|
|
std::string const& library_name)
|
|
|
|
: filename_(filename),
|
|
|
|
name_(),
|
|
|
|
module_(new mapnik_lib_t)
|
|
|
|
{
|
|
|
|
#ifdef _WINDOWS
|
|
|
|
if (module_) module_->dl = LoadLibraryA(filename.c_str());
|
2014-12-16 21:03:51 +00:00
|
|
|
if (module_ && module_->dl)
|
|
|
|
{
|
2016-04-05 19:31:19 +00:00
|
|
|
callable_returning_string name = reinterpret_cast<callable_returning_string>(dlsym(module_->dl, library_name.c_str()));
|
2014-12-16 21:03:51 +00:00
|
|
|
if (name) name_ = name();
|
2016-04-06 09:35:49 +00:00
|
|
|
callable_returning_void init_once = reinterpret_cast<callable_returning_void>(dlsym(module_->dl, "on_plugin_load"));
|
2016-04-05 20:43:07 +00:00
|
|
|
if (init_once) {
|
|
|
|
init_once();
|
|
|
|
}
|
2014-12-16 21:03:51 +00:00
|
|
|
}
|
2013-04-10 02:37:02 +00:00
|
|
|
#else
|
2014-12-16 21:03:51 +00:00
|
|
|
#ifdef MAPNIK_HAS_DLCFN
|
2013-04-10 02:37:02 +00:00
|
|
|
if (module_) module_->dl = dlopen(filename.c_str(),RTLD_LAZY);
|
|
|
|
if (module_ && module_->dl)
|
|
|
|
{
|
2016-04-05 19:31:19 +00:00
|
|
|
callable_returning_string name = reinterpret_cast<callable_returning_string>(dlsym(module_->dl, library_name.c_str()));
|
2013-04-10 02:37:02 +00:00
|
|
|
if (name) name_ = name();
|
2016-04-06 09:35:49 +00:00
|
|
|
callable_returning_void init_once = reinterpret_cast<callable_returning_void>(dlsym(module_->dl, "on_plugin_load"));
|
2016-04-05 20:43:07 +00:00
|
|
|
if (init_once) {
|
|
|
|
init_once();
|
|
|
|
}
|
2013-04-10 02:37:02 +00:00
|
|
|
}
|
2014-12-16 21:03:51 +00:00
|
|
|
#else
|
|
|
|
throw std::runtime_error("no support for loading dynamic objects (Mapnik not compiled with -DMAPNIK_HAS_DLCFN)");
|
|
|
|
#endif
|
|
|
|
#endif
|
2013-04-10 02:37:02 +00:00
|
|
|
}
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2010-06-02 11:03:30 +00:00
|
|
|
PluginInfo::~PluginInfo()
|
|
|
|
{
|
|
|
|
if (module_)
|
2005-06-14 15:06:59 +00:00
|
|
|
{
|
2014-12-19 05:13:47 +00:00
|
|
|
#ifdef MAPNIK_SUPPORTS_DLOPEN
|
2015-02-23 22:56:18 +00:00
|
|
|
/*
|
|
|
|
We do not call dlclose for plugins that link libgdal.
|
|
|
|
This is a terrible hack, but necessary to prevent crashes
|
|
|
|
at exit when gdal attempts to shutdown. The problem arises
|
|
|
|
when Mapnik is used with another library that uses thread-local
|
|
|
|
storage (like libuv). In this case GDAL also tries to cleanup thread
|
|
|
|
local storage and leaves things in a state that causes libuv to crash.
|
|
|
|
This is partially fixed by http://trac.osgeo.org/gdal/ticket/5509 but only
|
|
|
|
in the case that gdal is linked as a shared library. This workaround therefore
|
|
|
|
prevents crashes with gdal 1.11.x and gdal 2.x when using a static libgdal.
|
|
|
|
*/
|
|
|
|
if (module_->dl && name_ != "gdal" && name_ != "ogr")
|
|
|
|
{
|
2016-03-03 23:02:03 +00:00
|
|
|
#ifndef MAPNIK_NO_DLCLOSE
|
2015-02-23 22:56:18 +00:00
|
|
|
dlclose(module_->dl),module_->dl=0;
|
2016-03-03 23:02:03 +00:00
|
|
|
#endif
|
2015-02-23 22:56:18 +00:00
|
|
|
}
|
2014-12-16 21:03:51 +00:00
|
|
|
#endif
|
2014-12-19 05:13:47 +00:00
|
|
|
delete module_;
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2013-04-10 02:37:02 +00:00
|
|
|
|
|
|
|
void * PluginInfo::get_symbol(std::string const& sym_name) const
|
|
|
|
{
|
2014-12-19 05:13:47 +00:00
|
|
|
#ifdef MAPNIK_SUPPORTS_DLOPEN
|
2013-06-03 00:34:44 +00:00
|
|
|
return static_cast<void *>(dlsym(module_->dl, sym_name.c_str()));
|
2014-12-16 21:03:51 +00:00
|
|
|
#else
|
2016-04-05 19:27:32 +00:00
|
|
|
return nullptr;
|
2014-12-16 21:03:51 +00:00
|
|
|
#endif
|
2013-04-10 02:37:02 +00:00
|
|
|
}
|
|
|
|
|
2012-09-03 17:27:48 +00:00
|
|
|
std::string const& PluginInfo::name() const
|
2010-06-02 11:03:30 +00:00
|
|
|
{
|
|
|
|
return name_;
|
|
|
|
}
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2013-04-10 02:37:02 +00:00
|
|
|
bool PluginInfo::valid() const
|
2010-06-02 11:03:30 +00:00
|
|
|
{
|
2014-12-19 05:13:47 +00:00
|
|
|
#ifdef MAPNIK_SUPPORTS_DLOPEN
|
2013-04-10 02:37:02 +00:00
|
|
|
if (module_ && module_->dl && !name_.empty()) return true;
|
2014-12-19 05:13:47 +00:00
|
|
|
#endif
|
2013-04-10 02:37:02 +00:00
|
|
|
return false;
|
2010-06-02 11:03:30 +00:00
|
|
|
}
|
2005-06-14 15:06:59 +00:00
|
|
|
|
2013-04-10 02:37:02 +00:00
|
|
|
std::string PluginInfo::get_error() const
|
|
|
|
{
|
|
|
|
return std::string("could not open: '") + name_ + "'";
|
|
|
|
}
|
|
|
|
|
|
|
|
void PluginInfo::init()
|
|
|
|
{
|
|
|
|
// do any initialization needed
|
|
|
|
}
|
|
|
|
|
|
|
|
void PluginInfo::exit()
|
|
|
|
{
|
|
|
|
// do any shutdown needed
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-14 15:06:59 +00:00
|
|
|
}
|