]> git.tdb.fi Git - builder.git/blob - source/datatool.cpp
Reimplement datafile building
[builder.git] / source / datatool.cpp
1 #include <stdexcept>
2 #include <msp/fs/utils.h>
3 #include <msp/strings/format.h>
4 #include "builder.h"
5 #include "component.h"
6 #include "datacollection.h"
7 #include "datapack.h"
8 #include "datatool.h"
9 #include "datatransform.h"
10 #include "externaltask.h"
11 #include "sourcepackage.h"
12
13 using namespace std;
14 using namespace Msp;
15
16 DataTool::DataTool(Builder &b):
17         Tool(b, "DATA")
18 {
19         executable = builder.get_vfs().find_binary("mspdatatool");
20         if(!executable)
21                 builder.problem(string(), format("Can't find executable mspdatatool for tool %s", tag));
22
23         input_suffixes.push_back(".mdt");
24 }
25
26 Target *DataTool::create_source(const Component &comp, const FS::Path &path) const
27 {
28         return new DataTransform(builder, comp, path);
29 }
30
31 Target *DataTool::create_target(const list<Target *> &sources, const string &arg) const
32 {
33         if(arg=="collection")
34         {
35                 if(sources.size()!=1)
36                         throw invalid_argument("DataTool::create_target");
37                 DataTransform &source = dynamic_cast<DataTransform &>(*sources.front());
38                 DataCollection *coll = new DataCollection(builder, *source.get_component(), source);
39                 coll->set_tool(*this);
40                 return coll;
41         }
42         else if(arg=="pack")
43         {
44                 list<FileTarget *> files;
45                 for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
46                         files.push_back(&dynamic_cast<FileTarget &>(**i));
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 Task *DataTool::run(const Target &tgt) const
56 {
57         const Component &comp = *tgt.get_component();
58         FS::Path work_dir = comp.get_package().get_source_directory();
59
60         vector<string> argv;
61         argv.push_back(executable->get_path().str());
62
63         argv.push_back("-o");
64         argv.push_back(FS::relative(dynamic_cast<const FileTarget &>(tgt).get_path(), work_dir).str());
65
66         const BuildInfo &binfo = comp.get_build_info();
67         if(binfo.debug)
68                 argv.push_back("-g");
69         if(binfo.optimize>0)
70         {
71                 argv.push_back("-b");
72                 if(binfo.optimize>1)
73                         argv.push_back("-z");
74         }
75
76         if(const DataCollection *coll = dynamic_cast<const DataCollection *>(&tgt))
77         {
78                 argv.push_back("-c");
79                 argv.push_back(FS::relative(coll->get_source().get_path(), work_dir).str());
80         }
81         else if(const DataPack *pack = dynamic_cast<const DataPack *>(&tgt))
82         {
83                 argv.push_back("-p");
84                 const DataPack::FileList &files = pack->get_files();
85                 for(DataPack::FileList::const_iterator i=files.begin(); i!=files.end(); ++i)
86                         argv.push_back(FS::relative((*i)->get_path(), work_dir).str());
87         }
88
89         return new ExternalTask(argv, work_dir);
90 }