]> git.tdb.fi Git - builder.git/blob - source/datatool.cpp
b24de1396c22381ca279b9a27194f1cdc99e8541
[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         input_suffixes.push_back(".mdt");
20 }
21
22 Target *DataTool::create_source(const Component &comp, const FS::Path &path) const
23 {
24         return new DataTransform(builder, comp, path);
25 }
26
27 Target *DataTool::create_target(const vector<Target *> &sources, const string &arg)
28 {
29         if(arg=="collection")
30         {
31                 if(sources.size()!=1)
32                         throw invalid_argument("DataTool::create_target");
33                 DataTransform &source = dynamic_cast<DataTransform &>(*sources.front());
34                 DataCollection *coll = new DataCollection(builder, *source.get_component(), source);
35                 coll->set_tool(*this);
36                 return coll;
37         }
38         else if(arg=="pack")
39         {
40                 if(sources.empty())
41                         throw invalid_argument("DataTool::create_target");
42                 vector<FileTarget *> files;
43                 files.reserve(sources.size());
44                 for(Target *t: sources)
45                         files.push_back(&dynamic_cast<FileTarget &>(*t));
46                 DataPack *pack = new DataPack(builder, *files.front()->get_component(), files);
47                 pack->set_tool(*this);
48                 return pack;
49         }
50         else
51                 throw invalid_argument("DataTool::create_target");
52 }
53
54 string DataTool::create_build_signature(const BuildInfo &binfo) const
55 {
56         string result = Tool::create_build_signature(binfo);
57         if(binfo.debug || binfo.optimize)
58                 result += ',';
59         if(binfo.debug)
60                 result += 'g';
61         if(binfo.optimize>0)
62         {
63                 result += 'b';
64                 if(binfo.optimize>1)
65                         result += 'z';
66         }
67         return result;
68 }
69
70 Task *DataTool::run(const Target &tgt) const
71 {
72         const Component &comp = *tgt.get_component();
73         FS::Path work_dir = comp.get_package().get_source_directory();
74
75         vector<string> argv;
76         argv.push_back(executable->get_path().str());
77
78         argv.push_back("-o");
79         argv.push_back(FS::relative(dynamic_cast<const FileTarget &>(tgt).get_path(), work_dir).str());
80
81         BuildInfo binfo;
82         tgt.collect_build_info(binfo);
83         if(binfo.debug)
84                 argv.push_back("-g");
85         if(binfo.optimize>0)
86         {
87                 argv.push_back("-b");
88                 if(binfo.optimize>1)
89                         argv.push_back("-z");
90         }
91
92         if(const DataCollection *coll = dynamic_cast<const DataCollection *>(&tgt))
93         {
94                 argv.push_back("-c");
95                 argv.push_back(FS::relative(coll->get_source().get_path(), work_dir).str());
96         }
97         else if(const DataPack *pack = dynamic_cast<const DataPack *>(&tgt))
98         {
99                 argv.push_back("-p");
100                 for(const FileTarget *f: pack->get_files())
101                         argv.push_back(FS::relative(f->get_path(), work_dir).str());
102         }
103
104         return new ExternalTask(argv, work_dir);
105 }