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"
18 GnuCompiler::GnuCompiler(Builder &b, const Architecture &a, const string &t, const FS::Path &sysroot):
23 build_info.sysroot = sysroot;
24 system_path.push_back(sysroot/"usr/include");
26 else if(architecture->is_native())
27 system_path.push_back("/usr/include");
29 system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/include");
32 Target *GnuCompiler::create_target(const list<Target *> &sources, const string &)
35 throw invalid_argument("GnuCCompiler::create_target");
36 SourceFile &source = dynamic_cast<SourceFile &>(*sources.front());
37 ObjectFile *obj = new ObjectFile(builder, *source.get_component(), source);
42 string GnuCompiler::create_build_signature(const BuildInfo &binfo) const
44 string result = FS::basename(executable->get_path());
45 if(!architecture->get_cpu().empty())
48 result += architecture->get_cpu();
56 result += (binfo.optimize>0 ? '0'+binfo.optimize : 's');
61 void GnuCompiler::do_prepare()
63 executable = builder.get_vfs().find_binary(command);
66 ExternalTask::Arguments argv;
67 argv.push_back(executable->get_path().str());
68 argv.push_back("-dumpversion");
70 builder.get_logger().log("auxcommands", format("Running %s", join(argv.begin(), argv.end())));
73 version = strip(ExternalTask::run_and_capture_output(argv));
74 builder.get_logger().log("tools", format("%s version is %s", FS::basename(executable->get_path()), version));
76 catch(const runtime_error &)
81 Task *GnuCompiler::run(const Target &target) const
83 const ObjectFile &object = dynamic_cast<const ObjectFile &>(target);
84 const Component &comp = *object.get_component();
86 ExternalTask::Arguments argv;
87 argv.push_back(executable->get_path().str());
91 target.collect_build_info(binfo);
93 string tag_for_std = (tag=="OBJC" ? "CC" : tag);
94 if(binfo.standards.count(tag_for_std))
95 argv.push_back("-std="+get_item(binfo.standards, tag_for_std));
96 if(tag=="OBJC" && binfo.standards.count(tag))
97 argv.push_back("-fobjc-std="+get_item(binfo.standards, tag));
99 if(binfo.warning_level>=1)
101 argv.push_back("-Wall");
102 if(binfo.warning_level>=2)
104 argv.push_back("-Wextra");
105 argv.push_back("-Wundef");
107 if(binfo.warning_level>=3)
109 argv.push_back("-pedantic");
110 argv.push_back("-Wno-long-long");
111 argv.push_back("-Wshadow");
114 argv.push_back("-Wc++-compat");
115 argv.push_back("-Wstrict-prototypes");
118 if(binfo.warning_level>=4)
120 // Some truly paranoid warnings
121 argv.push_back("-Wstrict-overflow=4");
122 argv.push_back("-Wfloat-equal");
123 argv.push_back("-Wconversion");
124 argv.push_back("-Wwrite-strings");
125 argv.push_back("-Winline");
127 if(binfo.fatal_warnings)
128 argv.push_back("-Werror");
131 const FS::Path &sysroot = binfo.sysroot;
133 argv.push_back("--sysroot="+sysroot.str());
134 for(BuildInfo::PathList::const_iterator i=binfo.local_incpath.begin(); i!=binfo.local_incpath.end(); ++i)
136 argv.push_back("-iquote");
137 argv.push_back(i->str());
139 for(BuildInfo::PathList::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i)
140 argv.push_back("-I"+i->str());
142 for(BuildInfo::DefineMap::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i)
144 if(i->second.empty())
145 argv.push_back(format("-D%s", i->first));
147 argv.push_back(format("-D%s=%s", i->first, i->second));
151 argv.push_back("-ggdb");
155 argv.push_back("-Os");
157 argv.push_back(format("-O%d", binfo.optimize));
159 if(binfo.threads && architecture->get_system()!="windows" && architecture->get_system()!="darwin")
160 argv.push_back("-pthread");
161 if(object.is_used_in_shared_library() && architecture->get_system()!="windows")
162 argv.push_back("-fPIC");
164 const Architecture &native_arch = builder.get_native_arch();
165 if(architecture->is_native() && architecture->get_bits()!=native_arch.get_bits())
166 argv.push_back(format("-m%d", architecture->get_bits()));
168 const string &cpu = architecture->get_cpu();
170 argv.push_back("-march="+cpu);
172 FS::Path obj_path = object.get_path();
173 FS::Path src_path = object.get_source().get_path();
174 FS::Path work_dir = comp.get_package().get_source_directory();
176 argv.push_back("-o");
177 argv.push_back(relative(obj_path, work_dir).str());
178 argv.push_back(relative(src_path, work_dir).str());
180 return new ExternalTask(argv, work_dir);