]> git.tdb.fi Git - builder.git/blob - source/datatool.cpp
Redesign how tools are run
[builder.git] / source / datatool.cpp
1 #include <stdexcept>
2 #include <msp/fs/utils.h>
3 #include "builder.h"
4 #include "component.h"
5 #include "datacollection.h"
6 #include "datapack.h"
7 #include "datatool.h"
8 #include "datatransform.h"
9 #include "externaltask.h"
10 #include "sourcepackage.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 DataTool::DataTool(Builder &b):
16         Tool(b, "DATA")
17 {
18         set_command("mspdatatool");
19         set_run(_run);
20         input_suffixes.push_back(".mdt");
21 }
22
23 Target *DataTool::create_source(const Component &comp, const FS::Path &path) const
24 {
25         return new DataTransform(builder, comp, path);
26 }
27
28 Target *DataTool::create_target(const vector<Target *> &sources, const string &arg)
29 {
30         if(arg=="collection")
31         {
32                 if(sources.size()!=1)
33                         throw invalid_argument("DataTool::create_target");
34                 DataTransform &source = dynamic_cast<DataTransform &>(*sources.front());
35                 DataCollection *coll = new DataCollection(builder, *source.get_component(), source);
36                 coll->set_tool(*this);
37                 return coll;
38         }
39         else if(arg=="pack")
40         {
41                 if(sources.empty())
42                         throw invalid_argument("DataTool::create_target");
43                 vector<FileTarget *> files;
44                 files.reserve(sources.size());
45                 for(Target *t: sources)
46                         files.push_back(&dynamic_cast<FileTarget &>(*t));
47                 DataPack *pack = new DataPack(builder, *files.front()->get_component(), files);
48                 pack->set_tool(*this);
49                 return pack;
50         }
51         else
52                 throw invalid_argument("DataTool::create_target");
53 }
54
55 string DataTool::create_build_signature(const BuildInfo &binfo) const
56 {
57         string result = Tool::create_build_signature(binfo);
58         if(binfo.debug || binfo.optimize)
59                 result += ',';
60         if(binfo.debug)
61                 result += 'g';
62         if(binfo.optimize>0)
63         {
64                 result += 'b';
65                 if(binfo.optimize>1)
66                         result += 'z';
67         }
68         return result;
69 }
70
71 Task *DataTool::_run(const Target &tgt)
72 {
73         const Tool &tool = *tgt.get_tool();
74         const Component &comp = *tgt.get_component();
75         FS::Path work_dir = comp.get_package().get_source_directory();
76
77         vector<string> argv;
78         argv.push_back(tool.get_executable()->get_path().str());
79
80         argv.push_back("-o");
81         argv.push_back(FS::relative(dynamic_cast<const FileTarget &>(tgt).get_path(), work_dir).str());
82
83         BuildInfo binfo;
84         tgt.collect_build_info(binfo);
85         if(binfo.debug)
86                 argv.push_back("-g");
87         if(binfo.optimize>0)
88         {
89                 argv.push_back("-b");
90                 if(binfo.optimize>1)
91                         argv.push_back("-z");
92         }
93
94         if(const DataCollection *coll = dynamic_cast<const DataCollection *>(&tgt))
95         {
96                 argv.push_back("-c");
97                 argv.push_back(FS::relative(coll->get_source().get_path(), work_dir).str());
98         }
99         else if(const DataPack *pack = dynamic_cast<const DataPack *>(&tgt))
100         {
101                 argv.push_back("-p");
102                 for(const FileTarget *f: pack->get_files())
103                         argv.push_back(FS::relative(f->get_path(), work_dir).str());
104         }
105
106         return new ExternalTask(argv, work_dir);
107 }