Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(705)

Side by Side Diff: tools/gn/ninja_action_target_writer.cc

Issue 387663003: Improve GN handling of directory templates. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/gn/function_write_file.cc ('k') | tools/gn/ninja_action_target_writer_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/file_template.h" 9 #include "tools/gn/file_template.h"
10 #include "tools/gn/string_utils.h" 10 #include "tools/gn/string_utils.h"
11 #include "tools/gn/target.h" 11 #include "tools/gn/target.h"
12 12
13 NinjaActionTargetWriter::NinjaActionTargetWriter(const Target* target, 13 NinjaActionTargetWriter::NinjaActionTargetWriter(const Target* target,
14 const Toolchain* toolchain, 14 const Toolchain* toolchain,
15 std::ostream& out) 15 std::ostream& out)
16 : NinjaTargetWriter(target, toolchain, out), 16 : NinjaTargetWriter(target, toolchain, 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 FileTemplate args_template(target_->settings(), 26 FileTemplate args_template(
27 target_->action_values().args()); 27 target_->settings(),
28 target_->action_values().args(),
29 FileTemplate::OUTPUT_RELATIVE,
30 target_->settings()->build_settings()->build_dir());
28 std::string custom_rule_name = WriteRuleDefinition(args_template); 31 std::string custom_rule_name = WriteRuleDefinition(args_template);
29 32
30 // Collect our deps to pass as "extra hard dependencies" for input deps. This 33 // Collect our deps to pass as "extra hard dependencies" for input deps. This
31 // will force all of the action's dependencies to be completed before the 34 // will force all of the action's dependencies to be completed before the
32 // action is run. Usually, if an action has a dependency, it will be 35 // action is run. Usually, if an action has a dependency, it will be
33 // operating on the result of that previous step, so we need to be sure to 36 // operating on the result of that previous step, so we need to be sure to
34 // serialize these. 37 // serialize these.
35 std::vector<const Target*> extra_hard_deps; 38 std::vector<const Target*> extra_hard_deps;
36 for (size_t i = 0; i < target_->deps().size(); i++) 39 for (size_t i = 0; i < target_->deps().size(); i++)
37 extra_hard_deps.push_back(target_->deps()[i].ptr); 40 extra_hard_deps.push_back(target_->deps()[i].ptr);
(...skipping 13 matching lines...) Expand all
51 if (target_->output_type() == Target::ACTION_FOREACH) { 54 if (target_->output_type() == Target::ACTION_FOREACH) {
52 // Write separate build lines for each input source file. 55 // Write separate build lines for each input source file.
53 WriteSourceRules(custom_rule_name, implicit_deps, args_template, 56 WriteSourceRules(custom_rule_name, implicit_deps, args_template,
54 &output_files); 57 &output_files);
55 } else { 58 } else {
56 DCHECK(target_->output_type() == Target::ACTION); 59 DCHECK(target_->output_type() == Target::ACTION);
57 60
58 // Write a rule that invokes the script once with the outputs as outputs, 61 // Write a rule that invokes the script once with the outputs as outputs,
59 // and the data as inputs. 62 // and the data as inputs.
60 out_ << "build"; 63 out_ << "build";
61 const Target::FileList& outputs = target_->action_values().outputs(); 64 const std::vector<std::string>& outputs =
65 target_->action_values().outputs();
62 for (size_t i = 0; i < outputs.size(); i++) { 66 for (size_t i = 0; i < outputs.size(); i++) {
63 OutputFile output_path( 67 OutputFile output_path(
64 RemovePrefix(outputs[i].value(), 68 RemovePrefix(outputs[i],
65 settings_->build_settings()->build_dir().value())); 69 settings_->build_settings()->build_dir().value()));
66 output_files.push_back(output_path); 70 output_files.push_back(output_path);
67 out_ << " "; 71 out_ << " ";
68 path_output_.WriteFile(out_, output_path); 72 path_output_.WriteFile(out_, output_path);
69 } 73 }
70 74
71 out_ << ": " << custom_rule_name << implicit_deps << std::endl; 75 out_ << ": " << custom_rule_name << implicit_deps << std::endl;
72 if (target_->action_values().has_depfile()) { 76 if (target_->action_values().has_depfile()) {
73 out_ << " depfile = "; 77 out_ << " depfile = ";
74 WriteDepfile(SourceFile()); 78 WriteDepfile(SourceFile());
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 return custom_rule_name; 138 return custom_rule_name;
135 } 139 }
136 140
137 void NinjaActionTargetWriter::WriteArgsSubstitutions( 141 void NinjaActionTargetWriter::WriteArgsSubstitutions(
138 const SourceFile& source, 142 const SourceFile& source,
139 const FileTemplate& args_template) { 143 const FileTemplate& args_template) {
140 EscapeOptions template_escape_options; 144 EscapeOptions template_escape_options;
141 template_escape_options.mode = ESCAPE_NINJA_COMMAND; 145 template_escape_options.mode = ESCAPE_NINJA_COMMAND;
142 146
143 args_template.WriteNinjaVariablesForSubstitution( 147 args_template.WriteNinjaVariablesForSubstitution(
144 out_, target_->settings(), source, template_escape_options); 148 out_, source, template_escape_options);
145 } 149 }
146 150
147 void NinjaActionTargetWriter::WriteSourceRules( 151 void NinjaActionTargetWriter::WriteSourceRules(
148 const std::string& custom_rule_name, 152 const std::string& custom_rule_name,
149 const std::string& implicit_deps, 153 const std::string& implicit_deps,
150 const FileTemplate& args_template, 154 const FileTemplate& args_template,
151 std::vector<OutputFile>* output_files) { 155 std::vector<OutputFile>* output_files) {
152 FileTemplate output_template(GetOutputTemplate()); 156 FileTemplate output_template = FileTemplate::GetForTargetOutputs(target_);
153 157
154 const Target::FileList& sources = target_->sources(); 158 const Target::FileList& sources = target_->sources();
155 for (size_t i = 0; i < sources.size(); i++) { 159 for (size_t i = 0; i < sources.size(); i++) {
156 out_ << "build"; 160 out_ << "build";
157 WriteOutputFilesForBuildLine(output_template, sources[i], output_files); 161 WriteOutputFilesForBuildLine(output_template, sources[i], output_files);
158 162
159 out_ << ": " << custom_rule_name << " "; 163 out_ << ": " << custom_rule_name << " ";
160 path_output_.WriteFile(out_, sources[i]); 164 path_output_.WriteFile(out_, sources[i]);
161 out_ << implicit_deps << std::endl; 165 out_ << implicit_deps << std::endl;
162 166
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 out_ << std::endl; 205 out_ << std::endl;
202 } 206 }
203 207
204 void NinjaActionTargetWriter::WriteOutputFilesForBuildLine( 208 void NinjaActionTargetWriter::WriteOutputFilesForBuildLine(
205 const FileTemplate& output_template, 209 const FileTemplate& output_template,
206 const SourceFile& source, 210 const SourceFile& source,
207 std::vector<OutputFile>* output_files) { 211 std::vector<OutputFile>* output_files) {
208 std::vector<std::string> output_template_result; 212 std::vector<std::string> output_template_result;
209 output_template.Apply(source, &output_template_result); 213 output_template.Apply(source, &output_template_result);
210 for (size_t out_i = 0; out_i < output_template_result.size(); out_i++) { 214 for (size_t out_i = 0; out_i < output_template_result.size(); out_i++) {
211 OutputFile output_path(output_template_result[out_i]); 215 // All output files should be in the build directory, so we can rebase
216 // them just by trimming the prefix.
217 OutputFile output_path(
218 RemovePrefix(output_template_result[out_i],
219 settings_->build_settings()->build_dir().value()));
212 output_files->push_back(output_path); 220 output_files->push_back(output_path);
213 out_ << " "; 221 out_ << " ";
214 path_output_.WriteFile(out_, output_path); 222 path_output_.WriteFile(out_, output_path);
215 } 223 }
216 } 224 }
217 225
218 void NinjaActionTargetWriter::WriteDepfile(const SourceFile& source) { 226 void NinjaActionTargetWriter::WriteDepfile(const SourceFile& source) {
219 std::vector<std::string> result; 227 std::vector<std::string> result;
220 GetDepfileTemplate().Apply(source, &result); 228 GetDepfileTemplate().Apply(source, &result);
221 path_output_.WriteFile(out_, OutputFile(result[0])); 229 path_output_.WriteFile(out_, OutputFile(result[0]));
222 } 230 }
223 231
224 FileTemplate NinjaActionTargetWriter::GetDepfileTemplate() const { 232 FileTemplate NinjaActionTargetWriter::GetDepfileTemplate() const {
225 std::vector<std::string> template_args; 233 std::vector<std::string> template_args;
226 std::string depfile_relative_to_build_dir = 234 std::string depfile_relative_to_build_dir =
227 RemovePrefix(target_->action_values().depfile().value(), 235 RemovePrefix(target_->action_values().depfile().value(),
228 settings_->build_settings()->build_dir().value()); 236 settings_->build_settings()->build_dir().value());
229 template_args.push_back(depfile_relative_to_build_dir); 237 template_args.push_back(depfile_relative_to_build_dir);
230 return FileTemplate(settings_, template_args); 238 return FileTemplate(settings_, template_args, FileTemplate::OUTPUT_ABSOLUTE,
239 SourceDir());
231 } 240 }
OLDNEW
« no previous file with comments | « tools/gn/function_write_file.cc ('k') | tools/gn/ninja_action_target_writer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698