]> git.tdb.fi Git - libs/game.git/commitdiff
Create a builder plugin for the setup generator tool
authorMikko Rasa <tdb@tdb.fi>
Sat, 7 Jan 2023 13:25:13 +0000 (15:25 +0200)
committerMikko Rasa <tdb@tdb.fi>
Sat, 7 Jan 2023 13:25:13 +0000 (15:25 +0200)
Build
tools/builder-plugin/gameplugin.cpp [new file with mode: 0644]
tools/builder-plugin/gameplugin.h [new file with mode: 0644]
tools/builder-plugin/gamesetupdefinitions.cpp [new file with mode: 0644]
tools/builder-plugin/gamesetupdefinitions.h [new file with mode: 0644]
tools/builder-plugin/gamesetupgenerator.cpp [new file with mode: 0644]
tools/builder-plugin/gamesetupgenerator.h [new file with mode: 0644]
tools/builder-plugin/gametools.cpp [new file with mode: 0644]
tools/builder-plugin/gametools.h [new file with mode: 0644]

diff --git a/Build b/Build
index 7548a1a1e678eccf3e3d7c48fac384875edbc960..f7385bedfb2568deb6abbd0ca20a6a108325bf65 100644 (file)
--- a/Build
+++ b/Build
@@ -40,4 +40,15 @@ package "mspgame"
                source "tools/setupgen";
                install true;
        };
+
+       module "builder-gametools"
+       {
+               source "tools/builder-plugin";
+               require "builder";
+               install true;
+               install_map
+               {
+                       map "." "lib/builder";
+               };
+       };
 };
