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_action_target_writer.h" | 5 #include "tools/gn/ninja_action_target_writer.h" |
6 | 6 |
7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
8 #include "tools/gn/err.h" | 8 #include "tools/gn/err.h" |
| 9 #include "tools/gn/settings.h" |
9 #include "tools/gn/string_utils.h" | 10 #include "tools/gn/string_utils.h" |
10 #include "tools/gn/substitution_writer.h" | 11 #include "tools/gn/substitution_writer.h" |
11 #include "tools/gn/target.h" | 12 #include "tools/gn/target.h" |
12 | 13 |
13 NinjaActionTargetWriter::NinjaActionTargetWriter(const Target* target, | 14 NinjaActionTargetWriter::NinjaActionTargetWriter(const Target* target, |
14 const Toolchain* toolchain, | |
15 std::ostream& out) | 15 std::ostream& out) |
16 : NinjaTargetWriter(target, toolchain, out), | 16 : NinjaTargetWriter(target, out), |
17 path_output_no_escaping_( | 17 path_output_no_escaping_( |
18 target->settings()->build_settings()->build_dir(), | 18 target->settings()->build_settings()->build_dir(), |
19 ESCAPE_NONE) { | 19 ESCAPE_NONE) { |
20 } | 20 } |
21 | 21 |
22 NinjaActionTargetWriter::~NinjaActionTargetWriter() { | 22 NinjaActionTargetWriter::~NinjaActionTargetWriter() { |
23 } | 23 } |
24 | 24 |
25 void NinjaActionTargetWriter::Run() { | 25 void NinjaActionTargetWriter::Run() { |
26 std::string custom_rule_name = WriteRuleDefinition(); | 26 std::string custom_rule_name = WriteRuleDefinition(); |
(...skipping 23 matching lines...) Expand all Loading... |
50 // Write separate build lines for each input source file. | 50 // Write separate build lines for each input source file. |
51 WriteSourceRules(custom_rule_name, implicit_deps, &output_files); | 51 WriteSourceRules(custom_rule_name, implicit_deps, &output_files); |
52 } else { | 52 } else { |
53 DCHECK(target_->output_type() == Target::ACTION); | 53 DCHECK(target_->output_type() == Target::ACTION); |
54 | 54 |
55 // Write a rule that invokes the script once with the outputs as outputs, | 55 // Write a rule that invokes the script once with the outputs as outputs, |
56 // and the data as inputs. It does not depend on the sources. | 56 // and the data as inputs. It does not depend on the sources. |
57 out_ << "build"; | 57 out_ << "build"; |
58 SubstitutionWriter::GetListAsOutputFiles( | 58 SubstitutionWriter::GetListAsOutputFiles( |
59 settings_, target_->action_values().outputs(), &output_files); | 59 settings_, target_->action_values().outputs(), &output_files); |
60 for (size_t i = 0; i < output_files.size(); i++) { | 60 path_output_.WriteFiles(out_, output_files); |
61 out_ << " "; | |
62 path_output_.WriteFile(out_, output_files[i]); | |
63 } | |
64 | 61 |
65 out_ << ": " << custom_rule_name << implicit_deps << std::endl; | 62 out_ << ": " << custom_rule_name << implicit_deps << std::endl; |
66 if (target_->action_values().has_depfile()) { | 63 if (target_->action_values().has_depfile()) { |
67 out_ << " depfile = "; | 64 out_ << " depfile = "; |
68 WriteDepfile(SourceFile()); | 65 WriteDepfile(SourceFile()); |
69 out_ << std::endl; | 66 out_ << std::endl; |
70 } | 67 } |
71 } | 68 } |
72 out_ << std::endl; | 69 out_ << std::endl; |
73 | 70 |
74 WriteStamp(output_files); | 71 // Write the stamp, which also depends on all datadeps. These are needed at |
| 72 // runtime and should be compiled when the action is, but don't need to be |
| 73 // done before we run the action. |
| 74 std::vector<OutputFile> data_outs; |
| 75 for (size_t i = 0; i < target_->datadeps().size(); i++) |
| 76 data_outs.push_back(target_->datadeps()[i].ptr->dependency_output_file()); |
| 77 WriteStampForTarget(output_files, data_outs); |
75 } | 78 } |
76 | 79 |
77 std::string NinjaActionTargetWriter::WriteRuleDefinition() { | 80 std::string NinjaActionTargetWriter::WriteRuleDefinition() { |
78 // Make a unique name for this rule. | 81 // Make a unique name for this rule. |
79 // | 82 // |
80 // Use a unique name for the response file when there are multiple build | 83 // Use a unique name for the response file when there are multiple build |
81 // steps so that they don't stomp on each other. When there are no sources, | 84 // steps so that they don't stomp on each other. When there are no sources, |
82 // there will be only one invocation so we can use a simple name. | 85 // there will be only one invocation so we can use a simple name. |
83 std::string target_label = target_->label().GetUserVisibleName(true); | 86 std::string target_label = target_->label().GetUserVisibleName(true); |
84 std::string custom_rule_name(target_label); | 87 std::string custom_rule_name(target_label); |
85 base::ReplaceChars(custom_rule_name, ":/()", "_", &custom_rule_name); | 88 base::ReplaceChars(custom_rule_name, ":/()", "_", &custom_rule_name); |
86 custom_rule_name.append("_rule"); | 89 custom_rule_name.append("_rule"); |
87 | 90 |
88 const SubstitutionList& args = target_->action_values().args(); | 91 const SubstitutionList& args = target_->action_values().args(); |
89 EscapeOptions args_escape_options; | 92 EscapeOptions args_escape_options; |
90 args_escape_options.mode = ESCAPE_NINJA_COMMAND; | 93 args_escape_options.mode = ESCAPE_NINJA_COMMAND; |
91 | 94 |
92 if (settings_->IsWin()) { | 95 if (settings_->IsWin()) { |
93 // Send through gyp-win-tool and use a response file. | 96 // Send through gyp-win-tool and use a response file. |
94 std::string rspfile = custom_rule_name; | 97 std::string rspfile = custom_rule_name; |
95 if (has_sources()) | 98 if (!target_->sources().empty()) |
96 rspfile += ".$unique_name"; | 99 rspfile += ".$unique_name"; |
97 rspfile += ".rsp"; | 100 rspfile += ".rsp"; |
98 | 101 |
99 out_ << "rule " << custom_rule_name << std::endl; | 102 out_ << "rule " << custom_rule_name << std::endl; |
100 out_ << " command = "; | 103 out_ << " command = "; |
101 path_output_.WriteFile(out_, settings_->build_settings()->python_path()); | 104 path_output_.WriteFile(out_, settings_->build_settings()->python_path()); |
102 // TODO(brettw) this hardcodes "environment.x86" which is something that | 105 // TODO(brettw) this hardcodes "environment.x86" which is something that |
103 // the Chrome Windows toolchain writes. We should have a way to invoke | 106 // the Chrome Windows toolchain writes. We should have a way to invoke |
104 // python without requiring this gyp_win_tool thing. | 107 // python without requiring this gyp_win_tool thing. |
105 out_ << " gyp-win-tool action-wrapper environment.x86 " << rspfile | 108 out_ << " gyp-win-tool action-wrapper environment.x86 " << rspfile |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 args_escape_options, out_); | 173 args_escape_options, out_); |
171 | 174 |
172 if (target_->action_values().has_depfile()) { | 175 if (target_->action_values().has_depfile()) { |
173 out_ << " depfile = "; | 176 out_ << " depfile = "; |
174 WriteDepfile(sources[i]); | 177 WriteDepfile(sources[i]); |
175 out_ << std::endl; | 178 out_ << std::endl; |
176 } | 179 } |
177 } | 180 } |
178 } | 181 } |
179 | 182 |
180 void NinjaActionTargetWriter::WriteStamp( | |
181 const std::vector<OutputFile>& output_files) { | |
182 out_ << "build "; | |
183 path_output_.WriteFile(out_, helper_.GetTargetOutputFile(target_)); | |
184 out_ << ": " | |
185 << helper_.GetRulePrefix(target_->settings()) | |
186 << "stamp"; | |
187 | |
188 // The action stamp depends on all output files from running the action. | |
189 for (size_t i = 0; i < output_files.size(); i++) { | |
190 out_ << " "; | |
191 path_output_.WriteFile(out_, output_files[i]); | |
192 } | |
193 | |
194 // It also depends on all datadeps. These are needed at runtime and should | |
195 // be compiled when the action is, but don't need to be done before we run | |
196 // the action. | |
197 for (size_t i = 0; i < target_->datadeps().size(); i++) { | |
198 out_ << " "; | |
199 path_output_.WriteFile(out_, | |
200 helper_.GetTargetOutputFile(target_->datadeps()[i].ptr)); | |
201 } | |
202 | |
203 out_ << std::endl; | |
204 } | |
205 | |
206 void NinjaActionTargetWriter::WriteOutputFilesForBuildLine( | 183 void NinjaActionTargetWriter::WriteOutputFilesForBuildLine( |
207 const SourceFile& source, | 184 const SourceFile& source, |
208 std::vector<OutputFile>* output_files) { | 185 std::vector<OutputFile>* output_files) { |
209 size_t first_output_index = output_files->size(); | 186 size_t first_output_index = output_files->size(); |
210 | 187 |
211 SubstitutionWriter::ApplyListToSourceAsOutputFile( | 188 SubstitutionWriter::ApplyListToSourceAsOutputFile( |
212 settings_, target_->action_values().outputs(), source, output_files); | 189 settings_, target_->action_values().outputs(), source, output_files); |
213 | 190 |
214 for (size_t i = first_output_index; i < output_files->size(); i++) { | 191 for (size_t i = first_output_index; i < output_files->size(); i++) { |
215 out_ << " "; | 192 out_ << " "; |
216 path_output_.WriteFile(out_, (*output_files)[i]); | 193 path_output_.WriteFile(out_, (*output_files)[i]); |
217 } | 194 } |
218 } | 195 } |
219 | 196 |
220 void NinjaActionTargetWriter::WriteDepfile(const SourceFile& source) { | 197 void NinjaActionTargetWriter::WriteDepfile(const SourceFile& source) { |
221 path_output_.WriteFile(out_, | 198 path_output_.WriteFile(out_, |
222 SubstitutionWriter::ApplyPatternToSourceAsOutputFile( | 199 SubstitutionWriter::ApplyPatternToSourceAsOutputFile( |
223 settings_, target_->action_values().depfile(), source)); | 200 settings_, target_->action_values().depfile(), source)); |
224 } | 201 } |
OLD | NEW |