]> git.tdb.fi Git - builder.git/blob - source/gnucompiler.cpp
Refactor GnuCompiler::prepare_version a bit
[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         set_run(_run);
55 }
56
57 Target *GnuCompiler::create_source(const Component &comp, const FS::Path &path) const
58 {
59         if(tag=="OBJC")
60                 return new ObjCSourceFile(builder, comp, path);
61         else
62                 return new CSourceFile(builder, comp, path);
63 }
64
65 Target *GnuCompiler::create_source(const FS::Path &path) const
66 {
67         if(tag=="OBJC")
68                 return new ObjCSourceFile(builder, path);
69         else
70                 return new CSourceFile(builder, path);
71 }
72
73 Target *GnuCompiler::create_target(const vector<Target *> &sources, const string &)
74 {
75         if(sources.size()!=1)
76                 throw invalid_argument("GnuCompiler::create_target");
77         SourceFile &source = dynamic_cast<SourceFile &>(*sources.front());
78         ObjectFile *obj = new ObjectFile(builder, *source.get_component(), source);
79         obj->set_tool(*this);
80         return obj;
81 }
82
83 string GnuCompiler::create_build_signature(const BuildInfo &binfo) const
84 {
85         if(!executable)
86                 return string();
87
88         string result = Tool::create_build_signature(binfo);
89         if(!architecture->get_cpu().empty())
90         {
91                 result += ",m";
92                 result += architecture->get_cpu();
93         }
94         if(binfo.debug || binfo.optimize)
95                 result += ',';
96         if(binfo.debug)
97                 result += 'g';
98         if(binfo.optimize)
99         {
100                 result += 'O';
101                 result += (binfo.optimize>0 ? '0'+binfo.optimize : 's');
102         }
103         return result;
104 }
105
106 void GnuCompiler::do_prepare()
107 {
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         string exe_path = executable->get_path().str();
176         version = query_version("-dumpversion");
177         if(version>=0x70000)
178                 version = query_version("-dumpfullversion");
179         builder.get_logger().log("tools", "%s version is %d.%d.%d", FS::basename(executable->get_path()), version>>16, (version>>8)&0xFF, version&0xFF);
180 }
181
182 unsigned GnuCompiler::query_version(const string &arg) const
183 {
184         ExternalTask::Arguments argv;
185         argv.push_back(executable->get_path().str());
186         argv.push_back(arg);
187
188         builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
189         try
190         {
191                 string version_str = strip(ExternalTask::run_and_capture_output(argv));
192
193                 vector<string> version_parts = split(version_str, '.');
194                 unsigned ver = 0;
195                 for(unsigned i=0; (i<3 && i<version_parts.size()); ++i)
196                         ver |= lexical_cast<unsigned>(version_parts[i])<<(16-8*i);
197                 return ver;
198         }
199         catch(const runtime_error &)
200         { }
201
202         return 0;
203 }
204
205 Task *GnuCompiler::_run(const ObjectFile &object)
206 {
207         const GnuCompiler &tool = dynamic_cast<const GnuCompiler &>(*object.get_tool());
208         const Architecture &arch = *tool.get_architecture();
209
210         ExternalTask::Arguments argv;
211         argv.push_back(tool.get_executable()->get_path().str());
212         argv.push_back("-c");
213
214         BuildInfo binfo;
215         object.collect_build_info(binfo);
216
217         const std::string &tool_tag = tool.get_tag();
218         string tag_for_std = (tool_tag=="OBJC" ? "CC" : tool_tag);
219         if(binfo.standards.count(tag_for_std))
220                 argv.push_back("-std="+get_item(binfo.standards, tag_for_std).str());
221         if(tool_tag=="OBJC" && binfo.standards.count(tool_tag))
222                 argv.push_back("-fobjc-std="+get_item(binfo.standards, tool_tag).str());
223
224         if(binfo.warning_level>=1)
225         {
226                 argv.push_back("-Wall");
227                 if(binfo.warning_level>=2)
228                 {
229                         argv.push_back("-Wextra");
230                         argv.push_back("-Wundef");
231                         if(tool.version>=0x80000)
232                                 argv.push_back("-Wno-cast-function-type");
233                 }
234                 if(binfo.warning_level>=3)
235                 {
236                         argv.push_back("-pedantic");
237                         argv.push_back("-Wno-long-long");
238                         argv.push_back("-Wshadow");
239                         if(tool_tag=="CC")
240                         {
241                                 argv.push_back("-Wc++-compat");
242                                 argv.push_back("-Wstrict-prototypes");
243                         }
244                 }
245                 if(binfo.warning_level>=4)
246                 {
247                         // Some truly paranoid warnings
248                         argv.push_back("-Wstrict-overflow=4");
249                         argv.push_back("-Wfloat-equal");
250                         argv.push_back("-Wconversion");
251                         argv.push_back("-Wwrite-strings");
252                         argv.push_back("-Winline");
253                 }
254                 if(binfo.fatal_warnings)
255                 {
256                         argv.push_back("-Werror");
257                         argv.push_back("-Wno-error=deprecated-declarations");
258                 }
259         }
260
261         const FS::Path &sysroot = binfo.sysroot;
262         if(!sysroot.empty())
263                 argv.push_back("--sysroot="+sysroot.str());
264         for(const FS::Path &p: binfo.local_incpath)
265         {
266                 argv.push_back("-iquote");
267                 argv.push_back(p.str());
268         }
269         for(const FS::Path &p: binfo.incpath)
270                 argv.push_back("-I"+p.str());
271
272         for(const auto &kvp: binfo.defines)
273         {
274                 if(kvp.second.empty())
275                         argv.push_back(format("-D%s", kvp.first));
276                 else
277                         argv.push_back(format("-D%s=%s", kvp.first, kvp.second));
278         }
279
280         if(binfo.debug)
281                 argv.push_back("-ggdb");
282         if(binfo.optimize)
283         {
284                 if(binfo.optimize<0)
285                         argv.push_back("-Os");
286                 else
287                         argv.push_back(format("-O%d", binfo.optimize));
288                 if(binfo.debug)
289                         argv.push_back("-fno-omit-frame-pointer");
290         }
291         if(binfo.threads && arch.get_system()!="windows" && arch.get_system()!="darwin")
292                 argv.push_back("-pthread");
293         if(object.is_used_in_shared_library() && arch.get_system()!="windows")
294                 argv.push_back("-fPIC");
295
296         if((arch.get_type()=="x86" || arch.get_type()=="ppc") && !arch.is_native())
297                 argv.push_back(format("-m%d", arch.get_bits()));
298
299         string cpu = arch.get_cpu();
300         if(!cpu.empty())
301         {
302                 for(unsigned i=0; cpus[i]; i+=2)
303                         if(cpu==cpus[i])
304                         {
305                                 cpu = cpus[i+1];
306                                 break;
307                         }
308                 argv.push_back("-march="+cpu);
309         }
310
311         if(!arch.get_fpu().empty())
312         {
313                 if(arch.get_type()=="x86")
314                 {
315                         if(arch.get_fpu()=="387")
316                                 argv.push_back("-mfpmath=387");
317                         else if(!arch.get_fpu().compare(0, 3, "sse"))
318                                 argv.push_back("-mfpmath=sse");
319
320                         if(arch.get_fpu()=="sse")
321                                 argv.push_back("-msse2");
322                         else if(arch.get_fpu()=="sse3")
323                                 argv.push_back("-msse3");
324                         else if(arch.get_fpu()=="sse4.1")
325                                 argv.push_back("-msse4.1");
326                 }
327                 else if(arch.get_type()=="arm")
328                 {
329                         argv.push_back("-mfpu="+arch.get_fpu());
330                         argv.push_back("-mfloat-abi=softfp");
331                 }
332         }
333
334         FS::Path obj_path = object.get_path();
335         FS::Path src_path = object.get_source().get_path();
336         FS::Path work_dir = object.get_component()->get_package().get_source_directory();
337
338         argv.push_back("-o");
339         argv.push_back(relative(obj_path, work_dir).str());
340         argv.push_back(relative(src_path, work_dir).str());
341
342         return new ExternalTask(argv, work_dir);
343 }