]> git.tdb.fi Git - builder.git/blob - plugins/gnu/gnucompiler.cpp
Move some path and filename manipulations into the Component class
[builder.git] / plugins / gnu / gnucompiler.cpp
1 #include <msp/builder/architecture.h>
2 #include <msp/builder/builder.h>
3 #include <msp/builder/component.h>
4 #include <msp/builder/csourcefile.h>
5 #include <msp/builder/objcsourcefile.h>
6 #include <msp/builder/objectfile.h>
7 #include <msp/builder/sourcefile.h>
8 #include <msp/builder/sourcepackage.h>
9 #include <msp/core/maputils.h>
10 #include <msp/fs/utils.h>
11 #include <msp/strings/format.h>
12 #include <msp/strings/utils.h>
13 #include "gnucompiler.h"
14
15 using namespace std;
16 using namespace Msp;
17
18 namespace {
19
20 const char *cpus[] =
21 {
22         "athlonxp", "athlon-xp",
23         "armv7a",   "armv7-a",
24         0
25 };
26
27 }
28
29 GnuCompiler::GnuCompiler(Builder &b, const Architecture &a, const string &t):
30         Tool(b, &a, t)
31 {
32         if(tag=="CC")
33         {
34                 input_suffixes.push_back(".c");
35                 aux_suffixes.push_back(".h");
36         }
37         else if(tag=="CXX")
38         {
39                 input_suffixes.push_back(".cpp");
40                 input_suffixes.push_back(".cc");
41                 aux_suffixes.push_back(".hpp");
42         }
43         else if(tag=="OBJC")
44         {
45                 input_suffixes.push_back(".m");
46                 build_info.libs.push_back("objc");
47         }
48         else
49                 throw invalid_argument("GnuCompiler::GnuCompiler");
50
51         set_command((tag=="CXX" ? "g++" : "gcc"), true);
52         set_run_external(_run);
53 }
54
55 Target *GnuCompiler::create_source(const Component &comp, const FS::Path &path) const
56 {
57         if(tag=="OBJC")
58                 return new ObjCSourceFile(builder, comp, path);
59         else
60                 return new CSourceFile(builder, comp, path);
61 }
62
63 Target *GnuCompiler::create_source(const FS::Path &path) const
64 {
65         if(tag=="OBJC")
66                 return new ObjCSourceFile(builder, path);
67         else
68                 return new CSourceFile(builder, path);
69 }
70
71 Target *GnuCompiler::create_target(const vector<Target *> &sources, const string &)
72 {
73         if(sources.size()!=1)
74                 throw invalid_argument("GnuCompiler::create_target");
75         SourceFile &source = dynamic_cast<SourceFile &>(*sources.front());
76         ObjectFile *obj = new ObjectFile(builder, *source.get_component(), source);
77         obj->set_tool(*this);
78         return obj;
79 }
80
81 string GnuCompiler::create_build_signature(const BuildInfo &binfo) const
82 {
83         if(!executable)
84                 return string();
85
86         string result = Tool::create_build_signature(binfo);
87         if(!architecture->get_cpu().empty())
88         {
89                 result += ",m";
90                 result += architecture->get_cpu();
91         }
92         if(binfo.debug || binfo.optimize)
93                 result += ',';
94         if(binfo.debug)
95                 result += 'g';
96         if(binfo.optimize)
97         {
98                 result += 'O';
99                 result += (binfo.optimize>0 ? '0'+binfo.optimize : 's');
100         }
101         return result;
102 }
103
104 void GnuCompiler::do_prepare(ToolData &tool) const
105 {
106         tool.extra_data = 0U;
107         prepare_syspath(tool);
108         prepare_version(tool);
109
110         if(tag=="CXX")
111                 tool.build_info.libs.push_back("stdc++");
112 }
113
114 void GnuCompiler::prepare_syspath(ToolData &tool) const
115 {
116         bool path_found = false;
117         const FS::Path &sysroot = tool.build_info.sysroot;
118         const std::string &tool_tag = static_cast<Tool &>(tool).get_tag();
119
120         const FileTarget *exe = static_cast<Tool &>(tool).get_executable();
121         if(exe)
122         {
123                 ExternalTask::Arguments argv;
124                 argv.push_back(exe->get_path().str());
125                 argv.push_back("-Wp,-v");
126                 argv.push_back("-E");
127                 if(tag=="CXX")
128                         argv.push_back("-xc++");
129                 if(!sysroot.empty())
130                         argv.push_back("--sysroot="+sysroot.str());
131                 argv.push_back("-");
132
133                 builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
134                 try
135                 {
136                         string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
137                         string::size_type start = 0;
138                         bool record_path = false;
139                         while(start<output.size())
140                         {
141                                 string::size_type newline = output.find('\n', start);
142                                 if(!output.compare(start, 34, "#include <...> search starts here:"))
143                                 {
144                                         record_path = true;
145                                         path_found = true;
146                                 }
147                                 else if(!output.compare(start, 19, "End of search list."))
148                                         record_path = false;
149                                 else if(record_path)
150                                 {
151                                         FS::Path path = strip(output.substr(start, newline-start));
152                                         builder.get_logger().log("tools", "Got %s system path: %s", tool_tag, path);
153                                         tool.system_path.push_back(path);
154                                 }
155                                 start = newline+1;
156                         }
157                 }
158                 catch(const runtime_error &)
159                 { }
160         }
161
162         if(!path_found)
163         {
164                 builder.get_logger().log("tools", "No %s system path found, using defaults", tool_tag);
165                 const Architecture &arch = *static_cast<Tool &>(tool).get_architecture();
166                 if(!sysroot.empty())
167                         tool.system_path.push_back(sysroot/"usr/include");
168                 else if(arch.is_native())
169                         tool.system_path.push_back("/usr/include");
170                 else
171                         tool.system_path.push_back(format("/usr/%s/include", arch.get_cross_prefix()));
172         }
173 }
174
175 void GnuCompiler::prepare_version(ToolData &tool) const
176 {
177         const FileTarget *exe = static_cast<Tool &>(tool).get_executable();
178         if(!exe)
179                 return;
180
181         string exe_path = exe->get_path().str();
182         unsigned version = query_version(exe_path, "-dumpversion");
183         if(version>=0x70000)
184         {
185                 unsigned v = query_version(exe_path, "-dumpfullversion");
186                 if(v)
187                         version = v;
188         }
189         tool.extra_data = version;
190         builder.get_logger().log("tools", "%s version is %d.%d.%d", FS::basename(exe->get_path()), version>>16, (version>>8)&0xFF, version&0xFF);
191 }
192
193 unsigned GnuCompiler::query_version(const string &exe_path, const string &arg) const
194 {
195         ExternalTask::Arguments argv;
196         argv.push_back(exe_path);
197         argv.push_back(arg);
198
199         builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
200         unsigned ver = 0;
201         try
202         {
203                 string version_str = strip(ExternalTask::run_and_capture_output(argv));
204
205                 vector<string> version_parts = split(version_str, '.');
206                 for(unsigned i=0; (i<3 && i<version_parts.size()); ++i)
207                         ver |= lexical_cast<unsigned>(version_parts[i])<<(16-8*i);
208         }
209         catch(const runtime_error &)
210         { }
211
212         return ver;
213 }
214
215 ExternalTask::Arguments GnuCompiler::_run(const ObjectFile &object, FS::Path &work_dir)
216 {
217         const Tool &tool = *object.get_tool();
218         const Architecture &arch = *tool.get_architecture();
219
220         ExternalTask::Arguments argv;
221         argv.push_back(tool.get_executable()->get_path().str());
222         argv.push_back("-c");
223
224         BuildInfo binfo;
225         object.collect_build_info(binfo);
226
227         const std::string &tool_tag = tool.get_tag();
228         string tag_for_std = (tool_tag=="OBJC" ? "CC" : tool_tag);
229         if(binfo.standards.count(tag_for_std))
230                 argv.push_back("-std="+get_item(binfo.standards, tag_for_std).str());
231         if(tool_tag=="OBJC" && binfo.standards.count(tool_tag))
232                 argv.push_back("-fobjc-std="+get_item(binfo.standards, tool_tag).str());
233
234         if(binfo.warning_level>=1)
235         {
236                 argv.push_back("-Wall");
237                 if(binfo.warning_level>=2)
238                 {
239                         argv.push_back("-Wextra");
240                         argv.push_back("-Wundef");
241                         unsigned version = tool.get_extra_data();
242                         if(version>=0x80000)
243                                 argv.push_back("-Wno-cast-function-type");
244                 }
245                 if(binfo.warning_level>=3)
246                 {
247                         argv.push_back("-pedantic");
248                         argv.push_back("-Wno-long-long");
249                         argv.push_back("-Wshadow");
250                         if(tool_tag=="CC")
251                         {
252                                 argv.push_back("-Wc++-compat");
253                                 argv.push_back("-Wstrict-prototypes");
254                         }
255                 }
256                 if(binfo.warning_level>=4)
257                 {
258                         // Some truly paranoid warnings
259                         argv.push_back("-Wstrict-overflow=4");
260                         argv.push_back("-Wfloat-equal");
261                         argv.push_back("-Wconversion");
262                         argv.push_back("-Wwrite-strings");
263                         argv.push_back("-Winline");
264                 }
265                 if(binfo.fatal_warnings)
266                 {
267                         argv.push_back("-Werror");
268                         argv.push_back("-Wno-error=deprecated-declarations");
269                 }
270         }
271
272         const FS::Path &sysroot = binfo.sysroot;
273         if(!sysroot.empty())
274                 argv.push_back("--sysroot="+sysroot.str());
275         for(const FS::Path &p: binfo.local_incpath)
276         {
277                 argv.push_back("-iquote");
278                 argv.push_back(p.str());
279         }
280         for(const FS::Path &p: binfo.incpath)
281                 argv.push_back("-I"+p.str());
282
283         for(const auto &kvp: binfo.defines)
284         {
285                 if(kvp.second.empty())
286                         argv.push_back(format("-D%s", kvp.first));
287                 else
288                         argv.push_back(format("-D%s=%s", kvp.first, kvp.second));
289         }
290
291         if(binfo.debug)
292                 argv.push_back("-ggdb");
293         if(binfo.optimize)
294         {
295                 if(binfo.optimize<0)
296                         argv.push_back("-Os");
297                 else
298                         argv.push_back(format("-O%d", binfo.optimize));
299                 if(binfo.debug)
300                         argv.push_back("-fno-omit-frame-pointer");
301         }
302         if(binfo.threads && arch.get_system()!="windows" && arch.get_system()!="darwin")
303                 argv.push_back("-pthread");
304         if(object.is_used_in_shared_library() && arch.get_system()!="windows")
305         {
306                 argv.push_back("-fvisibility=hidden");
307                 argv.push_back("-fPIC");
308         }
309
310         if((arch.get_type()=="x86" || arch.get_type()=="ppc") && !arch.is_native())
311                 argv.push_back(format("-m%d", arch.get_bits()));
312
313         string cpu = arch.get_cpu();
314         if(!cpu.empty())
315         {
316                 for(unsigned i=0; cpus[i]; i+=2)
317                         if(cpu==cpus[i])
318                         {
319                                 cpu = cpus[i+1];
320                                 break;
321                         }
322                 argv.push_back("-march="+cpu);
323         }
324
325         if(!arch.get_fpu().empty())
326         {
327                 if(arch.get_type()=="x86")
328                 {
329                         if(arch.get_fpu()=="387")
330                                 argv.push_back("-mfpmath=387");
331                         else if(!arch.get_fpu().compare(0, 3, "sse"))
332                                 argv.push_back("-mfpmath=sse");
333
334                         if(arch.get_fpu()=="sse")
335                                 argv.push_back("-msse2");
336                         else if(arch.get_fpu()=="sse3")
337                                 argv.push_back("-msse3");
338                         else if(arch.get_fpu()=="sse4.1")
339                                 argv.push_back("-msse4.1");
340                 }
341                 else if(arch.get_type()=="arm")
342                 {
343                         argv.push_back("-mfpu="+arch.get_fpu());
344                         argv.push_back("-mfloat-abi=softfp");
345                 }
346         }
347
348         FS::Path obj_path = object.get_path();
349         FS::Path src_path = object.get_source().get_path();
350
351         argv.push_back("-o");
352         argv.push_back(relative(obj_path, work_dir).str());
353         argv.push_back(relative(src_path, work_dir).str());
354
355         return argv;
356 }