1 #include <msp/fs/utils.h>
3 #include "executable.h"
4 #include "externaltask.h"
5 #include "sourcegenerator.h"
6 #include "sourcepackage.h"
7 #include "templatefile.h"
12 SourceGenerator::SourceGenerator(Builder &b, const SourcePackage &p, const string &t):
17 Target *SourceGenerator::create_source(const Component &comp, const FS::Path &path) const
19 return new TemplateFile(builder, comp, path);
22 Target *SourceGenerator::create_target(const list<Target *> &sources, const string &)
25 throw invalid_argument("SourceGenerator::create_target");
27 TemplateFile &tmpl = dynamic_cast<TemplateFile &>(*sources.front());
28 const Component *comp = tmpl.get_component();
29 const SourcePackage *pkg = tmpl.get_package();
30 string base = FS::basepart(FS::basename(tmpl.get_path()));
33 for(list<string>::const_iterator i=out_suffixes.begin(); i!=out_suffixes.end(); ++i)
35 Tool *tool = builder.get_toolchain().get_tool_for_suffix(*i, true);
38 FS::Path fn = pkg->get_temp_directory()/comp->get_name()/(base+*i);
39 Target *target = tool->create_source(*comp, fn);
40 target->set_tool(*this);
41 target->add_dependency(tmpl);
43 primary->add_side_effect(*target);
52 void SourceGenerator::do_prepare()
54 FS::Path exe_fn = package.get_source_directory()/command;
55 executable = builder.get_vfs().get_target(exe_fn);
57 executable = new Executable(builder, exe_fn);
60 Task *SourceGenerator::run(const Target &target) const
62 const SourceFile &out_src = dynamic_cast<const SourceFile &>(target);
63 const FS::Path &work_dir = out_src.get_package()->get_source_directory();
66 args.push_back(executable->get_path().str());
68 const Target::Dependencies &deps = target.get_dependencies();
69 for(Target::Dependencies::const_iterator i=deps.begin(); i!=deps.end(); ++i)
70 if(const TemplateFile *tmpl = dynamic_cast<const TemplateFile *>(*i))
71 args.push_back(FS::relative(tmpl->get_path(), work_dir).str());
73 args.push_back(FS::relative(out_src.get_path(), work_dir).str());
75 return new ExternalTask(args, work_dir);
79 SourceGenerator::Loader::Loader(SourceGenerator &sg):
80 DataFile::ObjectLoader<SourceGenerator>(sg)
82 add("command", &Loader::command);
83 add("in_suffix", &Loader::in_suffix);
84 add("out_suffix", &Loader::out_suffix);
87 void SourceGenerator::Loader::command(const string &c)
92 void SourceGenerator::Loader::in_suffix(const string &s)
94 obj.input_suffixes.push_back(s);
97 void SourceGenerator::Loader::out_suffix(const string &s)
99 obj.out_suffixes.push_back(s);