-
Notifications
You must be signed in to change notification settings - Fork 836
Expand file tree
/
Copy pathplugin.cpp
More file actions
159 lines (145 loc) · 4.73 KB
/
plugin.cpp
File metadata and controls
159 lines (145 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2025 Artem Pavlenko
*
* 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,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* 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
*
*****************************************************************************/
#include <mapnik/plugin.hpp>
#include <stdexcept>
#include <mapnik/datasource_plugin.hpp>
#ifdef _WIN32
#define NOMINMAX
#include <windows.h>
#define handle HMODULE
#define dlsym GetProcAddress
#define dlclose FreeLibrary
#define dlerror GetLastError
#define MAPNIK_SUPPORTS_DLOPEN
#else
#ifdef MAPNIK_HAS_DLCFN
#include <dlfcn.h>
#define MAPNIK_SUPPORTS_DLOPEN
#endif
#define handle void*
#endif
// TODO - handle/report dlerror
namespace mapnik {
struct _mapnik_lib_t
{
std::string name;
std::string error_str;
handle dl;
_mapnik_lib_t()
: name{"unknown"},
error_str{""},
dl{nullptr}
{}
~_mapnik_lib_t()
{
#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 (dl /*&& name_ != "gdal" && name_ != "ogr"*/) // is the gdal issue sill present? We are now
// unregister all drivers for ogal and gdal (todo:
// before_unload call)
{
#ifndef MAPNIK_NO_DLCLOSE
dlclose(dl);
dl = nullptr;
#endif
}
#endif
}
};
PluginInfo::PluginInfo(std::string const& filename, std::string const& symbol_name)
: filename_(filename),
symbol_name_(symbol_name),
module_{std::make_unique<mapnik_lib_t>()}
{
assert(module_ != nullptr);
#if defined(_WIN32)
module_->dl = LoadLibraryA(filename.c_str());
#elif defined(MAPNIK_HAS_DLCFN)
module_->dl = dlopen(filename.c_str(), RTLD_LAZY);
#else
throw std::runtime_error("no support for loading dynamic objects (Mapnik not compiled with -DMAPNIK_HAS_DLCFN)");
#endif
#if defined(MAPNIK_HAS_DLCFN) || defined(_WIN32)
if (module_->dl)
{
datasource_plugin const* plugin{get_plugin()};
if (!plugin)
throw std::runtime_error("plugin has a false interface"); //! todo: better error text
module_->name = plugin->name();
module_->error_str = "";
plugin->after_load();
}
else
{
auto const errcode = dlerror();
#ifdef _WIN32
module_->error_str = std::system_category().message(errcode);
#else
module_->error_str = errcode ? errcode : "";
#endif
}
#endif // defined(MAPNIK_HAS_DLCFN) || defined(_WIN32)
}
PluginInfo::~PluginInfo() {}
// void* PluginInfo::get_symbol(std::string const& sym_name) const
// {
// #ifdef MAPNIK_SUPPORTS_DLOPEN
// return reinterpret_cast<void*>(dlsym(module_->dl, sym_name.c_str()));
// #else
// return nullptr;
// #endif
// }
datasource_plugin const* PluginInfo::get_plugin() const
{
#ifdef MAPNIK_SUPPORTS_DLOPEN
return reinterpret_cast<datasource_plugin*>(dlsym(module_->dl, symbol_name_.c_str()));
#else
return nullptr;
#endif
}
std::string const& PluginInfo::name() const
{
return module_->name;
}
bool PluginInfo::valid() const
{
#ifdef MAPNIK_SUPPORTS_DLOPEN
if (module_->dl && !module_->name.empty())
return true;
#endif
return false;
}
std::string PluginInfo::get_error() const
{
return std::string{"could not open: '"} + module_->name + "'. Error: " + module_->error_str;
}
} // namespace mapnik