OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "testing/gtest/include/gtest/gtest.h" | 5 #include "testing/gtest/include/gtest/gtest.h" |
6 #include "tools/gn/analyzer.h" | 6 #include "tools/gn/analyzer.h" |
7 #include "tools/gn/build_settings.h" | 7 #include "tools/gn/build_settings.h" |
8 #include "tools/gn/builder.h" | 8 #include "tools/gn/builder.h" |
9 #include "tools/gn/loader.h" | 9 #include "tools/gn/loader.h" |
10 #include "tools/gn/settings.h" | 10 #include "tools/gn/settings.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 settings_.set_toolchain_label(Label(SourceDir("//tc/"), "default")); | 42 settings_.set_toolchain_label(Label(SourceDir("//tc/"), "default")); |
43 settings_.set_default_toolchain_label(settings_.toolchain_label()); | 43 settings_.set_default_toolchain_label(settings_.toolchain_label()); |
44 tc_dir_ = settings_.toolchain_label().dir(); | 44 tc_dir_ = settings_.toolchain_label().dir(); |
45 tc_name_ = settings_.toolchain_label().name(); | 45 tc_name_ = settings_.toolchain_label().name(); |
46 } | 46 } |
47 | 47 |
48 Target* MakeTarget(const std::string dir, | 48 Target* MakeTarget(const std::string dir, |
49 const std::string name, | 49 const std::string name, |
50 Target::OutputType type, | 50 Target::OutputType type, |
51 const std::vector<std::string>& sources, | 51 const std::vector<std::string>& sources, |
52 const std::vector<Target*>& deps) { | 52 const std::vector<Target*>& deps, |
| 53 const std::vector<std::string>& gn_files) { |
53 Label lbl(SourceDir(dir), name, tc_dir_, tc_name_); | 54 Label lbl(SourceDir(dir), name, tc_dir_, tc_name_); |
54 Target* target = new Target(&settings_, lbl); | 55 std::set<uint32_t> source_files_hashes; |
| 56 for (const auto& gn : gn_files) |
| 57 source_files_hashes.insert(base::Hash(gn)); |
| 58 Target* target = new Target(&settings_, lbl, source_files_hashes); |
55 target->set_output_type(type); | 59 target->set_output_type(type); |
56 for (const auto& s : sources) | 60 for (const auto& s : sources) |
57 target->sources().push_back(SourceFile(s)); | 61 target->sources().push_back(SourceFile(s)); |
58 for (const auto* d : deps) | 62 for (const auto* d : deps) |
59 target->public_deps().push_back(LabelTargetPair(d)); | 63 target->public_deps().push_back(LabelTargetPair(d)); |
60 builder_.ItemDefined(std::unique_ptr<Item>(target)); | 64 builder_.ItemDefined(std::unique_ptr<Item>(target)); |
61 return target; | 65 return target; |
62 } | 66 } |
63 | 67 |
64 void AddSource(Target* a, std::string path) {} | 68 void AddSource(Target* a, std::string path) {} |
65 | 69 |
66 void AddDep(Target* a, Target* b) {} | 70 void AddDep(Target* a, Target* b) {} |
67 | 71 |
68 void SetUpABasicBuildGraph() { | 72 void SetUpABasicBuildGraph() { |
69 std::vector<std::string> no_sources; | 73 std::vector<std::string> no_sources; |
70 std::vector<Target*> no_deps; | 74 std::vector<Target*> no_deps; |
71 | 75 |
72 // All of the targets below are owned by the builder, so none of them | 76 // All of the targets below are owned by the builder, so none of them |
73 // get leaked. | 77 // get leaked. |
74 | 78 |
75 // Ignore the returned target since nothing depends on it. | 79 // Ignore the returned target since nothing depends on it. |
76 MakeTarget("//", "a", Target::EXECUTABLE, {"//a.cc"}, no_deps); | 80 MakeTarget("//", "a", Target::EXECUTABLE, {"//a.cc"}, no_deps, |
| 81 {"//BUILD.gn", "//features.gni"}); |
77 | 82 |
78 Target* b = | 83 Target* b = MakeTarget("//d", "b", Target::SOURCE_SET, {"//d/b.cc"}, |
79 MakeTarget("//d", "b", Target::SOURCE_SET, {"//d/b.cc"}, no_deps); | 84 no_deps, {"//d/BUILD.gn"}); |
80 | 85 |
81 Target* b_unittests = MakeTarget("//d", "b_unittests", Target::EXECUTABLE, | 86 Target* b_unittests = |
82 {"//d/b_unittest.cc"}, {b}); | 87 MakeTarget("//d", "b_unittests", Target::EXECUTABLE, |
| 88 {"//d/b_unittest.cc"}, {b}, {"//d/BUILD.gn"}); |
83 | 89 |
84 Target* c = MakeTarget("//d", "c", Target::EXECUTABLE, {"//d/c.cc"}, {b}); | 90 Target* c = MakeTarget("//d", "c", Target::EXECUTABLE, {"//d/c.cc"}, {b}, |
| 91 {"//d/BUILD.gn"}); |
85 | 92 |
86 Target* b_unittests_and_c = | 93 Target* b_unittests_and_c = |
87 MakeTarget("//d", "b_unittests_and_c", Target::GROUP, no_sources, | 94 MakeTarget("//d", "b_unittests_and_c", Target::GROUP, no_sources, |
88 {b_unittests, c}); | 95 {b_unittests, c}, {"//d/BUILD.gn"}); |
89 | 96 |
90 Target* e = | 97 Target* e = MakeTarget("//d", "e", Target::EXECUTABLE, {"//d/e.cc"}, |
91 MakeTarget("//d", "e", Target::EXECUTABLE, {"//d/e.cc"}, no_deps); | 98 no_deps, {"//d/BUILD.gn"}); |
92 | 99 |
93 // Also ignore this returned target since nothing depends on it. | 100 // Also ignore this returned target since nothing depends on it. |
94 MakeTarget("//d", "d", Target::GROUP, no_sources, {b_unittests_and_c, e}); | 101 MakeTarget("//d", "d", Target::GROUP, no_sources, {b_unittests_and_c, e}, |
| 102 {"//d/BUILD.gn"}); |
95 } | 103 } |
96 | 104 |
97 void RunBasicTest(const std::string& input, | 105 void RunBasicTest(const std::string& input, |
98 const std::string& expected_output) { | 106 const std::string& expected_output) { |
99 SetUpABasicBuildGraph(); | 107 SetUpABasicBuildGraph(); |
100 Err err; | 108 Err err; |
101 std::string actual_output = Analyzer(builder_).Analyze(input, &err); | 109 std::string actual_output = Analyzer(builder_).Analyze(input, &err); |
102 EXPECT_EQ(err.has_error(), false); | 110 EXPECT_EQ(err.has_error(), false); |
103 EXPECT_EQ(expected_output, actual_output); | 111 EXPECT_EQ(expected_output, actual_output); |
104 } | 112 } |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
194 " \"test_targets\": [ \"//:a\" ]" | 202 " \"test_targets\": [ \"//:a\" ]" |
195 "}", | 203 "}", |
196 "{" | 204 "{" |
197 "\"error\":" | 205 "\"error\":" |
198 "\"Input does not have a key named " | 206 "\"Input does not have a key named " |
199 "\\\"additional_compile_targets\\\" with a list value.\"," | 207 "\\\"additional_compile_targets\\\" with a list value.\"," |
200 "\"invalid_targets\":[]" | 208 "\"invalid_targets\":[]" |
201 "}"); | 209 "}"); |
202 } | 210 } |
203 | 211 |
204 TEST_F(AnalyzerTest, BuildFilesWereModified) { | 212 TEST_F(AnalyzerTest, DotGnFileWasModified) { |
205 // This tests that if a build file is modified, we bail out early with | |
206 // "Found dependency (all)" error since we can't handle changes to | |
207 // build files yet (crbug.com/555273). | |
208 RunBasicTest( | 213 RunBasicTest( |
209 "{" | 214 "{" |
210 " \"files\": [ \"//a.cc\", \"//BUILD.gn\" ]," | 215 " \"files\": [ \"//.gn\" ]," |
211 " \"additional_compile_targets\": []," | 216 " \"additional_compile_targets\": []," |
212 " \"test_targets\": [ \"//:a\" ]" | 217 " \"test_targets\": [ \"//:a\" ]" |
213 "}", | 218 "}", |
214 "{" | 219 "{" |
215 "\"compile_targets\":[\"//:a\"]," | 220 "\"compile_targets\":[\"//:a\"]," |
216 "\"status\":\"Found dependency (all)\"," | 221 "\"status\":\"Found dependency (all)\"," |
217 "\"test_targets\":[\"//:a\"]" | 222 "\"test_targets\":[\"//:a\"]" |
218 "}"); | 223 "}"); |
219 } | 224 } |
220 | 225 |
221 TEST_F(AnalyzerTest, BuildFilesWereModifiedAndCompilingAll) { | 226 TEST_F(AnalyzerTest, DotGnFileWasModifiedAndCompilingAll) { |
222 // This tests that if a build file is modified, we bail out early with | |
223 // "Found dependency (all)" error since we can't handle changes to | |
224 // build files yet (crbug.com/555273). | |
225 RunBasicTest( | 227 RunBasicTest( |
226 "{" | 228 "{" |
227 " \"files\": [ \"//a.cc\", \"//BUILD.gn\" ]," | 229 " \"files\": [ \"//.gn\" ]," |
228 " \"additional_compile_targets\": [ \"all\" ]," | 230 " \"additional_compile_targets\": [ \"all\" ]," |
229 " \"test_targets\": [ \"//:a\" ]" | 231 " \"test_targets\": [ \"//:a\" ]" |
230 "}", | 232 "}", |
231 "{" | 233 "{" |
232 "\"compile_targets\":[\"all\"]," | 234 "\"compile_targets\":[\"all\"]," |
233 "\"status\":\"Found dependency (all)\"," | 235 "\"status\":\"Found dependency (all)\"," |
234 "\"test_targets\":[\"//:a\"]" | 236 "\"test_targets\":[\"//:a\"]" |
235 "}"); | 237 "}"); |
236 } | 238 } |
| 239 |
| 240 TEST_F(AnalyzerTest, BuildFileWasModified) { |
| 241 RunBasicTest( |
| 242 "{" |
| 243 " \"files\": [ \"//BUILD.gn\" ]," |
| 244 " \"additional_compile_targets\": []," |
| 245 " \"test_targets\": [ \"//:a\" ]" |
| 246 "}", |
| 247 "{" |
| 248 "\"compile_targets\":[]," |
| 249 "\"status\":\"Found dependency\"," |
| 250 "\"test_targets\":[\"//:a\"]" |
| 251 "}"); |
| 252 } |
| 253 |
| 254 TEST_F(AnalyzerTest, BuildFileWasModifiedAndCompilingAll) { |
| 255 RunBasicTest( |
| 256 "{" |
| 257 " \"files\": [ \"//BUILD.gn\" ]," |
| 258 " \"additional_compile_targets\": [ \"all\" ]," |
| 259 " \"test_targets\": [ \"//:a\" ]" |
| 260 "}", |
| 261 "{" |
| 262 "\"compile_targets\":[\"//:a\"]," |
| 263 "\"status\":\"Found dependency\"," |
| 264 "\"test_targets\":[\"//:a\"]" |
| 265 "}"); |
| 266 } |
| 267 |
| 268 TEST_F(AnalyzerTest, UnnededBuildFileWasModified) { |
| 269 RunBasicTest( |
| 270 "{" |
| 271 " \"files\": [ \"//BUILD.gn\" ]," |
| 272 " \"additional_compile_targets\": []," |
| 273 " \"test_targets\": [ \"//d:b_unittests\" ]" |
| 274 "}", |
| 275 "{" |
| 276 "\"compile_targets\":[]," |
| 277 "\"status\":\"No dependency\"," |
| 278 "\"test_targets\":[]" |
| 279 "}"); |
| 280 } |
OLD | NEW |