Skip to content

Commit 3df51fb

Browse files
committed
Resolve directory plugins by platform-neutral name
1 parent 124d885 commit 3df51fb

8 files changed

Lines changed: 75 additions & 46 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ Static and runtime plugins differ only in how their instances enter the factory:
190190

191191
- The root `iid` field is still required and must match the IID passed to the `PluginSystem` constructor.
192192
- The root `metadata` field is required and follows the `PluginSystem` schema below.
193-
- The root `binary` field is reserved by the `Directory` layout and names the plugin library inside its directory.
193+
- The root `name` field is reserved by the `Directory` layout and gives the plugin library's platform-independent name. It is separate from the user-visible `metadata.name` field.
194194
- Other root fields remain user-defined and are not interpreted by `PluginSystem`.
195195

196196
The `metadata` object has the following schema:
@@ -237,7 +237,7 @@ A plugin with compatibility version `C` and current version `V` satisfies a requ
237237
`PluginSystem` supports two filesystem layouts:
238238

239239
- `Flat` is the default. It puts plugin libraries directly below each search path and reads their embedded manifests.
240-
- `Directory` gives every plugin a child directory containing its library and a sidecar `plugin.json`. The manifest must contain the reserved root `binary` field naming the library in that directory.
240+
- `Directory` gives every plugin a child directory containing its library and a sidecar `plugin.json`. The manifest must contain a root `name` without a platform library prefix or suffix. For example, `"name": "editor"` can resolve to `editor.dll`, `libeditor.dll`, `libeditor.so`, or `libeditor.dylib`.
241241

242242
### Plugin Implementation
243243

‎include/stdcorelib/plugin/pluginfactory.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ namespace stdc::plugin {
104104
class Impl;
105105
std::unique_ptr<Impl> _impl;
106106

107-
explicit PluginFactory(Impl &impl);
108-
109107
STDC_DISABLE_COPY(PluginFactory)
110108
};
111109

