]> git.tdb.fi Git - builder.git/blob - source/sourcegenerator.cpp
4f757217958a7acf8eb9a3a981a985d579e60ac3
[builder.git] / source / sourcegenerator.cpp
1 #include <msp/fs/utils.h>
2 #include "builder.h"
3 #include "executable.h"
4 #include "externaltask.h"
5 #include "sourcegenerator.h"
6 #include "sourcepackage.h"
7 #include "templatefile.h"
8
9 using namespace std;
10 using namespace Msp;
11
12 SourceGenerator::SourceGenerator(Builder &b, const SourcePackage &p, const string &t):
13         Tool(b, t),
14         package(p)
15 { }
16
17 Target *SourceGenerator::create_source(const Component &comp, const FS::Path &path) const
18 {
19         return new TemplateFile(builder, comp, path);
20 }
21
22 Target *SourceGenerator::create_target(const list<Target *> &sources, const string &)
23 {
24         if(sources.size()!=1)
25                 throw invalid_argument("SourceGenerator::create_target");
26
27         TemplateFile &tmpl = dynamic_cast<TemplateFile &>(*sources.front());
28         const Component *comp = tmpl.get_component();
29         const SourcePackage *pkg = tmpl.get_package();
30         string base = FS::basepart(FS::basename(tmpl.get_path()));
31
32         Target *primary = 0;
33         for(list<string>::const_iterator i=out_suffixes.begin(); i!=out_suffixes.end(); ++i)
34         {
35                 Tool *tool = builder.get_toolchain().get_tool_for_suffix(*i, true);
36                 if(tool)
37                 {
38                         FS::Path fn = pkg->get_temp_directory()/comp->get_name()/(base+*i);
39                         Target *target = tool->create_source(*comp, fn);
40                         target->set_tool(*this);
41                         target->add_dependency(tmpl);
42                         if(primary)
43                                 primary->add_side_effect(*target);
44                         else
45                                 primary = target;
46                 }
47         }
48
49         return primary;
50 }
51
52 Task *SourceGenerator::run(const Target &target) const
53 {
54         const SourceFile &out_src = dynamic_cast<const SourceFile &>(target);
55         const FS::Path &work_dir = out_src.get_package()->get_source_directory();
56
57         vector<string> args;
58         args.push_back(executable->get_path().str());
59
60         const Target::Dependencies &deps = target.get_dependencies();
61         for(Target::Dependencies::const_iterator i=deps.begin(); i!=deps.end(); ++i)
62                 if(const TemplateFile *tmpl = dynamic_cast<const TemplateFile *>(*i))
63                         args.push_back(FS::relative(tmpl->get_path(), work_dir).str());
64
65         args.push_back(FS::relative(out_src.get_path(), work_dir).str());
66
67         return new ExternalTask(args, work_dir);
68 }
69
70
71 SourceGenerator::Loader::Loader(SourceGenerator &sg):
72         DataFile::ObjectLoader<SourceGenerator>(sg)
73 {
74         add("command",    &Loader::command);
75         add("in_suffix",  &Loader::in_suffix);
76         add("out_suffix", &Loader::out_suffix);
77 }
78
79 void SourceGenerator::Loader::command(const string &c)
80 {
81         obj.set_command((obj.package.get_source_directory()/c).str());
82 }
83
84 void SourceGenerator::Loader::in_suffix(const string &s)
85 {
86         obj.input_suffixes.push_back(s);
87 }
88
89 void SourceGenerator::Loader::out_suffix(const string &s)
90 {
91         obj.out_suffixes.push_back(s);
92 }