]> git.tdb.fi Git - builder.git/blob - source/datatool.cpp
Add a utility function for setting the executable for a Tool
[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         input_suffixes.push_back(".mdt");
19 }
20
21 Target *DataTool::create_source(const Component &comp, const FS::Path &path) const
22 {
23         return new DataTransform(builder, comp, path);
24 }
25
26 Target *DataTool::create_target(const list<Target *> &sources, const string &arg)
27 {
28         if(arg=="collection")
29         {
30                 if(sources.size()!=1)
31                         throw invalid_argument("DataTool::create_target");
32                 DataTransform &source = dynamic_cast<DataTransform &>(*sources.front());
33                 DataCollection *coll = new DataCollection(builder, *source.get_component(), source);
34                 coll->set_tool(*this);
35                 return coll;
36         }
37         else if(arg=="pack")
38         {
39                 list<FileTarget *> files;
40                 for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
41                         files.push_back(&dynamic_cast<FileTarget &>(**i));
42                 DataPack *pack = new DataPack(builder, *files.front()->get_component(), files);
43                 pack->set_tool(*this);
44                 return pack;
45         }
46         else
47                 throw invalid_argument("DataTool::create_target");
48 }
49
50 void DataTool::do_prepare()
51 {
52         set_executable("mspdatatool");
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 }