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