processing_unit = COMPONENT;
- default_linker = new Linker(*this, "CC");
- cxx_linker = new Linker(*this, "CXX");
-}
-
-GnuLinker::~GnuLinker()
-{
- delete default_linker;
- delete cxx_linker;
+ set_command("gcc");
+ set_run(_run);
}
Target *GnuLinker::create_target(const vector<Target *> &sources, const string &arg)
throw invalid_argument("GnuLinker::create_target");
vector<ObjectFile *> objs;
objs.reserve(sources.size());
- Linker *linker = default_linker;
for(Target *s: sources)
- {
- ObjectFile &obj = dynamic_cast<ObjectFile &>(*s);
- objs.push_back(&obj);
- if(obj.get_tool()->get_tag()=="CXX")
- linker = cxx_linker;
- }
+ objs.push_back(&dynamic_cast<ObjectFile &>(*s));
const Component &comp = *objs.front()->get_component();
Binary *bin = 0;
}
else
bin = new Executable(builder, comp, objs);
- bin->set_tool(*linker);
+ bin->set_tool(*this);
return bin;
}
return 0;
}
+string GnuLinker::create_build_signature(const BuildInfo &binfo) const
+{
+ string result = Tool::create_build_signature(binfo);
+ result += ',';
+ if(binfo.libmode<=BuildInfo::STATIC)
+ result += 't';
+ else
+ result += 'd';
+ if(binfo.strip)
+ result += 's';
+ if(!binfo.libs.empty())
+ {
+ result += ",l";
+ result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
+ }
+ return result;
+}
+
void GnuLinker::do_prepare()
{
bool path_found = false;
const FS::Path &sysroot = build_info.sysroot;
- Tool &compiler = builder.get_toolchain().get_tool("CC");
- if(dynamic_cast<GnuCompiler *>(&compiler))
+ if(executable)
{
- compiler.prepare();
- FileTarget *compiler_exe = compiler.get_executable();
- if(compiler_exe)
+ ExternalTask::Arguments argv;
+ argv.push_back(executable->get_path().str());
+ argv.push_back("-Wl,--verbose");
+ argv.push_back("-nostdlib");
+ if(!sysroot.empty())
+ argv.push_back("--sysroot="+sysroot.str());
+
+ builder.get_logger().log("auxcommands", "Running %s", join(argv.begin(), argv.end()));
+ try
{
- ExternalTask::Arguments argv;
- argv.push_back(compiler_exe->get_path().str());
- argv.push_back("-Wl,--verbose");
- argv.push_back("-nostdlib");
- if(!sysroot.empty())
- argv.push_back("--sysroot="+sysroot.str());
-
- 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;
+ while(start<output.size())
{
- string output = ExternalTask::run_and_capture_output(argv, FS::Path(), true);
- string::size_type start = 0;
- while(start<output.size())
+ string::size_type search_dir = output.find("SEARCH_DIR(\"", start);
+ if(search_dir==string::npos)
+ break;
+
+ search_dir += 12;
+ string::size_type end = output.find("\");", search_dir);
+ if(end==string::npos)
+ break;
+
+ FS::Path path;
+ if(!output.compare(search_dir, 2, "=/"))
{
- string::size_type search_dir = output.find("SEARCH_DIR(\"", start);
- if(search_dir==string::npos)
- break;
-
- search_dir += 12;
- string::size_type end = output.find("\");", search_dir);
- if(end==string::npos)
- break;
-
- FS::Path path;
- if(!output.compare(search_dir, 2, "=/"))
- {
- search_dir += 2;
- if(sysroot.empty())
- path = "/";
- else
- path = sysroot;
- }
-
- path /= output.substr(search_dir, end-search_dir);
- builder.get_logger().log("tools", "Got %s system path: %s", tag, path);
- system_path.push_back(path);
- path_found = true;
-
- start = end+3;
+ search_dir += 2;
+ if(sysroot.empty())
+ path = "/";
+ else
+ path = sysroot;
}
+
+ path /= output.substr(search_dir, end-search_dir);
+ builder.get_logger().log("tools", "Got %s system path: %s", tag, path);
+ system_path.push_back(path);
+ path_found = true;
+
+ start = end+3;
}
- catch(...)
- { }
}
+ catch(...)
+ { }
}
if(!path_found)
}
}
-
-GnuLinker::Linker::Linker(GnuLinker &p, const string &ct):
- SubTool(p),
- compiler_tag(ct)
-{
- if(compiler_tag=="CC")
- set_command("gcc", true);
- else if(compiler_tag=="CXX")
- set_command("g++", true);
- else
- throw invalid_argument("GnuLinker::Linker::Linker");
-
- set_run(_run);
-}
-
-string GnuLinker::Linker::create_build_signature(const BuildInfo &binfo) const
-{
- string result = Tool::create_build_signature(binfo);
- result += ',';
- if(binfo.libmode<=BuildInfo::STATIC)
- result += 't';
- else
- result += 'd';
- if(binfo.strip)
- result += 's';
- if(!binfo.libs.empty())
- {
- result += ",l";
- result += join(binfo.libs.begin(), binfo.libs.end(), ",l");
- }
- return result;
-}
-
-void GnuLinker::Linker::do_prepare()
+Task *GnuLinker::_run(const Binary &bin)
{
- parent.prepare();
- build_info = parent.get_build_info();
- system_path = parent.get_system_path();
-
- Tool &compiler = builder.get_toolchain().get_tool(compiler_tag);
- if(dynamic_cast<GnuCompiler *>(&compiler))
- {
- compiler.prepare();
- executable = compiler.get_executable();
- }
-}
-
-Task *GnuLinker::Linker::_run(const Binary &bin)
-{
- const Linker &tool = dynamic_cast<const Linker &>(*bin.get_tool());
+ const Tool &tool = *bin.get_tool();
const Builder &builder = tool.get_builder();
const Architecture &arch = *tool.get_architecture();
bool static_link_ok = (binfo.libmode<=BuildInfo::STATIC);
+ bool has_cplusplus = false;
for(Target *d: bin.get_dependencies())
{
FileTarget *file = dynamic_cast<FileTarget *>(d);
Target *tgt = d->get_real_target();
if(ObjectFile *obj = dynamic_cast<ObjectFile *>(tgt))
+ {
argv.push_back(relative(obj->get_path(), work_dir).str());
+ if(obj->get_tool()->get_tag()=="CXX")
+ has_cplusplus = true;
+ }
else if(StaticLibrary *stlib = dynamic_cast<StaticLibrary *>(tgt))
argv.push_back((file?file:stlib)->get_path().str());
else if(SharedLibrary *shlib = dynamic_cast<SharedLibrary *>(tgt))
argv.push_back(l.substr(0, l.size()-10));
}
+ if(has_cplusplus)
+ argv.push_back("-lstdc++");
+
if(static_link_ok)
argv.push_back("-static");
else
{
- if(tool.compiler_tag=="CXX")
+ if(has_cplusplus)
{
auto i = binfo.libmodes.find("stdc++");
if(i!=binfo.libmodes.end() && i->second<=BuildInfo::STATIC)