]> git.tdb.fi Git - builder.git/blobdiff - source/builder.cpp
Exception handling fixes
[builder.git] / source / builder.cpp
index 2ef5a709e1913d33cae79f1677ce1b21290d1cee..8bef2c6e5d7a67d5126d86fbfa53b1295f5eb482 100644 (file)
@@ -5,14 +5,17 @@ Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
 Distributed under the LGPL
 */
 
-#include <fstream>
 #include <iostream>
 #include <set>
-#include <msp/core/error.h>
+#include <msp/core/except.h>
 #include <msp/core/getopt.h>
 #include <msp/datafile/parser.h>
+#include <msp/io/buffered.h>
+#include <msp/io/except.h>
+#include <msp/io/file.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>
@@ -113,7 +116,7 @@ Builder::Builder(int argc, char **argv):
        if(!work_dir.empty())
                chdir(work_dir.c_str());
 
-       cwd=Path::getcwd();
+       cwd=getcwd();
 
        Architecture &native_arch=archs.insert(ArchMap::value_type("native", Architecture(*this, "native"))).first->second;
        native_arch.set_tool("CC",  "gcc");
@@ -124,7 +127,7 @@ Builder::Builder(int argc, char **argv):
 
        const char *home=getenv("HOME");
        if(home)
-               load_build_file((Path::Path(home)/".builderrc").str());
+               load_build_file((Path(home)/".builderrc").str());
 }
 
 /**
@@ -145,6 +148,8 @@ Package *Builder::get_package(const string &n)
        argv.push_back("pkg-config");
        argv.push_back("--variable=source");
        argv.push_back(n);
+       if(verbose>=4)
+               cout<<"Running "<<join(argv.begin(), argv.end())<<'\n';
        string srcdir=strip(run_command(argv));
 
        PathList dirs;
@@ -196,7 +201,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);
@@ -208,21 +213,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::Path(from)/fn)))
-               ;
-       else if((tgt=get_header(Path::Path("/usr/include")/fn)))
-               ;
-       //XXX Determine the C++ header location dynamically
-       else if((tgt=get_header(Path::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]\\.[0-9.]+").match(run_command(argv))[0].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(arch=="native")
+               syspath.push_back("/usr/include");
+       else
+               syspath.push_back("/usr/"+get_architecture(arch).get_prefix()+"/include");
+       syspath.push_back((Path("/usr/include/c++/")/cxx_ver/fn).str());
+
+       Target *tgt=0;
+       if(include[0]=='\"')
+               tgt=get_header(Path(from)/fn);
+       for(list<string>::const_iterator j=path.begin(); (!tgt && j!=path.end()); ++j)
+               tgt=get_header(cwd/ *j/fn);
+       for(list<string>::const_iterator j=syspath.begin(); (!tgt && j!=syspath.end()); ++j)
+               tgt=get_header(Path(*j)/fn);
+
        includes.insert(TargetMap::value_type(id, tgt));
 
        return tgt;
@@ -260,11 +287,14 @@ 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);
        for(StringList::const_iterator j=path.begin(); (!tgt && j!=path.end()); ++j)
                tgt=get_library(lib, arch, cwd/ *j, mode);
+       for(StringList::iterator j=syspath.begin(); (!tgt && j!=syspath.end()); ++j)
+               tgt=get_library(lib, arch, *j, mode);
 
        libraries.insert(TargetMap::value_type(id, tgt));
 
@@ -340,15 +370,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;
 
@@ -383,6 +404,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)
@@ -442,18 +472,24 @@ Loads the given build file.
 
 @return  0 on success, -1 if the file could not be opened
 */
-int Builder::load_build_file(const Path::Path &fn)
+int Builder::load_build_file(const Path &fn)
 {
-       ifstream in(fn.str().c_str());
-       if(!in)
-               return -1;
+       try
+       {
+               IO::File inf(fn.str());
+               IO::Buffered in(inf);
 
-       if(verbose>=3)
-               cout<<"Reading "<<fn<<'\n';
+               if(verbose>=3)
+                       cout<<"Reading "<<fn<<'\n';
 
-       DataFile::Parser parser(in, fn.str());
-       Loader loader(*this, fn.subpath(0, fn.size()-1));
-       loader.load(parser);
+               DataFile::Parser parser(in, fn.str());
+               Loader loader(*this, fn.subpath(0, fn.size()-1));
+               loader.load(parser);
+       }
+       catch(const IO::FileNotFound &)
+       {
+               return -1;
+       }
 
        return 0;
 }
@@ -559,12 +595,12 @@ int Builder::create_targets()
 Check if a header exists, either as a target or a file.  Either an existing
 target or a new SystemHeader target will be returned.
 */
-Target *Builder::get_header(const Msp::Path::Path &fn)
+Target *Builder::get_header(const Msp::Path &fn)
 {
        Target *tgt=get_target(fn.str());
        if(tgt) return tgt;
 
-       if(Path::exists(fn))
+       if(exists(fn))
        {
                tgt=new SystemHeader(*this, fn.str());
                return tgt;
@@ -572,7 +608,7 @@ Target *Builder::get_header(const Msp::Path::Path &fn)
        return 0;
 }
 
-Target *Builder::get_library(const string &lib, const string &arch, const Path::Path &path, LibMode mode)
+Target *Builder::get_library(const string &lib, const string &arch, const Path &path, LibMode mode)
 {
        // Populate a list of candidate filenames
        StringList candidates;
@@ -580,7 +616,10 @@ Target *Builder::get_library(const string &lib, const string &arch, const Path::
        if(mode!=ALL_STATIC)
        {
                if(arch=="win32")
+               {
                        candidates.push_back("lib"+lib+".dll");
+                       candidates.push_back(lib+".dll");
+               }
                else
                        candidates.push_back("lib"+lib+".so");
        }
@@ -609,7 +648,7 @@ Target *Builder::get_library(const string &lib, const string &arch, const Path::
                        else if(tgt)
                                return tgt;
                }
-               else if(Path::exists(full))
+               else if(exists(full))
                {
                        tgt=new SystemLibrary(*this, full);
                        return tgt;
@@ -767,7 +806,7 @@ void Builder::package_help()
 Application::RegApp<Builder> Builder::reg;
 
 
-Builder::Loader::Loader(Builder &b, const Path::Path &s):
+Builder::Loader::Loader(Builder &b, const Path &s):
        bld(b),
        src(s)
 {