1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef TOOLS_GN_ITEM_TREE_H_
6 #define TOOLS_GN_ITEM_TREE_H_
8 #include "base/containers/hash_tables.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/synchronization/lock.h"
11 #include "tools/gn/label.h"
18 // Represents the full dependency tree if labeled items in the system.
19 // Generally you will interact with this through the target manager, etc.
21 // There are two modes for filling out the dependency tree:
23 // - In greedy mode, every target we encounter will be generated. This means
24 // that we'll recursively load all of its subdependencies. So if you have
25 // a build file that's loaded for any reason, all targets in that build file
28 // - In non-greedy mode, we'll only generate and load dependncies for targets
29 // that have the should_generate bit set. This allows us to load the minimal
30 // set of buildfiles required for one or more targets.
32 // The main build is generally run in greedy mode, since people expect to be
33 // be able to write random tests and have them show up in the output. We'll
34 // switch into non-greed mode when doing diagnostics (like displaying the
35 // dependency tree on the command line) and for dependencies on targets in
36 // other toolchains. The toolchain behavior is important, if target A depends
37 // on B with an alternate toolchain, it doesn't mean we should recursively
38 // generate all targets in the buildfile just to get B: we should generate the
39 // and load the minimum number of files in order to resolve B.
45 // This lock must be held when calling the "Locked" functions below.
46 base::Lock
& lock() const { return lock_
; }
48 // Returns NULL if the item is not found.
50 // The lock must be held.
51 ItemNode
* GetExistingNodeLocked(const Label
& label
);
53 // There must not be an item with this label in the tree already. Takes
54 // ownership of the pointer.
56 // The lock must be held.
57 void AddNodeLocked(ItemNode
* node
);
59 // Mark the given item as being defined. If it has no unresolved
60 // dependencies, it will be marked resolved, and the resolved state will be
61 // recursively pushed into the dependency tree. Returns an error if there was
63 bool MarkItemDefinedLocked(const BuildSettings
* build_settings
,
67 // Fills the given vector with all known items.
68 void GetAllItemNodesLocked(std::vector
<const ItemNode
*>* dest
) const;
69 void GetAllItemsLocked(std::vector
<const Item
*>* dest
) const;
71 // Returns an error if there are unresolved dependencies, or no error if
74 // The lock should not be held.
75 Err
CheckForBadItems() const;
78 void MarkItemResolvedLocked(ItemNode
* node
);
80 // Given a set of unresolved nodes, looks for cycles and returns the error
81 // message describing any cycles it found.
82 std::string
CheckForCircularDependenciesLocked(
83 const std::vector
<const ItemNode
*>& bad_nodes
) const;
85 mutable base::Lock lock_
;
87 typedef base::hash_map
<Label
, ItemNode
*> StringToNodeHash
;
88 StringToNodeHash items_
; // Owning pointer.
90 DISALLOW_COPY_AND_ASSIGN(ItemTree
);
93 #endif // TOOLS_GN_ITEM_TREE_H_