Ninja
state.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_STATE_H_
00016 #define NINJA_STATE_H_
00017 
00018 #include <map>
00019 #include <set>
00020 #include <string>
00021 #include <vector>
00022 using namespace std;
00023 
00024 #include "eval_env.h"
00025 #include "hash_map.h"
00026 
00027 struct Edge;
00028 struct Node;
00029 struct Rule;
00030 
00031 /// A pool for delayed edges.
00032 /// Pools are scoped to a State. Edges within a State will share Pools. A Pool
00033 /// will keep a count of the total 'weight' of the currently scheduled edges. If
00034 /// a Plan attempts to schedule an Edge which would cause the total weight to
00035 /// exceed the depth of the Pool, the Pool will enque the Edge instead of
00036 /// allowing the Plan to schedule it. The Pool will relinquish queued Edges when
00037 /// the total scheduled weight diminishes enough (i.e. when a scheduled edge
00038 /// completes).
00039 struct Pool {
00040   explicit Pool(const string& name, int depth)
00041     : name_(name), current_use_(0), depth_(depth), delayed_(&WeightedEdgeCmp) { }
00042 
00043   // A depth of 0 is infinite
00044   bool is_valid() const { return depth_ >= 0; }
00045   int depth() const { return depth_; }
00046   const string& name() const { return name_; }
00047 
00048   /// true if the Pool might delay this edge
00049   bool ShouldDelayEdge() const { return depth_ != 0; }
00050 
00051   /// informs this Pool that the given edge is committed to be run.
00052   /// Pool will count this edge as using resources from this pool.
00053   void EdgeScheduled(const Edge& edge);
00054 
00055   /// informs this Pool that the given edge is no longer runnable, and should
00056   /// relinquish its resources back to the pool
00057   void EdgeFinished(const Edge& edge);
00058 
00059   /// adds the given edge to this Pool to be delayed.
00060   void DelayEdge(Edge* edge);
00061 
00062   /// Pool will add zero or more edges to the ready_queue
00063   void RetrieveReadyEdges(set<Edge*>* ready_queue);
00064 
00065   /// Dump the Pool and its edges (useful for debugging).
00066   void Dump() const;
00067 
00068  private:
00069   string name_;
00070 
00071   /// |current_use_| is the total of the weights of the edges which are
00072   /// currently scheduled in the Plan (i.e. the edges in Plan::ready_).
00073   int current_use_;
00074   int depth_;
00075 
00076   static bool WeightedEdgeCmp(const Edge* a, const Edge* b);
00077 
00078   typedef set<Edge*,bool(*)(const Edge*, const Edge*)> DelayedEdges;
00079   DelayedEdges delayed_;
00080 };
00081 
00082 /// Global state (file status, loaded rules) for a single run.
00083 struct State {
00084   static Pool kDefaultPool;
00085   static const Rule kPhonyRule;
00086 
00087   State();
00088 
00089   void AddRule(const Rule* rule);
00090   const Rule* LookupRule(const string& rule_name);
00091 
00092   void AddPool(Pool* pool);
00093   Pool* LookupPool(const string& pool_name);
00094 
00095   Edge* AddEdge(const Rule* rule);
00096 
00097   Node* GetNode(StringPiece path);
00098   Node* LookupNode(StringPiece path);
00099   Node* SpellcheckNode(const string& path);
00100 
00101   void AddIn(Edge* edge, StringPiece path);
00102   void AddOut(Edge* edge, StringPiece path);
00103   bool AddDefault(StringPiece path, string* error);
00104 
00105   /// Reset state.  Keeps all nodes and edges, but restores them to the
00106   /// state where we haven't yet examined the disk for dirty state.
00107   void Reset();
00108 
00109   /// Dump the nodes and Pools (useful for debugging).
00110   void Dump();
00111 
00112   /// @return the root node(s) of the graph. (Root nodes have no output edges).
00113   /// @param error where to write the error message if somethings went wrong.
00114   vector<Node*> RootNodes(string* error);
00115   vector<Node*> DefaultNodes(string* error);
00116 
00117   /// Mapping of path -> Node.
00118   typedef ExternalStringHashMap<Node*>::Type Paths;
00119   Paths paths_;
00120 
00121   /// All the rules used in the graph.
00122   map<string, const Rule*> rules_;
00123 
00124   /// All the pools used in the graph.
00125   map<string, Pool*> pools_;
00126 
00127   /// All the edges of the graph.
00128   vector<Edge*> edges_;
00129 
00130   BindingEnv bindings_;
00131   vector<Node*> defaults_;
00132 };
00133 
00134 #endif  // NINJA_STATE_H_