-#include <msp/core/maputils.h>
-#include <msp/fs/utils.h>
-#include <msp/strings/format.h>
-#include <msp/strings/utils.h>
-#include "architecture.h"
-#include "builder.h"
-#include "component.h"
-#include "csourcefile.h"
-#include "externaltask.h"
-#include "gnucompiler.h"
-#include "objcsourcefile.h"
-#include "objectfile.h"
-#include "sourcefile.h"
-#include "sourcepackage.h"
-
-using namespace std;
-using namespace Msp;
-
-namespace {
-
-const char *cpus[] =
-{
- "athlonxp", "athlon-xp",
- "armv7a", "armv7-a",
- 0
-};
-
-}
-
-GnuCompiler::GnuCompiler(Builder &b, const Architecture &a, const string &t):
- Tool(b, a, t),
- version(0)
-{
- if(tag=="CC")
- {
- input_suffixes.push_back(".c");
- aux_suffixes.push_back(".h");
- }
- else if(tag=="CXX")
- {
- input_suffixes.push_back(".cpp");
- input_suffixes.push_back(".cc");
- aux_suffixes.push_back(".hpp");
- }
- else if(tag=="OBJC")
- {
- input_suffixes.push_back(".m");
- build_info.libs.push_back("objc");
- }
- else
- throw invalid_argument("GnuCompiler::GnuCompiler");
-
- set_command((tag=="CXX" ? "g++" : "gcc"), true);
- set_run(_run);
-}
-
-Target *GnuCompiler::create_source(const Component &comp, const FS::Path &path) const
-{
- if(tag=="OBJC")
- return new ObjCSourceFile(builder, comp, path);
- else
- return new CSourceFile(builder, comp, path);
-}
-
-Target *GnuCompiler::create_source(const FS::Path &path) const
-{
- if(tag=="OBJC")
- return new ObjCSourceFile(builder, path);
- else
- return new CSourceFile(builder, path);
-}
-
-Target *GnuCompiler::create_target(const vector<Target *> &sources, const string &)
-{
- if(sources.size()!=1)
- throw invalid_argument("GnuCompiler::create_target");
- SourceFile &source = dynamic_cast<SourceFile &>(*sources.front());
- ObjectFile *obj = new ObjectFile(builder, *source.get_component(), source);
- obj->set_tool(*this);
- return obj;
-}
-
-string GnuCompiler::create_build_signature(const BuildInfo &binfo) const
-{
- if(!executable)
- return string();
-
- string result = Tool::create_build_signature(binfo);
- if(!architecture->get_cpu().empty())
- {
- result += ",m";
- result += architecture->get_cpu();
- }
- if(binfo.debug || binfo.optimize)
- result += ',';
- if(binfo.debug)
- result += 'g';
- if(binfo.optimize)
- {
- result += 'O';
- result += (binfo.optimize>0 ? '0'+binfo.optimize : 's');
- }
- return result;
-}
-
-void GnuCompiler::do_prepare()
-{
- executable = builder.get_vfs().find_binary(command);
- prepare_syspath();
- prepare_version();
-}
-
-void GnuCompiler::prepare_syspath()
-{
- bool path_found = false;
- const FS::Path &sysroot = build_info.sysroot;
-
- if(executable)
- {
- ExternalTask::Arguments argv;
- argv.push_back(executable->get_path().str());
- argv.push_back("-Wp,-v");
- argv.push_back("-E");
- if(tag=="CXX")
- argv.push_back("-xc++");
- if(!sysroot.empty())
- argv.push_back("--sysroot="+sysroot.str());
- argv.push_back("-");
-
- builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
- try
- {
- string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
- string::size_type start = 0;
- bool record_path = false;
- while(start<output.size())
- {
- string::size_type newline = output.find('\n', start);
- if(!output.compare(start, 34, "#include <...> search starts here:"))
- {
- record_path = true;
- path_found = true;
- }
- else if(!output.compare(start, 19, "End of search list."))
- record_path = false;
- else if(record_path)
- {
- FS::Path path = strip(output.substr(start, newline-start));
- builder.get_logger().log("tools", "Got %s system path: %s", tag, path);
- system_path.push_back(path);
- }
- start = newline+1;
- }
- }
- catch(const runtime_error &)
- { }
- }
-
- if(!path_found)
- {
- builder.get_logger().log("tools", "No %s system path found, using defaults", tag);
- if(!sysroot.empty())
- system_path.push_back(sysroot/"usr/include");
- else if(architecture->is_native())
- system_path.push_back("/usr/include");
- else
- system_path.push_back(format("/usr/%s/include", architecture->get_cross_prefix()));
- }
-}
-
-void GnuCompiler::prepare_version()
-{
- if(!executable)
- return;
-
- prepare_version("-dumpversion");
- if(version>=0x70000)
- prepare_version("-dumpfullversion");
- builder.get_logger().log("tools", "%s version is %d.%d.%d", FS::basename(executable->get_path()), version>>16, (version>>8)&0xFF, version&0xFF);
-}
-
-void GnuCompiler::prepare_version(const string &arg)
-{
- ExternalTask::Arguments argv;
- argv.push_back(executable->get_path().str());
- argv.push_back(arg);
-
- builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
- try
- {
- string version_str = strip(ExternalTask::run_and_capture_output(argv));
-
- vector<string> version_parts = split(version_str, '.');
- version = 0;
- for(unsigned i=0; (i<3 && i<version_parts.size()); ++i)
- version |= lexical_cast<unsigned>(version_parts[i])<<(16-8*i);
- }
- catch(const runtime_error &)
- { }
-}
-
-Task *GnuCompiler::_run(const ObjectFile &object)
-{
- const GnuCompiler &tool = dynamic_cast<const GnuCompiler &>(*object.get_tool());
- const Architecture &arch = *tool.get_architecture();
-
- ExternalTask::Arguments argv;
- argv.push_back(tool.get_executable()->get_path().str());
- argv.push_back("-c");
-
- BuildInfo binfo;
- object.collect_build_info(binfo);
-
- const std::string &tool_tag = tool.get_tag();
- string tag_for_std = (tool_tag=="OBJC" ? "CC" : tool_tag);
- if(binfo.standards.count(tag_for_std))
- argv.push_back("-std="+get_item(binfo.standards, tag_for_std).str());
- if(tool_tag=="OBJC" && binfo.standards.count(tool_tag))
- argv.push_back("-fobjc-std="+get_item(binfo.standards, tool_tag).str());
-
- if(binfo.warning_level>=1)
- {
- argv.push_back("-Wall");
- if(binfo.warning_level>=2)
- {
- argv.push_back("-Wextra");
- argv.push_back("-Wundef");
- if(tool.version>=0x80000)
- argv.push_back("-Wno-cast-function-type");
- }
- if(binfo.warning_level>=3)
- {
- argv.push_back("-pedantic");
- argv.push_back("-Wno-long-long");
- argv.push_back("-Wshadow");
- if(tool_tag=="CC")
- {
- argv.push_back("-Wc++-compat");
- argv.push_back("-Wstrict-prototypes");
- }
- }
- if(binfo.warning_level>=4)
- {
- // Some truly paranoid warnings
- argv.push_back("-Wstrict-overflow=4");
- argv.push_back("-Wfloat-equal");
- argv.push_back("-Wconversion");
- argv.push_back("-Wwrite-strings");
- argv.push_back("-Winline");
- }
- if(binfo.fatal_warnings)
- {
- argv.push_back("-Werror");
- argv.push_back("-Wno-error=deprecated-declarations");
- }
- }
-
- const FS::Path &sysroot = binfo.sysroot;
- if(!sysroot.empty())
- argv.push_back("--sysroot="+sysroot.str());
- for(const FS::Path &p: binfo.local_incpath)
- {
- argv.push_back("-iquote");
- argv.push_back(p.str());
- }
- for(const FS::Path &p: binfo.incpath)
- argv.push_back("-I"+p.str());
-
- for(const auto &kvp: binfo.defines)
- {
- if(kvp.second.empty())
- argv.push_back(format("-D%s", kvp.first));
- else
- argv.push_back(format("-D%s=%s", kvp.first, kvp.second));
- }
-
- if(binfo.debug)
- argv.push_back("-ggdb");
- if(binfo.optimize)
- {
- if(binfo.optimize<0)
- argv.push_back("-Os");
- else
- argv.push_back(format("-O%d", binfo.optimize));
- if(binfo.debug)
- argv.push_back("-fno-omit-frame-pointer");
- }
- if(binfo.threads && arch.get_system()!="windows" && arch.get_system()!="darwin")
- argv.push_back("-pthread");
- if(object.is_used_in_shared_library() && arch.get_system()!="windows")
- argv.push_back("-fPIC");
-
- if((arch.get_type()=="x86" || arch.get_type()=="ppc") && !arch.is_native())
- argv.push_back(format("-m%d", arch.get_bits()));
-
- string cpu = arch.get_cpu();
- if(!cpu.empty())
- {
- for(unsigned i=0; cpus[i]; i+=2)
- if(cpu==cpus[i])
- {
- cpu = cpus[i+1];
- break;
- }
- argv.push_back("-march="+cpu);
- }
-
- if(!arch.get_fpu().empty())
- {
- if(arch.get_type()=="x86")
- {
- if(arch.get_fpu()=="387")
- argv.push_back("-mfpmath=387");
- else if(!arch.get_fpu().compare(0, 3, "sse"))
- argv.push_back("-mfpmath=sse");
-
- if(arch.get_fpu()=="sse")
- argv.push_back("-msse2");
- else if(arch.get_fpu()=="sse3")
- argv.push_back("-msse3");
- else if(arch.get_fpu()=="sse4.1")
- argv.push_back("-msse4.1");
- }
- else if(arch.get_type()=="arm")
- {
- argv.push_back("-mfpu="+arch.get_fpu());
- argv.push_back("-mfloat-abi=softfp");
- }
- }
-
- FS::Path obj_path = object.get_path();
- FS::Path src_path = object.get_source().get_path();
- FS::Path work_dir = object.get_component()->get_package().get_source_directory();
-
- argv.push_back("-o");
- argv.push_back(relative(obj_path, work_dir).str());
- argv.push_back(relative(src_path, work_dir).str());
-
- return new ExternalTask(argv, work_dir);
-}