]> git.tdb.fi Git - builder.git/blob - source/gnucompiler.cpp
Refactor logger to do message formatting internally
[builder.git] / source / gnucompiler.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/fs/utils.h>
3 #include <msp/strings/format.h>
4 #include <msp/strings/utils.h>
5 #include "architecture.h"
6 #include "builder.h"
7 #include "component.h"
8 #include "csourcefile.h"
9 #include "externaltask.h"
10 #include "gnucompiler.h"
11 #include "objcsourcefile.h"
12 #include "objectfile.h"
13 #include "sourcefile.h"
14 #include "sourcepackage.h"
15
16 using namespace std;
17 using namespace Msp;
18
19 namespace {
20
21 const char *cpus[] =
22 {
23         "athlonxp", "athlon-xp",
24         "armv7a",   "armv7-a",
25         0
26 };
27
28 }
29
30 GnuCompiler::GnuCompiler(Builder &b, const Architecture &a, const string &t):
31         Tool(b, a, t),
32         version(0)
33 {
34         if(tag=="CC")
35         {
36                 input_suffixes.push_back(".c");
37                 aux_suffixes.push_back(".h");
38         }
39         else if(tag=="CXX")
40         {
41                 input_suffixes.push_back(".cpp");
42                 input_suffixes.push_back(".cc");
43                 aux_suffixes.push_back(".hpp");
44         }
45         else if(tag=="OBJC")
46         {
47                 input_suffixes.push_back(".m");
48                 build_info.libs.push_back("objc");
49         }
50         else
51                 throw invalid_argument("GnuCompiler::GnuCompiler");
52
53         set_command((tag=="CXX" ? "g++" : "gcc"), true);
54 }
55
56 Target *GnuCompiler::create_source(const Component &comp, const FS::Path &path) const
57 {
58         if(tag=="OBJC")
59                 return new ObjCSourceFile(builder, comp, path);
60         else
61                 return new CSourceFile(builder, comp, path);
62 }
63
64 Target *GnuCompiler::create_source(const FS::Path &path) const
65 {
66         if(tag=="OBJC")
67                 return new ObjCSourceFile(builder, path);
68         else
69                 return new CSourceFile(builder, path);
70 }
71
72 Target *GnuCompiler::create_target(const vector<Target *> &sources, const string &)
73 {
74         if(sources.size()!=1)
75                 throw invalid_argument("GnuCompiler::create_target");
76         SourceFile &source = dynamic_cast<SourceFile &>(*sources.front());
77         ObjectFile *obj = new ObjectFile(builder, *source.get_component(), source);
78         obj->set_tool(*this);
79         return obj;
80 }
81
82 string GnuCompiler::create_build_signature(const BuildInfo &binfo) const
83 {
84         if(!executable)
85                 return string();
86
87         string result = Tool::create_build_signature(binfo);
88         if(!architecture->get_cpu().empty())
89         {
90                 result += ",m";
91                 result += architecture->get_cpu();
92         }
93         if(binfo.debug || binfo.optimize)
94                 result += ',';
95         if(binfo.debug)
96                 result += 'g';
97         if(binfo.optimize)
98         {
99                 result += 'O';
100                 result += (binfo.optimize>0 ? '0'+binfo.optimize : 's');
101         }
102         return result;
103 }
104
105 void GnuCompiler::do_prepare()
106 {
107         executable = builder.get_vfs().find_binary(command);
108         prepare_syspath();
109         prepare_version();
110 }
111
112 void GnuCompiler::prepare_syspath()
113 {
114         bool path_found = false;
115         const FS::Path &sysroot = build_info.sysroot;
116
117         if(executable)
118         {
119                 ExternalTask::Arguments argv;
120                 argv.push_back(executable->get_path().str());
121                 argv.push_back("-Wp,-v");
122                 argv.push_back("-E");
123                 if(tag=="CXX")
124                         argv.push_back("-xc++");
125                 if(!sysroot.empty())
126                         argv.push_back("--sysroot="+sysroot.str());
127                 argv.push_back("-");
128
129                 builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
130                 try
131                 {
132                         string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
133                         string::size_type start = 0;
134                         bool record_path = false;
135                         while(start<output.size())
136                         {
137                                 string::size_type newline = output.find('\n', start);
138                                 if(!output.compare(start, 34, "#include <...> search starts here:"))
139                                 {
140                                         record_path = true;
141                                         path_found = true;
142                                 }
143                                 else if(!output.compare(start, 19, "End of search list."))
144                                         record_path = false;
145                                 else if(record_path)
146                                 {
147                                         FS::Path path = strip(output.substr(start, newline-start));
148                                         builder.get_logger().log("tools", "Got %s system path: %s", tag, path);
149                                         system_path.push_back(path);
150                                 }
151                                 start = newline+1;
152                         }
153                 }
154                 catch(const runtime_error &)
155                 { }
156         }
157
158         if(!path_found)
159         {
160                 builder.get_logger().log("tools", "No %s system path found, using defaults", tag);
161                 if(!sysroot.empty())
162                         system_path.push_back(sysroot/"usr/include");
163                 else if(architecture->is_native())
164                         system_path.push_back("/usr/include");
165                 else
166                         system_path.push_back(format("/usr/%s/include", architecture->get_cross_prefix()));
167         }
168 }
169
170 void GnuCompiler::prepare_version()
171 {
172         if(!executable)
173                 return;
174
175         prepare_version("-dumpversion");
176         if(version>=0x70000)
177                 prepare_version("-dumpfullversion");
178         builder.get_logger().log("tools", "%s version is %d.%d.%d", FS::basename(executable->get_path()), version>>16, (version>>8)&0xFF, version&0xFF);
179 }
180
181 void GnuCompiler::prepare_version(const string &arg)
182 {
183         ExternalTask::Arguments argv;
184         argv.push_back(executable->get_path().str());
185         argv.push_back(arg);
186
187         builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
188         try
189         {
190                 string version_str = strip(ExternalTask::run_and_capture_output(argv));
191
192                 vector<string> version_parts = split(version_str, '.');
193                 version = 0;
194                 for(unsigned i=0; (i<3 && i<version_parts.size()); ++i)
195                         version |= lexical_cast<unsigned>(version_parts[i])<<(16-8*i);
196         }
197         catch(const runtime_error &)
198         { }
199 }
200
201 Task *GnuCompiler::run(const Target &target) const
202 {
203         const ObjectFile &object = dynamic_cast<const ObjectFile &>(target);
204
205         ExternalTask::Arguments argv;
206         argv.push_back(executable->get_path().str());
207         argv.push_back("-c");
208
209         BuildInfo binfo;
210         target.collect_build_info(binfo);
211
212         string tag_for_std = (tag=="OBJC" ? "CC" : tag);
213         if(binfo.standards.count(tag_for_std))
214                 argv.push_back("-std="+get_item(binfo.standards, tag_for_std).str());
215         if(tag=="OBJC" && binfo.standards.count(tag))
216                 argv.push_back("-fobjc-std="+get_item(binfo.standards, tag).str());
217
218         if(binfo.warning_level>=1)
219         {
220                 argv.push_back("-Wall");
221                 if(binfo.warning_level>=2)
222                 {
223                         argv.push_back("-Wextra");
224                         argv.push_back("-Wundef");
225                         if(version>=0x80000)
226                                 argv.push_back("-Wno-cast-function-type");
227                 }
228                 if(binfo.warning_level>=3)
229                 {
230                         argv.push_back("-pedantic");
231                         argv.push_back("-Wno-long-long");
232                         argv.push_back("-Wshadow");
233                         if(tag=="CC")
234                         {
235                                 argv.push_back("-Wc++-compat");
236                                 argv.push_back("-Wstrict-prototypes");
237                         }
238                 }
239                 if(binfo.warning_level>=4)
240                 {
241                         // Some truly paranoid warnings
242                         argv.push_back("-Wstrict-overflow=4");
243                         argv.push_back("-Wfloat-equal");
244                         argv.push_back("-Wconversion");
245                         argv.push_back("-Wwrite-strings");
246                         argv.push_back("-Winline");
247                 }
248                 if(binfo.fatal_warnings)
249                 {
250                         argv.push_back("-Werror");
251                         argv.push_back("-Wno-error=deprecated-declarations");
252                 }
253         }
254
255         const FS::Path &sysroot = binfo.sysroot;
256         if(!sysroot.empty())
257                 argv.push_back("--sysroot="+sysroot.str());
258         for(const FS::Path &p: binfo.local_incpath)
259         {
260                 argv.push_back("-iquote");
261                 argv.push_back(p.str());
262         }
263         for(const FS::Path &p: binfo.incpath)
264                 argv.push_back("-I"+p.str());
265
266         for(const auto &kvp: binfo.defines)
267         {
268                 if(kvp.second.empty())
269                         argv.push_back(format("-D%s", kvp.first));
270                 else
271                         argv.push_back(format("-D%s=%s", kvp.first, kvp.second));
272         }
273
274         if(binfo.debug)
275                 argv.push_back("-ggdb");
276         if(binfo.optimize)
277         {
278                 if(binfo.optimize<0)
279                         argv.push_back("-Os");
280                 else
281                         argv.push_back(format("-O%d", binfo.optimize));
282                 if(binfo.debug)
283                         argv.push_back("-fno-omit-frame-pointer");
284         }
285         if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
286                 argv.push_back("-pthread");
287         if(object.is_used_in_shared_library() && architecture->get_system()!="windows")
288                 argv.push_back("-fPIC");
289
290         if((architecture->get_type()=="x86" || architecture->get_type()=="ppc") && !architecture->is_native())
291                 argv.push_back(format("-m%d", architecture->get_bits()));
292
293         string cpu = architecture->get_cpu();
294         if(!cpu.empty())
295         {
296                 for(unsigned i=0; cpus[i]; i+=2)
297                         if(cpu==cpus[i])
298                         {
299                                 cpu = cpus[i+1];
300                                 break;
301                         }
302                 argv.push_back("-march="+cpu);
303         }
304
305         if(!architecture->get_fpu().empty())
306         {
307                 if(architecture->get_type()=="x86")
308                 {
309                         if(architecture->get_fpu()=="387")
310                                 argv.push_back("-mfpmath=387");
311                         else if(!architecture->get_fpu().compare(0, 3, "sse"))
312                                 argv.push_back("-mfpmath=sse");
313
314                         if(architecture->get_fpu()=="sse")
315                                 argv.push_back("-msse2");
316                         else if(architecture->get_fpu()=="sse3")
317                                 argv.push_back("-msse3");
318                         else if(architecture->get_fpu()=="sse4.1")
319                                 argv.push_back("-msse4.1");
320                 }
321                 else if(architecture->get_type()=="arm")
322                 {
323                         argv.push_back("-mfpu="+architecture->get_fpu());
324                         argv.push_back("-mfloat-abi=softfp");
325                 }
326         }
327
328         FS::Path obj_path = object.get_path();
329         FS::Path src_path = object.get_source().get_path();
330         FS::Path work_dir = object.get_component()->get_package().get_source_directory();
331
332         argv.push_back("-o");
333         argv.push_back(relative(obj_path, work_dir).str());
334         argv.push_back(relative(src_path, work_dir).str());
335
336         return new ExternalTask(argv, work_dir);
337 }