1 #include <msp/fs/utils.h>
2 #include <msp/strings/format.h>
4 #include "executable.h"
5 #include "externaltask.h"
6 #include "sourcegenerator.h"
7 #include "sourcepackage.h"
8 #include "templatefile.h"
13 SourceGenerator::SourceGenerator(Builder &b, const SourcePackage &p, const string &t):
18 Target *SourceGenerator::create_source(const Component &comp, const FS::Path &path) const
20 return new TemplateFile(builder, comp, path);
23 Target *SourceGenerator::create_target(const list<Target *> &sources, const string &)
26 throw invalid_argument("SourceGenerator::create_target");
27 if(out_suffixes.empty())
28 throw logic_error("No output suffixes");
30 TemplateFile &tmpl = dynamic_cast<TemplateFile &>(*sources.front());
31 const Component *comp = tmpl.get_component();
32 const SourcePackage *pkg = tmpl.get_package();
35 if(processing_unit==COMPONENT)
36 base = comp->get_name();
39 subdir = FS::dirname(FS::relative(tmpl.get_path(), pkg->get_source_directory()));
40 if(processing_unit==ONE_FILE)
41 base = FS::basepart(FS::basename(tmpl.get_path()));
42 else if(processing_unit==DIRECTORY)
44 base = FS::basename(subdir);
45 subdir = FS::dirname(subdir);
50 for(list<string>::const_iterator i=out_suffixes.begin(); i!=out_suffixes.end(); ++i)
52 Tool *tool = builder.get_toolchain().get_tool_for_suffix(*i, true);
55 FS::Path fn = pkg->get_temp_directory()/"generated"/subdir/(base+*i);
56 Target *target = tool->create_source(*comp, fn);
57 target->set_tool(*this);
58 for(list<Target *>::const_iterator j=sources.begin(); j!=sources.end(); ++j)
59 target->add_dependency(**j);
61 primary->add_side_effect(*target);
66 throw runtime_error("No tool found for suffix "+*i);
72 Task *SourceGenerator::run(const Target &target) const
74 const SourceFile &out_src = dynamic_cast<const SourceFile &>(target);
75 const FS::Path &work_dir = out_src.get_package()->get_source_directory();
78 args.push_back(executable->get_path().str());
79 args.insert(args.end(), arguments.begin(), arguments.end());
81 const Target::Dependencies &deps = target.get_dependencies();
82 for(Target::Dependencies::const_iterator i=deps.begin(); i!=deps.end(); ++i)
83 if(const TemplateFile *tmpl = dynamic_cast<const TemplateFile *>(*i))
84 args.push_back(FS::relative(tmpl->get_path(), work_dir).str());
86 if(!out_argument.empty())
87 args.push_back(out_argument);
88 args.push_back(FS::relative(out_src.get_path(), work_dir).str());
90 return new ExternalTask(args, work_dir);
94 SourceGenerator::Loader::Loader(SourceGenerator &sg):
95 DataFile::ObjectLoader<SourceGenerator>(sg),
96 ConditionalLoader(sg.package, format("%s/%s", sg.package.get_name(), sg.tag))
98 add("argument", &Loader::argument);
99 add("arguments", &Loader::arguments);
100 add("command", &Loader::command);
101 add("in_suffix", &Loader::in_suffix);
102 add("out_argument", &SourceGenerator::out_argument);
103 add("out_suffix", &Loader::out_suffix);
104 add("processing_unit", static_cast<ProcessingUnit SourceGenerator::*>(&SourceGenerator::processing_unit));
107 void SourceGenerator::Loader::argument(const string &a)
109 obj.arguments.push_back(a);
112 void SourceGenerator::Loader::arguments(const vector<string> &a)
114 obj.arguments.insert(obj.arguments.end(), a.begin(), a.end());
117 void SourceGenerator::Loader::command(const string &c)
119 if(c.find('/')!=string::npos)
120 obj.set_command((obj.package.get_source_directory()/c).str());
125 void SourceGenerator::Loader::in_suffix(const string &s)
127 obj.input_suffixes.push_back(s);
130 void SourceGenerator::Loader::out_suffix(const string &s)
132 obj.out_suffixes.push_back(s);