]> git.tdb.fi Git - builder.git/blobdiff - source/lib/builder.cpp
Add an interface for dynamically loaded plugins
[builder.git] / source / lib / builder.cpp
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),