1 #include <msp/builder/architecture.h>
2 #include <msp/builder/builder.h>
3 #include <msp/builder/component.h>
4 #include <msp/builder/csourcefile.h>
5 #include <msp/builder/objcsourcefile.h>
6 #include <msp/builder/objectfile.h>
7 #include <msp/builder/sourcefile.h>
8 #include <msp/builder/sourcepackage.h>
9 #include <msp/core/maputils.h>
10 #include <msp/fs/utils.h>
11 #include <msp/strings/format.h>
12 #include <msp/strings/utils.h>
13 #include "gnucompiler.h"
22 "athlonxp", "athlon-xp",
29 GnuCompiler::GnuCompiler(Builder &b, const Architecture &a, const string &t):
34 input_suffixes.push_back(".c");
35 aux_suffixes.push_back(".h");
39 input_suffixes.push_back(".cpp");
40 input_suffixes.push_back(".cc");
41 aux_suffixes.push_back(".hpp");
45 input_suffixes.push_back(".m");
46 build_info.libs.push_back("objc");
49 throw invalid_argument("GnuCompiler::GnuCompiler");
51 set_command((tag=="CXX" ? "g++" : "gcc"), true);
52 set_run_external(_run);
55 Target *GnuCompiler::create_source(const Component &comp, const FS::Path &path) const
58 return new ObjCSourceFile(builder, comp, path);
60 return new CSourceFile(builder, comp, path);
63 Target *GnuCompiler::create_source(const FS::Path &path) const
66 return new ObjCSourceFile(builder, path);
68 return new CSourceFile(builder, path);
71 Target *GnuCompiler::create_target(const vector<Target *> &sources, const string &)
74 throw invalid_argument("GnuCompiler::create_target");
75 SourceFile &source = dynamic_cast<SourceFile &>(*sources.front());
76 ObjectFile *obj = new ObjectFile(builder, *source.get_component(), source);
81 string GnuCompiler::create_build_signature(const BuildInfo &binfo) const
86 string result = Tool::create_build_signature(binfo);
87 if(!architecture->get_cpu().empty())
90 result += architecture->get_cpu();
92 if(binfo.debug || binfo.optimize)
99 result += (binfo.optimize>0 ? '0'+binfo.optimize : 's');
104 void GnuCompiler::do_prepare(ToolData &tool) const
106 tool.extra_data = 0U;
107 prepare_syspath(tool);
108 prepare_version(tool);
111 tool.build_info.libs.push_back("stdc++");
114 void GnuCompiler::prepare_syspath(ToolData &tool) const
116 bool path_found = false;
117 const FS::Path &sysroot = tool.build_info.sysroot;
118 const std::string &tool_tag = static_cast<Tool &>(tool).get_tag();
120 const FileTarget *exe = static_cast<Tool &>(tool).get_executable();
123 ExternalTask::Arguments argv;
124 argv.push_back(exe->get_path().str());
125 argv.push_back("-Wp,-v");
126 argv.push_back("-E");
128 argv.push_back("-xc++");
130 argv.push_back("--sysroot="+sysroot.str());
133 builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
136 string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
137 string::size_type start = 0;
138 bool record_path = false;
139 while(start<output.size())
141 string::size_type newline = output.find('\n', start);
142 if(!output.compare(start, 34, "#include <...> search starts here:"))
147 else if(!output.compare(start, 19, "End of search list."))
151 FS::Path path = strip(output.substr(start, newline-start));
152 builder.get_logger().log("tools", "Got %s system path: %s", tool_tag, path);
153 tool.system_path.push_back(path);
158 catch(const runtime_error &)
164 builder.get_logger().log("tools", "No %s system path found, using defaults", tool_tag);
165 const Architecture &arch = *static_cast<Tool &>(tool).get_architecture();
167 tool.system_path.push_back(sysroot/"usr/include");
168 else if(arch.is_native())
169 tool.system_path.push_back("/usr/include");
171 tool.system_path.push_back(format("/usr/%s/include", arch.get_cross_prefix()));
175 void GnuCompiler::prepare_version(ToolData &tool) const
177 const FileTarget *exe = static_cast<Tool &>(tool).get_executable();
181 string exe_path = exe->get_path().str();
182 unsigned version = query_version(exe_path, "-dumpversion");
185 unsigned v = query_version(exe_path, "-dumpfullversion");
189 tool.extra_data = version;
190 builder.get_logger().log("tools", "%s version is %d.%d.%d", FS::basename(exe->get_path()), version>>16, (version>>8)&0xFF, version&0xFF);
193 unsigned GnuCompiler::query_version(const string &exe_path, const string &arg) const
195 ExternalTask::Arguments argv;
196 argv.push_back(exe_path);
199 builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
203 string version_str = strip(ExternalTask::run_and_capture_output(argv));
205 vector<string> version_parts = split(version_str, '.');
206 for(unsigned i=0; (i<3 && i<version_parts.size()); ++i)
207 ver |= lexical_cast<unsigned>(version_parts[i])<<(16-8*i);
209 catch(const runtime_error &)
215 ExternalTask::Arguments GnuCompiler::_run(const ObjectFile &object, FS::Path &work_dir)
217 const Tool &tool = *object.get_tool();
218 const Architecture &arch = *tool.get_architecture();
220 ExternalTask::Arguments argv;
221 argv.push_back(tool.get_executable()->get_path().str());
222 argv.push_back("-c");
225 object.collect_build_info(binfo);
227 const std::string &tool_tag = tool.get_tag();
228 string tag_for_std = (tool_tag=="OBJC" ? "CC" : tool_tag);
229 if(binfo.standards.count(tag_for_std))
230 argv.push_back("-std="+get_item(binfo.standards, tag_for_std).str());
231 if(tool_tag=="OBJC" && binfo.standards.count(tool_tag))
232 argv.push_back("-fobjc-std="+get_item(binfo.standards, tool_tag).str());
234 if(binfo.warning_level>=1)
236 argv.push_back("-Wall");
237 if(binfo.warning_level>=2)
239 argv.push_back("-Wextra");
240 argv.push_back("-Wundef");
241 unsigned version = tool.get_extra_data();
243 argv.push_back("-Wno-cast-function-type");
245 if(binfo.warning_level>=3)
247 argv.push_back("-pedantic");
248 argv.push_back("-Wno-long-long");
249 argv.push_back("-Wshadow");
252 argv.push_back("-Wc++-compat");
253 argv.push_back("-Wstrict-prototypes");
256 if(binfo.warning_level>=4)
258 // Some truly paranoid warnings
259 argv.push_back("-Wstrict-overflow=4");
260 argv.push_back("-Wfloat-equal");
261 argv.push_back("-Wconversion");
262 argv.push_back("-Wwrite-strings");
263 argv.push_back("-Winline");
265 if(binfo.fatal_warnings)
267 argv.push_back("-Werror");
268 argv.push_back("-Wno-error=deprecated-declarations");
272 const FS::Path &sysroot = binfo.sysroot;
274 argv.push_back("--sysroot="+sysroot.str());
275 for(const FS::Path &p: binfo.local_incpath)
277 argv.push_back("-iquote");
278 argv.push_back(p.str());
280 for(const FS::Path &p: binfo.incpath)
281 argv.push_back("-I"+p.str());
283 for(const auto &kvp: binfo.defines)
285 if(kvp.second.empty())
286 argv.push_back(format("-D%s", kvp.first));
288 argv.push_back(format("-D%s=%s", kvp.first, kvp.second));
292 argv.push_back("-ggdb");
296 argv.push_back("-Os");
298 argv.push_back(format("-O%d", binfo.optimize));
300 argv.push_back("-fno-omit-frame-pointer");
302 if(binfo.threads && arch.get_system()!="windows" && arch.get_system()!="darwin")
303 argv.push_back("-pthread");
304 if(object.is_used_in_shared_library() && arch.get_system()!="windows")
306 argv.push_back("-fvisibility=hidden");
307 argv.push_back("-fPIC");
310 if((arch.get_type()=="x86" || arch.get_type()=="ppc") && !arch.is_native())
311 argv.push_back(format("-m%d", arch.get_bits()));
313 string cpu = arch.get_cpu();
316 for(unsigned i=0; cpus[i]; i+=2)
322 argv.push_back("-march="+cpu);
325 if(!arch.get_fpu().empty())
327 if(arch.get_type()=="x86")
329 if(arch.get_fpu()=="387")
330 argv.push_back("-mfpmath=387");
331 else if(!arch.get_fpu().compare(0, 3, "sse"))
332 argv.push_back("-mfpmath=sse");
334 if(arch.get_fpu()=="sse")
335 argv.push_back("-msse2");
336 else if(arch.get_fpu()=="sse3")
337 argv.push_back("-msse3");
338 else if(arch.get_fpu()=="sse4.1")
339 argv.push_back("-msse4.1");
341 else if(arch.get_type()=="arm")
343 argv.push_back("-mfpu="+arch.get_fpu());
344 argv.push_back("-mfloat-abi=softfp");
348 FS::Path obj_path = object.get_path();
349 FS::Path src_path = object.get_source().get_path();
351 argv.push_back("-o");
352 argv.push_back(relative(obj_path, work_dir).str());
353 argv.push_back(relative(src_path, work_dir).str());