• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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.
4 
5 #include <sstream>
6 
7 #include "gn/ninja_action_target_writer.h"
8 #include "gn/ninja_target_writer.h"
9 #include "gn/target.h"
10 #include "gn/test_with_scope.h"
11 #include "util/test/test.h"
12 
13 namespace {
14 
15 class TestingNinjaTargetWriter : public NinjaTargetWriter {
16  public:
TestingNinjaTargetWriter(const Target * target,const Toolchain * toolchain,std::ostream & out)17   TestingNinjaTargetWriter(const Target* target,
18                            const Toolchain* toolchain,
19                            std::ostream& out)
20       : NinjaTargetWriter(target, out) {}
21 
Run()22   void Run() override {}
23 
24   // Make this public so the test can call it.
WriteInputDepsStampAndGetDep(const std::vector<const Target * > & additional_hard_deps,size_t num_stamp_uses)25   std::vector<OutputFile> WriteInputDepsStampAndGetDep(
26       const std::vector<const Target*>& additional_hard_deps,
27       size_t num_stamp_uses) {
28     return NinjaTargetWriter::WriteInputDepsStampAndGetDep(additional_hard_deps,
29                                                            num_stamp_uses);
30   }
31 };
32 
33 }  // namespace
34 
TEST(NinjaTargetWriter,ResolvedCreatedOnDemand)35 TEST(NinjaTargetWriter, ResolvedCreatedOnDemand) {
36   TestWithScope setup;
37   Err err;
38 
39   // Make a base target that's a hard dep (action).
40   Target base_target(setup.settings(), Label(SourceDir("//foo/"), "base"));
41   base_target.set_output_type(Target::ACTION);
42   base_target.visibility().SetPublic();
43   base_target.SetToolchain(setup.toolchain());
44   base_target.action_values().set_script(SourceFile("//foo/script.py"));
45   ASSERT_TRUE(base_target.OnResolved(&err));
46 
47   std::ostringstream stream;
48   TestingNinjaTargetWriter writer(&base_target, setup.toolchain(), stream);
49 
50   const auto* resolved_ptr = &writer.resolved();
51   ASSERT_TRUE(resolved_ptr);
52 
53   // Same address should be returned on second call.
54   EXPECT_EQ(resolved_ptr, &writer.resolved());
55 }
56 
TEST(NinjaTargetWriter,ResolvedSetExplicitly)57 TEST(NinjaTargetWriter, ResolvedSetExplicitly) {
58   TestWithScope setup;
59   Err err;
60 
61   // Make a base target that's a hard dep (action).
62   Target base_target(setup.settings(), Label(SourceDir("//foo/"), "base"));
63   base_target.set_output_type(Target::ACTION);
64   base_target.visibility().SetPublic();
65   base_target.SetToolchain(setup.toolchain());
66   base_target.action_values().set_script(SourceFile("//foo/script.py"));
67   ASSERT_TRUE(base_target.OnResolved(&err));
68 
69   ResolvedTargetData resolved;
70   std::ostringstream stream;
71   TestingNinjaTargetWriter writer(&base_target, setup.toolchain(), stream);
72   writer.SetResolvedTargetData(&resolved);
73 
74   EXPECT_EQ(&resolved, &writer.resolved());
75 }
76 
TEST(NinjaTargetWriter,WriteInputDepsStampAndGetDep)77 TEST(NinjaTargetWriter, WriteInputDepsStampAndGetDep) {
78   TestWithScope setup;
79   Err err;
80 
81   // Make a base target that's a hard dep (action).
82   Target base_target(setup.settings(), Label(SourceDir("//foo/"), "base"));
83   base_target.set_output_type(Target::ACTION);
84   base_target.visibility().SetPublic();
85   base_target.SetToolchain(setup.toolchain());
86   base_target.action_values().set_script(SourceFile("//foo/script.py"));
87 
88   // Dependent target that also includes a source prerequisite (should get
89   // included) and a source (should not be included).
90   Target target(setup.settings(), Label(SourceDir("//foo/"), "target"));
91   target.set_output_type(Target::EXECUTABLE);
92   target.visibility().SetPublic();
93   target.SetToolchain(setup.toolchain());
94   target.config_values().inputs().push_back(SourceFile("//foo/input.txt"));
95   target.sources().push_back(SourceFile("//foo/source.txt"));
96   target.public_deps().push_back(LabelTargetPair(&base_target));
97 
98   // Dependent action to test that action sources will be treated the same as
99   // inputs.
100   Target action(setup.settings(), Label(SourceDir("//foo/"), "action"));
101   action.set_output_type(Target::ACTION);
102   action.visibility().SetPublic();
103   action.SetToolchain(setup.toolchain());
104   action.action_values().set_script(SourceFile("//foo/script.py"));
105   action.sources().push_back(SourceFile("//foo/action_source.txt"));
106   action.public_deps().push_back(LabelTargetPair(&target));
107 
108   ASSERT_TRUE(base_target.OnResolved(&err));
109   ASSERT_TRUE(target.OnResolved(&err));
110   ASSERT_TRUE(action.OnResolved(&err));
111 
112   // Input deps for the base (should be only the script itself).
113   {
114     std::ostringstream stream;
115     TestingNinjaTargetWriter writer(&base_target, setup.toolchain(), stream);
116     std::vector<OutputFile> dep =
117         writer.WriteInputDepsStampAndGetDep(std::vector<const Target*>(), 10u);
118 
119     // Since there is only one dependency, it should just be returned and
120     // nothing written to the stream.
121     ASSERT_EQ(1u, dep.size());
122     EXPECT_EQ("../../foo/script.py", dep[0].value());
123     EXPECT_EQ("", stream.str());
124   }
125 
126   // Input deps for the target (should depend on the base).
127   {
128     std::ostringstream stream;
129     TestingNinjaTargetWriter writer(&target, setup.toolchain(), stream);
130     std::vector<OutputFile> dep =
131         writer.WriteInputDepsStampAndGetDep(std::vector<const Target*>(), 10u);
132 
133     // Since there is only one dependency, a stamp file will be returned
134     // directly without writing any additional rules.
135     ASSERT_EQ(1u, dep.size());
136     EXPECT_EQ("obj/foo/base.stamp", dep[0].value());
137   }
138 
139   {
140     std::ostringstream stream;
141     NinjaActionTargetWriter writer(&action, stream);
142     writer.Run();
143     EXPECT_EQ(
144         "rule __foo_action___rule\n"
145         "  command =  ../../foo/script.py\n"
146         "  description = ACTION //foo:action()\n"
147         "  restat = 1\n"
148         "\n"
149         "build: __foo_action___rule | ../../foo/script.py"
150         " ../../foo/action_source.txt ./target\n"
151         "\n"
152         "build obj/foo/action.stamp: stamp\n",
153         stream.str());
154   }
155 
156   // Input deps for action which should depend on the base since its a hard dep
157   // that is a (indirect) dependency, as well as the the action source.
158   {
159     std::ostringstream stream;
160     TestingNinjaTargetWriter writer(&action, setup.toolchain(), stream);
161     std::vector<OutputFile> dep =
162         writer.WriteInputDepsStampAndGetDep(std::vector<const Target*>(), 10u);
163 
164     ASSERT_EQ(1u, dep.size());
165     EXPECT_EQ("obj/foo/action.inputdeps.stamp", dep[0].value());
166     EXPECT_EQ(
167         "build obj/foo/action.inputdeps.stamp: stamp ../../foo/script.py "
168         "../../foo/action_source.txt ./target\n",
169         stream.str());
170   }
171 }
172 
173 // Tests WriteInputDepsStampAndGetDep when toolchain deps are present.
TEST(NinjaTargetWriter,WriteInputDepsStampAndGetDepWithToolchainDeps)174 TEST(NinjaTargetWriter, WriteInputDepsStampAndGetDepWithToolchainDeps) {
175   TestWithScope setup;
176   Err err;
177 
178   // Toolchain dependency. Here we make a target in the same toolchain for
179   // simplicity, but in real life (using the Builder) this would be rejected
180   // because it would be a circular dependency (the target depends on its
181   // toolchain, and the toolchain depends on this target).
182   Target toolchain_dep_target(setup.settings(),
183                               Label(SourceDir("//foo/"), "setup"));
184   toolchain_dep_target.set_output_type(Target::ACTION);
185   toolchain_dep_target.SetToolchain(setup.toolchain());
186   ASSERT_TRUE(toolchain_dep_target.OnResolved(&err));
187   setup.toolchain()->deps().push_back(LabelTargetPair(&toolchain_dep_target));
188 
189   // Make a binary target
190   Target target(setup.settings(), Label(SourceDir("//foo/"), "target"));
191   target.set_output_type(Target::EXECUTABLE);
192   target.SetToolchain(setup.toolchain());
193   ASSERT_TRUE(target.OnResolved(&err));
194 
195   std::ostringstream stream;
196   TestingNinjaTargetWriter writer(&target, setup.toolchain(), stream);
197   std::vector<OutputFile> dep =
198       writer.WriteInputDepsStampAndGetDep(std::vector<const Target*>(), 10u);
199 
200   // Since there is more than one dependency, a stamp file will be returned
201   // and the rule for the stamp file will be written to the stream.
202   ASSERT_EQ(1u, dep.size());
203   EXPECT_EQ("obj/foo/setup.stamp", dep[0].value());
204   EXPECT_EQ("", stream.str());
205 }
206