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.
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "tools/gn/ninja_target_writer.h"
9 #include "tools/gn/test_with_scope.h"
13 class TestingNinjaTargetWriter
: public NinjaTargetWriter
{
15 TestingNinjaTargetWriter(const Target
* target
,
16 const Toolchain
* toolchain
,
18 : NinjaTargetWriter(target
, toolchain
, out
) {
21 virtual void Run() OVERRIDE
{}
23 // Make this public so the test can call it.
24 std::string
WriteInputDepsStampAndGetDep(
25 const std::vector
<const Target
*>& extra_hard_deps
) {
26 return NinjaTargetWriter::WriteInputDepsStampAndGetDep(extra_hard_deps
);
32 TEST(NinjaTargetWriter
, WriteInputDepsStampAndGetDep
) {
35 // Make a base target that's a hard dep (action).
36 Target
base_target(setup
.settings(), Label(SourceDir("//foo/"), "base"));
37 base_target
.set_output_type(Target::ACTION
);
38 base_target
.action_values().set_script(SourceFile("//foo/script.py"));
40 // Dependent target that also includes a source prerequisite (should get
41 // included) and a source (should not be included).
42 Target
target(setup
.settings(), Label(SourceDir("//foo/"), "target"));
43 target
.set_output_type(Target::EXECUTABLE
);
44 target
.inputs().push_back(SourceFile("//foo/input.txt"));
45 target
.sources().push_back(SourceFile("//foo/source.txt"));
46 target
.deps().push_back(LabelTargetPair(&base_target
));
48 // Dependent action to test that action sources will be treated the same as
50 Target
action(setup
.settings(), Label(SourceDir("//foo/"), "action"));
51 action
.set_output_type(Target::ACTION
);
52 action
.action_values().set_script(SourceFile("//foo/script.py"));
53 action
.sources().push_back(SourceFile("//foo/action_source.txt"));
54 action
.deps().push_back(LabelTargetPair(&target
));
56 base_target
.OnResolved();
60 // Input deps for the base (should be only the script itself).
62 std::ostringstream stream
;
63 TestingNinjaTargetWriter
writer(&base_target
, setup
.toolchain(), stream
);
65 writer
.WriteInputDepsStampAndGetDep(std::vector
<const Target
*>());
67 EXPECT_EQ(" | obj/foo/base.inputdeps.stamp", dep
);
68 EXPECT_EQ("build obj/foo/base.inputdeps.stamp: stamp "
69 "../../foo/script.py\n",
73 // Input deps for the target (should depend on the base).
75 std::ostringstream stream
;
76 TestingNinjaTargetWriter
writer(&target
, setup
.toolchain(), stream
);
78 writer
.WriteInputDepsStampAndGetDep(std::vector
<const Target
*>());
80 EXPECT_EQ(" | obj/foo/target.inputdeps.stamp", dep
);
81 EXPECT_EQ("build obj/foo/target.inputdeps.stamp: stamp "
82 "../../foo/input.txt obj/foo/base.stamp\n",
86 // Input deps for action which should depend on the base since its a hard dep
87 // that is a (indirect) dependency, as well as the the action source.
89 std::ostringstream stream
;
90 TestingNinjaTargetWriter
writer(&action
, setup
.toolchain(), stream
);
92 writer
.WriteInputDepsStampAndGetDep(std::vector
<const Target
*>());
94 EXPECT_EQ(" | obj/foo/action.inputdeps.stamp", dep
);
95 EXPECT_EQ("build obj/foo/action.inputdeps.stamp: stamp ../../foo/script.py "
96 "../../foo/action_source.txt obj/foo/base.stamp\n",
101 // Tests WriteInputDepsStampAndGetDep when toolchain deps are present.
102 TEST(NinjaTargetWriter
, WriteInputDepsStampAndGetDepWithToolchainDeps
) {
105 // Toolchain dependency. Here we make a target in the same toolchain for
106 // simplicity, but in real life (using the Builder) this would be rejected
107 // because it would be a circular dependency (the target depends on its
108 // toolchain, and the toolchain depends on this target).
109 Target
toolchain_dep_target(setup
.settings(),
110 Label(SourceDir("//foo/"), "setup"));
111 toolchain_dep_target
.set_output_type(Target::ACTION
);
112 setup
.toolchain()->deps().push_back(LabelTargetPair(&toolchain_dep_target
));
114 // Make a binary target
115 Target
target(setup
.settings(), Label(SourceDir("//foo/"), "target"));
116 target
.set_output_type(Target::EXECUTABLE
);
118 std::ostringstream stream
;
119 TestingNinjaTargetWriter
writer(&target
, setup
.toolchain(), stream
);
121 writer
.WriteInputDepsStampAndGetDep(std::vector
<const Target
*>());
123 EXPECT_EQ(" | obj/foo/target.inputdeps.stamp", dep
);
124 EXPECT_EQ("build obj/foo/target.inputdeps.stamp: stamp "
125 "obj/foo/setup.stamp\n",