]> git.tdb.fi Git - builder.git/blobdiff - source/sourcegenerator.cpp
Replace basic for loops with range-based loops or algorithms
[builder.git] / source / sourcegenerator.cpp
index 76d6c892a20942647316b8c2eb8ac83629c215f0..f4b43a82c8c32e7ffa016f9a57f00004174f5875 100644 (file)
@@ -22,29 +22,48 @@ Target *SourceGenerator::create_source(const Component &comp, const FS::Path &pa
 
 Target *SourceGenerator::create_target(const list<Target *> &sources, const string &)
 {
-       if(sources.size()!=1)
+       if(sources.empty())
                throw invalid_argument("SourceGenerator::create_target");
+       if(out_suffixes.empty())
+               throw logic_error("No output suffixes");
 
        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()));
+       FS::Path subdir;
+       string base;
+       if(processing_unit==COMPONENT)
+               base = comp->get_name();
+       else
+       {
+               subdir = FS::dirname(FS::relative(tmpl.get_path(), pkg->get_source_directory()));
+               if(processing_unit==ONE_FILE)
+                       base = FS::basepart(FS::basename(tmpl.get_path()));
+               else if(processing_unit==DIRECTORY)
+               {
+                       base = FS::basename(subdir);
+                       subdir = FS::dirname(subdir);
+               }
+       }
 
        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()/comp->get_name()/(base+*i);
+                       FS::Path fn = pkg->get_temp_directory()/"generated"/subdir/(base+s);
                        Target *target = tool->create_source(*comp, fn);
                        target->set_tool(*this);
-                       target->add_dependency(tmpl);
+                       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 "+s);
        }
 
        return primary;
@@ -57,12 +76,14 @@ Task *SourceGenerator::run(const Target &target) const
 
        vector<string> args;
        args.push_back(executable->get_path().str());
+       args.insert(args.end(), arguments.begin(), 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: target.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);
        args.push_back(FS::relative(out_src.get_path(), work_dir).str());
 
        return new ExternalTask(args, work_dir);
@@ -73,14 +94,31 @@ SourceGenerator::Loader::Loader(SourceGenerator &sg):
        DataFile::ObjectLoader<SourceGenerator>(sg),
        ConditionalLoader(sg.package, format("%s/%s", sg.package.get_name(), sg.tag))
 {
+       add("argument",   &Loader::argument);
+       add("arguments",  &Loader::arguments);
        add("command",    &Loader::command);
        add("in_suffix",  &Loader::in_suffix);
+       add("out_argument", &SourceGenerator::out_argument);
        add("out_suffix", &Loader::out_suffix);
+       add("processing_unit", static_cast<ProcessingUnit SourceGenerator::*>(&SourceGenerator::processing_unit));
+}
+
+void SourceGenerator::Loader::argument(const string &a)
+{
+       obj.arguments.push_back(a);
+}
+
+void SourceGenerator::Loader::arguments(const vector<string> &a)
+{
+       obj.arguments.insert(obj.arguments.end(), a.begin(), a.end());
 }
 
 void SourceGenerator::Loader::command(const string &c)
 {
-       obj.set_command((obj.package.get_source_directory()/c).str());
+       if(c.find('/')!=string::npos)
+               obj.set_command((obj.package.get_source_directory()/c).str());
+       else
+               obj.set_command(c);
 }
 
 void SourceGenerator::Loader::in_suffix(const string &s)