1 #include <msp/fs/dir.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 "externaltask.h"
9 #include "gnucompiler.h"
10 #include "objectfile.h"
11 #include "sourcefile.h"
12 #include "sourcepackage.h"
17 GnuCompiler::GnuCompiler(Builder &b, const Architecture &a, const string &t, const FS::Path &sysroot):
22 build_info.sysroot = sysroot;
23 system_path.push_back(sysroot/"usr/include");
25 else if(architecture->is_native())
26 system_path.push_back("/usr/include");
28 system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/include");
31 Target *GnuCompiler::create_target(const list<Target *> &sources, const string &)
34 throw invalid_argument("GnuCCompiler::create_target");
35 SourceFile &source = dynamic_cast<SourceFile &>(*sources.front());
36 ObjectFile *obj = new ObjectFile(builder, *source.get_component(), source);
41 string GnuCompiler::create_build_signature(const BuildInfo &binfo) const
43 string result = FS::basename(executable->get_path());
44 if(!architecture->get_cpu().empty())
47 result += architecture->get_cpu();
55 result += (binfo.optimize>0 ? '0'+binfo.optimize : 's');
60 void GnuCompiler::do_prepare()
62 executable = builder.get_vfs().find_binary(command);
65 ExternalTask::Arguments argv;
66 argv.push_back(executable->get_path().str());
67 argv.push_back("-dumpversion");
69 builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
72 version = strip(ExternalTask::run_and_capture_output(argv));
73 builder.get_logger().log("tools", format("%s version is %s", FS::basename(executable->get_path()), version));
75 catch(const runtime_error &)
80 Task *GnuCompiler::run(const Target &target) const
82 const ObjectFile &object = dynamic_cast<const ObjectFile &>(target);
83 const Component &comp = *object.get_component();
85 ExternalTask::Arguments argv;
86 argv.push_back(executable->get_path().str());
90 target.collect_build_info(binfo);
92 if(binfo.warning_level>=1)
94 argv.push_back("-Wall");
95 if(binfo.warning_level>=2)
97 argv.push_back("-Wextra");
98 argv.push_back("-Wundef");
100 if(binfo.warning_level>=3)
102 argv.push_back("-pedantic");
103 argv.push_back("-Wno-long-long");
104 argv.push_back("-Wshadow");
107 argv.push_back("-Wc++-compat");
108 argv.push_back("-Wstrict-prototypes");
111 if(binfo.warning_level>=4)
113 // Some truly paranoid warnings
114 argv.push_back("-Wstrict-overflow=4");
115 argv.push_back("-Wfloat-equal");
116 argv.push_back("-Wconversion");
117 argv.push_back("-Wwrite-strings");
118 argv.push_back("-Winline");
120 if(binfo.fatal_warnings)
121 argv.push_back("-Werror");
124 const FS::Path &sysroot = binfo.sysroot;
126 argv.push_back("--sysroot="+sysroot.str());
127 for(BuildInfo::PathList::const_iterator i=binfo.local_incpath.begin(); i!=binfo.local_incpath.end(); ++i)
129 argv.push_back("-iquote");
130 argv.push_back(i->str());
132 for(BuildInfo::PathList::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i)
133 argv.push_back("-I"+i->str());
135 for(BuildInfo::DefineMap::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i)
137 if(i->second.empty())
138 argv.push_back(format("-D%s", i->first));
140 argv.push_back(format("-D%s=%s", i->first, i->second));
144 argv.push_back("-ggdb");
148 argv.push_back("-Os");
150 argv.push_back(format("-O%d", binfo.optimize));
152 if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
153 argv.push_back("-pthread");
154 if(object.is_used_in_shared_library() && architecture->get_system()!="windows")
155 argv.push_back("-fPIC");
157 const Architecture &native_arch = builder.get_native_arch();
158 if(architecture->is_native() && architecture->get_bits()!=native_arch.get_bits())
159 argv.push_back(format("-m%d", architecture->get_bits()));
161 const string &cpu = architecture->get_cpu();
163 argv.push_back("-march="+cpu);
165 FS::Path obj_path = object.get_path();
166 FS::Path src_path = object.get_source().get_path();
167 FS::Path work_dir = comp.get_package().get_source_directory();
169 argv.push_back("-o");
170 argv.push_back(relative(obj_path, work_dir).str());
171 argv.push_back(relative(src_path, work_dir).str());
173 return new ExternalTask(argv, work_dir);