‎src/plugin/pluginfactory.cpp‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,6 @@ namespace stdc::plugin {
107107
PluginFactory::PluginFactory() : _impl(std::make_unique<Impl>()) {
108108
}
109109

110-
PluginFactory::PluginFactory(Impl &impl) : _impl(&impl) {
111-
}
112-
113110
PluginFactory::~PluginFactory() = default;
114111

115112
PluginFactory::PluginFactory(PluginFactory &&RHS) noexcept = default;

‎src/plugin/pluginfactory_p.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
namespace stdc::plugin {
1515

16-
class STDC_PLUGIN_EXPORT PluginFactory::Impl {
16+
class PluginFactory::Impl {
1717
public:
1818
Impl();
19-
virtual ~Impl();
19+
~Impl();
2020

2121
public:
2222
/// Builds an empty loader this factory owns.

‎src/plugin/pluginloader.cpp‎

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <sstream>
99
#include <utility>
1010

11-
#include <stdcorelib/path.h>
1211
#include <stdcorelib/pimpl.h>
1312
#include <stdcorelib/str.h>
1413

@@ -79,7 +78,7 @@ namespace stdc::plugin {
7978
if (parseError) {
8079
return reportError(formatN(R"(%1: %2)", sourcePath, parseError.message()));
8180
}
82-
return readManifest(root, sourcePath, filePath);
81+
return readManifest(root, sourcePath);
8382
}
8483

8584
bool PluginLoader::Impl::validateManifest(const json::Value &root, std::string_view source,
@@ -103,37 +102,11 @@ namespace stdc::plugin {
103102
}
104103

105104
bool PluginLoader::Impl::readManifest(const json::Value &root,
106-
const std::filesystem::path &sourcePath,
107-
const std::filesystem::path &boundFilePath) {
105+
const std::filesystem::path &sourcePath) {
108106
std::string validatedIid;
109107
if (!validateManifest(root, formatN("%1", sourcePath), &validatedIid)) {
110108
return false;
111109
}
112-
const auto &obj = root.toObject();
113-
114-
const auto stringField = [&obj](const std::string_view &key,
115-
std::string_view *out) -> bool {
116-
auto it = obj.find(key);
117-
if (it == obj.end() || !it->second.isString()) {
118-
return false;
119-
}
120-
*out = it->second.toString();
121-
return !out->empty();
122-
};
123-
124-
std::string_view binary;
125-
if (boundFilePath.empty()) {
126-
if (!stringField("binary", &binary)) {
127-
return reportError(formatN(R"(%1: missing or invalid "binary" field)", sourcePath));
128-
}
129-
filePath = sourcePath.parent_path() / path::from_utf8(binary);
130-
} else {
131-
filePath = boundFilePath;
132-
}
133-
if (!fs::is_regular_file(filePath)) {
134-
return reportError(
135-
formatN(R"(%1: "binary" names "%2", which is not there)", sourcePath, filePath));
136-
}
137110

138111
iid = std::move(validatedIid);
139112
manifest = root;

‎src/plugin/pluginloader_p.h‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ namespace stdc::plugin {
3838
const std::optional<std::filesystem::path> &manifestPath = {});
3939
bool validateManifest(const json::Value &root, std::string_view source,
4040
std::string *validatedIid);
41-
bool readManifest(const json::Value &root, const std::filesystem::path &sourcePath,
42-
const std::filesystem::path &boundFilePath = {});
41+
bool readManifest(const json::Value &root, const std::filesystem::path &sourcePath);
4342
bool setStaticPlugin(const StaticPlugin &plugin);
4443
bool setRuntimePlugin(Plugin *plugin, const json::Value &manifest);
4544

‎src/pluginsystem/pluginsystem.cpp‎

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "pluginsystem_p.h"
55

66
#include <algorithm>
7+
#include <array>
78
#include <fstream>
89
#include <functional>
910
#include <map>
@@ -14,6 +15,7 @@
1415

1516
#include <stdcorelib/path.h>
1617
#include <stdcorelib/stlextra/algorithms.h>
18+
#include <stdcorelib/support/sharedlibrary.h>
1719

1820
#include "pluginfactory.h"
1921

@@ -25,6 +27,45 @@ namespace stdc::pluginsystem {
2527

2628
constexpr const char *manifestName = "plugin.json";
2729

30+
std::optional<fs::path> resolveLibraryName(const fs::path &directory,
31+
std::string_view name) {
32+
const auto requestedPath = stdc::path::from_utf8(name);
33+
if (requestedPath.empty() || requestedPath.is_absolute() ||
34+
requestedPath.has_parent_path()) {
35+
return std::nullopt;
36+
}
37+
38+
const auto basePath = directory / requestedPath;
39+
const auto parentPath = basePath.parent_path();
40+
const auto baseName = basePath.filename();
41+
constexpr std::array<std::string_view, 2> prefixes{"", "lib"};
42+
constexpr std::array<std::string_view, 2> suffixes{
43+
"",
44+
#ifdef _WIN32
45+
".dll",
46+
#elif defined(__APPLE__)
47+
".dylib",
48+
#else
49+
".so",
50+
#endif
51+
};
52+
53+
for (const auto prefix : prefixes) {
54+
for (const auto suffix : suffixes) {
55+
fs::path candidateName = std::string(prefix);
56+
candidateName += baseName.native();
57+
candidateName += std::string(suffix);
58+
const auto candidate = parentPath / candidateName;
59+
std::error_code ec;
60+
if (fs::is_regular_file(candidate, ec) &&
61+
SharedLibrary::isLibrary(candidate)) {
62+
return candidate;
63+
}
64+
}
65+
}
66+
return std::nullopt;
67+
}
68+
2869
class DirectoryPluginFactory final : public plugin::PluginFactory {
2970
protected:
3071
bool scanPluginPaths(const fs::path &path,
@@ -66,12 +107,17 @@ namespace stdc::pluginsystem {
66107
return false;
67108
}
68109

69-
const auto &binary = root["binary"];
70-
if (!binary.isString() || binary.toString().empty()) {
110+
const auto &name = root["name"];
111+
if (!name.isString() || name.toString().empty()) {
112+
return false;
113+
}
114+
115+
auto resolvedPath = resolveLibraryName(path, name.toString());
116+
if (!resolvedPath) {
71117
return false;
72118
}
73119

74-
*pluginPath = path / stdc::path::from_utf8(binary.toString());
120+
*pluginPath = std::move(*resolvedPath);
75121
*manifestPath = std::move(manifest);
76122
return true;
77123
}
@@ -408,7 +454,7 @@ namespace stdc::pluginsystem {
408454
std::lock_guard<std::mutex> lifecycleLock(impl.lifecycleMtx);
409455
{
410456
std::unique_lock<std::shared_mutex> lock(impl.configMtx);
411-
if (impl.loadStarted || impl.shutdownFinished) {
457+
if (impl.loadStarted) {
412458
return;
413459
}
414460
impl.plugins(true);

‎tests/auto/test_pluginsystem.cpp‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ namespace {
5353
std::filesystem::copy_file(source, pluginPath);
5454

5555
std::ofstream manifest(pluginDirectory / "plugin.json");
56-
manifest << R"({"iid":")" << iid << R"(","binary":")" << source.filename().string()
56+
manifest << R"({"iid":")" << iid << R"(","name":")" << source.stem().string()
5757
<< R"(","metadata":)" << metadata;
5858
if (!manifestData.empty()) {
5959
manifest << "," << manifestData;
@@ -170,6 +170,22 @@ BOOST_AUTO_TEST_CASE(test_directory_layout) {
170170
checkPluginSystemLayout(stdc::pluginsystem::PluginSystem::Directory);
171171
}
172172

173+
BOOST_AUTO_TEST_CASE(test_directory_layout_resolves_library_prefix) {
174+
TemporaryPluginSystemDirectory directory(stdc::pluginsystem::PluginSystem::Directory, false);
175+
const auto original = directory.addPlugin(
176+
"plugin", R"({"id":"Plugin","name":"Plugin","version":"1.0"})");
177+
const auto prefixed = original.parent_path() / ("lib" + original.filename().string());
178+
std::filesystem::rename(original, prefixed);
179+
180+
stdc::pluginsystem::PluginSystem system("org.stdcorelib.PluginSystem",
181+
stdc::pluginsystem::PluginSystem::Directory);
182+
system.setPluginPaths(directory.path());
183+
184+
const auto specs = system.plugins();
185+
BOOST_REQUIRE_EQUAL(specs.size(), 1u);
186+
BOOST_CHECK_EQUAL(specs.front()->id(), "Plugin");
187+
}
188+
173189
BOOST_AUTO_TEST_CASE(test_replacing_paths_discards_unloaded_specs) {
174190
TemporaryPluginSystemDirectory directory(stdc::pluginsystem::PluginSystem::Flat);
175191
stdc::pluginsystem::PluginSystem system("org.stdcorelib.PluginSystem");
@@ -266,7 +282,7 @@ BOOST_AUTO_TEST_CASE(test_spec_exposes_complete_manifest) {
266282
const auto &manifest = spec->manifest();
267283
BOOST_CHECK_EQUAL(manifest["iid"].toString(), "org.stdcorelib.PluginSystem");
268284
BOOST_CHECK_EQUAL(manifest["applicationData"]["answer"].toInt(), 42);
269-
BOOST_CHECK(!manifest["binary"].toString().empty());
285+
BOOST_CHECK(!manifest["name"].toString().empty());
270286
}
271287

272288
BOOST_AUTO_TEST_CASE(test_global_and_local_settings_precedence_and_freeze_at_load) {

0 commit comments

Comments
 (0)