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