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_target_writer.h" | 5 #include "tools/gn/ninja_target_writer.h" |
6 | 6 |
7 #include <fstream> | 7 #include <fstream> |
8 #include <sstream> | 8 #include <sstream> |
9 | 9 |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "tools/gn/err.h" | 12 #include "tools/gn/err.h" |
13 #include "tools/gn/filesystem_utils.h" | 13 #include "tools/gn/filesystem_utils.h" |
14 #include "tools/gn/ninja_action_target_writer.h" | 14 #include "tools/gn/ninja_action_target_writer.h" |
15 #include "tools/gn/ninja_binary_target_writer.h" | 15 #include "tools/gn/ninja_binary_target_writer.h" |
16 #include "tools/gn/ninja_copy_target_writer.h" | 16 #include "tools/gn/ninja_copy_target_writer.h" |
17 #include "tools/gn/ninja_group_target_writer.h" | 17 #include "tools/gn/ninja_group_target_writer.h" |
18 #include "tools/gn/ninja_utils.h" | 18 #include "tools/gn/ninja_utils.h" |
| 19 #include "tools/gn/output_file.h" |
19 #include "tools/gn/scheduler.h" | 20 #include "tools/gn/scheduler.h" |
20 #include "tools/gn/string_utils.h" | 21 #include "tools/gn/string_utils.h" |
21 #include "tools/gn/substitution_writer.h" | 22 #include "tools/gn/substitution_writer.h" |
22 #include "tools/gn/target.h" | 23 #include "tools/gn/target.h" |
23 #include "tools/gn/trace.h" | 24 #include "tools/gn/trace.h" |
24 | 25 |
25 NinjaTargetWriter::NinjaTargetWriter(const Target* target, | 26 NinjaTargetWriter::NinjaTargetWriter(const Target* target, |
26 std::ostream& out) | 27 std::ostream& out) |
27 : settings_(target->settings()), | 28 : settings_(target->settings()), |
28 target_(target), | 29 target_(target), |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 << std::endl; | 136 << std::endl; |
136 written_anything = true; | 137 written_anything = true; |
137 } | 138 } |
138 | 139 |
139 // If we wrote any vars, separate them from the rest of the file that follows | 140 // If we wrote any vars, separate them from the rest of the file that follows |
140 // with a blank line. | 141 // with a blank line. |
141 if (written_anything) | 142 if (written_anything) |
142 out_ << std::endl; | 143 out_ << std::endl; |
143 } | 144 } |
144 | 145 |
145 std::string NinjaTargetWriter::WriteInputDepsStampAndGetDep( | 146 OutputFile NinjaTargetWriter::WriteInputDepsStampAndGetDep( |
146 const std::vector<const Target*>& extra_hard_deps) const { | 147 const std::vector<const Target*>& extra_hard_deps) const { |
147 CHECK(target_->toolchain()) | 148 CHECK(target_->toolchain()) |
148 << "Toolchain not set on target " | 149 << "Toolchain not set on target " |
149 << target_->label().GetUserVisibleName(true); | 150 << target_->label().GetUserVisibleName(true); |
150 | 151 |
151 // For an action (where we run a script only once) the sources are the same | 152 // For an action (where we run a script only once) the sources are the same |
152 // as the source prereqs. | 153 // as the source prereqs. |
153 bool list_sources_as_input_deps = (target_->output_type() == Target::ACTION); | 154 bool list_sources_as_input_deps = (target_->output_type() == Target::ACTION); |
154 | 155 |
155 // Actions get implicit dependencies on the script itself. | 156 // Actions get implicit dependencies on the script itself. |
156 bool add_script_source_as_dep = | 157 bool add_script_source_as_dep = |
157 (target_->output_type() == Target::ACTION) || | 158 (target_->output_type() == Target::ACTION) || |
158 (target_->output_type() == Target::ACTION_FOREACH); | 159 (target_->output_type() == Target::ACTION_FOREACH); |
159 | 160 |
160 if (!add_script_source_as_dep && | 161 if (!add_script_source_as_dep && |
161 extra_hard_deps.empty() && | 162 extra_hard_deps.empty() && |
162 target_->inputs().empty() && | 163 target_->inputs().empty() && |
163 target_->recursive_hard_deps().empty() && | 164 target_->recursive_hard_deps().empty() && |
164 (!list_sources_as_input_deps || target_->sources().empty()) && | 165 (!list_sources_as_input_deps || target_->sources().empty()) && |
165 target_->toolchain()->deps().empty()) | 166 target_->toolchain()->deps().empty()) |
166 return std::string(); // No input/hard deps. | 167 return OutputFile(); // No input/hard deps. |
167 | 168 |
168 // One potential optimization is if there are few input dependencies (or | 169 // One potential optimization is if there are few input dependencies (or |
169 // potentially few sources that depend on these) it's better to just write | 170 // potentially few sources that depend on these) it's better to just write |
170 // all hard deps on each sources line than have this intermediate stamp. We | 171 // all hard deps on each sources line than have this intermediate stamp. We |
171 // do the stamp file because duplicating all the order-only deps for each | 172 // do the stamp file because duplicating all the order-only deps for each |
172 // source file can really explode the ninja file but this won't be the most | 173 // source file can really explode the ninja file but this won't be the most |
173 // optimal thing in all cases. | 174 // optimal thing in all cases. |
174 | 175 |
175 OutputFile input_stamp_file( | 176 OutputFile input_stamp_file( |
176 RebaseSourceAbsolutePath(GetTargetOutputDir(target_).value(), | 177 RebaseSourceAbsolutePath(GetTargetOutputDir(target_).value(), |
177 settings_->build_settings()->build_dir())); | 178 settings_->build_settings()->build_dir())); |
178 input_stamp_file.value().append(target_->label().name()); | 179 input_stamp_file.value().append(target_->label().name()); |
179 input_stamp_file.value().append(".inputdeps.stamp"); | 180 input_stamp_file.value().append(".inputdeps.stamp"); |
180 | 181 |
181 std::ostringstream stamp_file_stream; | 182 out_ << "build "; |
182 path_output_.WriteFile(stamp_file_stream, input_stamp_file); | 183 path_output_.WriteFile(out_, input_stamp_file); |
183 std::string stamp_file_string = stamp_file_stream.str(); | 184 out_ << ": " |
184 | |
185 out_ << "build " << stamp_file_string << ": " | |
186 << GetNinjaRulePrefixForToolchain(settings_) | 185 << GetNinjaRulePrefixForToolchain(settings_) |
187 << Toolchain::ToolTypeToName(Toolchain::TYPE_STAMP); | 186 << Toolchain::ToolTypeToName(Toolchain::TYPE_STAMP); |
188 | 187 |
189 // Script file (if applicable). | 188 // Script file (if applicable). |
190 if (add_script_source_as_dep) { | 189 if (add_script_source_as_dep) { |
191 out_ << " "; | 190 out_ << " "; |
192 path_output_.WriteFile(out_, target_->action_values().script()); | 191 path_output_.WriteFile(out_, target_->action_values().script()); |
193 } | 192 } |
194 | 193 |
195 // Input files are order-only deps. | 194 // Input files are order-only deps. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 unique_deps.insert(toolchain_deps[i].ptr); | 228 unique_deps.insert(toolchain_deps[i].ptr); |
230 | 229 |
231 for (std::set<const Target*>::const_iterator i = unique_deps.begin(); | 230 for (std::set<const Target*>::const_iterator i = unique_deps.begin(); |
232 i != unique_deps.end(); ++i) { | 231 i != unique_deps.end(); ++i) { |
233 DCHECK(!(*i)->dependency_output_file().value().empty()); | 232 DCHECK(!(*i)->dependency_output_file().value().empty()); |
234 out_ << " "; | 233 out_ << " "; |
235 path_output_.WriteFile(out_, (*i)->dependency_output_file()); | 234 path_output_.WriteFile(out_, (*i)->dependency_output_file()); |
236 } | 235 } |
237 | 236 |
238 out_ << "\n"; | 237 out_ << "\n"; |
239 return " | " + stamp_file_string; | 238 return input_stamp_file; |
240 } | 239 } |
241 | 240 |
242 void NinjaTargetWriter::WriteStampForTarget( | 241 void NinjaTargetWriter::WriteStampForTarget( |
243 const std::vector<OutputFile>& files, | 242 const std::vector<OutputFile>& files, |
244 const std::vector<OutputFile>& order_only_deps) { | 243 const std::vector<OutputFile>& order_only_deps) { |
245 const OutputFile& stamp_file = target_->dependency_output_file(); | 244 const OutputFile& stamp_file = target_->dependency_output_file(); |
246 | 245 |
247 // First validate that the target's dependency is a stamp file. Otherwise, | 246 // First validate that the target's dependency is a stamp file. Otherwise, |
248 // we shouldn't have gotten here! | 247 // we shouldn't have gotten here! |
249 CHECK(EndsWith(stamp_file.value(), ".stamp", false)) | 248 CHECK(EndsWith(stamp_file.value(), ".stamp", false)) |
250 << "Output should end in \".stamp\" for stamp file output. Instead got: " | 249 << "Output should end in \".stamp\" for stamp file output. Instead got: " |
251 << "\"" << stamp_file.value() << "\""; | 250 << "\"" << stamp_file.value() << "\""; |
252 | 251 |
253 out_ << "build "; | 252 out_ << "build "; |
254 path_output_.WriteFile(out_, stamp_file); | 253 path_output_.WriteFile(out_, stamp_file); |
255 | 254 |
256 out_ << ": " | 255 out_ << ": " |
257 << GetNinjaRulePrefixForToolchain(settings_) | 256 << GetNinjaRulePrefixForToolchain(settings_) |
258 << Toolchain::ToolTypeToName(Toolchain::TYPE_STAMP); | 257 << Toolchain::ToolTypeToName(Toolchain::TYPE_STAMP); |
259 path_output_.WriteFiles(out_, files); | 258 path_output_.WriteFiles(out_, files); |
260 | 259 |
261 if (!order_only_deps.empty()) { | 260 if (!order_only_deps.empty()) { |
262 out_ << " ||"; | 261 out_ << " ||"; |
263 path_output_.WriteFiles(out_, order_only_deps); | 262 path_output_.WriteFiles(out_, order_only_deps); |
264 } | 263 } |
265 out_ << std::endl; | 264 out_ << std::endl; |
266 } | 265 } |
OLD | NEW |