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