]> git.tdb.fi Git - builder.git/blob - source/sourcegenerator.cpp
Refactor transitive dependencies to work on all targets
[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.empty())
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;
34         if(processing_unit==ONE_FILE)
35                 base = FS::basepart(FS::basename(tmpl.get_path()));
36         else if(processing_unit==DIRECTORY)
37                 base = FS::basename(FS::dirname(tmpl.get_path()));
38         else
39                 base = comp->get_name();
40
41         Target *primary = 0;
42         for(list<string>::const_iterator i=out_suffixes.begin(); i!=out_suffixes.end(); ++i)
43         {
44                 Tool *tool = builder.get_toolchain().get_tool_for_suffix(*i, true);
45                 if(tool)
46                 {
47                         FS::Path fn = pkg->get_temp_directory()/comp->get_name()/(base+*i);
48                         Target *target = tool->create_source(*comp, fn);
49                         target->set_tool(*this);
50                         for(list<Target *>::const_iterator j=sources.begin(); j!=sources.end(); ++j)
51                                 target->add_dependency(**j);
52                         if(primary)
53                                 primary->add_side_effect(*target);
54                         else
55                                 primary = target;
56                 }
57                 else
58                         throw runtime_error("No tool found for suffix "+*i);
59         }
60
61         return primary;
62 }
63
64 Task *SourceGenerator::run(const Target &target) const
65 {
66         const SourceFile &out_src = dynamic_cast<const SourceFile &>(target);
67         const FS::Path &work_dir = out_src.get_package()->get_source_directory();
68
69         vector<string> args;
70         args.push_back(executable->get_path().str());
71         args.insert(args.end(), arguments.begin(), arguments.end());
72
73         const Target::Dependencies &deps = target.get_dependencies();
74         for(Target::Dependencies::const_iterator i=deps.begin(); i!=deps.end(); ++i)
75                 if(const TemplateFile *tmpl = dynamic_cast<const TemplateFile *>(*i))
76                         args.push_back(FS::relative(tmpl->get_path(), work_dir).str());
77
78         if(!out_argument.empty())
79                 args.push_back(out_argument);
80         args.push_back(FS::relative(out_src.get_path(), work_dir).str());
81
82         return new ExternalTask(args, work_dir);
83 }
84
85
86 SourceGenerator::Loader::Loader(SourceGenerator &sg):
87         DataFile::ObjectLoader<SourceGenerator>(sg),
88         ConditionalLoader(sg.package, format("%s/%s", sg.package.get_name(), sg.tag))
89 {
90         add("argument",   &Loader::argument);
91         add("arguments",  &Loader::arguments);
92         add("command",    &Loader::command);
93         add("in_suffix",  &Loader::in_suffix);
94         add("out_argument", &SourceGenerator::out_argument);
95         add("out_suffix", &Loader::out_suffix);
96         add("processing_unit", static_cast<ProcessingUnit SourceGenerator::*>(&SourceGenerator::processing_unit));
97 }
98
99 void SourceGenerator::Loader::argument(const string &a)
100 {
101         obj.arguments.push_back(a);
102 }
103
104 void SourceGenerator::Loader::arguments(const vector<string> &a)
105 {
106         obj.arguments.insert(obj.arguments.end(), a.begin(), a.end());
107 }
108
109 void SourceGenerator::Loader::command(const string &c)
110 {
111         if(c.find('/')!=string::npos)
112                 obj.set_command((obj.package.get_source_directory()/c).str());
113         else
114                 obj.set_command(c);
115 }
116
117 void SourceGenerator::Loader::in_suffix(const string &s)
118 {
119         obj.input_suffixes.push_back(s);
120 }
121
122 void SourceGenerator::Loader::out_suffix(const string &s)
123 {
124         obj.out_suffixes.push_back(s);
125 }