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, const FS::Path &sysroot):
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);
55 build_info.sysroot = sysroot;
58 Target *GnuCompiler::create_source(const Component &comp, const FS::Path &path) const
61 return new ObjCSourceFile(builder, comp, path);
63 return new CSourceFile(builder, comp, path);
66 Target *GnuCompiler::create_source(const FS::Path &path) const
69 return new ObjCSourceFile(builder, path);
71 return new CSourceFile(builder, path);
74 Target *GnuCompiler::create_target(const list<Target *> &sources, const string &)
77 throw invalid_argument("GnuCCompiler::create_target");
78 SourceFile &source = dynamic_cast<SourceFile &>(*sources.front());
79 ObjectFile *obj = new ObjectFile(builder, *source.get_component(), source);
84 string GnuCompiler::create_build_signature(const BuildInfo &binfo) const
86 string result = FS::basename(executable->get_path());
87 if(!architecture->get_cpu().empty())
90 result += architecture->get_cpu();
98 result += (binfo.optimize>0 ? '0'+binfo.optimize : 's');
103 void GnuCompiler::do_prepare()
105 executable = builder.get_vfs().find_binary(command);
108 ExternalTask::Arguments argv;
109 argv.push_back(executable->get_path().str());
110 argv.push_back("-Wp,-v");
111 argv.push_back("-E");
113 argv.push_back("-xc++");
114 const FS::Path &sysroot = build_info.sysroot;
116 argv.push_back("--sysroot="+sysroot.str());
119 string prog = FS::basename(executable->get_path());
120 builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
123 string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
124 string::size_type start = 0;
125 bool record_path = false;
126 while(start<output.size())
128 string::size_type newline = output.find('\n', start);
129 if(!output.compare(start, 34, "#include <...> search starts here:"))
131 else if(!output.compare(start, 19, "End of search list."))
135 FS::Path path = strip(output.substr(start, newline-start));
136 builder.get_logger().log("tools", format("Got %s (%s) system path: %s", prog, tag, path));
137 system_path.push_back(path);
142 catch(const runtime_error &e)
144 problems.push_back("Unable to determine system path");
149 Task *GnuCompiler::run(const Target &target) const
151 const ObjectFile &object = dynamic_cast<const ObjectFile &>(target);
153 ExternalTask::Arguments argv;
154 argv.push_back(executable->get_path().str());
155 argv.push_back("-c");
158 target.collect_build_info(binfo);
160 string tag_for_std = (tag=="OBJC" ? "CC" : tag);
161 if(binfo.standards.count(tag_for_std))
162 argv.push_back("-std="+get_item(binfo.standards, tag_for_std));
163 if(tag=="OBJC" && binfo.standards.count(tag))
164 argv.push_back("-fobjc-std="+get_item(binfo.standards, tag));
166 if(binfo.warning_level>=1)
168 argv.push_back("-Wall");
169 if(binfo.warning_level>=2)
171 argv.push_back("-Wextra");
172 argv.push_back("-Wundef");
174 if(binfo.warning_level>=3)
176 argv.push_back("-pedantic");
177 argv.push_back("-Wno-long-long");
178 argv.push_back("-Wshadow");
181 argv.push_back("-Wc++-compat");
182 argv.push_back("-Wstrict-prototypes");
185 if(binfo.warning_level>=4)
187 // Some truly paranoid warnings
188 argv.push_back("-Wstrict-overflow=4");
189 argv.push_back("-Wfloat-equal");
190 argv.push_back("-Wconversion");
191 argv.push_back("-Wwrite-strings");
192 argv.push_back("-Winline");
194 if(binfo.fatal_warnings)
196 argv.push_back("-Werror");
197 argv.push_back("-Wno-error=deprecated-declarations");
201 const FS::Path &sysroot = binfo.sysroot;
203 argv.push_back("--sysroot="+sysroot.str());
204 for(BuildInfo::PathList::const_iterator i=binfo.local_incpath.begin(); i!=binfo.local_incpath.end(); ++i)
206 argv.push_back("-iquote");
207 argv.push_back(i->str());
209 for(BuildInfo::PathList::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i)
210 argv.push_back("-I"+i->str());
212 for(BuildInfo::DefineMap::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i)
214 if(i->second.empty())
215 argv.push_back(format("-D%s", i->first));
217 argv.push_back(format("-D%s=%s", i->first, i->second));
221 argv.push_back("-ggdb");
225 argv.push_back("-Os");
227 argv.push_back(format("-O%d", binfo.optimize));
229 if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
230 argv.push_back("-pthread");
231 if(object.is_used_in_shared_library() && architecture->get_system()!="windows")
232 argv.push_back("-fPIC");
234 const Architecture &native_arch = builder.get_native_arch();
235 if(architecture->is_native() && architecture->get_bits()!=native_arch.get_bits())
236 argv.push_back(format("-m%d", architecture->get_bits()));
238 string cpu = architecture->get_cpu();
241 for(unsigned i=0; cpus[i]; i+=2)
247 argv.push_back("-march="+cpu);
250 if(!architecture->get_fpu().empty())
252 if(architecture->get_type()=="x86")
254 argv.push_back("-mfpmath="+architecture->get_fpu());
255 if(architecture->get_fpu()=="sse")
256 argv.push_back("-msse2");
258 else if(architecture->get_type()=="arm")
260 argv.push_back("-mfpu="+architecture->get_fpu());
261 argv.push_back("-mfloat-abi=softfp");
265 FS::Path obj_path = object.get_path();
266 FS::Path src_path = object.get_source().get_path();
267 FS::Path work_dir = object.get_component()->get_package().get_source_directory();
269 argv.push_back("-o");
270 argv.push_back(relative(obj_path, work_dir).str());
271 argv.push_back(relative(src_path, work_dir).str());
273 return new ExternalTask(argv, work_dir);