]> git.tdb.fi Git - builder.git/blob - source/datatool.cpp
Delay locating tool executables until the tool is needed
[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         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                 list<FileTarget *> files;
41                 for(list<Target *>::const_iterator i=sources.begin(); i!=sources.end(); ++i)
42                         files.push_back(&dynamic_cast<FileTarget &>(**i));
43                 DataPack *pack = new DataPack(builder, *files.front()->get_component(), files);
44                 pack->set_tool(*this);
45                 return pack;
46         }
47         else
48                 throw invalid_argument("DataTool::create_target");
49 }
50
51 void DataTool::do_prepare()
52 {
53         executable = builder.get_vfs().find_binary("mspdatatool");
54         if(!executable)
55                 builder.problem(string(), format("Can't find executable mspdatatool for tool %s", tag));
56 }
57
58 Task *DataTool::run(const Target &tgt) const
59 {
60         const Component &comp = *tgt.get_component();
61         FS::Path work_dir = comp.get_package().get_source_directory();
62
63         vector<string> argv;
64         argv.push_back(executable->get_path().str());
65
66         argv.push_back("-o");
67         argv.push_back(FS::relative(dynamic_cast<const FileTarget &>(tgt).get_path(), work_dir).str());
68
69         const BuildInfo &binfo = comp.get_build_info();
70         if(binfo.debug)
71                 argv.push_back("-g");
72         if(binfo.optimize>0)
73         {
74                 argv.push_back("-b");
75                 if(binfo.optimize>1)
76                         argv.push_back("-z");
77         }
78
79         if(const DataCollection *coll = dynamic_cast<const DataCollection *>(&tgt))
80         {
81                 argv.push_back("-c");
82                 argv.push_back(FS::relative(coll->get_source().get_path(), work_dir).str());
83         }
84         else if(const DataPack *pack = dynamic_cast<const DataPack *>(&tgt))
85         {
86                 argv.push_back("-p");
87                 const DataPack::FileList &files = pack->get_files();
88                 for(DataPack::FileList::const_iterator i=files.begin(); i!=files.end(); ++i)
89                         argv.push_back(FS::relative((*i)->get_path(), work_dir).str());
90         }
91
92         return new ExternalTask(argv, work_dir);
93 }