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