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.
4
5#include "tools/gn/ninja_copy_target_writer.h"
6
7#include "base/strings/string_util.h"
8#include "tools/gn/file_template.h"
9#include "tools/gn/string_utils.h"
10
11NinjaCopyTargetWriter::NinjaCopyTargetWriter(const Target* target,
12                                             const Toolchain* toolchain,
13                                             std::ostream& out)
14    : NinjaTargetWriter(target, toolchain, out) {
15}
16
17NinjaCopyTargetWriter::~NinjaCopyTargetWriter() {
18}
19
20void NinjaCopyTargetWriter::Run() {
21  CHECK(target_->script_values().outputs().size() == 1);
22  FileTemplate output_template(GetOutputTemplate());
23
24  std::vector<OutputFile> output_files;
25
26  std::string rule_prefix = helper_.GetRulePrefix(target_->settings());
27
28  for (size_t i = 0; i < target_->sources().size(); i++) {
29    const SourceFile& input_file = target_->sources()[i];
30
31    // Make the output file from the template.
32    std::vector<std::string> template_result;
33    output_template.ApplyString(input_file.value(), &template_result);
34    CHECK(template_result.size() == 1);
35    OutputFile output_file(template_result[0]);
36
37    output_files.push_back(output_file);
38
39    out_ << "build ";
40    path_output_.WriteFile(out_, output_file);
41    out_ << ": " << rule_prefix << "copy ";
42    path_output_.WriteFile(out_, input_file);
43    out_ << std::endl;
44  }
45
46  // Write out the rule for the target to copy all of them.
47  out_ << std::endl << "build ";
48  path_output_.WriteFile(out_, helper_.GetTargetOutputFile(target_));
49  out_ << ": " << rule_prefix << "stamp";
50  for (size_t i = 0; i < output_files.size(); i++) {
51    out_ << " ";
52    path_output_.WriteFile(out_, output_files[i]);
53  }
54  out_ << std::endl;
55}
56