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