Ninja
build.h
Go to the documentation of this file.
00001 // Copyright 2011 Google Inc. All Rights Reserved.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //     http://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #ifndef NINJA_BUILD_H_
00016 #define NINJA_BUILD_H_
00017 
00018 #include <map>
00019 #include <set>
00020 #include <string>
00021 #include <queue>
00022 #include <vector>
00023 using namespace std;
00024 
00025 struct BuildLog;
00026 struct Edge;
00027 struct DiskInterface;
00028 struct Node;
00029 struct State;
00030 
00031 /// Plan stores the state of a build plan: what we intend to build,
00032 /// which steps we're ready to execute.
00033 struct Plan {
00034   Plan();
00035 
00036   /// Add a target to our plan (including all its dependencies).
00037   /// Returns false if we don't need to build this target; may
00038   /// fill in |err| with an error message if there's a problem.
00039   bool AddTarget(Node* node, string* err);
00040 
00041   // Pop a ready edge off the queue of edges to build.
00042   // Returns NULL if there's no work to do.
00043   Edge* FindWork();
00044 
00045   /// Returns true if there's more work to be done.
00046   bool more_to_do() const { return wanted_edges_; }
00047 
00048   /// Dumps the current state of the plan.
00049   void Dump();
00050 
00051   /// Mark an edge as done building.  Used internally and by
00052   /// tests.
00053   void EdgeFinished(Edge* edge);
00054 
00055   /// Clean the given node during the build.
00056   void CleanNode(BuildLog* build_log, Node* node);
00057 
00058   /// Number of edges with commands to run.
00059   int command_edge_count() const { return command_edges_; }
00060 
00061 private:
00062   bool AddSubTarget(Node* node, vector<Node*>* stack, string* err);
00063   bool CheckDependencyCycle(Node* node, vector<Node*>* stack, string* err);
00064   void NodeFinished(Node* node);
00065 
00066   /// Keep track of which edges we want to build in this plan.  If this map does
00067   /// not contain an entry for an edge, we do not want to build the entry or its
00068   /// dependents.  If an entry maps to false, we do not want to build it, but we
00069   /// might want to build one of its dependents.  If the entry maps to true, we
00070   /// want to build it.
00071   map<Edge*, bool> want_;
00072 
00073   set<Edge*> ready_;
00074 
00075   /// Total number of edges that have commands (not phony).
00076   int command_edges_;
00077 
00078   /// Total remaining number of wanted edges.
00079   int wanted_edges_;
00080 };
00081 
00082 /// CommandRunner is an interface that wraps running the build
00083 /// subcommands.  This allows tests to abstract out running commands.
00084 /// RealCommandRunner is an implementation that actually runs commands.
00085 struct CommandRunner {
00086   virtual ~CommandRunner() {}
00087   virtual bool CanRunMore() = 0;
00088   virtual bool StartCommand(Edge* edge) = 0;
00089   /// Wait for a command to complete.
00090   virtual Edge* WaitForCommand(bool* success, string* output) = 0;
00091 };
00092 
00093 /// Options (e.g. verbosity, parallelism) passed to a build.
00094 struct BuildConfig {
00095   BuildConfig() : verbosity(NORMAL), dry_run(false), parallelism(1),
00096                   swallow_failures(0) {}
00097 
00098   enum Verbosity {
00099     NORMAL,
00100     QUIET,  // No output -- used when testing.
00101     VERBOSE
00102   };
00103   Verbosity verbosity;
00104   bool dry_run;
00105   int parallelism;
00106   int swallow_failures;
00107 };
00108 
00109 /// Builder wraps the build process: starting commands, updating status.
00110 struct Builder {
00111   Builder(State* state, const BuildConfig& config);
00112 
00113   Node* AddTarget(const string& name, string* err);
00114 
00115   /// Add a target to the build, scanning dependencies.
00116   /// @return false on error.
00117   bool AddTarget(Node* target, string* err);
00118 
00119   /// Returns true if the build targets are already up to date.
00120   bool AlreadyUpToDate() const;
00121 
00122   /// Run the build.  Returns false on error.
00123   /// It is an error to call this function when AlreadyUpToDate() is true.
00124   bool Build(string* err);
00125 
00126   bool StartEdge(Edge* edge, string* err);
00127   void FinishEdge(Edge* edge, bool success, const string& output);
00128 
00129   State* state_;
00130   const BuildConfig& config_;
00131   Plan plan_;
00132   DiskInterface* disk_interface_;
00133   CommandRunner* command_runner_;
00134   struct BuildStatus* status_;
00135   struct BuildLog* log_;
00136 };
00137 
00138 #endif  // NINJA_BUILD_H_