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();
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()));
39 base = comp->get_name();
42 for(list<string>::const_iterator i=out_suffixes.begin(); i!=out_suffixes.end(); ++i)
44 Tool *tool = builder.get_toolchain().get_tool_for_suffix(*i, true);
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);
53 primary->add_side_effect(*target);
58 throw runtime_error("No tool found for suffix "+*i);
64 Task *SourceGenerator::run(const Target &target) const
66 const SourceFile &out_src = dynamic_cast<const SourceFile &>(target);
67 const FS::Path &work_dir = out_src.get_package()->get_source_directory();
70 args.push_back(executable->get_path().str());
71 args.insert(args.end(), arguments.begin(), arguments.end());
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());
78 args.push_back(FS::relative(out_src.get_path(), work_dir).str());
80 return new ExternalTask(args, work_dir);
84 SourceGenerator::Loader::Loader(SourceGenerator &sg):
85 DataFile::ObjectLoader<SourceGenerator>(sg),
86 ConditionalLoader(sg.package, format("%s/%s", sg.package.get_name(), sg.tag))
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));
95 void SourceGenerator::Loader::argument(const string &a)
97 obj.arguments.push_back(a);
100 void SourceGenerator::Loader::command(const string &c)
102 obj.set_command((obj.package.get_source_directory()/c).str());
105 void SourceGenerator::Loader::in_suffix(const string &s)
107 obj.input_suffixes.push_back(s);
110 void SourceGenerator::Loader::out_suffix(const string &s)
112 obj.out_suffixes.push_back(s);