]> git.tdb.fi Git - builder.git/blob - source/gnucompiler.cpp
Rewrite BuildInfo to be compiler-agnostic
[builder.git] / source / gnucompiler.cpp
1 #include <msp/fs/dir.h>
2 #include <msp/fs/utils.h>
3 #include <msp/strings/format.h>
4 #include "builder.h"
5 #include "component.h"
6 #include "externaltask.h"
7 #include "gnucompiler.h"
8 #include "objectfile.h"
9 #include "sourcefile.h"
10 #include "sourcepackage.h"
11
12 using namespace std;
13 using namespace Msp;
14
15 GnuCompiler::GnuCompiler(Builder &b, const string &t, const string &n):
16         Tool(b, t),
17         name(n)
18 { }
19
20 Target *GnuCompiler::create_source(const Component &comp, const FS::Path &path) const
21 {
22         return new SourceFile(builder, comp, path);
23 }
24
25 Target *GnuCompiler::create_target(const list<Target *> &sources, const std::string &) const
26 {
27         if(sources.size()!=1)
28                 throw invalid_argument("GnuCCompiler::create_target");
29         SourceFile &source = dynamic_cast<SourceFile &>(*sources.front());
30         ObjectFile *obj = new ObjectFile(builder, *source.get_component(), source);
31         obj->set_tool(*this);
32         return obj;
33 }
34
35 Task *GnuCompiler::run(const Target &target) const
36 {
37         const ObjectFile &object = dynamic_cast<const ObjectFile &>(target);
38         const Component &comp = object.get_component();
39
40         vector<string> argv;
41         argv.push_back(name);
42         argv.push_back("-c");
43
44         const BuildInfo &binfo = comp.get_build_info();
45         for(list<string>::const_iterator i=binfo.warnings.begin(); i!=binfo.warnings.end(); ++i)
46                 argv.push_back("-W"+*i);
47         for(list<string>::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i)
48                 argv.push_back("-I"+*i);
49         for(BuildInfo::DefineMap::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i)
50         {
51                 if(i->second.empty())
52                         argv.push_back(format("-D%s", i->first));
53                 else
54                         argv.push_back(format("-D%s=%s", i->first, i->second));
55         }
56         if(binfo.debug)
57                 argv.push_back("-ggdb");
58         if(binfo.optimize)
59         {
60                 if(binfo.optimize<0)
61                         argv.push_back("-Os");
62                 else
63                         argv.push_back(format("-O%d", binfo.optimize));
64         }
65         if(binfo.threads)
66                 argv.push_back("-pthread");
67         if(comp.get_type()==Component::LIBRARY)
68                 argv.push_back("-fPIC");
69
70         const Architecture &arch = builder.get_current_arch();
71         const Architecture &native_arch = builder.get_native_arch();
72         if(arch.get_bits()!=native_arch.get_bits())
73                 argv.push_back(format("-m%d", arch.get_bits()));
74
75         const string &cpu = arch.get_cpu();
76         if(!cpu.empty())
77                 argv.push_back("-march="+cpu);
78
79         FS::Path obj_path = object.get_path();
80         FS::Path src_path = object.get_source().get_path();
81         FS::Path work_dir = comp.get_package().get_source();
82
83         argv.push_back("-o");
84         argv.push_back(relative(obj_path, work_dir).str());
85         argv.push_back(relative(src_path, work_dir).str());
86
87         if(!builder.get_dry_run())
88                 FS::mkpath(FS::dirname(obj_path), 0755);
89
90         return new ExternalTask(argv, work_dir);
91 }