]> git.tdb.fi Git - builder.git/commitdiff
Refactor arch handling a bit
authorMikko Rasa <tdb@tdb.fi>
Fri, 15 Aug 2008 21:26:11 +0000 (21:26 +0000)
committerMikko Rasa <tdb@tdb.fi>
Fri, 15 Aug 2008 21:26:11 +0000 (21:26 +0000)
Add detection of native arch name

source/architecture.cpp
source/architecture.h
source/builder.cpp
source/builder.h
source/sourcepackage.cpp

index 50013e88de7adc955dbb37b10f0ac381edbf65a9..1f578af409f72ce367f5fb09ee0fa968e5020976 100644 (file)
@@ -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");
index a205e5389998b854a6401dc83302095c2927a9b2..af586192f9ef44b4dafa9103c68ca2b797000e43 100644 (file)
@@ -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;
 };
index b77b8fc3d453f17d755115cdc23f480f3be99b63..286f53b049cf8284b770821b0098fe58a4f2c77c 100644 (file)
@@ -7,6 +7,7 @@ Distributed under the LGPL
 
 #include <iostream>
 #include <set>
+#include <sys/utsname.h>
 #include <msp/core/except.h>
 #include <msp/core/getopt.h>
 #include <msp/datafile/parser.h>
@@ -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 "<<fn<<" with path "<<join(path.begin(), path.end())<<'\n';
 
        StringList syspath;
-       if(current_arch=="native")
+       if(current_arch->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<string> &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<string> parts=split(pt, '-');
@@ -379,6 +385,8 @@ int Builder::main()
        if(conf_only)
                return 0;
 
+       if(verbose>=2)
+               cout<<"Building on "<<native_arch->get_name()<<", for "<<current_arch->get_name()<<'\n';
        if(verbose>=1)
                cout<<all_reqs.size()<<" active packages, "<<targets.size()<<" targets\n";
        if(verbose>=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)
index 19885c56291729d971776ec57a3295c546073edb..8eae21681f579ae21b7b686a7880c058f935e230 100644 (file)
@@ -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;
 
index 9da79739d56854130f42d5fdbf58963557fa9450..3fddfecf0c1fb062be4cae9065a881038bf996b7 100644 (file)
@@ -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;
 }
 
 /**