2006-03-31 12:32:02 +02:00
|
|
|
/*****************************************************************************
|
2012-02-02 02:53:35 +01:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2021-01-05 15:39:07 +01:00
|
|
|
* Copyright (C) 2021 Artem Pavlenko
|
2005-06-14 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02: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 17:06:59 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 12:32:02 +02: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 17:06:59 +02:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
*****************************************************************************/
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
#include <mapnik/plugin.hpp>
|
2013-04-10 04:37:02 +02:00
|
|
|
#include <stdexcept>
|
|
|
|
|
2021-10-21 18:22:53 +02:00
|
|
|
#ifdef _WIN32
|
2013-11-07 00:34:53 +01:00
|
|
|
#define NOMINMAX
|
2013-04-10 04:37:02 +02:00
|
|
|
#include <windows.h>
|
|
|
|
#define handle HMODULE
|
|
|
|
#define dlsym GetProcAddress
|
|
|
|
#define dlclose FreeLibrary
|
|
|
|
#define dlerror GetLastError
|
2014-12-19 06:13:47 +01:00
|
|
|
#define MAPNIK_SUPPORTS_DLOPEN
|
2013-04-10 04:37:02 +02:00
|
|
|
#else
|
2014-12-16 22:03:51 +01:00
|
|
|
#ifdef MAPNIK_HAS_DLCFN
|
|
|
|
#include <dlfcn.h>
|
2014-12-19 06:13:47 +01:00
|
|
|
#define MAPNIK_SUPPORTS_DLOPEN
|
2014-12-16 22:03:51 +01:00
|
|
|
#endif
|
2013-04-10 04:37:02 +02:00
|
|
|
#define handle void *
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// TODO - handle/report dlerror
|
2005-06-14 17:06:59 +02:00
|
|
|
|
|
|
|
namespace mapnik
|
|
|
|
{
|
|
|
|
|
2013-04-10 04:37:02 +02: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)
|
|
|
|
{
|
2021-10-21 18:22:53 +02:00
|
|
|
#ifdef _WIN32
|
2013-04-10 04:37:02 +02:00
|
|
|
if (module_) module_->dl = LoadLibraryA(filename.c_str());
|
2014-12-16 22:03:51 +01:00
|
|
|
if (module_ && module_->dl)
|
|
|
|
{
|
2017-01-12 17:15:05 +01:00
|
|
|
callable_returning_string name_call = reinterpret_cast<callable_returning_string>(dlsym(module_->dl, library_name.c_str()));
|
|
|
|
if (name_call) name_ = name_call();
|
2016-04-06 11:35:49 +02:00
|
|
|
callable_returning_void init_once = reinterpret_cast<callable_returning_void>(dlsym(module_->dl, "on_plugin_load"));
|
2016-04-05 22:43:07 +02:00
|
|
|
if (init_once) {
|
|
|
|
init_once();
|
|
|
|
}
|
2014-12-16 22:03:51 +01:00
|
|
|
}
|
2013-04-10 04:37:02 +02:00
|
|
|
#else
|
2014-12-16 22:03:51 +01:00
|
|
|
#ifdef MAPNIK_HAS_DLCFN
|
2013-04-10 04:37:02 +02:00
|
|
|
if (module_) module_->dl = dlopen(filename.c_str(),RTLD_LAZY);
|
|
|
|
if (module_ && module_->dl)
|
|
|
|
{
|
2017-01-12 17:15:05 +01:00
|
|
|
callable_returning_string name_call = reinterpret_cast<callable_returning_string>(dlsym(module_->dl, library_name.c_str()));
|
|
|
|
if (name_call) name_ = name_call();
|
2016-04-06 11:35:49 +02:00
|
|
|
callable_returning_void init_once = reinterpret_cast<callable_returning_void>(dlsym(module_->dl, "on_plugin_load"));
|
2016-04-05 22:43:07 +02:00
|
|
|
if (init_once) {
|
|
|
|
init_once();
|
|
|
|
}
|
2013-04-10 04:37:02 +02:00
|
|
|
}
|
2014-12-16 22:03:51 +01:00
|
|
|
#else
|
|
|
|
throw std::runtime_error("no support for loading dynamic objects (Mapnik not compiled with -DMAPNIK_HAS_DLCFN)");
|
|
|
|
#endif
|
|
|
|
#endif
|
2013-04-10 04:37:02 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
PluginInfo::~PluginInfo()
|
|
|
|
{
|
|
|
|
if (module_)
|
2005-06-14 17:06:59 +02:00
|
|
|
{
|
2014-12-19 06:13:47 +01:00
|
|
|
#ifdef MAPNIK_SUPPORTS_DLOPEN
|
2015-02-23 23:56:18 +01: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-04 00:02:03 +01:00
|
|
|
#ifndef MAPNIK_NO_DLCLOSE
|
2015-02-23 23:56:18 +01:00
|
|
|
dlclose(module_->dl),module_->dl=0;
|
2016-03-04 00:02:03 +01:00
|
|
|
#endif
|
2015-02-23 23:56:18 +01:00
|
|
|
}
|
2014-12-16 22:03:51 +01:00
|
|
|
#endif
|
2014-12-19 06:13:47 +01:00
|
|
|
delete module_;
|
2005-06-14 17:06:59 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2013-04-10 04:37:02 +02:00
|
|
|
|
|
|
|
void * PluginInfo::get_symbol(std::string const& sym_name) const
|
|
|
|
{
|
2014-12-19 06:13:47 +01:00
|
|
|
#ifdef MAPNIK_SUPPORTS_DLOPEN
|
2013-06-03 02:34:44 +02:00
|
|
|
return static_cast<void *>(dlsym(module_->dl, sym_name.c_str()));
|
2014-12-16 22:03:51 +01:00
|
|
|
#else
|
2016-04-05 21:27:32 +02:00
|
|
|
return nullptr;
|
2014-12-16 22:03:51 +01:00
|
|
|
#endif
|
2013-04-10 04:37:02 +02:00
|
|
|
}
|
|
|
|
|
2012-09-03 19:27:48 +02:00
|
|
|
std::string const& PluginInfo::name() const
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
return name_;
|
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2013-04-10 04:37:02 +02:00
|
|
|
bool PluginInfo::valid() const
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2014-12-19 06:13:47 +01:00
|
|
|
#ifdef MAPNIK_SUPPORTS_DLOPEN
|
2013-04-10 04:37:02 +02:00
|
|
|
if (module_ && module_->dl && !name_.empty()) return true;
|
2014-12-19 06:13:47 +01:00
|
|
|
#endif
|
2013-04-10 04:37:02 +02:00
|
|
|
return false;
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2005-06-14 17:06:59 +02:00
|
|
|
|
2013-04-10 04:37:02 +02: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 17:06:59 +02:00
|
|
|
}
|