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