]> git.tdb.fi Git - builder.git/blobdiff - source/builder.cpp
Improve Builder::get_header
[builder.git] / source / builder.cpp
index 34c65af48be88df4d0be392b9735bad57472a3ea..d9444ce8a2c878d1eed9e4f9a80456460d43ccbc 100644 (file)
@@ -13,6 +13,7 @@ Distributed under the LGPL
 #include <msp/datafile/parser.h>
 #include <msp/path/utils.h>
 #include <msp/strings/formatter.h>
+#include <msp/strings/regex.h>
 #include <msp/strings/utils.h>
 #include <msp/time/units.h>
 #include <msp/time/utils.h>
@@ -198,7 +199,7 @@ path.  Considers known targets as well as existing files.  If a matching target
 is not found but a file exists, a new SystemHeader target will be created and
 returned.
 */
-Target *Builder::get_header(const string &include, const string &, const string &from, const list<string> &path)
+Target *Builder::get_header(const string &include, const string &arch, const string &from, const list<string> &path)
 {
        string hash(8, 0);
        update_hash(hash, from);
@@ -210,21 +211,43 @@ Target *Builder::get_header(const string &include, const string &, const string
        if(i!=includes.end())
                return i->second;
 
-       string fn=include.substr(1);
-       Target *tgt=0;
-       if(include[0]=='"' && (tgt=get_header(Path(from)/fn)))
-               ;
-       else if((tgt=get_header(Path("/usr/include")/fn)))
-               ;
-       //XXX Determine the C++ header location dynamically
-       else if((tgt=get_header(Path("/usr/include/c++/4.1.2")/fn)))
-               ;
-       else
+       static string cxx_ver;
+       if(cxx_ver.empty())
        {
-               for(list<string>::const_iterator j=path.begin(); (j!=path.end() && !tgt); ++j)
-                       tgt=get_header(cwd/ *j/fn);
+               StringList argv;
+               argv.push_back(get_architecture(arch).get_tool("CXX"));
+               argv.push_back("--version");
+               cxx_ver=Regex(" ([0-9.]+) ").match(run_command(argv))[1].str;
+               while(!cxx_ver.empty() && !exists(Path("/usr/include/c++")/cxx_ver))
+               {
+                       unsigned dot=cxx_ver.rfind('.');
+                       if(dot==string::npos)
+                               break;
+                       cxx_ver.erase(dot);
+               }
+               if(verbose>=5)
+                       cout<<"C++ version is "<<cxx_ver<<'\n';
        }
 
+       string fn=include.substr(1);
+       if(verbose>=5)
+               cout<<"Looking for include "<<fn<<" with path "<<join(path.begin(), path.end())<<'\n';
+
+       StringList syspath;
+       if(include[0]=='\"')
+               syspath.push_back(from);
+       if(arch=="native")
+               syspath.push_back("/usr/include");
+       else
+               syspath.push_back("/usr/"+get_architecture(arch).get_prefix()+"/lib");
+       syspath.push_back((Path("/usr/include/c++/")/cxx_ver/fn).str());
+
+       Target *tgt=0;
+       for(list<string>::const_iterator j=syspath.begin(); (!tgt && j!=syspath.end()); ++j)
+               tgt=get_header(Path(*j)/fn);
+       for(list<string>::const_iterator j=path.begin(); (!tgt && j!=path.end()); ++j)
+               tgt=get_header(cwd/ *j/fn);
+
        includes.insert(TargetMap::value_type(id, tgt));
 
        return tgt;
@@ -262,6 +285,9 @@ Target *Builder::get_library(const string &lib, const string &arch, const list<s
        else
                syspath.push_back("/usr/"+get_architecture(arch).get_prefix()+"/lib");
 
+       if(verbose>=5)
+               cout<<"Looking for library "<<lib<<" with path "<<join(path.begin(), path.end())<<'\n';
+
        Target *tgt=0;
        for(StringList::iterator j=syspath.begin(); (!tgt && j!=syspath.end()); ++j)
                tgt=get_library(lib, arch, *j, mode);
@@ -342,15 +368,6 @@ int Builder::main()
                        problem(spkg->get_name(), format("wrong architecture (%s)", spkg->get_arch()));
        }
 
-       if(!problems.empty())
-       {
-               cerr<<"The following problems were detected:\n";
-               for(ProblemList::iterator i=problems.begin(); i!=problems.end(); ++i)
-                       cerr<<"  "<<i->package<<": "<<i->descr<<'\n';
-               cerr<<"Please fix them and try again.\n";
-               return 1;
-       }
-
        if(conf_only)
                return 0;
 
@@ -385,6 +402,15 @@ int Builder::main()
        if(analyzer)
                analyzer->analyze();
 
+       if(!problems.empty())
+       {
+               cerr<<"The following problems were detected:\n";
+               for(ProblemList::iterator i=problems.begin(); i!=problems.end(); ++i)
+                       cerr<<"  "<<i->package<<": "<<i->descr<<'\n';
+               cerr<<"Please fix them and try again.\n";
+               return 1;
+       }
+
        //if(create_makefile
 
        if(clean)