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 #include "testing/gtest/include/gtest/gtest.h"
6 #include "tools/gn/builder.h"
7 #include "tools/gn/loader.h"
8 #include "tools/gn/target.h"
9 #include "tools/gn/test_with_scope.h"
10 #include "tools/gn/toolchain.h"
14 class MockLoader
: public Loader
{
19 // Loader implementation:
20 virtual void Load(const SourceFile
& file
,
21 const LocationRange
& origin
,
22 const Label
& toolchain_name
) OVERRIDE
{
23 files_
.push_back(file
);
25 virtual void ToolchainLoaded(const Toolchain
* toolchain
) OVERRIDE
{
27 virtual Label
GetDefaultToolchain() const OVERRIDE
{
30 virtual const Settings
* GetToolchainSettings(
31 const Label
& label
) const OVERRIDE
{
35 bool HasLoadedNone() const {
36 return files_
.empty();
39 // Returns true if two loads have been requested and they match the given
40 // file. This will clear the records so it will be empty for the next call.
41 bool HasLoadedTwo(const SourceFile
& a
, const SourceFile
& b
) {
42 if (files_
.size() != 2u) {
48 (files_
[0] == a
&& files_
[1] == b
) ||
49 (files_
[0] == b
&& files_
[1] == a
));
55 virtual ~MockLoader() {}
57 std::vector
<SourceFile
> files_
;
60 class BuilderTest
: public testing::Test
{
63 : loader_(new MockLoader
),
64 builder_(new Builder(loader_
.get())),
65 settings_(&build_settings_
, std::string()),
67 build_settings_
.SetBuildDir(SourceDir("//out/"));
68 settings_
.set_toolchain_label(Label(SourceDir("//tc/"), "default"));
69 settings_
.set_default_toolchain_label(settings_
.toolchain_label());
72 Toolchain
* DefineToolchain() {
73 Toolchain
* tc
= new Toolchain(&settings_
, settings_
.toolchain_label());
74 builder_
->ItemDefined(scoped_ptr
<Item
>(tc
));
79 scoped_refptr
<MockLoader
> loader_
;
80 scoped_refptr
<Builder
> builder_
;
81 BuildSettings build_settings_
;
88 TEST_F(BuilderTest
, BasicDeps
) {
89 SourceDir toolchain_dir
= settings_
.toolchain_label().dir();
90 std::string toolchain_name
= settings_
.toolchain_label().name();
92 // Construct a dependency chain: A -> B -> C. Define A first with a
93 // forward-reference to B, then C, then B to test the different orders that
94 // the dependencies are hooked up.
95 Label
a_label(SourceDir("//a/"), "a", toolchain_dir
, toolchain_name
);
96 Label
b_label(SourceDir("//b/"), "b", toolchain_dir
, toolchain_name
);
97 Label
c_label(SourceDir("//c/"), "c", toolchain_dir
, toolchain_name
);
99 // The builder will take ownership of the pointers.
100 Target
* a
= new Target(&settings_
, a_label
);
101 a
->deps().push_back(LabelTargetPair(b_label
));
102 a
->set_output_type(Target::EXECUTABLE
);
103 builder_
->ItemDefined(scoped_ptr
<Item
>(a
));
105 // Should have requested that B and the toolchain is loaded.
106 EXPECT_TRUE(loader_
->HasLoadedTwo(SourceFile("//tc/BUILD.gn"),
107 SourceFile("//b/BUILD.gn")));
109 // Define the toolchain.
111 BuilderRecord
* toolchain_record
=
112 builder_
->GetRecord(settings_
.toolchain_label());
113 ASSERT_TRUE(toolchain_record
);
114 EXPECT_EQ(BuilderRecord::ITEM_TOOLCHAIN
, toolchain_record
->type());
116 // A should be unresolved with an item
117 BuilderRecord
* a_record
= builder_
->GetRecord(a_label
);
118 EXPECT_TRUE(a_record
->item());
119 EXPECT_FALSE(a_record
->resolved());
120 EXPECT_FALSE(a_record
->can_resolve());
122 // B should be unresolved, have no item, and no deps.
123 BuilderRecord
* b_record
= builder_
->GetRecord(b_label
);
124 EXPECT_FALSE(b_record
->item());
125 EXPECT_FALSE(b_record
->resolved());
126 EXPECT_FALSE(b_record
->can_resolve());
127 EXPECT_TRUE(b_record
->all_deps().empty());
129 // A should have two deps: B and the toolchain. Only B should be unresolved.
130 EXPECT_EQ(2u, a_record
->all_deps().size());
131 EXPECT_EQ(1u, a_record
->unresolved_deps().size());
132 EXPECT_NE(a_record
->all_deps().end(),
133 a_record
->all_deps().find(toolchain_record
));
134 EXPECT_NE(a_record
->all_deps().end(),
135 a_record
->all_deps().find(b_record
));
136 EXPECT_NE(a_record
->unresolved_deps().end(),
137 a_record
->unresolved_deps().find(b_record
));
139 // B should be marked as having A waiting on it.
140 EXPECT_EQ(1u, b_record
->waiting_on_resolution().size());
141 EXPECT_NE(b_record
->waiting_on_resolution().end(),
142 b_record
->waiting_on_resolution().find(a_record
));
145 Target
* c
= new Target(&settings_
, c_label
);
146 c
->set_output_type(Target::STATIC_LIBRARY
);
147 builder_
->ItemDefined(scoped_ptr
<Item
>(c
));
149 // C only depends on the already-loaded toolchain so we shouldn't have
150 // requested anything else.
151 EXPECT_TRUE(loader_
->HasLoadedNone());
154 Target
* b
= new Target(&settings_
, b_label
);
155 a
->deps().push_back(LabelTargetPair(c_label
));
156 b
->set_output_type(Target::SHARED_LIBRARY
);
157 builder_
->ItemDefined(scoped_ptr
<Item
>(b
));
159 // B depends only on the already-loaded C and toolchain so we shouldn't have
160 // requested anything else.
161 EXPECT_TRUE(loader_
->HasLoadedNone());
163 // All targets should now be resolved.
164 BuilderRecord
* c_record
= builder_
->GetRecord(c_label
);
165 EXPECT_TRUE(a_record
->resolved());
166 EXPECT_TRUE(b_record
->resolved());
167 EXPECT_TRUE(c_record
->resolved());
169 EXPECT_TRUE(a_record
->unresolved_deps().empty());
170 EXPECT_TRUE(b_record
->unresolved_deps().empty());
171 EXPECT_TRUE(c_record
->unresolved_deps().empty());
173 EXPECT_TRUE(a_record
->waiting_on_resolution().empty());
174 EXPECT_TRUE(b_record
->waiting_on_resolution().empty());
175 EXPECT_TRUE(c_record
->waiting_on_resolution().empty());
178 // Tests that the should generate bit is set and propogated properly.
179 TEST_F(BuilderTest
, ShouldGenerate
) {
182 // Define a secondary toolchain.
183 Settings
settings2(&build_settings_
, "secondary/");
184 Label
toolchain_label2(SourceDir("//tc/"), "secondary");
185 settings2
.set_toolchain_label(toolchain_label2
);
186 Toolchain
* tc2
= new Toolchain(&settings2
, toolchain_label2
);
187 builder_
->ItemDefined(scoped_ptr
<Item
>(tc2
));
189 // Construct a dependency chain: A -> B. A is in the default toolchain, B
191 Label
a_label(SourceDir("//foo/"), "a",
192 settings_
.toolchain_label().dir(), "a");
193 Label
b_label(SourceDir("//foo/"), "b",
194 toolchain_label2
.dir(), toolchain_label2
.name());
197 Target
* b
= new Target(&settings2
, b_label
);
198 b
->set_output_type(Target::EXECUTABLE
);
199 builder_
->ItemDefined(scoped_ptr
<Item
>(b
));
201 // B should not be marked generated by default.
202 BuilderRecord
* b_record
= builder_
->GetRecord(b_label
);
203 EXPECT_FALSE(b_record
->should_generate());
205 // Define A with a dependency on B.
206 Target
* a
= new Target(&settings_
, a_label
);
207 a
->deps().push_back(LabelTargetPair(b_label
));
208 a
->set_output_type(Target::EXECUTABLE
);
209 builder_
->ItemDefined(scoped_ptr
<Item
>(a
));
211 // A should have the generate bit set since it's in the default toolchain.
212 BuilderRecord
* a_record
= builder_
->GetRecord(a_label
);
213 EXPECT_TRUE(a_record
->should_generate());
215 // It should have gotten pushed to B.
216 EXPECT_TRUE(b_record
->should_generate());