]> git.tdb.fi Git - builder.git/blob - source/sourcegenerator.cpp
Only use source files that are actually files
[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         args.push_back(FS::relative(out_src.get_path(), work_dir).str());
79
80         return new ExternalTask(args, work_dir);
81 }
82
83
84 SourceGenerator::Loader::Loader(SourceGenerator &sg):
85         DataFile::ObjectLoader<SourceGenerator>(sg),
86         ConditionalLoader(sg.package, format("%s/%s", sg.package.get_name(), sg.tag))
87 {
88         add("argument",   &Loader::argument);
89         add("command",    &Loader::command);
90         add("in_suffix",  &Loader::in_suffix);
91         add("out_suffix", &Loader::out_suffix);
92         add("processing_unit", static_cast<ProcessingUnit SourceGenerator::*>(&SourceGenerator::processing_unit));
93 }
94
95 void SourceGenerator::Loader::argument(const string &a)
96 {
97         obj.arguments.push_back(a);
98 }
99
100 void SourceGenerator::Loader::command(const string &c)
101 {
102         obj.set_command((obj.package.get_source_directory()/c).str());
103 }
104
105 void SourceGenerator::Loader::in_suffix(const string &s)
106 {
107         obj.input_suffixes.push_back(s);
108 }
109
110 void SourceGenerator::Loader::out_suffix(const string &s)
111 {
112         obj.out_suffixes.push_back(s);
113 }