]> git.tdb.fi Git - builder.git/blobdiff - source/sourcegenerator.cpp
Add support for generating source files
[builder.git] / source / sourcegenerator.cpp
diff --git a/source/sourcegenerator.cpp b/source/sourcegenerator.cpp
new file mode 100644 (file)
index 0000000..c09e5c7
--- /dev/null
@@ -0,0 +1,100 @@
+#include <msp/fs/utils.h>
+#include "builder.h"
+#include "executable.h"
+#include "externaltask.h"
+#include "sourcegenerator.h"
+#include "sourcepackage.h"
+#include "templatefile.h"
+
+using namespace std;
+using namespace Msp;
+
+SourceGenerator::SourceGenerator(Builder &b, const SourcePackage &p, const string &t):
+       Tool(b, t),
+       package(p)
+{ }
+
+Target *SourceGenerator::create_source(const Component &comp, const FS::Path &path) const
+{
+       return new TemplateFile(builder, comp, path);
+}
+
+Target *SourceGenerator::create_target(const list<Target *> &sources, const string &)
+{
+       if(sources.size()!=1)
+               throw invalid_argument("SourceGenerator::create_target");
+
+       TemplateFile &tmpl = dynamic_cast<TemplateFile &>(*sources.front());
+       const Component *comp = tmpl.get_component();
+       const SourcePackage *pkg = tmpl.get_package();
+       string base = FS::basepart(FS::basename(tmpl.get_path()));
+
+       Target *primary = 0;
+       for(list<string>::const_iterator i=out_suffixes.begin(); i!=out_suffixes.end(); ++i)
+       {
+               Tool *tool = builder.get_toolchain().get_tool_for_suffix(*i, true);
+               if(tool)
+               {
+                       FS::Path fn = pkg->get_temp_directory()/comp->get_name()/(base+*i);
+                       Target *target = tool->create_source(*comp, fn);
+                       target->set_tool(*this);
+                       target->add_dependency(tmpl);
+                       if(primary)
+                               primary->add_side_effect(*target);
+                       else
+                               primary = target;
+               }
+       }
+
+       return primary;
+}
+
+void SourceGenerator::do_prepare()
+{
+       FS::Path exe_fn = package.get_source_directory()/command;
+       executable = builder.get_vfs().get_target(exe_fn);
+       if(!executable)
+               executable = new Executable(builder, exe_fn);
+}
+
+Task *SourceGenerator::run(const Target &target) const
+{
+       const SourceFile &out_src = dynamic_cast<const SourceFile &>(target);
+       const FS::Path &work_dir = out_src.get_package()->get_source_directory();
+
+       vector<string> args;
+       args.push_back(executable->get_path().str());
+
+       const Target::Dependencies &deps = target.get_dependencies();
+       for(Target::Dependencies::const_iterator i=deps.begin(); i!=deps.end(); ++i)
+               if(const TemplateFile *tmpl = dynamic_cast<const TemplateFile *>(*i))
+                       args.push_back(FS::relative(tmpl->get_path(), work_dir).str());
+
+       args.push_back(FS::relative(out_src.get_path(), work_dir).str());
+
+       return new ExternalTask(args, work_dir);
+}
+
+
+SourceGenerator::Loader::Loader(SourceGenerator &sg):
+       DataFile::ObjectLoader<SourceGenerator>(sg)
+{
+       add("command",    &Loader::command);
+       add("in_suffix",  &Loader::in_suffix);
+       add("out_suffix", &Loader::out_suffix);
+}
+
+void SourceGenerator::Loader::command(const string &c)
+{
+       obj.set_command(c);
+}
+
+void SourceGenerator::Loader::in_suffix(const string &s)
+{
+       obj.input_suffixes.push_back(s);
+}
+
+void SourceGenerator::Loader::out_suffix(const string &s)
+{
+       obj.out_suffixes.push_back(s);
+}