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"
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"
23 "athlonxp", "athlon-xp",
30 GnuCompiler::GnuCompiler(Builder &b, const Architecture &a, const string &t):
36 input_suffixes.push_back(".c");
37 aux_suffixes.push_back(".h");
41 input_suffixes.push_back(".cpp");
42 input_suffixes.push_back(".cc");
43 aux_suffixes.push_back(".hpp");
47 input_suffixes.push_back(".m");
48 build_info.libs.push_back("objc");
51 throw invalid_argument("GnuCompiler::GnuCompiler");
53 set_command((tag=="CXX" ? "g++" : "gcc"), true);
56 Target *GnuCompiler::create_source(const Component &comp, const FS::Path &path) const
59 return new ObjCSourceFile(builder, comp, path);
61 return new CSourceFile(builder, comp, path);
64 Target *GnuCompiler::create_source(const FS::Path &path) const
67 return new ObjCSourceFile(builder, path);
69 return new CSourceFile(builder, path);
72 Target *GnuCompiler::create_target(const list<Target *> &sources, const string &)
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);
82 string GnuCompiler::create_build_signature(const BuildInfo &binfo) const
87 string result = FS::basename(executable->get_path());
88 if(!architecture->get_cpu().empty())
91 result += architecture->get_cpu();
99 result += (binfo.optimize>0 ? '0'+binfo.optimize : 's');
104 void GnuCompiler::do_prepare()
106 executable = builder.get_vfs().find_binary(command);
111 void GnuCompiler::prepare_syspath()
113 bool path_found = false;
114 const FS::Path &sysroot = build_info.sysroot;
118 ExternalTask::Arguments argv;
119 argv.push_back(executable->get_path().str());
120 argv.push_back("-Wp,-v");
121 argv.push_back("-E");
123 argv.push_back("-xc++");
125 argv.push_back("--sysroot="+sysroot.str());
128 builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
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())
136 string::size_type newline = output.find('\n', start);
137 if(!output.compare(start, 34, "#include <...> search starts here:"))
142 else if(!output.compare(start, 19, "End of search list."))
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);
153 catch(const runtime_error &)
159 builder.get_logger().log("tools", format("No %s system path found, using defaults", tag));
161 system_path.push_back(sysroot/"usr/include");
162 else if(architecture->is_native())
163 system_path.push_back("/usr/include");
165 system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/include");
169 void GnuCompiler::prepare_version()
174 prepare_version("-dumpversion");
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));
180 void GnuCompiler::prepare_version(const string &arg)
182 ExternalTask::Arguments argv;
183 argv.push_back(executable->get_path().str());
186 builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
189 string version_str = strip(ExternalTask::run_and_capture_output(argv));
191 vector<string> version_parts = split(version_str, '.');
193 for(unsigned i=0; (i<3 && i<version_parts.size()); ++i)
194 version |= lexical_cast<unsigned>(version_parts[i])<<(16-8*i);
196 catch(const runtime_error &)
200 Task *GnuCompiler::run(const Target &target) const
202 const ObjectFile &object = dynamic_cast<const ObjectFile &>(target);
204 ExternalTask::Arguments argv;
205 argv.push_back(executable->get_path().str());
206 argv.push_back("-c");
209 target.collect_build_info(binfo);
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));
217 if(binfo.warning_level>=1)
219 argv.push_back("-Wall");
220 if(binfo.warning_level>=2)
222 argv.push_back("-Wextra");
223 argv.push_back("-Wundef");
225 argv.push_back("-Wno-cast-function-type");
227 if(binfo.warning_level>=3)
229 argv.push_back("-pedantic");
230 argv.push_back("-Wno-long-long");
231 argv.push_back("-Wshadow");
234 argv.push_back("-Wc++-compat");
235 argv.push_back("-Wstrict-prototypes");
238 if(binfo.warning_level>=4)
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");
247 if(binfo.fatal_warnings)
249 argv.push_back("-Werror");
250 argv.push_back("-Wno-error=deprecated-declarations");
254 const FS::Path &sysroot = binfo.sysroot;
256 argv.push_back("--sysroot="+sysroot.str());
257 for(BuildInfo::PathList::const_iterator i=binfo.local_incpath.begin(); i!=binfo.local_incpath.end(); ++i)
259 argv.push_back("-iquote");
260 argv.push_back(i->str());
262 for(BuildInfo::PathList::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i)
263 argv.push_back("-I"+i->str());
265 for(BuildInfo::DefineMap::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i)
267 if(i->second.empty())
268 argv.push_back(format("-D%s", i->first));
270 argv.push_back(format("-D%s=%s", i->first, i->second));
274 argv.push_back("-ggdb");
278 argv.push_back("-Os");
280 argv.push_back(format("-O%d", binfo.optimize));
282 argv.push_back("-fno-omit-frame-pointer");
284 if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
285 argv.push_back("-pthread");
286 if(object.is_used_in_shared_library() && architecture->get_system()!="windows")
287 argv.push_back("-fPIC");
289 if((architecture->get_type()=="x86" || architecture->get_type()=="ppc") && !architecture->is_native())
290 argv.push_back(format("-m%d", architecture->get_bits()));
292 string cpu = architecture->get_cpu();
295 for(unsigned i=0; cpus[i]; i+=2)
301 argv.push_back("-march="+cpu);
304 if(!architecture->get_fpu().empty())
306 if(architecture->get_type()=="x86")
308 argv.push_back("-mfpmath="+architecture->get_fpu());
309 if(architecture->get_fpu()=="sse")
310 argv.push_back("-msse2");
312 else if(architecture->get_type()=="arm")
314 argv.push_back("-mfpu="+architecture->get_fpu());
315 argv.push_back("-mfloat-abi=softfp");
319 FS::Path obj_path = object.get_path();
320 FS::Path src_path = object.get_source().get_path();
321 FS::Path work_dir = object.get_component()->get_package().get_source_directory();
323 argv.push_back("-o");
324 argv.push_back(relative(obj_path, work_dir).str());
325 argv.push_back(relative(src_path, work_dir).str());
327 return new ExternalTask(argv, work_dir);