]> git.tdb.fi Git - builder.git/blob - source/sourcegenerator.cpp
Redesign how tools are run
[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         set_run(&_run);
18 }
19
20 Target *SourceGenerator::create_source(const Component &comp, const FS::Path &path) const
21 {
22         return new TemplateFile(builder, comp, path);
23 }
24
25 Target *SourceGenerator::create_target(const vector<Target *> &sources, const string &)
26 {
27         if(sources.empty())
28                 throw invalid_argument("SourceGenerator::create_target");
29         if(out_suffixes.empty())
30                 throw logic_error("No output suffixes");
31
32         TemplateFile &tmpl = dynamic_cast<TemplateFile &>(*sources.front());
33         const Component *comp = tmpl.get_component();
34         const SourcePackage *pkg = tmpl.get_package();
35         FS::Path subdir;
36         string base;
37         if(processing_unit==COMPONENT)
38                 base = comp->get_name();
39         else
40         {
41                 subdir = FS::dirname(FS::relative(tmpl.get_path(), pkg->get_source_directory()));
42                 if(processing_unit==ONE_FILE)
43                         base = FS::basepart(FS::basename(tmpl.get_path()));
44                 else if(processing_unit==DIRECTORY)
45                 {
46                         base = FS::basename(subdir);
47                         subdir = FS::dirname(subdir);
48                 }
49         }
50
51         Target *primary = 0;
52         for(const string &s: out_suffixes)
53         {
54                 Tool *tool = builder.get_toolchain().get_tool_for_suffix(s, true);
55                 if(tool)
56                 {
57                         FS::Path fn = pkg->get_temp_directory()/"generated"/subdir/(base+s);
58                         Target *target = tool->create_source(*comp, fn);
59                         target->set_tool(*this);
60                         for(Target *t: sources)
61                                 target->add_dependency(*t);
62                         if(primary)
63                                 primary->add_side_effect(*target);
64                         else
65                                 primary = target;
66                 }
67                 else
68                         throw runtime_error("No tool found for suffix "+s);
69         }
70
71         return primary;
72 }
73
74 Task *SourceGenerator::_run(const SourceFile &out_src)
75 {
76         const FS::Path &work_dir = out_src.get_package()->get_source_directory();
77         const SourceGenerator &tool = dynamic_cast<const SourceGenerator &>(*out_src.get_tool());
78
79         vector<string> args;
80         args.push_back(tool.get_executable()->get_path().str());
81         args.insert(args.end(), tool.arguments.begin(), tool.arguments.end());
82
83         for(const Target *d: out_src.get_dependencies())
84                 if(const TemplateFile *tmpl = dynamic_cast<const TemplateFile *>(d))
85                         args.push_back(FS::relative(tmpl->get_path(), work_dir).str());
86
87         if(!tool.out_argument.empty())
88                 args.push_back(tool.out_argument);
89         args.push_back(FS::relative(out_src.get_path(), work_dir).str());
90
91         return new ExternalTask(args, work_dir);
92 }
93
94
95 SourceGenerator::Loader::Loader(SourceGenerator &sg):
96         DataFile::ObjectLoader<SourceGenerator>(sg),
97         ConditionalLoader(sg.package, format("%s/%s", sg.package.get_name(), sg.tag))
98 {
99         add("argument",   &Loader::argument);
100         add("arguments",  &Loader::arguments);
101         add("command",    &Loader::command);
102         add("in_suffix",  &Loader::in_suffix);
103         add("out_argument", &SourceGenerator::out_argument);
104         add("out_suffix", &Loader::out_suffix);
105         add("processing_unit", static_cast<ProcessingUnit SourceGenerator::*>(&SourceGenerator::processing_unit));
106 }
107
108 void SourceGenerator::Loader::argument(const string &a)
109 {
110         obj.arguments.push_back(a);
111 }
112
113 void SourceGenerator::Loader::arguments(const vector<string> &a)
114 {
115         obj.arguments.insert(obj.arguments.end(), a.begin(), a.end());
116 }
117
118 void SourceGenerator::Loader::command(const string &c)
119 {
120         if(c.find('/')!=string::npos)
121                 obj.set_command((obj.package.get_source_directory()/c).str());
122         else
123                 obj.set_command(c);
124 }
125
126 void SourceGenerator::Loader::in_suffix(const string &s)
127 {
128         obj.input_suffixes.push_back(s);
129 }
130
131 void SourceGenerator::Loader::out_suffix(const string &s)
132 {
133         obj.out_suffixes.push_back(s);
134 }