mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-09 13:52:23 +00:00
Fixes to plug-in loading in and outside of pyodide
This commit is contained in:
@@ -21,6 +21,12 @@
|
||||
|
||||
#include <boost/algorithm/string/case_conv.hpp>
|
||||
|
||||
namespace ifcopenshell {
|
||||
namespace plugin {
|
||||
PLUGIN_API std::filesystem::path add_search_paths_or_default(manager& manager, std::filesystem::path (*default_search_path)());
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
constexpr const char* opencascade_geometry_ifc_writer_plugin_prefix = "geometry.serialization.";
|
||||
}
|
||||
@@ -43,7 +49,7 @@ std::filesystem::path IfcGeom::opencascade_geometry_ifc_writer_plugin_directory(
|
||||
|
||||
void IfcGeom::load_opencascade_geometry_ifc_writer_plugins(opencascade_geometry_ifc_writer_registry& registry) {
|
||||
ifcopenshell::plugin::manager manager;
|
||||
manager.add_search_path(opencascade_geometry_ifc_writer_plugin_directory());
|
||||
ifcopenshell::plugin::add_search_paths_or_default(manager, &opencascade_geometry_ifc_writer_plugin_directory);
|
||||
|
||||
for (const auto& path : manager.discover(opencascade_geometry_ifc_writer_plugin_prefix)) {
|
||||
auto module = manager.load(path);
|
||||
@@ -58,7 +64,7 @@ void IfcGeom::load_opencascade_geometry_ifc_writer_plugins(opencascade_geometry_
|
||||
|
||||
bool IfcGeom::load_opencascade_geometry_ifc_writer_plugin(opencascade_geometry_ifc_writer_registry& registry, const std::string& schema_name) {
|
||||
ifcopenshell::plugin::manager manager;
|
||||
manager.add_search_path(opencascade_geometry_ifc_writer_plugin_directory());
|
||||
ifcopenshell::plugin::add_search_paths_or_default(manager, &opencascade_geometry_ifc_writer_plugin_directory);
|
||||
|
||||
const auto expected_schema = boost::to_upper_copy(schema_name);
|
||||
const auto basename = std::string(opencascade_geometry_ifc_writer_plugin_prefix) + boost::to_lower_copy(schema_name);
|
||||
|
||||
@@ -24,6 +24,12 @@
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace ifcopenshell {
|
||||
namespace plugin {
|
||||
PLUGIN_API std::filesystem::path add_search_paths_or_default(manager& manager, std::filesystem::path (*default_search_path)());
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
constexpr const char* kernel_plugin_prefix = "geometry.kernel.";
|
||||
|
||||
@@ -51,7 +57,7 @@ std::filesystem::path ifcopenshell::geometry::kernels::kernel_plugin_directory()
|
||||
|
||||
void ifcopenshell::geometry::kernels::load_kernel_plugins(kernel_registry& registry) {
|
||||
plugin::manager manager;
|
||||
manager.add_search_path(kernel_plugin_directory());
|
||||
plugin::add_search_paths_or_default(manager, &kernel_plugin_directory);
|
||||
|
||||
for (const auto& path : manager.discover(kernel_plugin_prefix)) {
|
||||
auto module = manager.load(path);
|
||||
@@ -66,7 +72,7 @@ void ifcopenshell::geometry::kernels::load_kernel_plugins(kernel_registry& regis
|
||||
|
||||
bool ifcopenshell::geometry::kernels::load_kernel_plugin(kernel_registry& registry, const std::string& backend_id) {
|
||||
plugin::manager manager;
|
||||
manager.add_search_path(kernel_plugin_directory());
|
||||
plugin::add_search_paths_or_default(manager, &kernel_plugin_directory);
|
||||
|
||||
const auto plugin_name = kernel_plugin_name(backend_id);
|
||||
const auto basename = std::string(kernel_plugin_prefix) + plugin_name;
|
||||
|
||||
@@ -27,13 +27,85 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
namespace ifcopenshell {
|
||||
namespace plugin {
|
||||
PLUGIN_API std::filesystem::path add_search_paths_or_default(manager& manager, std::filesystem::path (*default_search_path)());
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
constexpr const char* kernel_plugin_prefix = "geometry.kernel.";
|
||||
|
||||
std::string kernel_key(const std::string& backend_id) {
|
||||
return boost::to_lower_copy(backend_id);
|
||||
}
|
||||
|
||||
bool is_prefix(const std::string& text, const std::string& prefix) {
|
||||
return !prefix.empty() && text.rfind(prefix, 0) == 0;
|
||||
}
|
||||
|
||||
void register_kernel_module(ifcopenshell::geometry::kernels::kernel_registry& registry, const ifcopenshell::plugin::module& module) {
|
||||
auto register_plugin = module.get_alias<ifcopenshell::geometry::kernels::register_kernel_plugin_fn>(
|
||||
ifcopenshell::geometry::kernels::kernel_plugin_registration_symbol());
|
||||
register_plugin(registry, module);
|
||||
}
|
||||
|
||||
struct kernel_match {
|
||||
std::string backend_id;
|
||||
ifcopenshell::plugin::module module;
|
||||
bool has_module = false;
|
||||
};
|
||||
|
||||
void consider_kernel_info(kernel_match& match, const std::string& geometry_library_lower, const ifcopenshell::geometry::kernels::kernel_info& info) {
|
||||
const auto backend_id = kernel_key(info.backend_id);
|
||||
if (is_prefix(geometry_library_lower, backend_id) && backend_id.size() > match.backend_id.size()) {
|
||||
match.backend_id = backend_id;
|
||||
match.has_module = false;
|
||||
}
|
||||
}
|
||||
|
||||
kernel_match find_kernel_match(ifcopenshell::geometry::kernels::kernel_registry& registry, const std::string& geometry_library_lower) {
|
||||
kernel_match match;
|
||||
|
||||
for (const auto& info : registry.kernels()) {
|
||||
consider_kernel_info(match, geometry_library_lower, info);
|
||||
}
|
||||
|
||||
ifcopenshell::plugin::manager manager;
|
||||
ifcopenshell::plugin::add_search_paths_or_default(manager, &ifcopenshell::geometry::kernels::kernel_plugin_directory);
|
||||
for (const auto& path : manager.discover(kernel_plugin_prefix)) {
|
||||
ifcopenshell::plugin::module module;
|
||||
try {
|
||||
module = manager.load(path);
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "[ifcopenshell.plugin] skip kernel plugin " << path << ": " << e.what() << std::endl;
|
||||
continue;
|
||||
}
|
||||
if (module.meta().kind_ != ifcopenshell::plugin::kind::kernel) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ifcopenshell::geometry::kernels::kernel_registry plugin_registry;
|
||||
register_kernel_module(plugin_registry, module);
|
||||
for (const auto& info : plugin_registry.kernels()) {
|
||||
const auto backend_id = kernel_key(info.backend_id);
|
||||
if (is_prefix(geometry_library_lower, backend_id) && backend_id.size() > match.backend_id.size()) {
|
||||
match.backend_id = backend_id;
|
||||
match.module = module;
|
||||
match.has_module = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!match.backend_id.empty() && !registry.has(match.backend_id) && match.has_module) {
|
||||
register_kernel_module(registry, match.module);
|
||||
}
|
||||
|
||||
return match;
|
||||
}
|
||||
}
|
||||
|
||||
void ifcopenshell::geometry::kernels::kernel_registry::bind(const kernel_info& info, create_fn create, const plugin::module& module) {
|
||||
@@ -90,30 +162,11 @@ std::unique_ptr<ifcopenshell::geometry::kernels::AbstractKernel> ifcopenshell::g
|
||||
throw ifcopenshell::exception("Invalid hybrid kernel " + geometry_library);
|
||||
}
|
||||
|
||||
std::vector<std::string> candidates;
|
||||
candidates.push_back(geometry_library_lower);
|
||||
for (auto pos = geometry_library_lower.find('-'); pos != std::string::npos; pos = geometry_library_lower.find('-', pos + 1)) {
|
||||
candidates.push_back(geometry_library_lower.substr(0, pos));
|
||||
}
|
||||
std::sort(candidates.begin(), candidates.end(), [](const auto& a, const auto& b) {
|
||||
return a.size() > b.size();
|
||||
});
|
||||
for (const auto& candidate : candidates) {
|
||||
if (!registry.has(candidate) && load_kernel_plugin(registry, candidate)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
std::string matched_backend_id;
|
||||
for (const auto& info : registry.kernels()) {
|
||||
const auto backend_id = kernel_key(info.backend_id);
|
||||
if (geometry_library_lower.rfind(backend_id, 0) == 0 && backend_id.size() > matched_backend_id.size()) {
|
||||
matched_backend_id = backend_id;
|
||||
}
|
||||
}
|
||||
const auto match = find_kernel_match(registry, geometry_library_lower);
|
||||
const auto& matched_backend_id = match.backend_id;
|
||||
|
||||
if (matched_backend_id.empty()) {
|
||||
throw ifcopenshell::exception("Invalid hybrid kernel " + geometry_library);
|
||||
throw ifcopenshell::exception("Invalid hybrid kernel; no match for prefix of " + geometry_library_lower);
|
||||
}
|
||||
|
||||
kernels.push_back(registry.create(matched_backend_id, file, settings));
|
||||
|
||||
@@ -22,10 +22,14 @@
|
||||
|
||||
#include <boost/dll/alias.hpp>
|
||||
|
||||
#ifdef IFOPSH_SIMPLE_KERNEL
|
||||
#define cgal_plugin cgalsimple_plugin
|
||||
#endif
|
||||
|
||||
namespace ifcopenshell {
|
||||
namespace geometry {
|
||||
namespace kernels {
|
||||
namespace cgal_plugin {
|
||||
namespace cgal_plugin {
|
||||
|
||||
#ifdef IFOPSH_SIMPLE_KERNEL
|
||||
constexpr const char* plugin_name = "cgalsimple";
|
||||
|
||||
@@ -21,6 +21,12 @@
|
||||
|
||||
#include <boost/algorithm/string/case_conv.hpp>
|
||||
|
||||
namespace ifcopenshell {
|
||||
namespace plugin {
|
||||
PLUGIN_API std::filesystem::path add_search_paths_or_default(manager& manager, std::filesystem::path (*default_search_path)());
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
constexpr const char* mapping_plugin_prefix = "geometry.mapping.";
|
||||
}
|
||||
@@ -43,7 +49,7 @@ std::filesystem::path ifcopenshell::geometry::impl::mapping_plugin_directory() {
|
||||
|
||||
void ifcopenshell::geometry::impl::load_mapping_plugins(mapping_registry& registry) {
|
||||
plugin::manager manager;
|
||||
manager.add_search_path(mapping_plugin_directory());
|
||||
plugin::add_search_paths_or_default(manager, &mapping_plugin_directory);
|
||||
|
||||
for (const auto& path : manager.discover(mapping_plugin_prefix)) {
|
||||
auto module = manager.load(path);
|
||||
@@ -58,7 +64,7 @@ void ifcopenshell::geometry::impl::load_mapping_plugins(mapping_registry& regist
|
||||
|
||||
bool ifcopenshell::geometry::impl::load_mapping_plugin(mapping_registry& registry, const std::string& schema_name) {
|
||||
plugin::manager manager;
|
||||
manager.add_search_path(mapping_plugin_directory());
|
||||
plugin::add_search_paths_or_default(manager, &mapping_plugin_directory);
|
||||
|
||||
const auto expected_schema = boost::to_upper_copy(schema_name);
|
||||
const auto basename = std::string(mapping_plugin_prefix) + boost::to_lower_copy(schema_name);
|
||||
|
||||
@@ -21,6 +21,12 @@
|
||||
|
||||
#include <boost/algorithm/string/case_conv.hpp>
|
||||
|
||||
namespace ifcopenshell {
|
||||
namespace plugin {
|
||||
PLUGIN_API std::filesystem::path add_search_paths_or_default(manager& manager, std::filesystem::path (*default_search_path)());
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
constexpr const char* tree_plugin_prefix = "geometry.tree.";
|
||||
}
|
||||
@@ -42,7 +48,7 @@ std::filesystem::path ifcopenshell::geometry::trees::tree_plugin_directory() {
|
||||
|
||||
void ifcopenshell::geometry::trees::load_tree_plugins(tree_registry& registry) {
|
||||
plugin::manager manager;
|
||||
manager.add_search_path(tree_plugin_directory());
|
||||
plugin::add_search_paths_or_default(manager, &tree_plugin_directory);
|
||||
|
||||
for (const auto& path : manager.discover(tree_plugin_prefix)) {
|
||||
auto module = manager.load(path);
|
||||
@@ -57,7 +63,7 @@ void ifcopenshell::geometry::trees::load_tree_plugins(tree_registry& registry) {
|
||||
|
||||
bool ifcopenshell::geometry::trees::load_tree_plugin(tree_registry& registry, const std::string& backend_id) {
|
||||
plugin::manager manager;
|
||||
manager.add_search_path(tree_plugin_directory());
|
||||
plugin::add_search_paths_or_default(manager, &tree_plugin_directory);
|
||||
|
||||
const auto plugin_name = boost::to_lower_copy(backend_id);
|
||||
const auto basename = std::string(tree_plugin_prefix) + plugin_name;
|
||||
|
||||
Reference in New Issue
Block a user