From 1c80c1c33af10e8e325808906a95969ee57676b6 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 4 Oct 2014 01:04:28 +0300 Subject: [PATCH] Add sysroot support for BuildInfo and compilers Embedded SDKs commonly ship with a sysroot to use with their compilers. It might also come useful for compiling things for different Linux distributions. --- source/buildinfo.cpp | 7 +++++++ source/buildinfo.h | 2 ++ source/gnuccompiler.cpp | 4 ++-- source/gnuccompiler.h | 2 +- source/gnucompiler.cpp | 15 +++++++++++++-- source/gnucompiler.h | 2 +- source/gnucxxcompiler.cpp | 4 ++-- source/gnucxxcompiler.h | 2 +- source/gnulinker.cpp | 14 ++++++++++++-- source/gnulinker.h | 2 +- source/gnuobjccompiler.cpp | 4 ++-- source/gnuobjccompiler.h | 2 +- 12 files changed, 45 insertions(+), 15 deletions(-) diff --git a/source/buildinfo.cpp b/source/buildinfo.cpp index d346fe6..2246ad4 100644 --- a/source/buildinfo.cpp +++ b/source/buildinfo.cpp @@ -57,6 +57,7 @@ void BuildInfo::update_from(const BuildInfo &bi, UpdateLevel level) threads = bi.threads; if(level==LOCAL) { + sysroot = bi.sysroot; local_incpath.insert(local_incpath.begin(), bi.local_incpath.begin(), bi.local_incpath.end()); libmode = bi.libmode; for(LibModeMap::const_iterator i=bi.libmodes.begin(); i!=bi.libmodes.end(); ++i) @@ -87,6 +88,7 @@ BuildInfo::Loader::Loader(BuildInfo &bi): add("local_incpath", &Loader::local_incpath); add("optimize", &BuildInfo::optimize); add("strip", &BuildInfo::strip); + add("sysroot", &Loader::sysroot); add("threads", &BuildInfo::threads); add("warning_level", &BuildInfo::warning_level); add("fatal_warnings", &BuildInfo::fatal_warnings); @@ -122,6 +124,11 @@ void BuildInfo::Loader::local_incpath(const string &s) obj.local_incpath.push_back(s); } +void BuildInfo::Loader::sysroot(const string &s) +{ + obj.sysroot = s; +} + void operator>>(const LexicalConverter &conv, BuildInfo::LibraryMode &libmode) { diff --git a/source/buildinfo.h b/source/buildinfo.h index ee831f3..691ec54 100644 --- a/source/buildinfo.h +++ b/source/buildinfo.h @@ -32,6 +32,7 @@ public: void libpath(const std::string &); void library(const std::string &); void local_incpath(const std::string &); + void sysroot(const std::string &); }; enum UpdateLevel @@ -72,6 +73,7 @@ public: typedef std::list WordList; typedef std::map LibModeMap; + Tracked sysroot; DefineMap defines; PathList incpath; PathList local_incpath; diff --git a/source/gnuccompiler.cpp b/source/gnuccompiler.cpp index cedea10..0210a6c 100644 --- a/source/gnuccompiler.cpp +++ b/source/gnuccompiler.cpp @@ -4,8 +4,8 @@ using namespace std; using namespace Msp; -GnuCCompiler::GnuCCompiler(Builder &b, const Architecture &a): - GnuCompiler(b, a, "CC") +GnuCCompiler::GnuCCompiler(Builder &b, const Architecture &a, const FS::Path &sysroot): + GnuCompiler(b, a, "CC", sysroot) { set_command("gcc", true); input_suffixes.push_back(".c"); diff --git a/source/gnuccompiler.h b/source/gnuccompiler.h index c533337..c0b5701 100644 --- a/source/gnuccompiler.h +++ b/source/gnuccompiler.h @@ -9,7 +9,7 @@ The GNU C compiler, commonly known as gcc. class GnuCCompiler: public GnuCompiler { public: - GnuCCompiler(Builder &, const Architecture &); + GnuCCompiler(Builder &, const Architecture &, const Msp::FS::Path & = Msp::FS::Path()); virtual Target *create_source(const Component &, const Msp::FS::Path &) const; virtual Target *create_source(const Msp::FS::Path &) const; diff --git a/source/gnucompiler.cpp b/source/gnucompiler.cpp index 16e24c0..1b05a5c 100644 --- a/source/gnucompiler.cpp +++ b/source/gnucompiler.cpp @@ -14,10 +14,15 @@ using namespace std; using namespace Msp; -GnuCompiler::GnuCompiler(Builder &b, const Architecture &a, const string &t): +GnuCompiler::GnuCompiler(Builder &b, const Architecture &a, const string &t, const FS::Path &sysroot): Tool(b, a, t) { - if(architecture->is_native()) + if(!sysroot.empty()) + { + build_info.sysroot = sysroot; + system_path.push_back(sysroot/"usr/include"); + } + else if(architecture->is_native()) system_path.push_back("/usr/include"); else system_path.push_back("/usr/"+architecture->get_cross_prefix()+"/include"); @@ -115,6 +120,10 @@ Task *GnuCompiler::run(const Target &target) const if(binfo.fatal_warnings) argv.push_back("-Werror"); } + + const FS::Path &sysroot = binfo.sysroot; + if(!sysroot.empty()) + argv.push_back("--sysroot="+sysroot.str()); for(BuildInfo::PathList::const_iterator i=binfo.local_incpath.begin(); i!=binfo.local_incpath.end(); ++i) { argv.push_back("-iquote"); @@ -122,6 +131,7 @@ Task *GnuCompiler::run(const Target &target) const } for(BuildInfo::PathList::const_iterator i=binfo.incpath.begin(); i!=binfo.incpath.end(); ++i) argv.push_back("-I"+i->str()); + for(BuildInfo::DefineMap::const_iterator i=binfo.defines.begin(); i!=binfo.defines.end(); ++i) { if(i->second.empty()) @@ -129,6 +139,7 @@ Task *GnuCompiler::run(const Target &target) const else argv.push_back(format("-D%s=%s", i->first, i->second)); } + if(binfo.debug) argv.push_back("-ggdb"); if(binfo.optimize) diff --git a/source/gnucompiler.h b/source/gnucompiler.h index 9f0304d..cd6aa52 100644 --- a/source/gnucompiler.h +++ b/source/gnucompiler.h @@ -15,7 +15,7 @@ class GnuCompiler: public Tool protected: std::string version; - GnuCompiler(Builder &, const Architecture &, const std::string &); + GnuCompiler(Builder &, const Architecture &, const std::string &, const Msp::FS::Path & = Msp::FS::Path()); public: virtual Target *create_target(const std::list &, const std::string &); diff --git a/source/gnucxxcompiler.cpp b/source/gnucxxcompiler.cpp index af001b3..d1d05d8 100644 --- a/source/gnucxxcompiler.cpp +++ b/source/gnucxxcompiler.cpp @@ -5,8 +5,8 @@ using namespace std; using namespace Msp; -GnuCxxCompiler::GnuCxxCompiler(Builder &b, const Architecture &a): - GnuCompiler(b, a, "CXX") +GnuCxxCompiler::GnuCxxCompiler(Builder &b, const Architecture &a, const FS::Path &sysroot): + GnuCompiler(b, a, "CXX", sysroot) { set_command("g++", true); input_suffixes.push_back(".cpp"); diff --git a/source/gnucxxcompiler.h b/source/gnucxxcompiler.h index 04c1fb2..491307e 100644 --- a/source/gnucxxcompiler.h +++ b/source/gnucxxcompiler.h @@ -9,7 +9,7 @@ The GNU C++ compiler, commonly known as g++. class GnuCxxCompiler: public GnuCompiler { public: - GnuCxxCompiler(Builder &, const Architecture &); + GnuCxxCompiler(Builder &, const Architecture &, const Msp::FS::Path & = Msp::FS::Path()); virtual Target *create_source(const Component &, const Msp::FS::Path &) const; virtual Target *create_source(const Msp::FS::Path &) const; diff --git a/source/gnulinker.cpp b/source/gnulinker.cpp index 2d7438d..e266a8e 100644 --- a/source/gnulinker.cpp +++ b/source/gnulinker.cpp @@ -21,13 +21,18 @@ using namespace std; using namespace Msp; -GnuLinker::GnuLinker(Builder &b, const Architecture &a): +GnuLinker::GnuLinker(Builder &b, const Architecture &a, const FS::Path &sysroot): Tool(b, a, "LINK") { input_suffixes.push_back(".o"); input_suffixes.push_back(".a"); - if(architecture->is_native()) + if(!sysroot.empty()) + { + build_info.sysroot = sysroot; + system_path.push_back(sysroot/"usr"/"lib"); + } + else if(architecture->is_native()) { system_path.push_back("/lib"); system_path.push_back("/usr/lib"); @@ -201,6 +206,11 @@ Task *GnuLinker::Linker::run(const Target &target) const BuildInfo binfo; target.collect_build_info(binfo); + + const FS::Path &sysroot = binfo.sysroot; + if(!sysroot.empty()) + argv.push_back("--sysroot="+sysroot.str()); + for(BuildInfo::PathList::const_iterator i=binfo.libpath.begin(); i!=binfo.libpath.end(); ++i) argv.push_back("-L"+i->str()); if(binfo.strip) diff --git a/source/gnulinker.h b/source/gnulinker.h index 4673c07..8d63538 100644 --- a/source/gnulinker.h +++ b/source/gnulinker.h @@ -34,7 +34,7 @@ private: Linker *cxx_linker; public: - GnuLinker(Builder &, const Architecture &); + GnuLinker(Builder &, const Architecture &, const Msp::FS::Path & = Msp::FS::Path()); ~GnuLinker(); virtual Target *create_target(const std::list &, const std::string &); diff --git a/source/gnuobjccompiler.cpp b/source/gnuobjccompiler.cpp index 228eecb..16e5215 100644 --- a/source/gnuobjccompiler.cpp +++ b/source/gnuobjccompiler.cpp @@ -3,8 +3,8 @@ using namespace Msp; -GnuObjCCompiler::GnuObjCCompiler(Builder &b, const Architecture &a): - GnuCompiler(b, a, "OBJC") +GnuObjCCompiler::GnuObjCCompiler(Builder &b, const Architecture &a, const FS::Path &sysroot): + GnuCompiler(b, a, "OBJC", sysroot) { set_command("gcc", true); input_suffixes.push_back(".m"); diff --git a/source/gnuobjccompiler.h b/source/gnuobjccompiler.h index 11fbd91..d82da03 100644 --- a/source/gnuobjccompiler.h +++ b/source/gnuobjccompiler.h @@ -6,7 +6,7 @@ class GnuObjCCompiler: public GnuCompiler { public: - GnuObjCCompiler(Builder &, const Architecture &); + GnuObjCCompiler(Builder &, const Architecture &, const Msp::FS::Path & = Msp::FS::Path()); virtual Target *create_source(const Component &, const Msp::FS::Path &) const; virtual Target *create_source(const Msp::FS::Path &) const; -- 2.45.2