From: Mikko Rasa Date: Sat, 7 Jan 2023 13:25:13 +0000 (+0200) Subject: Create a builder plugin for the setup generator tool X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=bb1c22d58cc71e68555a144ac884137c775ef427;p=libs%2Fgame.git Create a builder plugin for the setup generator tool --- diff --git a/Build b/Build index 7548a1a..f7385be 100644 --- 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 index 0000000..7b01b20 --- /dev/null +++ b/tools/builder-plugin/gameplugin.cpp @@ -0,0 +1,23 @@ +#include "gameplugin.h" +#include +#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 index 0000000..3e3cdae --- /dev/null +++ b/tools/builder-plugin/gameplugin.h @@ -0,0 +1,14 @@ +#ifndef GAMEPLUGIN_H_ +#define GAMEPLUGIN_H_ + +#include + +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 index 0000000..a6abd23 --- /dev/null +++ b/tools/builder-plugin/gamesetupdefinitions.cpp @@ -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 index 0000000..090b412 --- /dev/null +++ b/tools/builder-plugin/gamesetupdefinitions.h @@ -0,0 +1,14 @@ +#ifndef GAMESETUPDEFINITIONS_H_ +#define GAMESETUPDEFINITIONS_H_ + +#include + +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 index 0000000..3302c21 --- /dev/null +++ b/tools/builder-plugin/gamesetupgenerator.cpp @@ -0,0 +1,63 @@ +#include "gamesetupgenerator.h" +#include +#include +#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 &sources, const string &) +{ + if(sources.size()!=1) + throw invalid_argument("GameSetupGenerator::create_target"); + + GameSetupDefinitions &defs = dynamic_cast(*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(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 index 0000000..8f3a881 --- /dev/null +++ b/tools/builder-plugin/gamesetupgenerator.h @@ -0,0 +1,19 @@ +#ifndef GAMESETUPGENERATOR_H_ +#define GAMESETUPGENERATOR_H_ + +#include +#include + +class GameSetupGenerator: public Tool +{ +public: + GameSetupGenerator(Builder &); + + Target *create_source(const Component &, const Msp::FS::Path &) const override; + Target *create_target(const std::vector &, 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 index 0000000..33a65bf --- /dev/null +++ b/tools/builder-plugin/gametools.cpp @@ -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 index 0000000..53be4e8 --- /dev/null +++ b/tools/builder-plugin/gametools.h @@ -0,0 +1,13 @@ +#ifndef GAMETOOLS_H_ +#define GAMETOOLS_H_ + +#include +#include + +class GameTools: public Toolchain +{ +public: + GameTools(Builder &); +}; + +#endif