]> git.tdb.fi Git - builder.git/commitdiff
Make it possible for plugins to require other plugins
authorMikko Rasa <tdb@tdb.fi>
Tue, 3 Jan 2023 12:16:42 +0000 (14:16 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 3 Jan 2023 12:16:42 +0000 (14:16 +0200)
This is used to ensure correct ordering if one plugin customizes tools
from another.

source/lib/builder.cpp
source/lib/builder.h
source/lib/plugin.h

index 738361bce2d3553db15844bc19b7e6e5a7353924..c8c7bf81efbccb47726edef19c1ae11417184103 100644 (file)
@@ -10,6 +10,7 @@
 #include <msp/io/file.h>
 #include <msp/io/print.h>
 #include <msp/strings/format.h>
+#include <msp/strings/utils.h>
 #include <msp/time/timedelta.h>
 #include <msp/time/utils.h>
 #include "binarypackage.h"
@@ -47,6 +48,7 @@ void Builder::load_plugins()
 
        FS::Path plugins_dir = FS::get_sys_lib_dir();
        logger->log("files", "Traversing %s", plugins_dir);
+       vector<LoadedPlugin> unordered_plugins;
        for(const string &f: list_filtered(plugins_dir, "\\.dlm$"))
        {
                LoadedPlugin plugin;
@@ -69,7 +71,7 @@ void Builder::load_plugins()
                        if(plugin.plugin)
                        {
                                logger->log("plugins", "Loaded plugin %s", f);
-                               plugins.emplace_back(move(plugin));
+                               unordered_plugins.emplace_back(move(plugin));
                                continue;
                        }
                        else
@@ -80,6 +82,40 @@ void Builder::load_plugins()
                        logger->log("plugins", "Failed to initialize plugin %s: %s", f, exc.what());
                }
        }
+
+       auto have_plugin = [this](const string &r){
+               return any_of(plugins.begin(), plugins.end(), [&r](const LoadedPlugin &p){ return FS::basepart(FS::basename(p.path))==r; });
+       };
+
+       while(!unordered_plugins.empty())
+       {
+               bool any_added = false;
+               for(auto i=unordered_plugins.begin(); i!=unordered_plugins.end(); )
+               {
+                       const vector<string> &required = i->plugin->get_required_plugins();
+                       if(all_of(required.begin(), required.end(), have_plugin))
+                       {
+                               plugins.push_back(move(*i));
+                               i = unordered_plugins.erase(i);
+                               any_added = true;
+                       }
+                       else
+                               ++i;
+               }
+
+               if(!any_added)
+                       break;
+       }
+
+       for(const LoadedPlugin &p: unordered_plugins)
+       {
+               vector<string> missing;
+               for(const string &r: p.plugin->get_required_plugins())
+                       if(!have_plugin(r))
+                               missing.push_back(r);
+               logger->log("plugins", "Missing required plugins for plugin %s: %s",
+                       FS::basename(p.path), join(missing.begin(), missing.end()));
+       }
 }
 
 void Builder::set_architecture(const string &name)
@@ -374,6 +410,18 @@ Builder::LoadedPlugin::LoadedPlugin(LoadedPlugin &&other):
        other.plugin = 0;
 }
 
+Builder::LoadedPlugin &Builder::LoadedPlugin::operator=(LoadedPlugin &&other)
+{
+       delete plugin;
+       delete module;
+       path = move(other.path);
+       module = other.module;
+       plugin = other.plugin;
+       other.module = 0;
+       other.plugin = 0;
+       return *this;
+}
+
 Builder::LoadedPlugin::~LoadedPlugin()
 {
        delete plugin;
index 984522646b61ac335bd777ae8e1c1ef45d12a95a..d9a942b4f65000d4b4eec7071a95e6658271f792 100644 (file)
@@ -54,6 +54,7 @@ private:
 
                LoadedPlugin() = default;
                LoadedPlugin(LoadedPlugin &&);
+               LoadedPlugin &operator=(LoadedPlugin &&);
                ~LoadedPlugin();
        };
 
index 1e79fca802ac466fc3d94d2001f295e2e2aa951a..19198a88d150669eaab95e34c6447e84d3fec5fd 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef PLUGIN_H_
 #define PLUGIN_H_
 
+#include <string>
+#include <vector>
 #include "libbuilder_api.h"
 
 class Architecture;
@@ -12,11 +14,14 @@ class LIBBUILDER_API Plugin
 {
 protected:
        Builder &builder;
+       std::vector<std::string> required_plugins;
 
        Plugin(Builder &b): builder(b) { }
 public:
        virtual ~Plugin() = default;
 
+       const std::vector<std::string> &get_required_plugins() const { return required_plugins; }
+
        virtual void add_tools(Toolchain &, const Architecture &) const { }
        virtual void create_targets(SourcePackage &) const { }
 };