1 #include <msp/core/maputils.h>
2 #include <msp/fs/dir.h>
3 #include <msp/fs/stat.h>
4 #include <msp/fs/utils.h>
5 #include <msp/strings/format.h>
6 #include <msp/strings/utils.h>
7 #include "architecture.h"
10 #include "csourcefile.h"
11 #include "externaltask.h"
12 #include "gnucompiler.h"
13 #include "objcsourcefile.h"
14 #include "objectfile.h"
15 #include "sourcefile.h"
16 #include "sourcepackage.h"
25 "athlonxp", "athlon-xp",
32 GnuCompiler::GnuCompiler(Builder &b, const Architecture &a, const string &t, const FS::Path &sysroot):
37 input_suffixes.push_back(".c");
38 aux_suffixes.push_back(".h");
42 input_suffixes.push_back(".cpp");
43 input_suffixes.push_back(".cc");
44 aux_suffixes.push_back(".hpp");
48 input_suffixes.push_back(".m");
49 build_info.libs.push_back("objc");
52 throw invalid_argument("GnuCompiler::GnuCompiler");
54 set_command((tag=="CXX" ? "g++" : "gcc"), true);
58 build_info.sysroot = sysroot;
59 system_path.push_back(sysroot/"usr/include");
61 else if(architecture->is_native())
62 system_path.push_back("/usr/include");
64 system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/include");
67 Target *GnuCompiler::create_source(const Component &comp, const FS::Path &path) const
70 return new ObjCSourceFile(builder, comp, path);
72 return new CSourceFile(builder, comp, path);
75 Target *GnuCompiler::create_source(const FS::Path &path) const
78 return new ObjCSourceFile(builder, path);
80 return new CSourceFile(builder, path);
83 Target *GnuCompiler::create_target(const list<Target *> &sources, const string &)
86 throw invalid_argument("GnuCCompiler::create_target");
87 SourceFile &source = dynamic_cast<SourceFile &>(*sources.front());
88 ObjectFile *obj = new ObjectFile(builder, *source.get_component(), source);
93 string GnuCompiler::create_build_signature(const BuildInfo &binfo) const
95 string result = FS::basename(executable->get_path());
96 if(!architecture->get_cpu().empty())
99 result += architecture->get_cpu();
107 result += (binfo.optimize>0 ? '0'+binfo.optimize : 's');
112 void GnuCompiler::do_prepare()
114 executable = builder.get_vfs().find_binary(command);
117 ExternalTask::Arguments argv;
118 argv.push_back(executable->get_path().str());
119 argv.push_back("-dumpversion");
121 builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
124 version = strip(ExternalTask::run_and_capture_output(argv));
125 builder.get_logger().log("tools", format("%s version is %s", FS::basename(executable->get_path()), version));
129 const FS::Path &sysroot = build_info.sysroot;
130 FS::Path cxx_path = sysroot/"usr"/"include"/"c++"/version;
131 if(FS::is_dir(cxx_path))
132 system_path.push_back(cxx_path);
135 catch(const runtime_error &)
140 Task *GnuCompiler::run(const Target &target) const
142 const ObjectFile &object = dynamic_cast<const ObjectFile &>(target);
144 ExternalTask::Arguments argv;
145 argv.push_back(executable->get_path().str());
146 argv.push_back("-c");
149 target.collect_build_info(binfo);
151 string tag_for_std = (tag=="OBJC" ? "CC" : tag);
152 if(binfo.standards.count(tag_for_std))
153 argv.push_back("-std="+get_item(binfo.standards, tag_for_std));
154 if(tag=="OBJC" && binfo.standards.count(tag))
155 argv.push_back("-fobjc-std="+get_item(binfo.standards, tag));
157 if(binfo.warning_level>=1)
159 argv.push_back("-Wall");
160 if(binfo.warning_level>=2)
162 argv.push_back("-Wextra");
163 argv.push_back("-Wundef");
165 if(binfo.warning_level>=3)
167 argv.push_back("-pedantic");
168 argv.push_back("-Wno-long-long");
169 argv.push_back("-Wshadow");
172 argv.push_back("-Wc++-compat");
173 argv.push_back("-Wstrict-prototypes");
176 if(binfo.warning_level>=4)
178 // Some truly paranoid warnings
179 argv.push_back("-Wstrict-overflow=4");
180 argv.push_back("-Wfloat-equal");
181 argv.push_back("-Wconversion");
182 argv.push_back("-Wwrite-strings");
183 argv.push_back("-Winline");
185 if(binfo.fatal_warnings)
187 argv.push_back("-Werror");
188 argv.push_back("-Wno-error=deprecated-declarations");
192 const FS::Path &sysroot = binfo.sysroot;
194 argv.push_back("--sysroot="+sysroot.str());
195 for(BuildInfo::PathList::const_iterator i=binfo.local_incpath.begin(); i!=binfo.local_incpath.end(); ++i)
197 argv.push_back("-iquote");
198 argv.push_back(i->str());
200 for(BuildInfo::PathList::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i)
201 argv.push_back("-I"+i->str());
203 for(BuildInfo::DefineMap::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i)
205 if(i->second.empty())
206 argv.push_back(format("-D%s", i->first));
208 argv.push_back(format("-D%s=%s", i->first, i->second));
212 argv.push_back("-ggdb");
216 argv.push_back("-Os");
218 argv.push_back(format("-O%d", binfo.optimize));
220 if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
221 argv.push_back("-pthread");
222 if(object.is_used_in_shared_library() && architecture->get_system()!="windows")
223 argv.push_back("-fPIC");
225 const Architecture &native_arch = builder.get_native_arch();
226 if(architecture->is_native() && architecture->get_bits()!=native_arch.get_bits())
227 argv.push_back(format("-m%d", architecture->get_bits()));
229 string cpu = architecture->get_cpu();
232 for(unsigned i=0; cpus[i]; i+=2)
238 argv.push_back("-march="+cpu);
241 if(!architecture->get_fpu().empty())
243 if(architecture->get_type()=="x86")
245 argv.push_back("-mfpmath="+architecture->get_fpu());
246 if(architecture->get_fpu()=="sse")
247 argv.push_back("-msse2");
249 else if(architecture->get_type()=="arm")
251 argv.push_back("-mfpu="+architecture->get_fpu());
252 argv.push_back("-mfloat-abi=softfp");
256 FS::Path obj_path = object.get_path();
257 FS::Path src_path = object.get_source().get_path();
258 FS::Path work_dir = object.get_component()->get_package().get_source_directory();
260 argv.push_back("-o");
261 argv.push_back(relative(obj_path, work_dir).str());
262 argv.push_back(relative(src_path, work_dir).str());
264 return new ExternalTask(argv, work_dir);