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):
35 input_suffixes.push_back(".c");
36 aux_suffixes.push_back(".h");
40 input_suffixes.push_back(".cpp");
41 input_suffixes.push_back(".cc");
42 aux_suffixes.push_back(".hpp");
46 input_suffixes.push_back(".m");
47 build_info.libs.push_back("objc");
50 throw invalid_argument("GnuCompiler::GnuCompiler");
52 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 vector<Target *> &sources, const string &)
75 throw invalid_argument("GnuCompiler::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 = Tool::create_build_signature(binfo);
88 if(!architecture->get_cpu().empty())
91 result += architecture->get_cpu();
93 if(binfo.debug || binfo.optimize)
100 result += (binfo.optimize>0 ? '0'+binfo.optimize : 's');
105 void GnuCompiler::do_prepare(ToolData &tool) const
107 tool.extra_data = 0U;
108 prepare_syspath(tool);
109 prepare_version(tool);
112 tool.build_info.libs.push_back("stdc++");
115 void GnuCompiler::prepare_syspath(ToolData &tool) const
117 bool path_found = false;
118 const FS::Path &sysroot = tool.build_info.sysroot;
119 const std::string &tool_tag = static_cast<Tool &>(tool).get_tag();
121 const FileTarget *exe = static_cast<Tool &>(tool).get_executable();
124 ExternalTask::Arguments argv;
125 argv.push_back(exe->get_path().str());
126 argv.push_back("-Wp,-v");
127 argv.push_back("-E");
129 argv.push_back("-xc++");
131 argv.push_back("--sysroot="+sysroot.str());
134 builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
137 string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
138 string::size_type start = 0;
139 bool record_path = false;
140 while(start<output.size())
142 string::size_type newline = output.find('\n', start);
143 if(!output.compare(start, 34, "#include <...> search starts here:"))
148 else if(!output.compare(start, 19, "End of search list."))
152 FS::Path path = strip(output.substr(start, newline-start));
153 builder.get_logger().log("tools", "Got %s system path: %s", tool_tag, path);
154 tool.system_path.push_back(path);
159 catch(const runtime_error &)
165 builder.get_logger().log("tools", "No %s system path found, using defaults", tool_tag);
166 const Architecture &arch = *static_cast<Tool &>(tool).get_architecture();
168 tool.system_path.push_back(sysroot/"usr/include");
169 else if(arch.is_native())
170 tool.system_path.push_back("/usr/include");
172 tool.system_path.push_back(format("/usr/%s/include", arch.get_cross_prefix()));
176 void GnuCompiler::prepare_version(ToolData &tool) const
178 const FileTarget *exe = static_cast<Tool &>(tool).get_executable();
182 string exe_path = exe->get_path().str();
183 unsigned version = query_version(exe_path, "-dumpversion");
186 unsigned v = query_version(exe_path, "-dumpfullversion");
190 tool.extra_data = version;
191 builder.get_logger().log("tools", "%s version is %d.%d.%d", FS::basename(exe->get_path()), version>>16, (version>>8)&0xFF, version&0xFF);
194 unsigned GnuCompiler::query_version(const string &exe_path, const string &arg) const
196 ExternalTask::Arguments argv;
197 argv.push_back(exe_path);
200 builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
204 string version_str = strip(ExternalTask::run_and_capture_output(argv));
206 vector<string> version_parts = split(version_str, '.');
207 for(unsigned i=0; (i<3 && i<version_parts.size()); ++i)
208 ver |= lexical_cast<unsigned>(version_parts[i])<<(16-8*i);
210 catch(const runtime_error &)
216 Task *GnuCompiler::_run(const ObjectFile &object)
218 const Tool &tool = *object.get_tool();
219 const Architecture &arch = *tool.get_architecture();
221 ExternalTask::Arguments argv;
222 argv.push_back(tool.get_executable()->get_path().str());
223 argv.push_back("-c");
226 object.collect_build_info(binfo);
228 const std::string &tool_tag = tool.get_tag();
229 string tag_for_std = (tool_tag=="OBJC" ? "CC" : tool_tag);
230 if(binfo.standards.count(tag_for_std))
231 argv.push_back("-std="+get_item(binfo.standards, tag_for_std).str());
232 if(tool_tag=="OBJC" && binfo.standards.count(tool_tag))
233 argv.push_back("-fobjc-std="+get_item(binfo.standards, tool_tag).str());
235 if(binfo.warning_level>=1)
237 argv.push_back("-Wall");
238 if(binfo.warning_level>=2)
240 argv.push_back("-Wextra");
241 argv.push_back("-Wundef");
242 unsigned version = tool.get_extra_data();
244 argv.push_back("-Wno-cast-function-type");
246 if(binfo.warning_level>=3)
248 argv.push_back("-pedantic");
249 argv.push_back("-Wno-long-long");
250 argv.push_back("-Wshadow");
253 argv.push_back("-Wc++-compat");
254 argv.push_back("-Wstrict-prototypes");
257 if(binfo.warning_level>=4)
259 // Some truly paranoid warnings
260 argv.push_back("-Wstrict-overflow=4");
261 argv.push_back("-Wfloat-equal");
262 argv.push_back("-Wconversion");
263 argv.push_back("-Wwrite-strings");
264 argv.push_back("-Winline");
266 if(binfo.fatal_warnings)
268 argv.push_back("-Werror");
269 argv.push_back("-Wno-error=deprecated-declarations");
273 const FS::Path &sysroot = binfo.sysroot;
275 argv.push_back("--sysroot="+sysroot.str());
276 for(const FS::Path &p: binfo.local_incpath)
278 argv.push_back("-iquote");
279 argv.push_back(p.str());
281 for(const FS::Path &p: binfo.incpath)
282 argv.push_back("-I"+p.str());
284 for(const auto &kvp: binfo.defines)
286 if(kvp.second.empty())
287 argv.push_back(format("-D%s", kvp.first));
289 argv.push_back(format("-D%s=%s", kvp.first, kvp.second));
293 argv.push_back("-ggdb");
297 argv.push_back("-Os");
299 argv.push_back(format("-O%d", binfo.optimize));
301 argv.push_back("-fno-omit-frame-pointer");
303 if(binfo.threads && arch.get_system()!="windows" && arch.get_system()!="darwin")
304 argv.push_back("-pthread");
305 if(object.is_used_in_shared_library() && arch.get_system()!="windows")
306 argv.push_back("-fPIC");
308 if((arch.get_type()=="x86" || arch.get_type()=="ppc") && !arch.is_native())
309 argv.push_back(format("-m%d", arch.get_bits()));
311 string cpu = arch.get_cpu();
314 for(unsigned i=0; cpus[i]; i+=2)
320 argv.push_back("-march="+cpu);
323 if(!arch.get_fpu().empty())
325 if(arch.get_type()=="x86")
327 if(arch.get_fpu()=="387")
328 argv.push_back("-mfpmath=387");
329 else if(!arch.get_fpu().compare(0, 3, "sse"))
330 argv.push_back("-mfpmath=sse");
332 if(arch.get_fpu()=="sse")
333 argv.push_back("-msse2");
334 else if(arch.get_fpu()=="sse3")
335 argv.push_back("-msse3");
336 else if(arch.get_fpu()=="sse4.1")
337 argv.push_back("-msse4.1");
339 else if(arch.get_type()=="arm")
341 argv.push_back("-mfpu="+arch.get_fpu());
342 argv.push_back("-mfloat-abi=softfp");
346 FS::Path obj_path = object.get_path();
347 FS::Path src_path = object.get_source().get_path();
348 FS::Path work_dir = object.get_component()->get_package().get_source_directory();
350 argv.push_back("-o");
351 argv.push_back(relative(obj_path, work_dir).str());
352 argv.push_back(relative(src_path, work_dir).str());
354 return new ExternalTask(argv, work_dir);