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):
20 Target *SourceGenerator::create_source(const Component &comp, const FS::Path &path) const
22 return new TemplateFile(builder, comp, path);
25 Target *SourceGenerator::create_target(const vector<Target *> &sources, const string &)
28 throw invalid_argument("SourceGenerator::create_target");
29 if(out_suffixes.empty())
30 throw logic_error("No output suffixes");
32 TemplateFile &tmpl = dynamic_cast<TemplateFile &>(*sources.front());
33 const Component *comp = tmpl.get_component();
34 const SourcePackage *pkg = tmpl.get_package();
37 if(processing_unit==COMPONENT)
38 base = comp->get_name();
41 subdir = FS::dirname(FS::relative(tmpl.get_path(), pkg->get_source_directory()));
42 if(processing_unit==ONE_FILE)
43 base = FS::basepart(FS::basename(tmpl.get_path()));
44 else if(processing_unit==DIRECTORY)
46 base = FS::basename(subdir);
47 subdir = FS::dirname(subdir);
52 for(const string &s: out_suffixes)
54 Tool *tool = builder.get_toolchain().get_tool_for_suffix(s, true);
57 FS::Path fn = pkg->get_temp_directory()/"generated"/subdir/(base+s);
58 Target *target = tool->create_source(*comp, fn);
59 target->set_tool(*this);
60 for(Target *t: sources)
61 target->add_dependency(*t);
63 primary->add_side_effect(*target);
68 throw runtime_error("No tool found for suffix "+s);
74 Task *SourceGenerator::_run(const SourceFile &out_src)
76 const FS::Path &work_dir = out_src.get_package()->get_source_directory();
77 const SourceGenerator &tool = dynamic_cast<const SourceGenerator &>(*out_src.get_tool());
80 args.push_back(tool.get_executable()->get_path().str());
81 args.insert(args.end(), tool.arguments.begin(), tool.arguments.end());
83 for(const Target *d: out_src.get_dependencies())
84 if(const TemplateFile *tmpl = dynamic_cast<const TemplateFile *>(d))
85 args.push_back(FS::relative(tmpl->get_path(), work_dir).str());
87 if(!tool.out_argument.empty())
88 args.push_back(tool.out_argument);
89 args.push_back(FS::relative(out_src.get_path(), work_dir).str());
91 return new ExternalTask(args, work_dir);
95 SourceGenerator::Loader::Loader(SourceGenerator &sg):
96 DataFile::ObjectLoader<SourceGenerator>(sg),
97 ConditionalLoader(sg.package, format("%s/%s", sg.package.get_name(), sg.tag))
99 add("argument", &Loader::argument);
100 add("arguments", &Loader::arguments);
101 add("command", &Loader::command);
102 add("in_suffix", &Loader::in_suffix);
103 add("out_argument", &SourceGenerator::out_argument);
104 add("out_suffix", &Loader::out_suffix);
105 add("processing_unit", static_cast<ProcessingUnit SourceGenerator::*>(&SourceGenerator::processing_unit));
108 void SourceGenerator::Loader::argument(const string &a)
110 obj.arguments.push_back(a);
113 void SourceGenerator::Loader::arguments(const vector<string> &a)
115 obj.arguments.insert(obj.arguments.end(), a.begin(), a.end());
118 void SourceGenerator::Loader::command(const string &c)
120 if(c.find('/')!=string::npos)
121 obj.set_command((obj.package.get_source_directory()/c).str());
126 void SourceGenerator::Loader::in_suffix(const string &s)
128 obj.input_suffixes.push_back(s);
131 void SourceGenerator::Loader::out_suffix(const string &s)
133 obj.out_suffixes.push_back(s);