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