]> git.tdb.fi Git - builder.git/blob - source/datatool.cpp
Refuse to create an empty data pack
[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;
56         if(binfo.debug)
57                 result += 'g';
58         if(binfo.optimize>0)
59         {
60                 result += 'b';
61                 if(binfo.optimize>1)
62                         result += 'z';
63         }
64         return result;
65 }
66
67 Task *DataTool::run(const Target &tgt) const
68 {
69         const Component &comp = *tgt.get_component();
70         FS::Path work_dir = comp.get_package().get_source_directory();
71
72         vector<string> argv;
73         argv.push_back(executable->get_path().str());
74
75         argv.push_back("-o");
76         argv.push_back(FS::relative(dynamic_cast<const FileTarget &>(tgt).get_path(), work_dir).str());
77
78         BuildInfo binfo;
79         tgt.collect_build_info(binfo);
80         if(binfo.debug)
81                 argv.push_back("-g");
82         if(binfo.optimize>0)
83         {
84                 argv.push_back("-b");
85                 if(binfo.optimize>1)
86                         argv.push_back("-z");
87         }
88
89         if(const DataCollection *coll = dynamic_cast<const DataCollection *>(&tgt))
90         {
91                 argv.push_back("-c");
92                 argv.push_back(FS::relative(coll->get_source().get_path(), work_dir).str());
93         }
94         else if(const DataPack *pack = dynamic_cast<const DataPack *>(&tgt))
95         {
96                 argv.push_back("-p");
97                 for(const FileTarget *f: pack->get_files())
98                         argv.push_back(FS::relative(f->get_path(), work_dir).str());
99         }
100
101         return new ExternalTask(argv, work_dir);
102 }