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);
57 Target *GnuCompiler::create_source(const Component &comp, const FS::Path &path) const
60 return new ObjCSourceFile(builder, comp, path);
62 return new CSourceFile(builder, comp, path);
65 Target *GnuCompiler::create_source(const FS::Path &path) const
68 return new ObjCSourceFile(builder, path);
70 return new CSourceFile(builder, path);
73 Target *GnuCompiler::create_target(const vector<Target *> &sources, const string &)
76 throw invalid_argument("GnuCompiler::create_target");
77 SourceFile &source = dynamic_cast<SourceFile &>(*sources.front());
78 ObjectFile *obj = new ObjectFile(builder, *source.get_component(), source);
83 string GnuCompiler::create_build_signature(const BuildInfo &binfo) const
88 string result = Tool::create_build_signature(binfo);
89 if(!architecture->get_cpu().empty())
92 result += architecture->get_cpu();
94 if(binfo.debug || binfo.optimize)
101 result += (binfo.optimize>0 ? '0'+binfo.optimize : 's');
106 void GnuCompiler::do_prepare()
112 void GnuCompiler::prepare_syspath()
114 bool path_found = false;
115 const FS::Path &sysroot = build_info.sysroot;
119 ExternalTask::Arguments argv;
120 argv.push_back(executable->get_path().str());
121 argv.push_back("-Wp,-v");
122 argv.push_back("-E");
124 argv.push_back("-xc++");
126 argv.push_back("--sysroot="+sysroot.str());
129 builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
132 string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
133 string::size_type start = 0;
134 bool record_path = false;
135 while(start<output.size())
137 string::size_type newline = output.find('\n', start);
138 if(!output.compare(start, 34, "#include <...> search starts here:"))
143 else if(!output.compare(start, 19, "End of search list."))
147 FS::Path path = strip(output.substr(start, newline-start));
148 builder.get_logger().log("tools", "Got %s system path: %s", tag, path);
149 system_path.push_back(path);
154 catch(const runtime_error &)
160 builder.get_logger().log("tools", "No %s system path found, using defaults", tag);
162 system_path.push_back(sysroot/"usr/include");
163 else if(architecture->is_native())
164 system_path.push_back("/usr/include");
166 system_path.push_back(format("/usr/%s/include", architecture->get_cross_prefix()));
170 void GnuCompiler::prepare_version()
175 prepare_version("-dumpversion");
177 prepare_version("-dumpfullversion");
178 builder.get_logger().log("tools", "%s version is %d.%d.%d", FS::basename(executable->get_path()), version>>16, (version>>8)&0xFF, version&0xFF);
181 void GnuCompiler::prepare_version(const string &arg)
183 ExternalTask::Arguments argv;
184 argv.push_back(executable->get_path().str());
187 builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
190 string version_str = strip(ExternalTask::run_and_capture_output(argv));
192 vector<string> version_parts = split(version_str, '.');
194 for(unsigned i=0; (i<3 && i<version_parts.size()); ++i)
195 version |= lexical_cast<unsigned>(version_parts[i])<<(16-8*i);
197 catch(const runtime_error &)
201 Task *GnuCompiler::_run(const ObjectFile &object)
203 const GnuCompiler &tool = dynamic_cast<const GnuCompiler &>(*object.get_tool());
204 const Architecture &arch = *tool.get_architecture();
206 ExternalTask::Arguments argv;
207 argv.push_back(tool.get_executable()->get_path().str());
208 argv.push_back("-c");
211 object.collect_build_info(binfo);
213 const std::string &tool_tag = tool.get_tag();
214 string tag_for_std = (tool_tag=="OBJC" ? "CC" : tool_tag);
215 if(binfo.standards.count(tag_for_std))
216 argv.push_back("-std="+get_item(binfo.standards, tag_for_std).str());
217 if(tool_tag=="OBJC" && binfo.standards.count(tool_tag))
218 argv.push_back("-fobjc-std="+get_item(binfo.standards, tool_tag).str());
220 if(binfo.warning_level>=1)
222 argv.push_back("-Wall");
223 if(binfo.warning_level>=2)
225 argv.push_back("-Wextra");
226 argv.push_back("-Wundef");
227 if(tool.version>=0x80000)
228 argv.push_back("-Wno-cast-function-type");
230 if(binfo.warning_level>=3)
232 argv.push_back("-pedantic");
233 argv.push_back("-Wno-long-long");
234 argv.push_back("-Wshadow");
237 argv.push_back("-Wc++-compat");
238 argv.push_back("-Wstrict-prototypes");
241 if(binfo.warning_level>=4)
243 // Some truly paranoid warnings
244 argv.push_back("-Wstrict-overflow=4");
245 argv.push_back("-Wfloat-equal");
246 argv.push_back("-Wconversion");
247 argv.push_back("-Wwrite-strings");
248 argv.push_back("-Winline");
250 if(binfo.fatal_warnings)
252 argv.push_back("-Werror");
253 argv.push_back("-Wno-error=deprecated-declarations");
257 const FS::Path &sysroot = binfo.sysroot;
259 argv.push_back("--sysroot="+sysroot.str());
260 for(const FS::Path &p: binfo.local_incpath)
262 argv.push_back("-iquote");
263 argv.push_back(p.str());
265 for(const FS::Path &p: binfo.incpath)
266 argv.push_back("-I"+p.str());
268 for(const auto &kvp: binfo.defines)
270 if(kvp.second.empty())
271 argv.push_back(format("-D%s", kvp.first));
273 argv.push_back(format("-D%s=%s", kvp.first, kvp.second));
277 argv.push_back("-ggdb");
281 argv.push_back("-Os");
283 argv.push_back(format("-O%d", binfo.optimize));
285 argv.push_back("-fno-omit-frame-pointer");
287 if(binfo.threads && arch.get_system()!="windows" && arch.get_system()!="darwin")
288 argv.push_back("-pthread");
289 if(object.is_used_in_shared_library() && arch.get_system()!="windows")
290 argv.push_back("-fPIC");
292 if((arch.get_type()=="x86" || arch.get_type()=="ppc") && !arch.is_native())
293 argv.push_back(format("-m%d", arch.get_bits()));
295 string cpu = arch.get_cpu();
298 for(unsigned i=0; cpus[i]; i+=2)
304 argv.push_back("-march="+cpu);
307 if(!arch.get_fpu().empty())
309 if(arch.get_type()=="x86")
311 if(arch.get_fpu()=="387")
312 argv.push_back("-mfpmath=387");
313 else if(!arch.get_fpu().compare(0, 3, "sse"))
314 argv.push_back("-mfpmath=sse");
316 if(arch.get_fpu()=="sse")
317 argv.push_back("-msse2");
318 else if(arch.get_fpu()=="sse3")
319 argv.push_back("-msse3");
320 else if(arch.get_fpu()=="sse4.1")
321 argv.push_back("-msse4.1");
323 else if(arch.get_type()=="arm")
325 argv.push_back("-mfpu="+arch.get_fpu());
326 argv.push_back("-mfloat-abi=softfp");
330 FS::Path obj_path = object.get_path();
331 FS::Path src_path = object.get_source().get_path();
332 FS::Path work_dir = object.get_component()->get_package().get_source_directory();
334 argv.push_back("-o");
335 argv.push_back(relative(obj_path, work_dir).str());
336 argv.push_back(relative(src_path, work_dir).str());
338 return new ExternalTask(argv, work_dir);