]> git.tdb.fi Git - builder.git/blob - source/gnucompiler.cpp
Avoid zeroing version of -dumpfullversion doesn't return anything
[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 {
33         if(tag=="CC")
34         {
35                 input_suffixes.push_back(".c");
36                 aux_suffixes.push_back(".h");
37         }
38         else if(tag=="CXX")
39         {
40                 input_suffixes.push_back(".cpp");
41                 input_suffixes.push_back(".cc");
42                 aux_suffixes.push_back(".hpp");
43         }
44         else if(tag=="OBJC")
45         {
46                 input_suffixes.push_back(".m");
47                 build_info.libs.push_back("objc");
48         }
49         else
50                 throw invalid_argument("GnuCompiler::GnuCompiler");
51
52         set_command((tag=="CXX" ? "g++" : "gcc"), true);
53         set_run(_run);
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(ToolData &tool) const
106 {
107         prepare_syspath(tool);
108         prepare_version(tool);
109 }
110
111 void GnuCompiler::prepare_syspath(ToolData &tool) const
112 {
113         bool path_found = false;
114         const FS::Path &sysroot = tool.build_info.sysroot;
115         const std::string &tool_tag = static_cast<Tool &>(tool).get_tag();
116
117         const FileTarget *exe = static_cast<Tool &>(tool).get_executable();
118         if(exe)
119         {
120                 ExternalTask::Arguments argv;
121                 argv.push_back(exe->get_path().str());
122                 argv.push_back("-Wp,-v");
123                 argv.push_back("-E");
124                 if(tag=="CXX")
125                         argv.push_back("-xc++");
126                 if(!sysroot.empty())
127                         argv.push_back("--sysroot="+sysroot.str());
128                 argv.push_back("-");
129
130                 builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
131                 try
132                 {
133                         string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
134                         string::size_type start = 0;
135                         bool record_path = false;
136                         while(start<output.size())
137                         {
138                                 string::size_type newline = output.find('\n', start);
139                                 if(!output.compare(start, 34, "#include <...> search starts here:"))
140                                 {
141                                         record_path = true;
142                                         path_found = true;
143                                 }
144                                 else if(!output.compare(start, 19, "End of search list."))
145                                         record_path = false;
146                                 else if(record_path)
147                                 {
148                                         FS::Path path = strip(output.substr(start, newline-start));
149                                         builder.get_logger().log("tools", "Got %s system path: %s", tool_tag, path);
150                                         tool.system_path.push_back(path);
151                                 }
152                                 start = newline+1;
153                         }
154                 }
155                 catch(const runtime_error &)
156                 { }
157         }
158
159         if(!path_found)
160         {
161                 builder.get_logger().log("tools", "No %s system path found, using defaults", tool_tag);
162                 const Architecture &arch = *static_cast<Tool &>(tool).get_architecture();
163                 if(!sysroot.empty())
164                         tool.system_path.push_back(sysroot/"usr/include");
165                 else if(arch.is_native())
166                         tool.system_path.push_back("/usr/include");
167                 else
168                         tool.system_path.push_back(format("/usr/%s/include", arch.get_cross_prefix()));
169         }
170 }
171
172 void GnuCompiler::prepare_version(ToolData &tool) const
173 {
174         const FileTarget *exe = static_cast<Tool &>(tool).get_executable();
175         if(!exe)
176                 return;
177
178         string exe_path = exe->get_path().str();
179         unsigned version = query_version(exe_path, "-dumpversion");
180         if(version>=0x70000)
181         {
182                 unsigned v = query_version(exe_path, "-dumpfullversion");
183                 if(v)
184                         version = v;
185         }
186         tool.extra_data = version;
187         builder.get_logger().log("tools", "%s version is %d.%d.%d", FS::basename(exe->get_path()), version>>16, (version>>8)&0xFF, version&0xFF);
188 }
189
190 unsigned GnuCompiler::query_version(const string &exe_path, const string &arg) const
191 {
192         ExternalTask::Arguments argv;
193         argv.push_back(exe_path);
194         argv.push_back(arg);
195
196         builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
197         try
198         {
199                 string version_str = strip(ExternalTask::run_and_capture_output(argv));
200
201                 vector<string> version_parts = split(version_str, '.');
202                 unsigned ver = 0;
203                 for(unsigned i=0; (i<3 && i<version_parts.size()); ++i)
204                         ver |= lexical_cast<unsigned>(version_parts[i])<<(16-8*i);
205                 return ver;
206         }
207         catch(const runtime_error &)
208         { }
209
210         return 0;
211 }
212
213 Task *GnuCompiler::_run(const ObjectFile &object)
214 {
215         const Tool &tool = *object.get_tool();
216         const Architecture &arch = *tool.get_architecture();
217
218         ExternalTask::Arguments argv;
219         argv.push_back(tool.get_executable()->get_path().str());
220         argv.push_back("-c");
221
222         BuildInfo binfo;
223         object.collect_build_info(binfo);
224
225         const std::string &tool_tag = tool.get_tag();
226         string tag_for_std = (tool_tag=="OBJC" ? "CC" : tool_tag);
227         if(binfo.standards.count(tag_for_std))
228                 argv.push_back("-std="+get_item(binfo.standards, tag_for_std).str());
229         if(tool_tag=="OBJC" && binfo.standards.count(tool_tag))
230                 argv.push_back("-fobjc-std="+get_item(binfo.standards, tool_tag).str());
231
232         if(binfo.warning_level>=1)
233         {
234                 argv.push_back("-Wall");
235                 if(binfo.warning_level>=2)
236                 {
237                         argv.push_back("-Wextra");
238                         argv.push_back("-Wundef");
239                         unsigned version = tool.get_extra_data();
240                         if(version>=0x80000)
241                                 argv.push_back("-Wno-cast-function-type");
242                 }
243                 if(binfo.warning_level>=3)
244                 {
245                         argv.push_back("-pedantic");
246                         argv.push_back("-Wno-long-long");
247                         argv.push_back("-Wshadow");
248                         if(tool_tag=="CC")
249                         {
250                                 argv.push_back("-Wc++-compat");
251                                 argv.push_back("-Wstrict-prototypes");
252                         }
253                 }
254                 if(binfo.warning_level>=4)
255                 {
256                         // Some truly paranoid warnings
257                         argv.push_back("-Wstrict-overflow=4");
258                         argv.push_back("-Wfloat-equal");
259                         argv.push_back("-Wconversion");
260                         argv.push_back("-Wwrite-strings");
261                         argv.push_back("-Winline");
262                 }
263                 if(binfo.fatal_warnings)
264                 {
265                         argv.push_back("-Werror");
266                         argv.push_back("-Wno-error=deprecated-declarations");
267                 }
268         }
269
270         const FS::Path &sysroot = binfo.sysroot;
271         if(!sysroot.empty())
272                 argv.push_back("--sysroot="+sysroot.str());
273         for(const FS::Path &p: binfo.local_incpath)
274         {
275                 argv.push_back("-iquote");
276                 argv.push_back(p.str());
277         }
278         for(const FS::Path &p: binfo.incpath)
279                 argv.push_back("-I"+p.str());
280
281         for(const auto &kvp: binfo.defines)
282         {
283                 if(kvp.second.empty())
284                         argv.push_back(format("-D%s", kvp.first));
285                 else
286                         argv.push_back(format("-D%s=%s", kvp.first, kvp.second));
287         }
288
289         if(binfo.debug)
290                 argv.push_back("-ggdb");
291         if(binfo.optimize)
292         {
293                 if(binfo.optimize<0)
294                         argv.push_back("-Os");
295                 else
296                         argv.push_back(format("-O%d", binfo.optimize));
297                 if(binfo.debug)
298                         argv.push_back("-fno-omit-frame-pointer");
299         }
300         if(binfo.threads && arch.get_system()!="windows" && arch.get_system()!="darwin")
301                 argv.push_back("-pthread");
302         if(object.is_used_in_shared_library() && arch.get_system()!="windows")
303                 argv.push_back("-fPIC");
304
305         if((arch.get_type()=="x86" || arch.get_type()=="ppc") && !arch.is_native())
306                 argv.push_back(format("-m%d", arch.get_bits()));
307
308         string cpu = arch.get_cpu();
309         if(!cpu.empty())
310         {
311                 for(unsigned i=0; cpus[i]; i+=2)
312                         if(cpu==cpus[i])
313                         {
314                                 cpu = cpus[i+1];
315                                 break;
316                         }
317                 argv.push_back("-march="+cpu);
318         }
319
320         if(!arch.get_fpu().empty())
321         {
322                 if(arch.get_type()=="x86")
323                 {
324                         if(arch.get_fpu()=="387")
325                                 argv.push_back("-mfpmath=387");
326                         else if(!arch.get_fpu().compare(0, 3, "sse"))
327                                 argv.push_back("-mfpmath=sse");
328
329                         if(arch.get_fpu()=="sse")
330                                 argv.push_back("-msse2");
331                         else if(arch.get_fpu()=="sse3")
332                                 argv.push_back("-msse3");
333                         else if(arch.get_fpu()=="sse4.1")
334                                 argv.push_back("-msse4.1");
335                 }
336                 else if(arch.get_type()=="arm")
337                 {
338                         argv.push_back("-mfpu="+arch.get_fpu());
339                         argv.push_back("-mfloat-abi=softfp");
340                 }
341         }
342
343         FS::Path obj_path = object.get_path();
344         FS::Path src_path = object.get_source().get_path();
345         FS::Path work_dir = object.get_component()->get_package().get_source_directory();
346
347         argv.push_back("-o");
348         argv.push_back(relative(obj_path, work_dir).str());
349         argv.push_back(relative(src_path, work_dir).str());
350
351         return new ExternalTask(argv, work_dir);
352 }