1 #include <msp/fs/utils.h>
2 #include <msp/strings/format.h>
4 #include "executable.h"
5 #include "sourcegenerator.h"
6 #include "sourcepackage.h"
7 #include "templatefile.h"
12 SourceGenerator::SourceGenerator(Builder &b, const SourcePackage &p, const string &t):
16 set_run_external(&_run);
19 Target *SourceGenerator::create_source(const Component &comp, const FS::Path &path) const
21 return new TemplateFile(builder, comp, path);
24 Target *SourceGenerator::create_target(const vector<Target *> &sources, const string &)
27 throw invalid_argument("SourceGenerator::create_target");
28 if(out_suffixes.empty())
29 throw logic_error("No output suffixes");
31 TemplateFile &tmpl = dynamic_cast<TemplateFile &>(*sources.front());
32 const Component *comp = tmpl.get_component();
33 const SourcePackage *pkg = tmpl.get_package();
36 if(processing_unit==COMPONENT)
37 base = comp->get_name();
40 subdir = FS::dirname(FS::relative(tmpl.get_path(), pkg->get_source_directory()));
41 if(processing_unit==ONE_FILE)
42 base = FS::basepart(FS::basename(tmpl.get_path()));
43 else if(processing_unit==DIRECTORY)
45 base = FS::basename(subdir);
46 subdir = FS::dirname(subdir);
51 for(const string &s: out_suffixes)
53 Tool *tool = builder.get_toolchain().get_tool_for_suffix(s, true);
56 FS::Path fn = pkg->get_temp_directory()/"generated"/subdir/(base+s);
57 Target *target = tool->create_source(*comp, fn);
58 target->set_tool(*this);
59 for(Target *t: sources)
60 target->add_dependency(*t);
62 primary->add_side_effect(*target);
67 throw runtime_error("No tool found for suffix "+s);
73 ExternalTask::Arguments SourceGenerator::_run(const SourceFile &out_src, FS::Path &work_dir)
75 const SourceGenerator &tool = dynamic_cast<const SourceGenerator &>(*out_src.get_tool());
78 args.push_back(tool.get_executable()->get_path().str());
79 args.insert(args.end(), tool.arguments.begin(), tool.arguments.end());
81 for(const Target *d: out_src.get_dependencies())
82 if(const TemplateFile *tmpl = dynamic_cast<const TemplateFile *>(d))
83 args.push_back(FS::relative(tmpl->get_path(), work_dir).str());
85 if(!tool.out_argument.empty())
86 args.push_back(tool.out_argument);
87 args.push_back(FS::relative(out_src.get_path(), work_dir).str());
93 SourceGenerator::Loader::Loader(SourceGenerator &sg):
94 DataFile::ObjectLoader<SourceGenerator>(sg),
95 ConditionalLoader(sg.package, format("%s/%s", sg.package.get_name(), sg.tag))
97 add("argument", &Loader::argument);
98 add("arguments", &Loader::arguments);
99 add("command", &Loader::command);
100 add("in_suffix", &Loader::in_suffix);
101 add("out_argument", &SourceGenerator::out_argument);
102 add("out_suffix", &Loader::out_suffix);
103 add("processing_unit", static_cast<ProcessingUnit SourceGenerator::*>(&SourceGenerator::processing_unit));
106 void SourceGenerator::Loader::argument(const string &a)
108 obj.arguments.push_back(a);
111 void SourceGenerator::Loader::arguments(const vector<string> &a)
113 obj.arguments.insert(obj.arguments.end(), a.begin(), a.end());
116 void SourceGenerator::Loader::command(const string &c)
118 if(c.find('/')!=string::npos)
119 obj.set_command((obj.package.get_source_directory()/c).str());
124 void SourceGenerator::Loader::in_suffix(const string &s)
126 obj.input_suffixes.push_back(s);
129 void SourceGenerator::Loader::out_suffix(const string &s)
131 obj.out_suffixes.push_back(s);