From: Mikko Rasa Date: Sun, 7 Apr 2013 19:55:03 +0000 (+0300) Subject: Move some more functionality into BuildGraph X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=09366b5da31707942f66e386cd0c07e5ece17b7f;p=builder.git Move some more functionality into BuildGraph --- diff --git a/Readme.txt b/Readme.txt index db18ed7..9ae07f6 100644 --- a/Readme.txt +++ b/Readme.txt @@ -236,9 +236,9 @@ install tarballs Depends on source tarballs of all packages. -cmdline - This target is an internal representation of the command line. Trying to - add it to the command line will cause Builder to abort. +goals + Depends on all targets on the command line. Trying to add this target to the + command line will cause Builder to abort due to a circular dependency. ------------------------------------------------------------------------------- diff --git a/source/analyzer.cpp b/source/analyzer.cpp index 39cc7e1..d15a747 100644 --- a/source/analyzer.cpp +++ b/source/analyzer.cpp @@ -43,15 +43,15 @@ void Analyzer::analyze() row.push_back("Rebuild"); table.push_back(row); - Target &cmdline = *builder.get_build_graph().get_target("cmdline"); + Target &goals = builder.get_build_graph().get_goals(); if(mode==RDEPS) { - const Target::Dependencies &deps = cmdline.get_dependencies(); + const Target::Dependencies &deps = goals.get_dependencies(); for(Target::Dependencies::const_iterator i=deps.begin(); i!=deps.end(); ++i) build_depend_table(**i, 0); } else - build_depend_table(cmdline, 0); + build_depend_table(goals, 0); print_table(); } diff --git a/source/builder.cpp b/source/builder.cpp index 8b900a4..c599e26 100644 --- a/source/builder.cpp +++ b/source/builder.cpp @@ -150,9 +150,6 @@ Builder::Builder(int argc, char **argv): cmdline_targets.push_back(*i); } - if(cmdline_targets.empty()) - cmdline_targets.push_back("default"); - if(!work_dir.empty()) FS::chdir(work_dir); @@ -351,8 +348,7 @@ bool Builder::prepare_build() { package_manager.get_main_package().prepare(); - // Make the cmdline target depend on all targets mentioned on the command line - Target *cmdline = new VirtualTarget(*this, "cmdline"); + // Add targets from command line as goals for(NameList::iterator i=cmdline_targets.begin(); i!=cmdline_targets.end(); ++i) { Target *tgt = build_graph.get_target(*i); @@ -366,10 +362,10 @@ bool Builder::prepare_build() return false; } - cmdline->add_dependency(*tgt); + build_graph.add_goal(*tgt); } - cmdline->prepare(); + build_graph.prepare(); // Apply what-ifs for(NameList::iterator i=what_if.begin(); i!=what_if.end(); ++i) @@ -384,11 +380,7 @@ bool Builder::prepare_build() } if(build_all) - { - for(TargetMap::iterator i=targets.begin(); i!=targets.end(); ++i) - if(i->second->is_buildable() && !i->second->needs_rebuild()) - i->second->force_rebuild(); - } + build_graph.force_full_rebuild(); if(!dry_run) { @@ -402,13 +394,7 @@ bool Builder::prepare_build() int Builder::do_build() { - Target *cmdline = build_graph.get_target("cmdline"); - - unsigned total = 0; - const BuildGraph::TargetMap &targets = build_graph.get_targets(); - for(map::const_iterator i=targets.begin(); i!=targets.end(); ++i) - if(i->second->is_buildable() && i->second->needs_rebuild()) - ++total; + unsigned total = build_graph.count_rebuild_targets(); if(!total) { @@ -429,7 +415,7 @@ int Builder::do_build() { if(tasks.size()get_buildable_target(); + Target *tgt = build_graph.get_buildable_target(); if(tgt) { if(tgt->get_tool()) diff --git a/source/buildgraph.cpp b/source/buildgraph.cpp index 8c0ff12..94b5ed6 100644 --- a/source/buildgraph.cpp +++ b/source/buildgraph.cpp @@ -8,7 +8,8 @@ using namespace std; BuildGraph::BuildGraph(Builder &b): - builder(b) + builder(b), + goals(new VirtualTarget(builder, "goals")) { Target *world = new VirtualTarget(builder, "world"); world->add_dependency(*new VirtualTarget(builder, "default")); @@ -53,3 +54,36 @@ void BuildGraph::add_installed_target(Target &t) inst_tgt = builder.get_toolchain().get_tool("CP").create_target(t); get_target("install")->add_dependency(*inst_tgt); } + +void BuildGraph::add_goal(Target &t) +{ + goals->add_dependency(t); +} + +void BuildGraph::prepare() +{ + if(goals->get_dependencies().empty()) + add_goal(*get_target("default")); + goals->prepare(); +} + +void BuildGraph::force_full_rebuild() +{ + for(TargetMap::iterator i=targets.begin(); i!=targets.end(); ++i) + if(i->second->is_buildable() && !i->second->needs_rebuild()) + i->second->force_rebuild(); +} + +unsigned BuildGraph::count_rebuild_targets() const +{ + unsigned count = 0; + for(map::const_iterator i=targets.begin(); i!=targets.end(); ++i) + if(i->second->is_buildable() && i->second->needs_rebuild()) + ++count; + return count; +} + +Target *BuildGraph::get_buildable_target() const +{ + return goals->get_buildable_target(); +} diff --git a/source/buildgraph.h b/source/buildgraph.h index 54409c0..e702b51 100644 --- a/source/buildgraph.h +++ b/source/buildgraph.h @@ -17,6 +17,7 @@ public: private: Builder &builder; TargetMap targets; + Target *goals; public: BuildGraph(Builder &); @@ -40,6 +41,28 @@ public: /** Adds a target that will be installed. A new InstalledFile target is created and added as a dependency to the "install" virtual target. */ void add_installed_target(Target &); + + /** Adds a target as a toplevel goal. These are stored as dependencies of + the "goals" virtual target. */ + void add_goal(Target &); + + Target &get_goals() const { return *goals; } + + /** Prepares all toplevel goals for building. If no goals are defined, the + "default" target is added as a goal. */ + void prepare(); + + /** Marks all buildable targets to be rebuilt. The graph must be prepared + first. */ + void force_full_rebuild(); + + /** Returns the number of targets that are going to be rebuilt. The graph + must be prepared first. */ + unsigned count_rebuild_targets() const; + + /** Returns a target that can be built and is needed for building the goal + targets. Null */ + Target *get_buildable_target() const; }; #endif