]> git.tdb.fi Git - builder.git/commitdiff
Add an interface for dynamically loaded plugins
authorMikko Rasa <tdb@tdb.fi>
Wed, 28 Dec 2022 13:16:21 +0000 (15:16 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 28 Dec 2022 15:38:25 +0000 (17:38 +0200)
source/cli/buildercli.cpp
source/lib/builder.cpp
source/lib/builder.h
source/lib/plugin.h [new file with mode: 0644]

index cb0897c1601775e2467e5be1753b3c35b5a1d326..9f8c89503f9c488af8aa699d99db818fd050c452 100644 (file)
@@ -87,6 +87,7 @@ BuilderCLI::BuilderCLI(int argc, char **argv):
                for(const string &p: split(c, ','))
                        logger.enable_channel(p);
        builder.set_logger(&logger);
+       builder.load_plugins();
 
        if(!analyze_mode.empty())
        {
index 92924ebc0956aee33680624e97deb5cabd6638cb..72ee524dffe41fc22111863953d1549e5ad22a67 100644 (file)
@@ -22,6 +22,7 @@
 #include "installedfile.h"
 #include "msvc/microsofttools.h"
 #include "package.h"
+#include "plugin.h"
 #include "sharedlibrary.h"
 #include "sourcepackage.h"
 #include "task.h"
@@ -46,6 +47,49 @@ Builder::~Builder()
                delete current_arch;
 }
 
+void Builder::load_plugins()
+{
+       using CreateFunc = Plugin *(Builder &);
+
+       FS::Path plugins_dir = FS::get_sys_lib_dir();
+       logger->log("files", "Traversing %s", plugins_dir);
+       for(const string &f: list_filtered(plugins_dir, "\\.dlm$"))
+       {
+               LoadedPlugin plugin;
+               plugin.path = plugins_dir/f;
+
+               try
+               {
+                       plugin.module = new Module(plugin.path.str());
+               }
+               catch(const exception &exc)
+               {
+                       logger->log("plugins", "Failed to load plugin %s: %s", f, exc.what());
+                       continue;
+               }
+
+               try
+               {
+                       CreateFunc *create_func = reinterpret_cast<CreateFunc *>(plugin.module->get_symbol("create_plugin"));
+                       plugin.plugin = create_func(*this);
+                       if(plugin.plugin)
+                       {
+                               logger->log("plugins", "Loaded plugin %s", f);
+                               plugins.emplace_back(move(plugin));
+                               continue;
+                       }
+                       else
+                               logger->log("plugins", "Plugin %s refused to initialize", f);
+               }
+               catch(const exception &exc)
+               {
+                       logger->log("plugins", "Failed to initialize plugin %s: %s", f, exc.what());
+               }
+
+               delete plugin.module;
+       }
+}
+
 void Builder::set_architecture(const string &name)
 {
        if(current_arch!=&native_arch)
@@ -110,6 +154,8 @@ void Builder::add_default_tools()
                toolchain.add_toolchain(new MicrosoftTools(*this, *current_arch));
        toolchain.add_toolchain(new BuiltinTools(*this));
        toolchain.add_tool(new DataTool(*this));
+       for(const LoadedPlugin &p: plugins)
+               p.plugin->add_tools(toolchain, *current_arch);
 
        auto i = find_if(toolchain.get_toolchains(), [](const Toolchain *tc){ return (tc->has_tool("CC") || tc->has_tool("CXX")); });
        if(i!=toolchain.get_toolchains().end())
@@ -310,6 +356,22 @@ int Builder::clean(bool all, bool dry_run)
 }
 
 
+Builder::LoadedPlugin::LoadedPlugin(LoadedPlugin &&other):
+       path(move(other.path)),
+       module(other.module),
+       plugin(other.plugin)
+{
+       other.module = 0;
+       other.plugin = 0;
+}
+
+Builder::LoadedPlugin::~LoadedPlugin()
+{
+       delete plugin;
+       delete module;
+}
+
+
 Builder::Loader::Loader(Builder &b, const Config::InputOptions *o, bool a):
        DataFile::ObjectLoader<Builder>(b),
        options(o),
index e06cec34e73c6efbea21f1dee7a5839d15ef7e96..32d06a0be62f1d0515cdd30bf9538795bdd9277a 100644 (file)
@@ -3,6 +3,7 @@
 
 #include <map>
 #include <string>
+#include <msp/core/module.h>
 #include <msp/datafile/loader.h>
 #include <msp/fs/path.h>
 #include "architecture.h"
@@ -17,6 +18,7 @@
 
 class FileTarget;
 class Package;
+class Plugin;
 class SourcePackage;
 
 /**
@@ -44,6 +46,18 @@ private:
        };
 
 private:
+       struct LoadedPlugin
+       {
+               Msp::FS::Path path;
+               Msp::Module *module = nullptr;
+               Plugin *plugin = nullptr;
+
+               LoadedPlugin() = default;
+               LoadedPlugin(LoadedPlugin &&);
+               ~LoadedPlugin();
+       };
+
+       std::vector<LoadedPlugin> plugins;
        PackageManager package_manager;
 
        Architecture native_arch;
@@ -66,6 +80,7 @@ public:
        Builder();
        ~Builder();
 
+       void load_plugins();
        PackageManager &get_package_manager() { return package_manager; }
 
        void set_architecture(const std::string &);
diff --git a/source/lib/plugin.h b/source/lib/plugin.h
new file mode 100644 (file)
index 0000000..910e259
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef PLUGIN_H_
+#define PLUGIN_H_
+
+class Architecture;
+class Builder;
+class Toolchain;
+
+class Plugin
+{
+protected:
+       Builder &builder;
+
+       Plugin(Builder &b): builder(b) { }
+public:
+       virtual ~Plugin() = default;
+
+       virtual void add_tools(Toolchain &, const Architecture &) const { }
+};
+
+#endif