]> git.tdb.fi Git - libs/game.git/blob - tools/builder-plugin/gamesetupgenerator.cpp
Add import functionality for setup modules
[libs/game.git] / tools / builder-plugin / gamesetupgenerator.cpp
1 #include "gamesetupgenerator.h"
2 #include <msp/builder/builder.h>
3 #include <msp/fs/utils.h>
4 #include "gamesetupdefinitions.h"
5
6 using namespace std;
7 using namespace Msp;
8
9 GameSetupGenerator::GameSetupGenerator(Builder &b):
10         Tool(b, "MGSG")
11 {
12         input_suffixes.push_back(".mgs");
13
14         set_command("mspgame-setupgen");
15         set_run_external(_run);
16 }
17
18 Target *GameSetupGenerator::create_source(const Component &comp, const FS::Path &path) const
19 {
20         return new GameSetupDefinitions(builder, comp, path);
21 }
22
23 Target *GameSetupGenerator::create_target(const vector<Target *> &sources, const string &)
24 {
25         if(sources.size()!=1)
26                 throw invalid_argument("GameSetupGenerator::create_target");
27
28         GameSetupDefinitions &defs = dynamic_cast<GameSetupDefinitions &>(*sources.front());
29
30         const Component &comp = *defs.get_component();
31         const SourcePackage &spkg = comp.get_package();
32         FS::Path dir = spkg.get_temp_directory()/"generated";
33         dir /= FS::dirname(FS::relative(defs.get_path(), spkg.get_source_directory()));
34
35         Tool &compiler = builder.get_toolchain().get_tool("CC");
36         string base = FS::basepart(FS::basename(defs.get_path()));
37         Target *source = compiler.create_source(comp, dir/(base+".cpp"));
38         source->add_dependency(defs);
39         source->set_tool(*this);
40
41         Target *header = compiler.create_source(comp, dir/(base+".h"));
42         header->add_dependency(defs);
43         source->add_side_effect(*header);
44
45         return source;
46 }
47
48 ExternalTask::Arguments GameSetupGenerator::_run(const CSourceFile &out_src, FS::Path &work_dir)
49 {
50         const Tool &tool = *out_src.get_tool();
51
52         ExternalTask::Arguments argv;
53         argv.push_back(tool.get_executable()->get_path().str());
54
55         BuildInfo binfo;
56         out_src.collect_build_info(binfo);
57
58         for(const FS::Path &i: binfo.incpath)
59                 argv.push_back("-I"+i.str());
60
61         for(const Target *d: out_src.get_dependencies())
62                 if(const GameSetupDefinitions *defs = dynamic_cast<const GameSetupDefinitions *>(d))
63                         argv.push_back(FS::relative(defs->get_path(), work_dir).str());
64
65         argv.push_back("-o");
66         argv.push_back(FS::relative(out_src.get_path(), work_dir).str());
67
68         return argv;
69 }