]> git.tdb.fi Git - builder.git/blob - source/gnucompiler.cpp
Big rewrite for a more tool-centric approach
[builder.git] / source / gnucompiler.cpp
1 #include <msp/fs/dir.h>
2 #include <msp/fs/utils.h>
3 #include "builder.h"
4 #include "component.h"
5 #include "externaltask.h"
6 #include "gnucompiler.h"
7 #include "objectfile.h"
8 #include "sourcefile.h"
9 #include "sourcepackage.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 GnuCompiler::GnuCompiler(Builder &b, const string &t, const string &n):
15         Tool(b, t),
16         name(n)
17 { }
18
19 Target *GnuCompiler::create_source(const Component &comp, const FS::Path &path) const
20 {
21         return new SourceFile(builder, comp, path);
22 }
23
24 Target *GnuCompiler::create_target(const list<Target *> &sources, const std::string &) const
25 {
26         if(sources.size()!=1)
27                 throw invalid_argument("GnuCCompiler::create_target");
28         SourceFile &source = dynamic_cast<SourceFile &>(*sources.front());
29         ObjectFile *obj = new ObjectFile(builder, *source.get_component(), source);
30         obj->set_tool(*this);
31         return obj;
32 }
33
34 Task *GnuCompiler::run(const Target &target) const
35 {
36         const ObjectFile &object = dynamic_cast<const ObjectFile &>(target);
37         const Component &comp = object.get_component();
38
39         vector<string> argv;
40         argv.push_back(name);
41         argv.push_back("-c");
42
43         const BuildInfo &binfo = comp.get_build_info();
44         for(list<string>::const_iterator i=binfo.warnings.begin(); i!=binfo.warnings.end(); ++i)
45                 argv.push_back("-W"+*i);
46         for(list<string>::const_iterator i=binfo.cflags.begin(); i!=binfo.cflags.end(); ++i)
47                 argv.push_back(*i);
48         for(list<string>::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i)
49                 argv.push_back("-I"+*i);
50         for(list<string>::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i)
51                 argv.push_back("-D"+*i);
52
53         FS::Path obj_path = object.get_path();
54         FS::Path src_path = object.get_source().get_path();
55         FS::Path work_dir = comp.get_package().get_source();
56
57         argv.push_back("-o");
58         argv.push_back(relative(obj_path, work_dir).str());
59         argv.push_back(relative(src_path, work_dir).str());
60
61         if(!builder.get_dry_run())
62                 FS::mkpath(FS::dirname(obj_path), 0755);
63
64         return new ExternalTask(argv, work_dir);
65 }