]> git.tdb.fi Git - builder.git/blob - source/gnucompiler.cpp
16e7ccac1d88da73a34776e288980b295f4f0747
[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, const FS::Path &sysroot):
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
54         if(!sysroot.empty())
55                 build_info.sysroot = sysroot;
56 }
57
58 Target *GnuCompiler::create_source(const Component &comp, const FS::Path &path) const
59 {
60         if(tag=="OBJC")
61                 return new ObjCSourceFile(builder, comp, path);
62         else
63                 return new CSourceFile(builder, comp, path);
64 }
65
66 Target *GnuCompiler::create_source(const FS::Path &path) const
67 {
68         if(tag=="OBJC")
69                 return new ObjCSourceFile(builder, path);
70         else
71                 return new CSourceFile(builder, path);
72 }
73
74 Target *GnuCompiler::create_target(const list<Target *> &sources, const string &)
75 {
76         if(sources.size()!=1)
77                 throw invalid_argument("GnuCCompiler::create_target");
78         SourceFile &source = dynamic_cast<SourceFile &>(*sources.front());
79         ObjectFile *obj = new ObjectFile(builder, *source.get_component(), source);
80         obj->set_tool(*this);
81         return obj;
82 }
83
84 string GnuCompiler::create_build_signature(const BuildInfo &binfo) const
85 {
86         string result = FS::basename(executable->get_path());
87         if(!architecture->get_cpu().empty())
88         {
89                 result += ",m";
90                 result += architecture->get_cpu();
91         }
92         result += ',';
93         if(binfo.debug)
94                 result += 'g';
95         if(binfo.optimize)
96         {
97                 result += 'O';
98                 result += (binfo.optimize>0 ? '0'+binfo.optimize : 's');
99         }
100         return result;
101 }
102
103 void GnuCompiler::do_prepare()
104 {
105         bool path_found = false;
106         const FS::Path &sysroot = build_info.sysroot;
107
108         executable = builder.get_vfs().find_binary(command);
109         if(executable)
110         {
111                 ExternalTask::Arguments argv;
112                 argv.push_back(executable->get_path().str());
113                 argv.push_back("-Wp,-v");
114                 argv.push_back("-E");
115                 if(tag=="CXX")
116                         argv.push_back("-xc++");
117                 if(!sysroot.empty())
118                         argv.push_back("--sysroot="+sysroot.str());
119                 argv.push_back("-");
120
121                 builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
122                 try
123                 {
124                         string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
125                         string::size_type start = 0;
126                         bool record_path = false;
127                         while(start<output.size())
128                         {
129                                 string::size_type newline = output.find('\n', start);
130                                 if(!output.compare(start, 34, "#include <...> search starts here:"))
131                                 {
132                                         record_path = true;
133                                         path_found = true;
134                                 }
135                                 else if(!output.compare(start, 19, "End of search list."))
136                                         record_path = false;
137                                 else if(record_path)
138                                 {
139                                         FS::Path path = strip(output.substr(start, newline-start));
140                                         builder.get_logger().log("tools", format("Got %s system path: %s", tag, path));
141                                         system_path.push_back(path);
142                                 }
143                                 start = newline+1;
144                         }
145                 }
146                 catch(const runtime_error &e)
147                 { }
148         }
149
150         if(!path_found)
151         {
152                 builder.get_logger().log("tools", format("No %s system path found, using defaults", tag));
153                 if(!sysroot.empty())
154                         system_path.push_back(sysroot/"usr/include");
155                 else if(architecture->is_native())
156                         system_path.push_back("/usr/include");
157                 else
158                         system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/include");
159         }
160 }
161
162 Task *GnuCompiler::run(const Target &target) const
163 {
164         const ObjectFile &object = dynamic_cast<const ObjectFile &>(target);
165
166         ExternalTask::Arguments argv;
167         argv.push_back(executable->get_path().str());
168         argv.push_back("-c");
169
170         BuildInfo binfo;
171         target.collect_build_info(binfo);
172
173         string tag_for_std = (tag=="OBJC" ? "CC" : tag);
174         if(binfo.standards.count(tag_for_std))
175                 argv.push_back("-std="+get_item(binfo.standards, tag_for_std));
176         if(tag=="OBJC" && binfo.standards.count(tag))
177                 argv.push_back("-fobjc-std="+get_item(binfo.standards, tag));
178
179         if(binfo.warning_level>=1)
180         {
181                 argv.push_back("-Wall");
182                 if(binfo.warning_level>=2)
183                 {
184                         argv.push_back("-Wextra");
185                         argv.push_back("-Wundef");
186                 }
187                 if(binfo.warning_level>=3)
188                 {
189                         argv.push_back("-pedantic");
190                         argv.push_back("-Wno-long-long");
191                         argv.push_back("-Wshadow");
192                         if(tag=="CC")
193                         {
194                                 argv.push_back("-Wc++-compat");
195                                 argv.push_back("-Wstrict-prototypes");
196                         }
197                 }
198                 if(binfo.warning_level>=4)
199                 {
200                         // Some truly paranoid warnings
201                         argv.push_back("-Wstrict-overflow=4");
202                         argv.push_back("-Wfloat-equal");
203                         argv.push_back("-Wconversion");
204                         argv.push_back("-Wwrite-strings");
205                         argv.push_back("-Winline");
206                 }
207                 if(binfo.fatal_warnings)
208                 {
209                         argv.push_back("-Werror");
210                         argv.push_back("-Wno-error=deprecated-declarations");
211                 }
212         }
213
214         const FS::Path &sysroot = binfo.sysroot;
215         if(!sysroot.empty())
216                 argv.push_back("--sysroot="+sysroot.str());
217         for(BuildInfo::PathList::const_iterator i=binfo.local_incpath.begin(); i!=binfo.local_incpath.end(); ++i)
218         {
219                 argv.push_back("-iquote");
220                 argv.push_back(i->str());
221         }
222         for(BuildInfo::PathList::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i)
223                 argv.push_back("-I"+i->str());
224
225         for(BuildInfo::DefineMap::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i)
226         {
227                 if(i->second.empty())
228                         argv.push_back(format("-D%s", i->first));
229                 else
230                         argv.push_back(format("-D%s=%s", i->first, i->second));
231         }
232
233         if(binfo.debug)
234                 argv.push_back("-ggdb");
235         if(binfo.optimize)
236         {
237                 if(binfo.optimize<0)
238                         argv.push_back("-Os");
239                 else
240                         argv.push_back(format("-O%d", binfo.optimize));
241         }
242         if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
243                 argv.push_back("-pthread");
244         if(object.is_used_in_shared_library() && architecture->get_system()!="windows")
245                 argv.push_back("-fPIC");
246
247         const Architecture &native_arch = builder.get_native_arch();
248         if(architecture->is_native() && architecture->get_bits()!=native_arch.get_bits())
249                 argv.push_back(format("-m%d", architecture->get_bits()));
250
251         string cpu = architecture->get_cpu();
252         if(!cpu.empty())
253         {
254                 for(unsigned i=0; cpus[i]; i+=2)
255                         if(cpu==cpus[i])
256                         {
257                                 cpu = cpus[i+1];
258                                 break;
259                         }
260                 argv.push_back("-march="+cpu);
261         }
262
263         if(!architecture->get_fpu().empty())
264         {
265                 if(architecture->get_type()=="x86")
266                 {
267                         argv.push_back("-mfpmath="+architecture->get_fpu());
268                         if(architecture->get_fpu()=="sse")
269                                 argv.push_back("-msse2");
270                 }
271                 else if(architecture->get_type()=="arm")
272                 {
273                         argv.push_back("-mfpu="+architecture->get_fpu());
274                         argv.push_back("-mfloat-abi=softfp");
275                 }
276         }
277
278         FS::Path obj_path = object.get_path();
279         FS::Path src_path = object.get_source().get_path();
280         FS::Path work_dir = object.get_component()->get_package().get_source_directory();
281
282         argv.push_back("-o");
283         argv.push_back(relative(obj_path, work_dir).str());
284         argv.push_back(relative(src_path, work_dir).str());
285
286         return new ExternalTask(argv, work_dir);
287 }