]> git.tdb.fi Git - builder.git/blob - source/gnucompiler.cpp
Let the tools deal with cross-compiling flags
[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.cflags.begin(); i!=binfo.cflags.end(); ++i)
48                 argv.push_back(*i);
49         for(list<string>::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i)
50                 argv.push_back("-I"+*i);
51         for(list<string>::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i)
52                 argv.push_back("-D"+*i);
53
54         const Architecture &arch = builder.get_current_arch();
55         const Architecture &native_arch = builder.get_native_arch();
56         if(arch.get_bits()!=native_arch.get_bits())
57                 argv.push_back(format("-m%d", arch.get_bits()));
58
59         const string &cpu = arch.get_cpu();
60         if(!cpu.empty())
61                 argv.push_back("-march="+cpu);
62
63         FS::Path obj_path = object.get_path();
64         FS::Path src_path = object.get_source().get_path();
65         FS::Path work_dir = comp.get_package().get_source();
66
67         argv.push_back("-o");
68         argv.push_back(relative(obj_path, work_dir).str());
69         argv.push_back(relative(src_path, work_dir).str());
70
71         if(!builder.get_dry_run())
72                 FS::mkpath(FS::dirname(obj_path), 0755);
73
74         return new ExternalTask(argv, work_dir);
75 }