]> git.tdb.fi Git - builder.git/blobdiff - source/sourcegenerator.cpp
Redesign how tools are run
[builder.git] / source / sourcegenerator.cpp
index 584822a1074b06a1fb959714cfbb463b63c54bbc..72297fd5eed411b19528681a6743492e16ca5e4a 100644 (file)
@@ -13,14 +13,16 @@ using namespace Msp;
 SourceGenerator::SourceGenerator(Builder &b, const SourcePackage &p, const string &t):
        Tool(b, t),
        package(p)
-{ }
+{
+       set_run(&_run);
+}
 
 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 &)
+Target *SourceGenerator::create_target(const vector<Target *> &sources, const string &)
 {
        if(sources.empty())
                throw invalid_argument("SourceGenerator::create_target");
@@ -47,44 +49,43 @@ Target *SourceGenerator::create_target(const list<Target *> &sources, const stri
        }
 
        Target *primary = 0;
-       for(list<string>::const_iterator i=out_suffixes.begin(); i!=out_suffixes.end(); ++i)
+       for(const string &s: out_suffixes)
        {
-               Tool *tool = builder.get_toolchain().get_tool_for_suffix(*i, true);
+               Tool *tool = builder.get_toolchain().get_tool_for_suffix(s, true);
                if(tool)
                {
-                       FS::Path fn = pkg->get_temp_directory()/"generated"/subdir/(base+*i);
+                       FS::Path fn = pkg->get_temp_directory()/"generated"/subdir/(base+s);
                        Target *target = tool->create_source(*comp, fn);
                        target->set_tool(*this);
-                       for(list<Target *>::const_iterator j=sources.begin(); j!=sources.end(); ++j)
-                               target->add_dependency(**j);
+                       for(Target *t: sources)
+                               target->add_dependency(*t);
                        if(primary)
                                primary->add_side_effect(*target);
                        else
                                primary = target;
                }
                else
-                       throw runtime_error("No tool found for suffix "+*i);
+                       throw runtime_error("No tool found for suffix "+s);
        }
 
        return primary;
 }
 
-Task *SourceGenerator::run(const Target &target) const
+Task *SourceGenerator::_run(const SourceFile &out_src)
 {
-       const SourceFile &out_src = dynamic_cast<const SourceFile &>(target);
        const FS::Path &work_dir = out_src.get_package()->get_source_directory();
+       const SourceGenerator &tool = dynamic_cast<const SourceGenerator &>(*out_src.get_tool());
 
        vector<string> args;
-       args.push_back(executable->get_path().str());
-       args.insert(args.end(), arguments.begin(), arguments.end());
+       args.push_back(tool.get_executable()->get_path().str());
+       args.insert(args.end(), tool.arguments.begin(), tool.arguments.end());
 
-       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))
+       for(const Target *d: out_src.get_dependencies())
+               if(const TemplateFile *tmpl = dynamic_cast<const TemplateFile *>(d))
                        args.push_back(FS::relative(tmpl->get_path(), work_dir).str());
 
-       if(!out_argument.empty())
-               args.push_back(out_argument);
+       if(!tool.out_argument.empty())
+               args.push_back(tool.out_argument);
        args.push_back(FS::relative(out_src.get_path(), work_dir).str());
 
        return new ExternalTask(args, work_dir);