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 "tools/gn/ninja_action_target_writer.h"
8 #include "tools/gn/ninja_target_writer.h"
9 #include "tools/gn/target.h"
10 #include "tools/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 * > & extra_hard_deps,size_t num_stamp_uses)25   std::vector<OutputFile> WriteInputDepsStampAndGetDep(
26       const std::vector<const Target*>& extra_hard_deps,
27       size_t num_stamp_uses) {
28     return NinjaTargetWriter::WriteInputDepsStampAndGetDep(extra_hard_deps,
29                                                            num_stamp_uses);
30   }
31 };
32 
33 }  // namespace
34 
TEST(NinjaTargetWriter,WriteInputDepsStampAndGetDep)35 TEST(NinjaTargetWriter, WriteInputDepsStampAndGetDep) {
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 
46   // Dependent target that also includes a source prerequisite (should get
47   // included) and a source (should not be included).
48   Target target(setup.settings(), Label(SourceDir("//foo/"), "target"));
49   target.set_output_type(Target::EXECUTABLE);
50   target.visibility().SetPublic();
51   target.SetToolchain(setup.toolchain());
52   target.config_values().inputs().push_back(SourceFile("//foo/input.txt"));
53   target.sources().push_back(SourceFile("//foo/source.txt"));
54   target.public_deps().push_back(LabelTargetPair(&base_target));
55 
56   // Dependent action to test that action sources will be treated the same as
57   // inputs.
58   Target action(setup.settings(), Label(SourceDir("//foo/"), "action"));
59   action.set_output_type(Target::ACTION);
60   action.visibility().SetPublic();
61   action.SetToolchain(setup.toolchain());
62   action.action_values().set_script(SourceFile("//foo/script.py"));
63   action.sources().push_back(SourceFile("//foo/action_source.txt"));
64   action.public_deps().push_back(LabelTargetPair(&target));
65 
66   ASSERT_TRUE(base_target.OnResolved(&err));
67   ASSERT_TRUE(target.OnResolved(&err));
68   ASSERT_TRUE(action.OnResolved(&err));
69 
70   // Input deps for the base (should be only the script itself).
71   {
72     std::ostringstream stream;
73     TestingNinjaTargetWriter writer(&base_target, setup.toolchain(), stream);
74     std::vector<OutputFile> dep =
75         writer.WriteInputDepsStampAndGetDep(std::vector<const Target*>(), 10u);
76 
77     // Since there is only one dependency, it should just be returned and
78     // nothing written to the stream.
79     ASSERT_EQ(1u, dep.size());
80     EXPECT_EQ("../../foo/script.py", dep[0].value());
81     EXPECT_EQ("", stream.str());
82   }
83 
84   // Input deps for the target (should depend on the base).
85   {
86     std::ostringstream stream;
87     TestingNinjaTargetWriter writer(&target, setup.toolchain(), stream);
88     std::vector<OutputFile> dep =
89         writer.WriteInputDepsStampAndGetDep(std::vector<const Target*>(), 10u);
90 
91     // Since there is only one dependency, a stamp file will be returned
92     // directly without writing any additional rules.
93     ASSERT_EQ(1u, dep.size());
94     EXPECT_EQ("obj/foo/base.stamp", dep[0].value());
95   }
96 
97   {
98     std::ostringstream stream;
99     NinjaActionTargetWriter writer(&action, stream);
100     writer.Run();
101     EXPECT_EQ(
102         "rule __foo_action___rule\n"
103         "  command =  ../../foo/script.py\n"
104         "  description = ACTION //foo:action()\n"
105         "  restat = 1\n"
106         "\n"
107         "build: __foo_action___rule | ../../foo/script.py"
108         " ../../foo/action_source.txt ./target\n"
109         "\n"
110         "build obj/foo/action.stamp: stamp\n",
111         stream.str());
112   }
113 
114   // Input deps for action which should depend on the base since its a hard dep
115   // that is a (indirect) dependency, as well as the the action source.
116   {
117     std::ostringstream stream;
118     TestingNinjaTargetWriter writer(&action, setup.toolchain(), stream);
119     std::vector<OutputFile> dep =
120         writer.WriteInputDepsStampAndGetDep(std::vector<const Target*>(), 10u);
121 
122     ASSERT_EQ(1u, dep.size());
123     EXPECT_EQ("obj/foo/action.inputdeps.stamp", dep[0].value());
124     EXPECT_EQ(
125         "build obj/foo/action.inputdeps.stamp: stamp ../../foo/script.py "
126         "../../foo/action_source.txt ./target\n",
127         stream.str());
128   }
129 }
130 
131 // Tests WriteInputDepsStampAndGetDep when toolchain deps are present.
TEST(NinjaTargetWriter,WriteInputDepsStampAndGetDepWithToolchainDeps)132 TEST(NinjaTargetWriter, WriteInputDepsStampAndGetDepWithToolchainDeps) {
133   TestWithScope setup;
134   Err err;
135 
136   // Toolchain dependency. Here we make a target in the same toolchain for
137   // simplicity, but in real life (using the Builder) this would be rejected
138   // because it would be a circular dependency (the target depends on its
139   // toolchain, and the toolchain depends on this target).
140   Target toolchain_dep_target(setup.settings(),
141                               Label(SourceDir("//foo/"), "setup"));
142   toolchain_dep_target.set_output_type(Target::ACTION);
143   toolchain_dep_target.SetToolchain(setup.toolchain());
144   ASSERT_TRUE(toolchain_dep_target.OnResolved(&err));
145   setup.toolchain()->deps().push_back(LabelTargetPair(&toolchain_dep_target));
146 
147   // Make a binary target
148   Target target(setup.settings(), Label(SourceDir("//foo/"), "target"));
149   target.set_output_type(Target::EXECUTABLE);
150   target.SetToolchain(setup.toolchain());
151   ASSERT_TRUE(target.OnResolved(&err));
152 
153   std::ostringstream stream;
154   TestingNinjaTargetWriter writer(&target, setup.toolchain(), stream);
155   std::vector<OutputFile> dep =
156       writer.WriteInputDepsStampAndGetDep(std::vector<const Target*>(), 10u);
157 
158   // Since there is more than one dependency, a stamp file will be returned
159   // and the rule for the stamp file will be written to the stream.
160   ASSERT_EQ(1u, dep.size());
161   EXPECT_EQ("obj/foo/setup.stamp", dep[0].value());
162   EXPECT_EQ("", stream.str());
163 }
164