From 3585dea6eabf5f1565886bf09c2bdb2eee978912 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 15 Aug 2008 21:26:11 +0000 Subject: [PATCH] Refactor arch handling a bit Add detection of native arch name --- source/architecture.cpp | 11 +++++---- source/architecture.h | 5 +++- source/builder.cpp | 50 +++++++++++++++++++++++----------------- source/builder.h | 6 +++-- source/sourcepackage.cpp | 6 ++--- 5 files changed, 46 insertions(+), 32 deletions(-) diff --git a/source/architecture.cpp b/source/architecture.cpp index 50013e8..1f578af 100644 --- a/source/architecture.cpp +++ b/source/architecture.cpp @@ -11,9 +11,10 @@ Distributed under the LGPL using namespace std; using namespace Msp; -Architecture::Architecture(Builder &b, const string &n): +Architecture::Architecture(Builder &b, const string &n, bool a): builder(b), - name(n) + name(n), + native(a) { } void Architecture::set_tool(const string &t, const string &p) @@ -32,10 +33,10 @@ std::string Architecture::get_tool(const string &t) const return i->second; } - if(name!="native") + if(!native) { - const Architecture &native=builder.get_architecture("native"); - return prefix+"-"+native.get_tool(t); + const Architecture &native_arch=builder.get_native_arch(); + return prefix+"-"+native_arch.get_tool(t); } else throw KeyError("Unknown tool"); diff --git a/source/architecture.h b/source/architecture.h index a205e53..af58619 100644 --- a/source/architecture.h +++ b/source/architecture.h @@ -13,6 +13,7 @@ Distributed under the LGPL class Builder; +// XXX Add lib/exe prefix/suffix fields. Some archs may need multiple alternatives, how to handle this? class Architecture { public: @@ -27,14 +28,16 @@ public: void tool(const std::string &t, const std::string &p); }; - Architecture(Builder &b, const std::string &n); + Architecture(Builder &b, const std::string &n, bool a=false); void set_tool(const std::string &t, const std::string &p); std::string get_tool(const std::string &t) const; const std::string &get_name() const { return name; } + bool is_native() const { return native; } const std::string &get_prefix() const { return prefix; } private: Builder &builder; std::string name; + bool native; std::string prefix; StringMap tools; }; diff --git a/source/builder.cpp b/source/builder.cpp index b77b8fc..286f53b 100644 --- a/source/builder.cpp +++ b/source/builder.cpp @@ -7,6 +7,7 @@ Distributed under the LGPL #include #include +#include #include #include #include @@ -54,8 +55,7 @@ Builder::Builder(int argc, char **argv): conf_all(false), conf_only(false), build_all(false), - create_makefile(false), - current_arch("native") + create_makefile(false) { string analyze_mode; string work_dir; @@ -63,6 +63,7 @@ Builder::Builder(int argc, char **argv): unsigned max_depth=5; StringList cmdline_warn; string prfx; + string arch; GetOpt getopt; getopt.add_option('a', "analyze", analyze_mode, GetOpt::REQUIRED_ARG); @@ -78,7 +79,7 @@ Builder::Builder(int argc, char **argv): getopt.add_option('C', "chdir", work_dir, GetOpt::REQUIRED_ARG); getopt.add_option('P', "progress", show_progress, GetOpt::NO_ARG); getopt.add_option('W', "what-if", what_if, GetOpt::REQUIRED_ARG); - getopt.add_option( "arch", current_arch, GetOpt::REQUIRED_ARG); + getopt.add_option( "arch", arch, GetOpt::REQUIRED_ARG); getopt.add_option( "conf-only", conf_only, GetOpt::NO_ARG); getopt.add_option( "full-paths", full_paths, GetOpt::NO_ARG); //getopt.add_option( "makefile", create_makefile, GetOpt::NO_ARG); @@ -126,21 +127,31 @@ Builder::Builder(int argc, char **argv): cwd=FS::getcwd(); - Architecture &native_arch=archs.insert(ArchMap::value_type("native", Architecture(*this, "native"))).first->second; - native_arch.set_tool("CC", "gcc"); - native_arch.set_tool("CXX", "g++"); - native_arch.set_tool("LD", "gcc"); - native_arch.set_tool("LXX", "g++"); - native_arch.set_tool("AR", "ar"); + utsname un; + string sysname="native"; + if(uname(&un)==0) + sysname=tolower(un.sysname); + + native_arch=&archs.insert(ArchMap::value_type(sysname, Architecture(*this, sysname, true))).first->second; + native_arch->set_tool("CC", "gcc"); + native_arch->set_tool("CXX", "g++"); + native_arch->set_tool("LD", "gcc"); + native_arch->set_tool("LXX", "g++"); + native_arch->set_tool("AR", "ar"); load_build_file((FS::get_home_dir()/".builderrc").str()); + if(arch.empty()) + current_arch=native_arch; + else + current_arch=&get_architecture(arch); + if(prfx.empty()) { - if(current_arch=="native") + if(current_arch->is_native()) prefix=(FS::get_home_dir()/"local").str(); else - prefix=(FS::get_home_dir()/"local"/current_arch).str(); + prefix=(FS::get_home_dir()/"local"/current_arch->get_name()).str(); } else prefix=FS::getcwd()/prfx; @@ -246,10 +257,10 @@ Target *Builder::get_header(const string &include, const string &from, const lis cout<<"Looking for include "<is_native()) syspath.push_back("/usr/include"); else - syspath.push_back("/usr/"+get_architecture(current_arch).get_prefix()+"/include"); + syspath.push_back("/usr/"+current_arch->get_prefix()+"/include"); syspath.push_back((FS::Path("/usr/include/c++/")/cxx_ver/fn).str()); Target *tgt=0; @@ -288,7 +299,7 @@ Target *Builder::get_library(const string &lib, const list &path, LibMod return i->second; StringList syspath; - if(current_arch=="native") + if(current_arch->is_native()) { syspath.push_back("/lib"); syspath.push_back("/usr/lib"); @@ -319,11 +330,6 @@ const Architecture &Builder::get_architecture(const string &arch) const return i->second; } -const Architecture &Builder::get_current_arch() const -{ - return get_architecture(current_arch); -} - void Builder::apply_profile_template(Config &config, const string &pt) const { vector parts=split(pt, '-'); @@ -379,6 +385,8 @@ int Builder::main() if(conf_only) return 0; + if(verbose>=2) + cout<<"Building on "<get_name()<<", for "<get_name()<<'\n'; if(verbose>=1) cout<=2) @@ -676,7 +684,7 @@ Target *Builder::get_library(const string &lib, const FS::Path &path, LibMode mo if(mode!=ALL_STATIC) { - if(current_arch=="win32") + if(current_arch->get_name()=="win32") { candidates.push_back("lib"+lib+".dll"); candidates.push_back(lib+".dll"); @@ -688,7 +696,7 @@ Target *Builder::get_library(const string &lib, const FS::Path &path, LibMode mo /* Static libraries are always considered, since sometimes shared versions may not be available */ candidates.push_back("lib"+lib+".a"); - if(current_arch=="win32") + if(current_arch->get_name()=="win32") candidates.push_back("lib"+lib+".dll.a"); for(StringList::iterator i=candidates.begin(); i!=candidates.end(); ++i) diff --git a/source/builder.h b/source/builder.h index 19885c5..8eae216 100644 --- a/source/builder.h +++ b/source/builder.h @@ -43,7 +43,8 @@ public: Target *get_library(const std::string &, const StringList &, LibMode); const Msp::FS::Path &get_cwd() const { return cwd; } const Architecture &get_architecture(const std::string &) const; - const Architecture &get_current_arch() const; + const Architecture &get_current_arch() const { return *current_arch; } + const Architecture &get_native_arch() const { return *native_arch; } const Msp::FS::Path &get_prefix() const { return prefix; } const StringList &get_warnings() const { return warnings; } void apply_profile_template(Config &, const std::string &) const; @@ -97,6 +98,8 @@ private: TargetMap libraries; ArchMap archs; + Architecture *native_arch; + const Architecture *current_arch; ProfileTemplateMap profile_tmpl; ProblemList problems; @@ -114,7 +117,6 @@ private: bool conf_only; bool build_all; bool create_makefile; - std::string current_arch; Msp::FS::Path prefix; StringList warnings; diff --git a/source/sourcepackage.cpp b/source/sourcepackage.cpp index 9da7973..3fddfec 100644 --- a/source/sourcepackage.cpp +++ b/source/sourcepackage.cpp @@ -35,11 +35,11 @@ Msp::FS::Path SourcePackage::get_temp_dir() const Msp::FS::Path SourcePackage::get_out_dir() const { - const std::string &arch_name=builder.get_current_arch().get_name(); - if(arch_name=="native") + const Architecture &arch=builder.get_current_arch(); + if(arch.is_native()) return source/config.get_option("outdir").value; else - return source/arch_name/config.get_option("outdir").value; + return source/arch.get_name()/config.get_option("outdir").value; } /** -- 2.45.2