1 #include <msp/core/maputils.h>
2 #include <msp/fs/dir.h>
3 #include <msp/fs/utils.h>
4 #include <msp/strings/format.h>
5 #include <msp/strings/utils.h>
6 #include "architecture.h"
9 #include "externaltask.h"
10 #include "gnucompiler.h"
11 #include "objectfile.h"
12 #include "sourcefile.h"
13 #include "sourcepackage.h"
22 "athlonxp", "athlon-xp",
29 GnuCompiler::GnuCompiler(Builder &b, const Architecture &a, const string &t, const FS::Path &sysroot):
34 build_info.sysroot = sysroot;
35 system_path.push_back(sysroot/"usr/include");
37 else if(architecture->is_native())
38 system_path.push_back("/usr/include");
40 system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/include");
43 Target *GnuCompiler::create_target(const list<Target *> &sources, const string &)
46 throw invalid_argument("GnuCCompiler::create_target");
47 SourceFile &source = dynamic_cast<SourceFile &>(*sources.front());
48 ObjectFile *obj = new ObjectFile(builder, *source.get_component(), source);
53 string GnuCompiler::create_build_signature(const BuildInfo &binfo) const
55 string result = FS::basename(executable->get_path());
56 if(!architecture->get_cpu().empty())
59 result += architecture->get_cpu();
67 result += (binfo.optimize>0 ? '0'+binfo.optimize : 's');
72 void GnuCompiler::do_prepare()
74 executable = builder.get_vfs().find_binary(command);
77 ExternalTask::Arguments argv;
78 argv.push_back(executable->get_path().str());
79 argv.push_back("-dumpversion");
81 builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
84 version = strip(ExternalTask::run_and_capture_output(argv));
85 builder.get_logger().log("tools", format("%s version is %s", FS::basename(executable->get_path()), version));
87 catch(const runtime_error &)
92 Task *GnuCompiler::run(const Target &target) const
94 const ObjectFile &object = dynamic_cast<const ObjectFile &>(target);
96 ExternalTask::Arguments argv;
97 argv.push_back(executable->get_path().str());
101 target.collect_build_info(binfo);
103 string tag_for_std = (tag=="OBJC" ? "CC" : tag);
104 if(binfo.standards.count(tag_for_std))
105 argv.push_back("-std="+get_item(binfo.standards, tag_for_std));
106 if(tag=="OBJC" && binfo.standards.count(tag))
107 argv.push_back("-fobjc-std="+get_item(binfo.standards, tag));
109 if(binfo.warning_level>=1)
111 argv.push_back("-Wall");
112 if(binfo.warning_level>=2)
114 argv.push_back("-Wextra");
115 argv.push_back("-Wundef");
117 if(binfo.warning_level>=3)
119 argv.push_back("-pedantic");
120 argv.push_back("-Wno-long-long");
121 argv.push_back("-Wshadow");
124 argv.push_back("-Wc++-compat");
125 argv.push_back("-Wstrict-prototypes");
128 if(binfo.warning_level>=4)
130 // Some truly paranoid warnings
131 argv.push_back("-Wstrict-overflow=4");
132 argv.push_back("-Wfloat-equal");
133 argv.push_back("-Wconversion");
134 argv.push_back("-Wwrite-strings");
135 argv.push_back("-Winline");
137 if(binfo.fatal_warnings)
138 argv.push_back("-Werror");
141 const FS::Path &sysroot = binfo.sysroot;
143 argv.push_back("--sysroot="+sysroot.str());
144 for(BuildInfo::PathList::const_iterator i=binfo.local_incpath.begin(); i!=binfo.local_incpath.end(); ++i)
146 argv.push_back("-iquote");
147 argv.push_back(i->str());
149 for(BuildInfo::PathList::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i)
150 argv.push_back("-I"+i->str());
152 for(BuildInfo::DefineMap::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i)
154 if(i->second.empty())
155 argv.push_back(format("-D%s", i->first));
157 argv.push_back(format("-D%s=%s", i->first, i->second));
161 argv.push_back("-ggdb");
165 argv.push_back("-Os");
167 argv.push_back(format("-O%d", binfo.optimize));
169 if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
170 argv.push_back("-pthread");
171 if(object.is_used_in_shared_library() && architecture->get_system()!="windows")
172 argv.push_back("-fPIC");
174 const Architecture &native_arch = builder.get_native_arch();
175 if(architecture->is_native() && architecture->get_bits()!=native_arch.get_bits())
176 argv.push_back(format("-m%d", architecture->get_bits()));
178 string cpu = architecture->get_cpu();
181 for(unsigned i=0; cpus[i]; i+=2)
187 argv.push_back("-march="+cpu);
190 FS::Path obj_path = object.get_path();
191 FS::Path src_path = object.get_source().get_path();
192 FS::Path work_dir = object.get_component()->get_package().get_source_directory();
194 argv.push_back("-o");
195 argv.push_back(relative(obj_path, work_dir).str());
196 argv.push_back(relative(src_path, work_dir).str());
198 return new ExternalTask(argv, work_dir);