]> git.tdb.fi Git - builder.git/blobdiff - source/builder.cpp
Improve prefix management
[builder.git] / source / builder.cpp
index 21592661624742d5085d48a73a1c4ccf76e79572..3fc60bec2afc5b22f61175bf6dab10ed6f23e7dd 100644 (file)
@@ -1,4 +1,7 @@
+#include <deque>
 #include <set>
+#include <msp/core/algorithm.h>
+#include <msp/core/except.h>
 #include <msp/core/maputils.h>
 #include <msp/datafile/parser.h>
 #include <msp/fs/dir.h>
@@ -30,13 +33,9 @@ using namespace Msp;
 Builder::Builder():
        package_manager(*this),
        native_arch(*this, string()),
-       current_arch(0),
-       build_type(0),
        vfs(*this),
        build_graph(*this),
-       logger(&default_logger),
-       tempdir("temp"),
-       top_loader(0)
+       logger(&default_logger)
 {
        set_architecture(string());
 }
@@ -49,16 +48,16 @@ Builder::~Builder()
 
 void Builder::set_architecture(const string &name)
 {
+       if(current_arch!=&native_arch)
+               delete current_arch;
+
        if(name.empty())
-       {
                current_arch = &native_arch;
-               prefix = FS::get_home_dir()/"local";
-       }
        else
-       {
                current_arch = new Architecture(*this, name);
-               prefix = FS::get_home_dir()/"local"/current_arch->get_name();
-       }
+
+       if(auto_prefix)
+               update_auto_prefix();
 }
 
 vector<string> Builder::get_build_types() const
@@ -70,6 +69,13 @@ vector<string> Builder::get_build_types() const
        return keys;
 }
 
+const BuildType &Builder::get_build_type() const
+{
+       if(!build_type)
+               throw invalid_state("no build type");
+       return *build_type;
+}
+
 void Builder::set_build_type(const string &name)
 {
        build_type = &get_item(build_types, name);
@@ -77,6 +83,7 @@ void Builder::set_build_type(const string &name)
 
 void Builder::set_prefix(const FS::Path &p)
 {
+       auto_prefix = false;
        prefix = p;
 }
 
@@ -85,19 +92,32 @@ void Builder::set_temp_directory(const FS::Path &p)
        tempdir = p;
 }
 
+void Builder::update_auto_prefix()
+{
+       if(current_arch->is_native())
+               prefix = FS::get_home_dir()/"local";
+       else
+               prefix = FS::get_home_dir()/"local"/current_arch->get_name();
+}
+
 void Builder::add_default_tools()
 {
-       const string &arch_tc = current_arch->get_toolchain();
+       toolchain.add_toolchain(new GnuTools(*this, *current_arch));
+       toolchain.add_toolchain(new ClangTools(*this, *current_arch));
        if(current_arch->get_system()=="android")
                toolchain.add_toolchain(new AndroidTools(*this, *current_arch));
-       else if(arch_tc=="msvc")
+       if(current_arch->get_system()=="windows")
                toolchain.add_toolchain(new MicrosoftTools(*this, *current_arch));
-       else if(arch_tc=="clang")
-               toolchain.add_toolchain(new ClangTools(*this, *current_arch));
-       else if(arch_tc=="gnu")
-               toolchain.add_toolchain(new GnuTools(*this, *current_arch));
        toolchain.add_toolchain(new BuiltinTools(*this));
        toolchain.add_tool(new DataTool(*this));
+
+       auto i = find_if(toolchain.get_toolchains(), [](const Toolchain *tc){ return (tc->has_tool("CC") || tc->has_tool("CXX")); });
+       if(i!=toolchain.get_toolchains().end())
+       {
+               current_arch->refine((*i)->get_name());
+               if(auto_prefix)
+                       update_auto_prefix();
+       }
 }
 
 void Builder::set_logger(const Logger *l)
@@ -105,9 +125,9 @@ void Builder::set_logger(const Logger *l)
        logger = (l ? l : &default_logger);
 }
 
-list<string> Builder::collect_problems() const
+vector<string> Builder::collect_problems() const
 {
-       list<string> problems;
+       vector<string> problems;
        set<const Package *> broken_packages;
        set<const Component *> broken_components;
        set<const Tool *> broken_tools;
@@ -151,7 +171,7 @@ void Builder::load_build_file(const FS::Path &fn, const Config::InputOptions *op
 {
        IO::BufferedFile in(fn.str());
 
-       get_logger().log("files", format("Reading %s", fn));
+       get_logger().log("files", "Reading %s", fn);
 
        DataFile::Parser parser(in, fn.str());
        Loader loader(*this, opts, all);
@@ -173,7 +193,7 @@ int Builder::build(unsigned jobs, bool dry_run, bool show_progress)
                get_logger().log("summary", "Already up to date");
                return 0;
        }
-       get_logger().log("summary", format("Will build %d target%s", total, (total!=1 ? "s" : "")));
+       get_logger().log("summary", "Will build %d target%s", total, (total!=1 ? "s" : ""));
 
        vector<Task *> tasks;
 
@@ -191,11 +211,15 @@ int Builder::build(unsigned jobs, bool dry_run, bool show_progress)
                        if(tgt)
                        {
                                if(tgt->get_tool())
-                                       get_logger().log("tasks", format("%-4s  %s", tgt->get_tool()->get_tag(), tgt->get_name()));
+                               {
+                                       if(show_progress)
+                                               IO::print("\033[K");
+                                       get_logger().log("tasks", "%-4s  %s", tgt->get_tool()->get_tag(), tgt->get_name());
+                               }
                                Task *task = tgt->build();
                                if(task)
                                {
-                                       get_logger().log("commands", format("%s", task->get_command()));
+                                       get_logger().log("commands", "%s", task->get_command());
                                        if(dry_run)
                                        {
                                                task->signal_finished.emit(true);
@@ -259,7 +283,7 @@ int Builder::clean(bool all, bool dry_run)
        // Cleaning doesn't care about ordering, so a simpler method can be used
 
        set<Target *> clean_tgts;
-       list<Target *> queue;
+       deque<Target *> queue;
        queue.push_back(&build_graph.get_goals());
 
        while(!queue.empty())
@@ -277,7 +301,7 @@ int Builder::clean(bool all, bool dry_run)
 
        for(Target *t: clean_tgts)
        {
-               get_logger().log("tasks", format("RM    %s", t->get_name()));
+               get_logger().log("tasks", "RM    %s", t->get_name());
                if(!dry_run)
                        t->clean();
        }
@@ -324,7 +348,7 @@ void Builder::Loader::build_type(const string &n)
 {
        BuildType btype(n);
        load_sub(btype);
-       auto i = obj.build_types.insert(BuildTypeMap::value_type(n, btype)).first;
+       auto i = obj.build_types.insert({ n, btype }).first;
        if(!obj.build_type)
                obj.build_type = &i->second;
 }