mapnik/src/plugin.cpp

153 lines
4.5 KiB
C++
Raw Normal View History

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
#include <mapnik/plugin.hpp>
#include <stdexcept>
#ifdef _WINDOWS
#define NOMINMAX
#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
#else
#ifdef MAPNIK_HAS_DLCFN
#include <dlfcn.h>
2014-12-19 06:13:47 +01:00
#define MAPNIK_SUPPORTS_DLOPEN
#endif
#define handle void *
#endif
// TODO - handle/report dlerror
2005-06-14 17:06:59 +02:00
namespace mapnik
{
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());
if (module_ && module_->dl)
{
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"));
if (init_once) {
init_once();
}
}
#else
#ifdef MAPNIK_HAS_DLCFN
if (module_) module_->dl = dlopen(filename.c_str(),RTLD_LAZY);
if (module_ && module_->dl)
{
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"));
if (init_once) {
init_once();
}
}
#else
throw std::runtime_error("no support for loading dynamic objects (Mapnik not compiled with -DMAPNIK_HAS_DLCFN)");
#endif
#endif
}
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
/*
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")
{
#ifndef MAPNIK_NO_DLCLOSE
dlclose(module_->dl),module_->dl=0;
#endif
}
#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
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()));
#else
return nullptr;
#endif
}
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
bool PluginInfo::valid() const
2010-06-02 13:03:30 +02:00
{
2014-12-19 06:13:47 +01:00
#ifdef MAPNIK_SUPPORTS_DLOPEN
if (module_ && module_->dl && !name_.empty()) return true;
2014-12-19 06:13:47 +01:00
#endif
return false;
2010-06-02 13:03:30 +02:00
}
2005-06-14 17:06:59 +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
}