diff --git a/tools/builder-plugin/gameplugin.cpp b/tools/builder-plugin/gameplugin.cpp
new file mode 100644 (file)
index 0000000..7b01b20
--- /dev/null
@@ -0,0 +1,23 @@
+#include "gameplugin.h"
+#include <msp/builder/toolchain.h>
+#include "gametools.h"
+
+void GamePlugin::add_tools(Toolchain &toolchain, const Architecture &) const
+{
+       toolchain.add_toolchain(new GameTools(builder));
+}
+
+
+#if defined(_WIN32)
+#define GAMETOOLS_API __declspec(dllexport)
+#elif defined(__GNUC__)
+#define GAMETOOLS_API __attribute__((visibility("default")))
+#else
+#define GAMETOOLS_API
+#endif
+
+extern "C"
+GAMETOOLS_API Plugin *create_plugin(Builder &builder)
+{
+       return new GamePlugin(builder);
+}
diff --git a/tools/builder-plugin/gameplugin.h b/tools/builder-plugin/gameplugin.h
new file mode 100644 (file)
index 0000000..3e3cdae
--- /dev/null
@@ -0,0 +1,14 @@
+#ifndef GAMEPLUGIN_H_
+#define GAMEPLUGIN_H_
+
+#include <msp/builder/plugin.h>
+
+class GamePlugin: public Plugin
+{
+public:
+       GamePlugin(Builder &b): Plugin(b) { }
+
+       void add_tools(Toolchain &, const Architecture &) const override;
+};
+
+#endif
diff --git a/tools/builder-plugin/gamesetupdefinitions.cpp b/tools/builder-plugin/gamesetupdefinitions.cpp
new file mode 100644 (file)
index 0000000..a6abd23
--- /dev/null
@@ -0,0 +1,7 @@
+#include "gamesetupdefinitions.h"
+
+using namespace Msp;
+
+GameSetupDefinitions::GameSetupDefinitions(Builder &b, const Component &c, const FS::Path &p):
+       SourceFile(b, c, p)
+{ }
diff --git a/tools/builder-plugin/gamesetupdefinitions.h b/tools/builder-plugin/gamesetupdefinitions.h
new file mode 100644 (file)
index 0000000..090b412
--- /dev/null
@@ -0,0 +1,14 @@
+#ifndef GAMESETUPDEFINITIONS_H_
+#define GAMESETUPDEFINITIONS_H_
+
+#include <msp/builder/sourcefile.h>
+
+class GameSetupDefinitions: public SourceFile
+{
+public:
+       GameSetupDefinitions(Builder &, const Component &, const Msp::FS::Path &);
+
+       const char *get_type() const override { return "GameSetupDefinitions"; }
+};
+
+#endif
diff --git a/tools/builder-plugin/gamesetupgenerator.cpp b/tools/builder-plugin/gamesetupgenerator.cpp
new file mode 100644 (file)
index 0000000..3302c21
--- /dev/null
@@ -0,0 +1,63 @@
+#include "gamesetupgenerator.h"
+#include <msp/builder/builder.h>
+#include <msp/fs/utils.h>
+#include "gamesetupdefinitions.h"
+
+using namespace std;
+using namespace Msp;
+
+GameSetupGenerator::GameSetupGenerator(Builder &b):
+       Tool(b, "MGSG")
+{
+       input_suffixes.push_back(".mgs");
+
+       set_command("mspgame-setupgen");
+       set_run_external(_run);
+}
+
+Target *GameSetupGenerator::create_source(const Component &comp, const FS::Path &path) const
+{
+       return new GameSetupDefinitions(builder, comp, path);
+}
+
+Target *GameSetupGenerator::create_target(const vector<Target *> &sources, const string &)
+{
+       if(sources.size()!=1)
+               throw invalid_argument("GameSetupGenerator::create_target");
+
+       GameSetupDefinitions &defs = dynamic_cast<GameSetupDefinitions &>(*sources.front());
+
+       const Component &comp = *defs.get_component();
+       const SourcePackage &spkg = comp.get_package();
+       FS::Path dir = spkg.get_temp_directory()/"generated";
+       dir /= FS::dirname(FS::relative(defs.get_path(), spkg.get_source_directory()));
+
+       Tool &compiler = builder.get_toolchain().get_tool("CC");
+       string base = FS::basepart(FS::basename(defs.get_path()));
+       Target *source = compiler.create_source(comp, dir/(base+".cpp"));
+       source->add_dependency(defs);
+       source->set_tool(*this);
+
+       Target *header = compiler.create_source(comp, dir/(base+".h"));
+       header->add_dependency(defs);
+       source->add_side_effect(*header);
+
+       return source;
+}
+
+ExternalTask::Arguments GameSetupGenerator::_run(const CSourceFile &out_src, FS::Path &work_dir)
+{
+       const Tool &tool = *out_src.get_tool();
+
+       ExternalTask::Arguments argv;
+       argv.push_back(tool.get_executable()->get_path().str());
+
+       for(const Target *d: out_src.get_dependencies())
+               if(const GameSetupDefinitions *defs = dynamic_cast<const GameSetupDefinitions *>(d))
+                       argv.push_back(FS::relative(defs->get_path(), work_dir).str());
+
+       argv.push_back("-o");
+       argv.push_back(FS::relative(out_src.get_path(), work_dir).str());
+
+       return argv;
+}
diff --git a/tools/builder-plugin/gamesetupgenerator.h b/tools/builder-plugin/gamesetupgenerator.h
new file mode 100644 (file)
index 0000000..8f3a881
--- /dev/null
@@ -0,0 +1,19 @@
+#ifndef GAMESETUPGENERATOR_H_
+#define GAMESETUPGENERATOR_H_
+
+#include <msp/builder/csourcefile.h>
+#include <msp/builder/tool.h>
+
+class GameSetupGenerator: public Tool
+{
+public:
+       GameSetupGenerator(Builder &);
+
+       Target *create_source(const Component &, const Msp::FS::Path &) const override;
+       Target *create_target(const std::vector<Target *> &, const std::string &) override;
+
+private:
+       static ExternalTask::Arguments _run(const CSourceFile &, Msp::FS::Path &);
+};
+
+#endif
diff --git a/tools/builder-plugin/gametools.cpp b/tools/builder-plugin/gametools.cpp
new file mode 100644 (file)
index 0000000..33a65bf
--- /dev/null
@@ -0,0 +1,8 @@
+#include "gametools.h"
+#include "gamesetupgenerator.h"
+
+GameTools::GameTools(Builder &builder):
+       Toolchain("game", 10)
+{
+       add_tool(new GameSetupGenerator(builder));
+}
diff --git a/tools/builder-plugin/gametools.h b/tools/builder-plugin/gametools.h
new file mode 100644 (file)
index 0000000..53be4e8
--- /dev/null
@@ -0,0 +1,13 @@
+#ifndef GAMETOOLS_H_
+#define GAMETOOLS_H_
+
+#include <msp/builder/builder.h>
+#include <msp/builder/toolchain.h>
+
+class GameTools: public Toolchain
+{
+public:
+       GameTools(Builder &);
+};
+
+#endif