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