]> git.tdb.fi Git - builder.git/blob - source/gnucompiler.cpp
Redesign how tools are run
[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         executable = builder.get_vfs().find_binary(command);
109         prepare_syspath();
110         prepare_version();
111 }
112
113 void GnuCompiler::prepare_syspath()
114 {
115         bool path_found = false;
116         const FS::Path &sysroot = build_info.sysroot;
117
118         if(executable)
119         {
120                 ExternalTask::Arguments argv;
121                 argv.push_back(executable->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", tag, path);
150                                         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", tag);
162                 if(!sysroot.empty())
163                         system_path.push_back(sysroot/"usr/include");
164                 else if(architecture->is_native())
165                         system_path.push_back("/usr/include");
166                 else
167                         system_path.push_back(format("/usr/%s/include", architecture->get_cross_prefix()));
168         }
169 }
170
171 void GnuCompiler::prepare_version()
172 {
173         if(!executable)
174                 return;
175
176         prepare_version("-dumpversion");
177         if(version>=0x70000)
178                 prepare_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 void GnuCompiler::prepare_version(const string &arg)
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                 version = 0;
195                 for(unsigned i=0; (i<3 && i<version_parts.size()); ++i)
196                         version |= lexical_cast<unsigned>(version_parts[i])<<(16-8*i);
197         }
198         catch(const runtime_error &)
199         { }
200 }
201
202 Task *GnuCompiler::_run(const ObjectFile &object)
203 {
204         const GnuCompiler &tool = dynamic_cast<const GnuCompiler &>(*object.get_tool());
205         const Architecture &arch = *tool.get_architecture();
206
207         ExternalTask::Arguments argv;
208         argv.push_back(tool.get_executable()->get_path().str());
209         argv.push_back("-c");
210
211         BuildInfo binfo;
212         object.collect_build_info(binfo);
213
214         const std::string &tool_tag = tool.get_tag();
215         string tag_for_std = (tool_tag=="OBJC" ? "CC" : tool_tag);
216         if(binfo.standards.count(tag_for_std))
217                 argv.push_back("-std="+get_item(binfo.standards, tag_for_std).str());
218         if(tool_tag=="OBJC" && binfo.standards.count(tool_tag))
219                 argv.push_back("-fobjc-std="+get_item(binfo.standards, tool_tag).str());
220
221         if(binfo.warning_level>=1)
222         {
223                 argv.push_back("-Wall");
224                 if(binfo.warning_level>=2)
225                 {
226                         argv.push_back("-Wextra");
227                         argv.push_back("-Wundef");
228                         if(tool.version>=0x80000)
229                                 argv.push_back("-Wno-cast-function-type");
230                 }
231                 if(binfo.warning_level>=3)
232                 {
233                         argv.push_back("-pedantic");
234                         argv.push_back("-Wno-long-long");
235                         argv.push_back("-Wshadow");
236                         if(tool_tag=="CC")
237                         {
238                                 argv.push_back("-Wc++-compat");
239                                 argv.push_back("-Wstrict-prototypes");
240                         }
241                 }
242                 if(binfo.warning_level>=4)
243                 {
244                         // Some truly paranoid warnings
245                         argv.push_back("-Wstrict-overflow=4");
246                         argv.push_back("-Wfloat-equal");
247                         argv.push_back("-Wconversion");
248                         argv.push_back("-Wwrite-strings");
249                         argv.push_back("-Winline");
250                 }
251                 if(binfo.fatal_warnings)
252                 {
253                         argv.push_back("-Werror");
254                         argv.push_back("-Wno-error=deprecated-declarations");
255                 }
256         }
257
258         const FS::Path &sysroot = binfo.sysroot;
259         if(!sysroot.empty())
260                 argv.push_back("--sysroot="+sysroot.str());
261         for(const FS::Path &p: binfo.local_incpath)
262         {
263                 argv.push_back("-iquote");
264                 argv.push_back(p.str());
265         }
266         for(const FS::Path &p: binfo.incpath)
267                 argv.push_back("-I"+p.str());
268
269         for(const auto &kvp: binfo.defines)
270         {
271                 if(kvp.second.empty())
272                         argv.push_back(format("-D%s", kvp.first));
273                 else
274                         argv.push_back(format("-D%s=%s", kvp.first, kvp.second));
275         }
276
277         if(binfo.debug)
278                 argv.push_back("-ggdb");
279         if(binfo.optimize)
280         {
281                 if(binfo.optimize<0)
282                         argv.push_back("-Os");
283                 else
284                         argv.push_back(format("-O%d", binfo.optimize));
285                 if(binfo.debug)
286                         argv.push_back("-fno-omit-frame-pointer");
287         }
288         if(binfo.threads && arch.get_system()!="windows" && arch.get_system()!="darwin")
289                 argv.push_back("-pthread");
290         if(object.is_used_in_shared_library() && arch.get_system()!="windows")
291                 argv.push_back("-fPIC");
292
293         if((arch.get_type()=="x86" || arch.get_type()=="ppc") && !arch.is_native())
294                 argv.push_back(format("-m%d", arch.get_bits()));
295
296         string cpu = arch.get_cpu();
297         if(!cpu.empty())
298         {
299                 for(unsigned i=0; cpus[i]; i+=2)
300                         if(cpu==cpus[i])
301                         {
302                                 cpu = cpus[i+1];
303                                 break;
304                         }
305                 argv.push_back("-march="+cpu);
306         }
307
308         if(!arch.get_fpu().empty())
309         {
310                 if(arch.get_type()=="x86")
311                 {
312                         if(arch.get_fpu()=="387")
313                                 argv.push_back("-mfpmath=387");
314                         else if(!arch.get_fpu().compare(0, 3, "sse"))
315                                 argv.push_back("-mfpmath=sse");
316
317                         if(arch.get_fpu()=="sse")
318                                 argv.push_back("-msse2");
319                         else if(arch.get_fpu()=="sse3")
320                                 argv.push_back("-msse3");
321                         else if(arch.get_fpu()=="sse4.1")
322                                 argv.push_back("-msse4.1");
323                 }
324                 else if(arch.get_type()=="arm")
325                 {
326                         argv.push_back("-mfpu="+arch.get_fpu());
327                         argv.push_back("-mfloat-abi=softfp");
328                 }
329         }
330
331         FS::Path obj_path = object.get_path();
332         FS::Path src_path = object.get_source().get_path();
333         FS::Path work_dir = object.get_component()->get_package().get_source_directory();
334
335         argv.push_back("-o");
336         argv.push_back(relative(obj_path, work_dir).str());
337         argv.push_back(relative(src_path, work_dir).str());
338
339         return new ExternalTask(argv, work_dir);
340 }