]> git.tdb.fi Git - builder.git/blob - source/sourcegenerator.cpp
Allow specifying fixed arguments for source generators
[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
28         TemplateFile &tmpl = dynamic_cast<TemplateFile &>(*sources.front());
29         const Component *comp = tmpl.get_component();
30         const SourcePackage *pkg = tmpl.get_package();
31         string base = FS::basepart(FS::basename(tmpl.get_path()));
32
33         Target *primary = 0;
34         for(list<string>::const_iterator i=out_suffixes.begin(); i!=out_suffixes.end(); ++i)
35         {
36                 Tool *tool = builder.get_toolchain().get_tool_for_suffix(*i, true);
37                 if(tool)
38                 {
39                         FS::Path fn = pkg->get_temp_directory()/comp->get_name()/(base+*i);
40                         Target *target = tool->create_source(*comp, fn);
41                         target->set_tool(*this);
42                         target->add_dependency(tmpl);
43                         if(primary)
44                                 primary->add_side_effect(*target);
45                         else
46                                 primary = target;
47                 }
48         }
49
50         return primary;
51 }
52
53 Task *SourceGenerator::run(const Target &target) const
54 {
55         const SourceFile &out_src = dynamic_cast<const SourceFile &>(target);
56         const FS::Path &work_dir = out_src.get_package()->get_source_directory();
57
58         vector<string> args;
59         args.push_back(executable->get_path().str());
60         args.insert(args.end(), arguments.begin(), arguments.end());
61
62         const Target::Dependencies &deps = target.get_dependencies();
63         for(Target::Dependencies::const_iterator i=deps.begin(); i!=deps.end(); ++i)
64                 if(const TemplateFile *tmpl = dynamic_cast<const TemplateFile *>(*i))
65                         args.push_back(FS::relative(tmpl->get_path(), work_dir).str());
66
67         args.push_back(FS::relative(out_src.get_path(), work_dir).str());
68
69         return new ExternalTask(args, work_dir);
70 }
71
72
73 SourceGenerator::Loader::Loader(SourceGenerator &sg):
74         DataFile::ObjectLoader<SourceGenerator>(sg),
75         ConditionalLoader(sg.package, format("%s/%s", sg.package.get_name(), sg.tag))
76 {
77         add("argument",   &Loader::argument);
78         add("command",    &Loader::command);
79         add("in_suffix",  &Loader::in_suffix);
80         add("out_suffix", &Loader::out_suffix);
81 }
82
83 void SourceGenerator::Loader::argument(const string &a)
84 {
85         obj.arguments.push_back(a);
86 }
87
88 void SourceGenerator::Loader::command(const string &c)
89 {
90         obj.set_command((obj.package.get_source_directory()/c).str());
91 }
92
93 void SourceGenerator::Loader::in_suffix(const string &s)
94 {
95         obj.input_suffixes.push_back(s);
96 }
97
98 void SourceGenerator::Loader::out_suffix(const string &s)
99 {
100         obj.out_suffixes.push_back(s);
101 }