1 // Copyright 2018 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 "gn/ninja_generated_file_target_writer.h"
6
7 #include "base/strings/string_util.h"
8 #include "gn/deps_iterator.h"
9 #include "gn/output_conversion.h"
10 #include "gn/output_file.h"
11 #include "gn/scheduler.h"
12 #include "gn/settings.h"
13 #include "gn/string_output_buffer.h"
14 #include "gn/string_utils.h"
15 #include "gn/target.h"
16 #include "gn/trace.h"
17
NinjaGeneratedFileTargetWriter(const Target * target,std::ostream & out)18 NinjaGeneratedFileTargetWriter::NinjaGeneratedFileTargetWriter(
19 const Target* target,
20 std::ostream& out)
21 : NinjaTargetWriter(target, out) {}
22
23 NinjaGeneratedFileTargetWriter::~NinjaGeneratedFileTargetWriter() = default;
24
Run()25 void NinjaGeneratedFileTargetWriter::Run() {
26 // Write the file.
27 GenerateFile();
28
29 // A generated_file target should generate a stamp file with dependencies
30 // on each of the deps and data_deps in the target. The actual collection is
31 // done at gen time, and so ninja doesn't need to know about it.
32 std::vector<OutputFile> output_files;
33 std::vector<OutputFile> data_output_files;
34 for (const auto& pair : target_->GetDeps(Target::DEPS_LINKED)) {
35 if (pair.ptr->IsDataOnly()) {
36 data_output_files.push_back(pair.ptr->dependency_output_file());
37 } else {
38 output_files.push_back(pair.ptr->dependency_output_file());
39 }
40 }
41
42 const LabelTargetVector& data_deps = target_->data_deps();
43 for (const auto& pair : data_deps)
44 data_output_files.push_back(pair.ptr->dependency_output_file());
45
46 WriteStampForTarget(output_files, data_output_files);
47 }
48
GenerateFile()49 void NinjaGeneratedFileTargetWriter::GenerateFile() {
50 Err err;
51
52 // If this is a metadata target, populate the write value with the appropriate
53 // data.
54 Value contents;
55 if (target_->contents().type() == Value::NONE) {
56 // Origin is set to the outputs location, so that errors with this value
57 // get flagged on the right target.
58 CHECK(target_->action_values().outputs().list().size() == 1U);
59 contents = Value(target_->action_values().outputs().list()[0].origin(),
60 Value::LIST);
61 TargetSet targets_walked;
62 if (!target_->GetMetadata(target_->data_keys(), target_->walk_keys(),
63 target_->rebase(), /*deps_only = */ true,
64 &contents.list_value(), &targets_walked, &err)) {
65 g_scheduler->FailWithError(err);
66 return;
67 }
68 } else {
69 contents = target_->contents();
70 }
71
72 std::vector<SourceFile> outputs_as_sources;
73 target_->action_values().GetOutputsAsSourceFiles(target_,
74 &outputs_as_sources);
75 CHECK(outputs_as_sources.size() == 1);
76
77 base::FilePath output =
78 settings_->build_settings()->GetFullPath(outputs_as_sources[0]);
79 ScopedTrace trace(TraceItem::TRACE_FILE_WRITE, outputs_as_sources[0].value());
80
81 // Compute output.
82 StringOutputBuffer storage;
83 std::ostream out(&storage);
84 ConvertValueToOutput(settings_, contents, target_->output_conversion(), out,
85 &err);
86
87 if (err.has_error()) {
88 g_scheduler->FailWithError(err);
89 return;
90 }
91
92 storage.WriteToFileIfChanged(output, &err);
93
94 if (err.has_error()) {
95 g_scheduler->FailWithError(err);
96 return;
97 }
98 }
99