]> git.tdb.fi Git - builder.git/blob - source/gnucompiler.cpp
Add more recognized architectures
[builder.git] / source / gnucompiler.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/fs/dir.h>
3 #include <msp/fs/utils.h>
4 #include <msp/strings/format.h>
5 #include <msp/strings/utils.h>
6 #include "architecture.h"
7 #include "builder.h"
8 #include "component.h"
9 #include "externaltask.h"
10 #include "gnucompiler.h"
11 #include "objectfile.h"
12 #include "sourcefile.h"
13 #include "sourcepackage.h"
14
15 using namespace std;
16 using namespace Msp;
17
18 namespace {
19
20 const char *cpus[] =
21 {
22         "athlonxp", "athlon-xp",
23         "armv7a",   "armv7-a",
24         0
25 };
26
27 }
28
29 GnuCompiler::GnuCompiler(Builder &b, const Architecture &a, const string &t, const FS::Path &sysroot):
30         Tool(b, a, t)
31 {
32         if(!sysroot.empty())
33         {
34                 build_info.sysroot = sysroot;
35                 system_path.push_back(sysroot/"usr/include");
36         }
37         else if(architecture->is_native())
38                 system_path.push_back("/usr/include");
39         else
40                 system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/include");
41 }
42
43 Target *GnuCompiler::create_target(const list<Target *> &sources, const string &)
44 {
45         if(sources.size()!=1)
46                 throw invalid_argument("GnuCCompiler::create_target");
47         SourceFile &source = dynamic_cast<SourceFile &>(*sources.front());
48         ObjectFile *obj = new ObjectFile(builder, *source.get_component(), source);
49         obj->set_tool(*this);
50         return obj;
51 }
52
53 string GnuCompiler::create_build_signature(const BuildInfo &binfo) const
54 {
55         string result = FS::basename(executable->get_path());
56         if(!architecture->get_cpu().empty())
57         {
58                 result += ",m";
59                 result += architecture->get_cpu();
60         }
61         result += ',';
62         if(binfo.debug)
63                 result += 'g';
64         if(binfo.optimize)
65         {
66                 result += 'O';
67                 result += (binfo.optimize>0 ? '0'+binfo.optimize : 's');
68         }
69         return result;
70 }
71
72 void GnuCompiler::do_prepare()
73 {
74         executable = builder.get_vfs().find_binary(command);
75         if(executable)
76         {
77                 ExternalTask::Arguments argv;
78                 argv.push_back(executable->get_path().str());
79                 argv.push_back("-dumpversion");
80
81                 builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
82                 try
83                 {
84                         version = strip(ExternalTask::run_and_capture_output(argv));
85                         builder.get_logger().log("tools", format("%s version is %s", FS::basename(executable->get_path()), version));
86                 }
87                 catch(const runtime_error &)
88                 { }
89         }
90 }
91
92 Task *GnuCompiler::run(const Target &target) const
93 {
94         const ObjectFile &object = dynamic_cast<const ObjectFile &>(target);
95         const Component &comp = *object.get_component();
96
97         ExternalTask::Arguments argv;
98         argv.push_back(executable->get_path().str());
99         argv.push_back("-c");
100
101         BuildInfo binfo;
102         target.collect_build_info(binfo);
103
104         string tag_for_std = (tag=="OBJC" ? "CC" : tag);
105         if(binfo.standards.count(tag_for_std))
106                 argv.push_back("-std="+get_item(binfo.standards, tag_for_std));
107         if(tag=="OBJC" && binfo.standards.count(tag))
108                 argv.push_back("-fobjc-std="+get_item(binfo.standards, tag));
109
110         if(binfo.warning_level>=1)
111         {
112                 argv.push_back("-Wall");
113                 if(binfo.warning_level>=2)
114                 {
115                         argv.push_back("-Wextra");
116                         argv.push_back("-Wundef");
117                 }
118                 if(binfo.warning_level>=3)
119                 {
120                         argv.push_back("-pedantic");
121                         argv.push_back("-Wno-long-long");
122                         argv.push_back("-Wshadow");
123                         if(tag=="CC")
124                         {
125                                 argv.push_back("-Wc++-compat");
126                                 argv.push_back("-Wstrict-prototypes");
127                         }
128                 }
129                 if(binfo.warning_level>=4)
130                 {
131                         // Some truly paranoid warnings
132                         argv.push_back("-Wstrict-overflow=4");
133                         argv.push_back("-Wfloat-equal");
134                         argv.push_back("-Wconversion");
135                         argv.push_back("-Wwrite-strings");
136                         argv.push_back("-Winline");
137                 }
138                 if(binfo.fatal_warnings)
139                         argv.push_back("-Werror");
140         }
141
142         const FS::Path &sysroot = binfo.sysroot;
143         if(!sysroot.empty())
144                 argv.push_back("--sysroot="+sysroot.str());
145         for(BuildInfo::PathList::const_iterator i=binfo.local_incpath.begin(); i!=binfo.local_incpath.end(); ++i)
146         {
147                 argv.push_back("-iquote");
148                 argv.push_back(i->str());
149         }
150         for(BuildInfo::PathList::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i)
151                 argv.push_back("-I"+i->str());
152
153         for(BuildInfo::DefineMap::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i)
154         {
155                 if(i->second.empty())
156                         argv.push_back(format("-D%s", i->first));
157                 else
158                         argv.push_back(format("-D%s=%s", i->first, i->second));
159         }
160
161         if(binfo.debug)
162                 argv.push_back("-ggdb");
163         if(binfo.optimize)
164         {
165                 if(binfo.optimize<0)
166                         argv.push_back("-Os");
167                 else
168                         argv.push_back(format("-O%d", binfo.optimize));
169         }
170         if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
171                 argv.push_back("-pthread");
172         if(object.is_used_in_shared_library() && architecture->get_system()!="windows")
173                 argv.push_back("-fPIC");
174
175         const Architecture &native_arch = builder.get_native_arch();
176         if(architecture->is_native() && architecture->get_bits()!=native_arch.get_bits())
177                 argv.push_back(format("-m%d", architecture->get_bits()));
178
179         string cpu = architecture->get_cpu();
180         if(!cpu.empty())
181         {
182                 for(unsigned i=0; cpus[i]; i+=2)
183                         if(cpu==cpus[i])
184                         {
185                                 cpu = cpus[i+1];
186                                 break;
187                         }
188                 argv.push_back("-march="+cpu);
189         }
190
191         FS::Path obj_path = object.get_path();
192         FS::Path src_path = object.get_source().get_path();
193         FS::Path work_dir = comp.get_package().get_source_directory();
194
195         argv.push_back("-o");
196         argv.push_back(relative(obj_path, work_dir).str());
197         argv.push_back(relative(src_path, work_dir).str());
198
199         return new ExternalTask(argv, work_dir);
200 }