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/ninja_utils.h" |
| 9 #include "tools/gn/output_file.h" |
| 10 #include "tools/gn/scheduler.h" |
8 #include "tools/gn/string_utils.h" | 11 #include "tools/gn/string_utils.h" |
9 #include "tools/gn/substitution_list.h" | 12 #include "tools/gn/substitution_list.h" |
10 #include "tools/gn/substitution_writer.h" | 13 #include "tools/gn/substitution_writer.h" |
| 14 #include "tools/gn/target.h" |
| 15 #include "tools/gn/toolchain.h" |
11 | 16 |
12 NinjaCopyTargetWriter::NinjaCopyTargetWriter(const Target* target, | 17 NinjaCopyTargetWriter::NinjaCopyTargetWriter(const Target* target, |
13 const Toolchain* toolchain, | |
14 std::ostream& out) | 18 std::ostream& out) |
15 : NinjaTargetWriter(target, toolchain, out) { | 19 : NinjaTargetWriter(target, out) { |
16 } | 20 } |
17 | 21 |
18 NinjaCopyTargetWriter::~NinjaCopyTargetWriter() { | 22 NinjaCopyTargetWriter::~NinjaCopyTargetWriter() { |
19 } | 23 } |
20 | 24 |
21 void NinjaCopyTargetWriter::Run() { | 25 void NinjaCopyTargetWriter::Run() { |
| 26 const Tool* copy_tool = target_->toolchain()->GetTool(Toolchain::TYPE_COPY); |
| 27 if (!copy_tool) { |
| 28 g_scheduler->FailWithError(Err(NULL, |
| 29 "Copy tool not defined", |
| 30 "The toolchain " + |
| 31 target_->toolchain()->label().GetUserVisibleName(false) + |
| 32 "\n used by target " + target_->label().GetUserVisibleName(false) + |
| 33 "\n doesn't define a \"copy\" tool.")); |
| 34 return; |
| 35 } |
| 36 |
| 37 const Tool* stamp_tool = target_->toolchain()->GetTool(Toolchain::TYPE_STAMP); |
| 38 if (!stamp_tool) { |
| 39 g_scheduler->FailWithError(Err(NULL, |
| 40 "Copy tool not defined", |
| 41 "The toolchain " + |
| 42 target_->toolchain()->label().GetUserVisibleName(false) + |
| 43 "\n used by target " + target_->label().GetUserVisibleName(false) + |
| 44 "\n doesn't define a \"stamp\" tool.")); |
| 45 return; |
| 46 } |
| 47 |
| 48 // Figure out the substitutions used by the copy and stamp tools. |
| 49 SubstitutionBits required_bits = copy_tool->substitution_bits(); |
| 50 required_bits.MergeFrom(stamp_tool->substitution_bits()); |
| 51 |
| 52 // General target-related substitutions needed by both tools. |
| 53 WriteSharedVars(required_bits); |
| 54 |
| 55 std::vector<OutputFile> output_files; |
| 56 WriteCopyRules(&output_files); |
| 57 out_ << std::endl; |
| 58 WriteStampForTarget(output_files, std::vector<OutputFile>()); |
| 59 } |
| 60 |
| 61 void NinjaCopyTargetWriter::WriteCopyRules( |
| 62 std::vector<OutputFile>* output_files) { |
22 CHECK(target_->action_values().outputs().list().size() == 1); | 63 CHECK(target_->action_values().outputs().list().size() == 1); |
23 const SubstitutionList& output_subst_list = | 64 const SubstitutionList& output_subst_list = |
24 target_->action_values().outputs(); | 65 target_->action_values().outputs(); |
25 CHECK_EQ(1u, output_subst_list.list().size()) | 66 CHECK_EQ(1u, output_subst_list.list().size()) |
26 << "Should have one entry exactly."; | 67 << "Should have one entry exactly."; |
27 const SubstitutionPattern& output_subst = output_subst_list.list()[0]; | 68 const SubstitutionPattern& output_subst = output_subst_list.list()[0]; |
28 | 69 |
29 std::vector<OutputFile> output_files; | 70 std::string tool_name = |
30 | 71 GetNinjaRulePrefixForToolchain(settings_) + |
31 std::string rule_prefix = helper_.GetRulePrefix(target_->settings()); | 72 Toolchain::ToolTypeToName(Toolchain::TYPE_COPY); |
32 | 73 |
33 // Note that we don't write implicit deps for copy steps. "copy" only | 74 // Note that we don't write implicit deps for copy steps. "copy" only |
34 // depends on the output files themselves, rather than having includes | 75 // depends on the output files themselves, rather than having includes |
35 // (the possibility of generated #includes is the main reason for implicit | 76 // (the possibility of generated #includes is the main reason for implicit |
36 // dependencies). | 77 // dependencies). |
37 // | 78 // |
38 // It would seem that specifying implicit dependencies on the deps of the | 79 // It would seem that specifying implicit dependencies on the deps of the |
39 // copy command would still be harmeless. But Chrome implements copy tools | 80 // copy command would still be harmeless. But Chrome implements copy tools |
40 // as hard links (much faster) which don't change the timestamp. If the | 81 // as hard links (much faster) which don't change the timestamp. If the |
41 // ninja rule looks like this: | 82 // ninja rule looks like this: |
42 // output: copy input | foo.stamp | 83 // output: copy input | foo.stamp |
43 // The copy will not make a new timestamp on the output file, but the | 84 // The copy will not make a new timestamp on the output file, but the |
44 // foo.stamp file generated from a previous step will have a new timestamp. | 85 // foo.stamp file generated from a previous step will have a new timestamp. |
45 // The copy rule will therefore look out-of-date to Ninja and the rule will | 86 // The copy rule will therefore look out-of-date to Ninja and the rule will |
46 // get rebuilt. | 87 // get rebuilt. |
47 // | 88 // |
48 // If this copy is copying a generated file, not listing the implicit | 89 // If this copy is copying a generated file, not listing the implicit |
49 // dependency will be fine as long as the input to the copy is properly | 90 // dependency will be fine as long as the input to the copy is properly |
50 // listed as the output from the step that generated it. | 91 // listed as the output from the step that generated it. |
51 // | 92 // |
52 // Moreover, doing this assumes that the copy step is always a simple | 93 // Moreover, doing this assumes that the copy step is always a simple |
53 // locally run command, so there is no need for a toolchain dependency. | 94 // locally run command, so there is no need for a toolchain dependency. |
54 for (size_t i = 0; i < target_->sources().size(); i++) { | 95 for (size_t i = 0; i < target_->sources().size(); i++) { |
55 const SourceFile& input_file = target_->sources()[i]; | 96 const SourceFile& input_file = target_->sources()[i]; |
56 | 97 |
57 OutputFile output_file = | 98 OutputFile output_file = |
58 SubstitutionWriter::ApplyPatternToSourceAsOutputFile( | 99 SubstitutionWriter::ApplyPatternToSourceAsOutputFile( |
59 target_->settings(), output_subst, input_file); | 100 target_->settings(), output_subst, input_file); |
60 output_files.push_back(output_file); | 101 output_files->push_back(output_file); |
61 | 102 |
62 out_ << "build "; | 103 out_ << "build "; |
63 path_output_.WriteFile(out_, output_file); | 104 path_output_.WriteFile(out_, output_file); |
64 out_ << ": " << rule_prefix << "copy "; | 105 out_ << ": " << tool_name << " "; |
65 path_output_.WriteFile(out_, input_file); | 106 path_output_.WriteFile(out_, input_file); |
66 out_ << std::endl; | 107 out_ << std::endl; |
67 } | 108 } |
68 | |
69 // Write out the rule for the target to copy all of them. | |
70 out_ << std::endl << "build "; | |
71 path_output_.WriteFile(out_, helper_.GetTargetOutputFile(target_)); | |
72 out_ << ": " << rule_prefix << "stamp"; | |
73 for (size_t i = 0; i < output_files.size(); i++) { | |
74 out_ << " "; | |
75 path_output_.WriteFile(out_, output_files[i]); | |
76 } | |
77 out_ << std::endl; | |
78 } | 109 } |
OLD | NEW |