| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "tools/gn/ninja_copy_target_writer.h" | 5 #include "tools/gn/ninja_copy_target_writer.h" |
| 6 | 6 |
| 7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
| 8 #include "tools/gn/file_template.h" | 8 #include "tools/gn/file_template.h" |
| 9 #include "tools/gn/string_utils.h" | 9 #include "tools/gn/string_utils.h" |
| 10 | 10 |
| 11 NinjaCopyTargetWriter::NinjaCopyTargetWriter(const Target* target, | 11 NinjaCopyTargetWriter::NinjaCopyTargetWriter(const Target* target, |
| 12 const Toolchain* toolchain, | 12 const Toolchain* toolchain, |
| 13 std::ostream& out) | 13 std::ostream& out) |
| 14 : NinjaTargetWriter(target, toolchain, out) { | 14 : NinjaTargetWriter(target, toolchain, out) { |
| 15 } | 15 } |
| 16 | 16 |
| 17 NinjaCopyTargetWriter::~NinjaCopyTargetWriter() { | 17 NinjaCopyTargetWriter::~NinjaCopyTargetWriter() { |
| 18 } | 18 } |
| 19 | 19 |
| 20 void NinjaCopyTargetWriter::Run() { | 20 void NinjaCopyTargetWriter::Run() { |
| 21 CHECK(target_->action_values().outputs().size() == 1); | 21 CHECK(target_->action_values().outputs().size() == 1); |
| 22 FileTemplate output_template = FileTemplate::GetForTargetOutputs(target_); | 22 FileTemplate output_template = FileTemplate::GetForTargetOutputs(target_); |
| 23 | 23 |
| 24 std::vector<OutputFile> output_files; | 24 std::vector<OutputFile> output_files; |
| 25 | 25 |
| 26 std::string rule_prefix = helper_.GetRulePrefix(target_->settings()); | 26 std::string rule_prefix = helper_.GetRulePrefix(target_->settings()); |
| 27 | 27 |
| 28 std::string implicit_deps = | 28 // Note that we don't write implicit deps for copy steps. "copy" only |
| 29 WriteInputDepsStampAndGetDep(std::vector<const Target*>()); | 29 // depends on the output files themselves, rather than having includes |
| 30 | 30 // (the possibility of generated #includes is the main reason for implicit |
| 31 // dependencies). |
| 32 // |
| 33 // It would seem that specifying implicit dependencies on the deps of the |
| 34 // copy command would still be harmeless. But Chrome implements copy tools |
| 35 // as hard links (much faster) which don't change the timestamp. If the |
| 36 // ninja rule looks like this: |
| 37 // output: copy input | foo.stamp |
| 38 // The copy will not make a new timestamp on the output file, but the |
| 39 // foo.stamp file generated from a previous step will have a new timestamp. |
| 40 // The copy rule will therefore look out-of-date to Ninja and the rule will |
| 41 // get rebuilt. |
| 42 // |
| 43 // If this copy is copying a generated file, not listing the implicit |
| 44 // dependency will be fine as long as the input to the copy is properly |
| 45 // listed as the output from the step that generated it. |
| 46 // |
| 47 // Moreover, doing this assumes that the copy step is always a simple |
| 48 // locally run command, so there is no need for a toolchain dependency. |
| 31 for (size_t i = 0; i < target_->sources().size(); i++) { | 49 for (size_t i = 0; i < target_->sources().size(); i++) { |
| 32 const SourceFile& input_file = target_->sources()[i]; | 50 const SourceFile& input_file = target_->sources()[i]; |
| 33 | 51 |
| 34 // Make the output file from the template. | 52 // Make the output file from the template. |
| 35 std::vector<std::string> template_result; | 53 std::vector<std::string> template_result; |
| 36 output_template.Apply(input_file, &template_result); | 54 output_template.Apply(input_file, &template_result); |
| 37 CHECK(template_result.size() == 1); | 55 CHECK(template_result.size() == 1); |
| 38 | 56 |
| 39 // All output files should be in the build directory, so we can rebase | 57 // All output files should be in the build directory, so we can rebase |
| 40 // them just by trimming the prefix. | 58 // them just by trimming the prefix. |
| 41 OutputFile output_file( | 59 OutputFile output_file( |
| 42 RemovePrefix(template_result[0], | 60 RemovePrefix(template_result[0], |
| 43 settings_->build_settings()->build_dir().value())); | 61 settings_->build_settings()->build_dir().value())); |
| 44 output_files.push_back(output_file); | 62 output_files.push_back(output_file); |
| 45 | 63 |
| 46 out_ << "build "; | 64 out_ << "build "; |
| 47 path_output_.WriteFile(out_, output_file); | 65 path_output_.WriteFile(out_, output_file); |
| 48 out_ << ": " << rule_prefix << "copy "; | 66 out_ << ": " << rule_prefix << "copy "; |
| 49 path_output_.WriteFile(out_, input_file); | 67 path_output_.WriteFile(out_, input_file); |
| 50 out_ << implicit_deps << std::endl; | 68 out_ << std::endl; |
| 51 } | 69 } |
| 52 | 70 |
| 53 // Write out the rule for the target to copy all of them. | 71 // Write out the rule for the target to copy all of them. |
| 54 out_ << std::endl << "build "; | 72 out_ << std::endl << "build "; |
| 55 path_output_.WriteFile(out_, helper_.GetTargetOutputFile(target_)); | 73 path_output_.WriteFile(out_, helper_.GetTargetOutputFile(target_)); |
| 56 out_ << ": " << rule_prefix << "stamp"; | 74 out_ << ": " << rule_prefix << "stamp"; |
| 57 for (size_t i = 0; i < output_files.size(); i++) { | 75 for (size_t i = 0; i < output_files.size(); i++) { |
| 58 out_ << " "; | 76 out_ << " "; |
| 59 path_output_.WriteFile(out_, output_files[i]); | 77 path_output_.WriteFile(out_, output_files[i]); |
| 60 } | 78 } |
| 61 out_ << std::endl; | 79 out_ << std::endl; |
| 62 } | 80 } |
| OLD | NEW |