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/filesystem_utils.h"
10 #include "gn/output_conversion.h"
11 #include "gn/output_file.h"
12 #include "gn/scheduler.h"
13 #include "gn/settings.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 for (const auto& pair : target_->GetDeps(Target::DEPS_LINKED))
34 output_files.push_back(pair.ptr->dependency_output_file());
35
36 std::vector<OutputFile> data_output_files;
37 const LabelTargetVector& data_deps = target_->data_deps();
38 for (const auto& pair : data_deps)
39 data_output_files.push_back(pair.ptr->dependency_output_file());
40
41 WriteStampForTarget(output_files, data_output_files);
42 }
43
GenerateFile()44 void NinjaGeneratedFileTargetWriter::GenerateFile() {
45 Err err;
46
47 // If this is a metadata target, populate the write value with the appropriate
48 // data.
49 Value contents;
50 if (target_->contents().type() == Value::NONE) {
51 // Origin is set to the outputs location, so that errors with this value
52 // get flagged on the right target.
53 CHECK(target_->action_values().outputs().list().size() == 1U);
54 contents = Value(target_->action_values().outputs().list()[0].origin(),
55 Value::LIST);
56 std::set<const Target*> targets_walked;
57 if (!target_->GetMetadata(target_->data_keys(), target_->walk_keys(),
58 target_->rebase(), /*deps_only = */ true,
59 &contents.list_value(), &targets_walked, &err)) {
60 g_scheduler->FailWithError(err);
61 return;
62 }
63 } else {
64 contents = target_->contents();
65 }
66
67 std::vector<SourceFile> outputs_as_sources;
68 target_->action_values().GetOutputsAsSourceFiles(target_,
69 &outputs_as_sources);
70 CHECK(outputs_as_sources.size() == 1);
71
72 base::FilePath output =
73 settings_->build_settings()->GetFullPath(outputs_as_sources[0]);
74 ScopedTrace trace(TraceItem::TRACE_FILE_WRITE, outputs_as_sources[0].value());
75
76 // Compute output.
77 std::ostringstream out;
78 ConvertValueToOutput(settings_, contents, target_->output_conversion(), out,
79 &err);
80
81 if (err.has_error()) {
82 g_scheduler->FailWithError(err);
83 return;
84 }
85
86 WriteFileIfChanged(output, out.str(), &err);
87
88 if (err.has_error()) {
89 g_scheduler->FailWithError(err);
90 return;
91 }
92 }
93