]> git.tdb.fi Git - libs/game.git/blobdiff - tools/builder-plugin/gamesetupgenerator.cpp
Create a builder plugin for the setup generator tool
[libs/game.git] / tools / builder-plugin / gamesetupgenerator.cpp
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;
+}