[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / tools / gn / ninja_target_writer.cc
blob098a58cb956da36210b3cd632efb01b05ebfba58
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 "tools/gn/ninja_target_writer.h"
7 #include <sstream>
9 #include "base/files/file_util.h"
10 #include "base/strings/string_util.h"
11 #include "tools/gn/err.h"
12 #include "tools/gn/filesystem_utils.h"
13 #include "tools/gn/ninja_action_target_writer.h"
14 #include "tools/gn/ninja_binary_target_writer.h"
15 #include "tools/gn/ninja_copy_target_writer.h"
16 #include "tools/gn/ninja_group_target_writer.h"
17 #include "tools/gn/ninja_utils.h"
18 #include "tools/gn/output_file.h"
19 #include "tools/gn/scheduler.h"
20 #include "tools/gn/string_utils.h"
21 #include "tools/gn/substitution_type.h"
22 #include "tools/gn/substitution_writer.h"
23 #include "tools/gn/target.h"
24 #include "tools/gn/trace.h"
26 NinjaTargetWriter::NinjaTargetWriter(const Target* target,
27 std::ostream& out)
28 : settings_(target->settings()),
29 target_(target),
30 out_(out),
31 path_output_(settings_->build_settings()->build_dir(),
32 settings_->build_settings()->root_path_utf8(),
33 ESCAPE_NINJA) {
36 NinjaTargetWriter::~NinjaTargetWriter() {
39 // static
40 void NinjaTargetWriter::RunAndWriteFile(const Target* target) {
41 const Settings* settings = target->settings();
43 ScopedTrace trace(TraceItem::TRACE_FILE_WRITE,
44 target->label().GetUserVisibleName(false));
45 trace.SetToolchain(settings->toolchain_label());
47 base::FilePath ninja_file(settings->build_settings()->GetFullPath(
48 GetNinjaFileForTarget(target)));
50 if (g_scheduler->verbose_logging())
51 g_scheduler->Log("Writing", FilePathToUTF8(ninja_file));
53 base::CreateDirectory(ninja_file.DirName());
55 // It's rediculously faster to write to a string and then write that to
56 // disk in one operation than to use an fstream here.
57 std::stringstream file;
59 // Call out to the correct sub-type of writer.
60 if (target->output_type() == Target::COPY_FILES) {
61 NinjaCopyTargetWriter writer(target, file);
62 writer.Run();
63 } else if (target->output_type() == Target::ACTION ||
64 target->output_type() == Target::ACTION_FOREACH) {
65 NinjaActionTargetWriter writer(target, file);
66 writer.Run();
67 } else if (target->output_type() == Target::GROUP) {
68 NinjaGroupTargetWriter writer(target, file);
69 writer.Run();
70 } else if (target->output_type() == Target::EXECUTABLE ||
71 target->output_type() == Target::STATIC_LIBRARY ||
72 target->output_type() == Target::SHARED_LIBRARY ||
73 target->output_type() == Target::SOURCE_SET) {
74 NinjaBinaryTargetWriter writer(target, file);
75 writer.Run();
76 } else {
77 CHECK(0);
80 std::string contents = file.str();
81 base::WriteFile(ninja_file, contents.c_str(),
82 static_cast<int>(contents.size()));
85 void NinjaTargetWriter::WriteSharedVars(const SubstitutionBits& bits) {
86 bool written_anything = false;
88 // Target label.
89 if (bits.used[SUBSTITUTION_LABEL]) {
90 out_ << kSubstitutionNinjaNames[SUBSTITUTION_LABEL] << " = "
91 << SubstitutionWriter::GetTargetSubstitution(
92 target_, SUBSTITUTION_LABEL)
93 << std::endl;
94 written_anything = true;
97 // Root gen dir.
98 if (bits.used[SUBSTITUTION_ROOT_GEN_DIR]) {
99 out_ << kSubstitutionNinjaNames[SUBSTITUTION_ROOT_GEN_DIR] << " = "
100 << SubstitutionWriter::GetTargetSubstitution(
101 target_, SUBSTITUTION_ROOT_GEN_DIR)
102 << std::endl;
103 written_anything = true;
106 // Root out dir.
107 if (bits.used[SUBSTITUTION_ROOT_OUT_DIR]) {
108 out_ << kSubstitutionNinjaNames[SUBSTITUTION_ROOT_OUT_DIR] << " = "
109 << SubstitutionWriter::GetTargetSubstitution(
110 target_, SUBSTITUTION_ROOT_OUT_DIR)
111 << std::endl;
112 written_anything = true;
115 // Target gen dir.
116 if (bits.used[SUBSTITUTION_TARGET_GEN_DIR]) {
117 out_ << kSubstitutionNinjaNames[SUBSTITUTION_TARGET_GEN_DIR] << " = "
118 << SubstitutionWriter::GetTargetSubstitution(
119 target_, SUBSTITUTION_TARGET_GEN_DIR)
120 << std::endl;
121 written_anything = true;
124 // Target out dir.
125 if (bits.used[SUBSTITUTION_TARGET_OUT_DIR]) {
126 out_ << kSubstitutionNinjaNames[SUBSTITUTION_TARGET_OUT_DIR] << " = "
127 << SubstitutionWriter::GetTargetSubstitution(
128 target_, SUBSTITUTION_TARGET_OUT_DIR)
129 << std::endl;
130 written_anything = true;
133 // Target output name.
134 if (bits.used[SUBSTITUTION_TARGET_OUTPUT_NAME]) {
135 out_ << kSubstitutionNinjaNames[SUBSTITUTION_TARGET_OUTPUT_NAME] << " = "
136 << SubstitutionWriter::GetTargetSubstitution(
137 target_, SUBSTITUTION_TARGET_OUTPUT_NAME)
138 << std::endl;
139 written_anything = true;
142 // If we wrote any vars, separate them from the rest of the file that follows
143 // with a blank line.
144 if (written_anything)
145 out_ << std::endl;
148 OutputFile NinjaTargetWriter::WriteInputDepsStampAndGetDep(
149 const std::vector<const Target*>& extra_hard_deps) const {
150 CHECK(target_->toolchain())
151 << "Toolchain not set on target "
152 << target_->label().GetUserVisibleName(true);
154 // For an action (where we run a script only once) the sources are the same
155 // as the source prereqs.
156 bool list_sources_as_input_deps = (target_->output_type() == Target::ACTION);
158 // Actions get implicit dependencies on the script itself.
159 bool add_script_source_as_dep =
160 (target_->output_type() == Target::ACTION) ||
161 (target_->output_type() == Target::ACTION_FOREACH);
163 if (!add_script_source_as_dep &&
164 extra_hard_deps.empty() &&
165 target_->inputs().empty() &&
166 target_->recursive_hard_deps().empty() &&
167 (!list_sources_as_input_deps || target_->sources().empty()) &&
168 target_->toolchain()->deps().empty())
169 return OutputFile(); // No input/hard deps.
171 // One potential optimization is if there are few input dependencies (or
172 // potentially few sources that depend on these) it's better to just write
173 // all hard deps on each sources line than have this intermediate stamp. We
174 // do the stamp file because duplicating all the order-only deps for each
175 // source file can really explode the ninja file but this won't be the most
176 // optimal thing in all cases.
178 OutputFile input_stamp_file(
179 RebasePath(GetTargetOutputDir(target_).value(),
180 settings_->build_settings()->build_dir(),
181 settings_->build_settings()->root_path_utf8()));
182 input_stamp_file.value().append(target_->label().name());
183 input_stamp_file.value().append(".inputdeps.stamp");
185 out_ << "build ";
186 path_output_.WriteFile(out_, input_stamp_file);
187 out_ << ": "
188 << GetNinjaRulePrefixForToolchain(settings_)
189 << Toolchain::ToolTypeToName(Toolchain::TYPE_STAMP);
191 // Script file (if applicable).
192 if (add_script_source_as_dep) {
193 out_ << " ";
194 path_output_.WriteFile(out_, target_->action_values().script());
197 // Input files are order-only deps.
198 for (const auto& input : target_->inputs()) {
199 out_ << " ";
200 path_output_.WriteFile(out_, input);
202 if (list_sources_as_input_deps) {
203 for (const auto& source : target_->sources()) {
204 out_ << " ";
205 path_output_.WriteFile(out_, source);
209 // The different souces of input deps may duplicate some targets, so uniquify
210 // them (ordering doesn't matter for this case).
211 std::set<const Target*> unique_deps;
213 // Hard dependencies that are direct or indirect dependencies.
214 const std::set<const Target*>& hard_deps = target_->recursive_hard_deps();
215 for (const auto& dep : hard_deps)
216 unique_deps.insert(dep);
218 // Extra hard dependencies passed in.
219 unique_deps.insert(extra_hard_deps.begin(), extra_hard_deps.end());
221 // Toolchain dependencies. These must be resolved before doing anything.
222 // This just writs all toolchain deps for simplicity. If we find that
223 // toolchains often have more than one dependency, we could consider writing
224 // a toolchain-specific stamp file and only include the stamp here.
225 const LabelTargetVector& toolchain_deps = target_->toolchain()->deps();
226 for (const auto& toolchain_dep : toolchain_deps)
227 unique_deps.insert(toolchain_dep.ptr);
229 for (const auto& dep : unique_deps) {
230 DCHECK(!dep->dependency_output_file().value().empty());
231 out_ << " ";
232 path_output_.WriteFile(out_, dep->dependency_output_file());
235 out_ << "\n";
236 return input_stamp_file;
239 void NinjaTargetWriter::WriteStampForTarget(
240 const std::vector<OutputFile>& files,
241 const std::vector<OutputFile>& order_only_deps) {
242 const OutputFile& stamp_file = target_->dependency_output_file();
244 // First validate that the target's dependency is a stamp file. Otherwise,
245 // we shouldn't have gotten here!
246 CHECK(base::EndsWith(stamp_file.value(), ".stamp",
247 base::CompareCase::INSENSITIVE_ASCII))
248 << "Output should end in \".stamp\" for stamp file output. Instead got: "
249 << "\"" << stamp_file.value() << "\"";
251 out_ << "build ";
252 path_output_.WriteFile(out_, stamp_file);
254 out_ << ": "
255 << GetNinjaRulePrefixForToolchain(settings_)
256 << Toolchain::ToolTypeToName(Toolchain::TYPE_STAMP);
257 path_output_.WriteFiles(out_, files);
259 if (!order_only_deps.empty()) {
260 out_ << " ||";
261 path_output_.WriteFiles(out_, order_only_deps);
263 out_ << std::endl;