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_script_target_writer.h" | 5 #include "tools/gn/ninja_script_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" |
(...skipping 20 matching lines...) Expand all Loading... |
31 std::vector<OutputFile> output_files; | 31 std::vector<OutputFile> output_files; |
32 | 32 |
33 if (has_sources()) { | 33 if (has_sources()) { |
34 // Write separate build lines for each input source file. | 34 // Write separate build lines for each input source file. |
35 WriteSourceRules(custom_rule_name, implicit_deps, args_template, | 35 WriteSourceRules(custom_rule_name, implicit_deps, args_template, |
36 &output_files); | 36 &output_files); |
37 } else { | 37 } else { |
38 // No sources, write a rule that invokes the script once with the | 38 // No sources, write a rule that invokes the script once with the |
39 // outputs as outputs, and the data as inputs. | 39 // outputs as outputs, and the data as inputs. |
40 out_ << "build"; | 40 out_ << "build"; |
| 41 if (target_->script_values().has_depfile()) { |
| 42 out_ << " "; |
| 43 WriteDepfile(SourceFile()); |
| 44 } |
41 const Target::FileList& outputs = target_->script_values().outputs(); | 45 const Target::FileList& outputs = target_->script_values().outputs(); |
42 for (size_t i = 0; i < outputs.size(); i++) { | 46 for (size_t i = 0; i < outputs.size(); i++) { |
43 OutputFile output_path( | 47 OutputFile output_path( |
44 RemovePrefix(outputs[i].value(), | 48 RemovePrefix(outputs[i].value(), |
45 settings_->build_settings()->build_dir().value())); | 49 settings_->build_settings()->build_dir().value())); |
46 output_files.push_back(output_path); | 50 output_files.push_back(output_path); |
47 out_ << " "; | 51 out_ << " "; |
48 path_output_.WriteFile(out_, output_path); | 52 path_output_.WriteFile(out_, output_path); |
49 } | 53 } |
50 out_ << ": " << custom_rule_name << implicit_deps << std::endl; | 54 out_ << ": " << custom_rule_name << implicit_deps << std::endl; |
| 55 if (target_->script_values().has_depfile()) { |
| 56 out_ << " depfile = "; |
| 57 WriteDepfile(SourceFile()); |
| 58 out_ << std::endl; |
| 59 } |
51 } | 60 } |
52 out_ << std::endl; | 61 out_ << std::endl; |
53 | 62 |
54 WriteStamp(output_files); | 63 WriteStamp(output_files); |
55 } | 64 } |
56 | 65 |
57 std::string NinjaScriptTargetWriter::WriteRuleDefinition( | 66 std::string NinjaScriptTargetWriter::WriteRuleDefinition( |
58 const FileTemplate& args_template) { | 67 const FileTemplate& args_template) { |
59 // Make a unique name for this rule. | 68 // Make a unique name for this rule. |
60 // | 69 // |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 out_ << ": " << custom_rule_name << " "; | 147 out_ << ": " << custom_rule_name << " "; |
139 path_output_.WriteFile(out_, sources[i]); | 148 path_output_.WriteFile(out_, sources[i]); |
140 out_ << implicit_deps << std::endl; | 149 out_ << implicit_deps << std::endl; |
141 | 150 |
142 // Windows needs a unique ID for the response file. | 151 // Windows needs a unique ID for the response file. |
143 if (target_->settings()->IsWin()) | 152 if (target_->settings()->IsWin()) |
144 out_ << " unique_name = " << i << std::endl; | 153 out_ << " unique_name = " << i << std::endl; |
145 | 154 |
146 if (args_template.has_substitutions()) | 155 if (args_template.has_substitutions()) |
147 WriteArgsSubstitutions(sources[i], args_template); | 156 WriteArgsSubstitutions(sources[i], args_template); |
| 157 |
| 158 if (target_->script_values().has_depfile()) { |
| 159 out_ << " depfile = "; |
| 160 WriteDepfile(sources[i]); |
| 161 out_ << std::endl; |
| 162 } |
148 } | 163 } |
149 } | 164 } |
150 | 165 |
151 void NinjaScriptTargetWriter::WriteStamp( | 166 void NinjaScriptTargetWriter::WriteStamp( |
152 const std::vector<OutputFile>& output_files) { | 167 const std::vector<OutputFile>& output_files) { |
153 out_ << "build "; | 168 out_ << "build "; |
154 path_output_.WriteFile(out_, helper_.GetTargetOutputFile(target_)); | 169 path_output_.WriteFile(out_, helper_.GetTargetOutputFile(target_)); |
155 out_ << ": " | 170 out_ << ": " |
156 << helper_.GetRulePrefix(target_->settings()) | 171 << helper_.GetRulePrefix(target_->settings()) |
157 << "stamp"; | 172 << "stamp"; |
158 for (size_t i = 0; i < output_files.size(); i++) { | 173 for (size_t i = 0; i < output_files.size(); i++) { |
159 out_ << " "; | 174 out_ << " "; |
160 path_output_.WriteFile(out_, output_files[i]); | 175 path_output_.WriteFile(out_, output_files[i]); |
161 } | 176 } |
162 out_ << std::endl; | 177 out_ << std::endl; |
163 } | 178 } |
164 | 179 |
165 void NinjaScriptTargetWriter::WriteOutputFilesForBuildLine( | 180 void NinjaScriptTargetWriter::WriteOutputFilesForBuildLine( |
166 const FileTemplate& output_template, | 181 const FileTemplate& output_template, |
167 const SourceFile& source, | 182 const SourceFile& source, |
168 std::vector<OutputFile>* output_files) { | 183 std::vector<OutputFile>* output_files) { |
| 184 // If there is a depfile specified we need to list it as the first output as |
| 185 // that is what ninja will expect the depfile to refer to itself as. |
| 186 if (target_->script_values().has_depfile()) { |
| 187 out_ << " "; |
| 188 WriteDepfile(source); |
| 189 } |
169 std::vector<std::string> output_template_result; | 190 std::vector<std::string> output_template_result; |
170 output_template.ApplyString(source.value(), &output_template_result); | 191 output_template.ApplyString(source.value(), &output_template_result); |
171 for (size_t out_i = 0; out_i < output_template_result.size(); out_i++) { | 192 for (size_t out_i = 0; out_i < output_template_result.size(); out_i++) { |
172 OutputFile output_path(output_template_result[out_i]); | 193 OutputFile output_path(output_template_result[out_i]); |
173 output_files->push_back(output_path); | 194 output_files->push_back(output_path); |
174 out_ << " "; | 195 out_ << " "; |
175 path_output_.WriteFile(out_, output_path); | 196 path_output_.WriteFile(out_, output_path); |
176 } | 197 } |
177 } | 198 } |
| 199 |
| 200 void NinjaScriptTargetWriter::WriteDepfile(const SourceFile& source) { |
| 201 std::vector<std::string> result; |
| 202 GetDepfileTemplate().ApplyString(source.value(), &result); |
| 203 path_output_.WriteFile(out_, OutputFile(result[0])); |
| 204 } |
| 205 |
| 206 FileTemplate NinjaScriptTargetWriter::GetDepfileTemplate() const { |
| 207 std::vector<std::string> template_args; |
| 208 std::string depfile_relative_to_build_dir = |
| 209 RemovePrefix(target_->script_values().depfile().value(), |
| 210 settings_->build_settings()->build_dir().value()); |
| 211 template_args.push_back(depfile_relative_to_build_dir); |
| 212 return FileTemplate(template_args); |
| 213 } |
OLD | NEW |