| 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_build_writer.h" | 5 #include "tools/gn/ninja_build_writer.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <fstream> | 9 #include <fstream> |
| 10 #include <map> | 10 #include <map> |
| 11 #include <sstream> | 11 #include <sstream> |
| 12 | 12 |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 15 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 16 #include "base/process/process_handle.h" | 16 #include "base/process/process_handle.h" |
| 17 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 19 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 20 #include "tools/gn/build_settings.h" | 20 #include "tools/gn/build_settings.h" |
| 21 #include "tools/gn/builder.h" |
| 21 #include "tools/gn/err.h" | 22 #include "tools/gn/err.h" |
| 22 #include "tools/gn/escape.h" | 23 #include "tools/gn/escape.h" |
| 23 #include "tools/gn/filesystem_utils.h" | 24 #include "tools/gn/filesystem_utils.h" |
| 24 #include "tools/gn/input_file_manager.h" | 25 #include "tools/gn/input_file_manager.h" |
| 26 #include "tools/gn/loader.h" |
| 25 #include "tools/gn/ninja_utils.h" | 27 #include "tools/gn/ninja_utils.h" |
| 26 #include "tools/gn/pool.h" | 28 #include "tools/gn/pool.h" |
| 27 #include "tools/gn/scheduler.h" | 29 #include "tools/gn/scheduler.h" |
| 28 #include "tools/gn/switches.h" | 30 #include "tools/gn/switches.h" |
| 29 #include "tools/gn/target.h" | 31 #include "tools/gn/target.h" |
| 30 #include "tools/gn/trace.h" | 32 #include "tools/gn/trace.h" |
| 31 | 33 |
| 32 #if defined(OS_WIN) | 34 #if defined(OS_WIN) |
| 33 #include <windows.h> | 35 #include <windows.h> |
| 34 #endif | 36 #endif |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 "\nCollisions:\n" + matches_string); | 122 "\nCollisions:\n" + matches_string); |
| 121 for (size_t i = 1; i < matches.size(); i++) | 123 for (size_t i = 1; i < matches.size(); i++) |
| 122 result.AppendSubErr(Err(matches[i]->defined_from(), "Collision.")); | 124 result.AppendSubErr(Err(matches[i]->defined_from(), "Collision.")); |
| 123 return result; | 125 return result; |
| 124 } | 126 } |
| 125 | 127 |
| 126 } // namespace | 128 } // namespace |
| 127 | 129 |
| 128 NinjaBuildWriter::NinjaBuildWriter( | 130 NinjaBuildWriter::NinjaBuildWriter( |
| 129 const BuildSettings* build_settings, | 131 const BuildSettings* build_settings, |
| 130 const std::vector<const Settings*>& all_settings, | 132 const std::map<const Settings*, const Toolchain*>& used_toolchains, |
| 131 const Toolchain* default_toolchain, | 133 const Toolchain* default_toolchain, |
| 132 const std::vector<const Target*>& default_toolchain_targets, | 134 const std::vector<const Target*>& default_toolchain_targets, |
| 133 const std::vector<const Pool*>& all_pools, | |
| 134 std::ostream& out, | 135 std::ostream& out, |
| 135 std::ostream& dep_out) | 136 std::ostream& dep_out) |
| 136 : build_settings_(build_settings), | 137 : build_settings_(build_settings), |
| 137 all_settings_(all_settings), | 138 used_toolchains_(used_toolchains), |
| 138 default_toolchain_(default_toolchain), | 139 default_toolchain_(default_toolchain), |
| 139 default_toolchain_targets_(default_toolchain_targets), | 140 default_toolchain_targets_(default_toolchain_targets), |
| 140 all_pools_(all_pools), | |
| 141 out_(out), | 141 out_(out), |
| 142 dep_out_(dep_out), | 142 dep_out_(dep_out), |
| 143 path_output_(build_settings->build_dir(), | 143 path_output_(build_settings->build_dir(), |
| 144 build_settings->root_path_utf8(), | 144 build_settings->root_path_utf8(), |
| 145 ESCAPE_NINJA) {} | 145 ESCAPE_NINJA) {} |
| 146 | 146 |
| 147 NinjaBuildWriter::~NinjaBuildWriter() { | 147 NinjaBuildWriter::~NinjaBuildWriter() { |
| 148 } | 148 } |
| 149 | 149 |
| 150 bool NinjaBuildWriter::Run(Err* err) { | 150 bool NinjaBuildWriter::Run(Err* err) { |
| 151 WriteNinjaRules(); | 151 WriteNinjaRules(); |
| 152 WriteAllPools(); | 152 WriteAllPools(); |
| 153 WriteSubninjas(); | 153 WriteSubninjas(); |
| 154 return WritePhonyAndAllRules(err); | 154 return WritePhonyAndAllRules(err); |
| 155 } | 155 } |
| 156 | 156 |
| 157 // static | 157 // static |
| 158 bool NinjaBuildWriter::RunAndWriteFile( | 158 bool NinjaBuildWriter::RunAndWriteFile( |
| 159 const BuildSettings* build_settings, | 159 const BuildSettings* build_settings, |
| 160 const std::vector<const Settings*>& all_settings, | 160 const Builder& builder, |
| 161 const Toolchain* default_toolchain, | |
| 162 const std::vector<const Target*>& default_toolchain_targets, | |
| 163 const std::vector<const Pool*>& all_pools, | |
| 164 Err* err) { | 161 Err* err) { |
| 165 ScopedTrace trace(TraceItem::TRACE_FILE_WRITE, "build.ninja"); | 162 ScopedTrace trace(TraceItem::TRACE_FILE_WRITE, "build.ninja"); |
| 166 | 163 |
| 164 std::vector<const Target*> all_targets = builder.GetAllResolvedTargets(); |
| 165 std::map<const Settings*, const Toolchain*> used_toolchains; |
| 166 |
| 167 // Find the default toolchain info. |
| 168 Label default_toolchain_label = builder.loader()->GetDefaultToolchain(); |
| 169 const Settings* default_toolchain_settings = |
| 170 builder.loader()->GetToolchainSettings(default_toolchain_label); |
| 171 const Toolchain* default_toolchain = |
| 172 builder.GetToolchain(default_toolchain_label); |
| 173 |
| 174 // Most targets will be in the default toolchain. Add it at the beginning and |
| 175 // skip adding it to the list every time in the loop. |
| 176 used_toolchains[default_toolchain_settings] = default_toolchain; |
| 177 |
| 178 std::vector<const Target*> default_toolchain_targets; |
| 179 default_toolchain_targets.reserve(all_targets.size()); |
| 180 for (const Target* target : all_targets) { |
| 181 if (target->settings() == default_toolchain_settings) { |
| 182 default_toolchain_targets.push_back(target); |
| 183 // The default toolchain will already have been added to the used |
| 184 // settings array. |
| 185 } else if (used_toolchains.find(target->settings()) == |
| 186 used_toolchains.end()) { |
| 187 used_toolchains[target->settings()] = |
| 188 builder.GetToolchain(target->settings()->toolchain_label()); |
| 189 } |
| 190 } |
| 191 |
| 167 std::stringstream file; | 192 std::stringstream file; |
| 168 std::stringstream depfile; | 193 std::stringstream depfile; |
| 169 NinjaBuildWriter gen(build_settings, all_settings, default_toolchain, | 194 NinjaBuildWriter gen(build_settings, used_toolchains, default_toolchain, |
| 170 default_toolchain_targets, all_pools, file, depfile); | 195 default_toolchain_targets, file, depfile); |
| 171 if (!gen.Run(err)) | 196 if (!gen.Run(err)) |
| 172 return false; | 197 return false; |
| 173 | 198 |
| 174 // Unconditionally write the build.ninja. Ninja's build-out-of-date checking | 199 // Unconditionally write the build.ninja. Ninja's build-out-of-date checking |
| 175 // will re-run GN when any build input is newer than build.ninja, so any time | 200 // will re-run GN when any build input is newer than build.ninja, so any time |
| 176 // the build is updated, build.ninja's timestamp needs to updated also, even | 201 // the build is updated, build.ninja's timestamp needs to updated also, even |
| 177 // if the contents haven't been changed. | 202 // if the contents haven't been changed. |
| 178 base::FilePath ninja_file_name(build_settings->GetFullPath( | 203 base::FilePath ninja_file_name(build_settings->GetFullPath( |
| 179 SourceFile(build_settings->build_dir().value() + "build.ninja"))); | 204 SourceFile(build_settings->build_dir().value() + "build.ninja"))); |
| 180 base::CreateDirectory(ninja_file_name.DirName()); | 205 base::CreateDirectory(ninja_file_name.DirName()); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 dep_out_ << " " << FilePathToUTF8(other_file); | 251 dep_out_ << " " << FilePathToUTF8(other_file); |
| 227 | 252 |
| 228 out_ << std::endl; | 253 out_ << std::endl; |
| 229 } | 254 } |
| 230 | 255 |
| 231 void NinjaBuildWriter::WriteAllPools() { | 256 void NinjaBuildWriter::WriteAllPools() { |
| 232 out_ << "pool link_pool\n" | 257 out_ << "pool link_pool\n" |
| 233 << " depth = " << default_toolchain_->concurrent_links() << std::endl | 258 << " depth = " << default_toolchain_->concurrent_links() << std::endl |
| 234 << std::endl; | 259 << std::endl; |
| 235 | 260 |
| 236 for (const Pool* pool : all_pools_) { | 261 // Compute the pools referenced by all tools of all used toolchains. |
| 262 std::set<const Pool*> used_pools; |
| 263 for (const auto& pair : used_toolchains_) { |
| 264 for (int j = Toolchain::TYPE_NONE + 1; j < Toolchain::TYPE_NUMTYPES; j++) { |
| 265 Toolchain::ToolType tool_type = static_cast<Toolchain::ToolType>(j); |
| 266 const Tool* tool = pair.second->GetTool(tool_type); |
| 267 if (tool && tool->pool().ptr) |
| 268 used_pools.insert(tool->pool().ptr); |
| 269 } |
| 270 } |
| 271 |
| 272 for (const Pool* pool : used_pools) { |
| 237 std::string pool_name = pool->GetNinjaName(default_toolchain_->label()); | 273 std::string pool_name = pool->GetNinjaName(default_toolchain_->label()); |
| 238 out_ << "pool " << pool_name << std::endl | 274 out_ << "pool " << pool_name << std::endl |
| 239 << " depth = " << pool->depth() << std::endl | 275 << " depth = " << pool->depth() << std::endl |
| 240 << std::endl; | 276 << std::endl; |
| 241 } | 277 } |
| 242 } | 278 } |
| 243 | 279 |
| 244 void NinjaBuildWriter::WriteSubninjas() { | 280 void NinjaBuildWriter::WriteSubninjas() { |
| 245 for (auto* elem : all_settings_) { | 281 for (const auto& pair : used_toolchains_) { |
| 246 out_ << "subninja "; | 282 out_ << "subninja "; |
| 247 path_output_.WriteFile(out_, GetNinjaFileForToolchain(elem)); | 283 path_output_.WriteFile(out_, GetNinjaFileForToolchain(pair.first)); |
| 248 out_ << std::endl; | 284 out_ << std::endl; |
| 249 } | 285 } |
| 250 out_ << std::endl; | 286 out_ << std::endl; |
| 251 } | 287 } |
| 252 | 288 |
| 253 bool NinjaBuildWriter::WritePhonyAndAllRules(Err* err) { | 289 bool NinjaBuildWriter::WritePhonyAndAllRules(Err* err) { |
| 254 // Track rules as we generate them so we don't accidentally write a phony | 290 // Track rules as we generate them so we don't accidentally write a phony |
| 255 // rule that collides with something else. | 291 // rule that collides with something else. |
| 256 // GN internally generates an "all" target, so don't duplicate it. | 292 // GN internally generates an "all" target, so don't duplicate it. |
| 257 base::hash_set<std::string> written_rules; | 293 base::hash_set<std::string> written_rules; |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 EscapeOptions ninja_escape; | 450 EscapeOptions ninja_escape; |
| 415 ninja_escape.mode = ESCAPE_NINJA; | 451 ninja_escape.mode = ESCAPE_NINJA; |
| 416 | 452 |
| 417 // Escape for special chars Ninja will handle. | 453 // Escape for special chars Ninja will handle. |
| 418 std::string escaped = EscapeString(phony_name, ninja_escape, nullptr); | 454 std::string escaped = EscapeString(phony_name, ninja_escape, nullptr); |
| 419 | 455 |
| 420 out_ << "build " << escaped << ": phony "; | 456 out_ << "build " << escaped << ": phony "; |
| 421 path_output_.WriteFile(out_, target->dependency_output_file()); | 457 path_output_.WriteFile(out_, target->dependency_output_file()); |
| 422 out_ << std::endl; | 458 out_ << std::endl; |
| 423 } | 459 } |
| OLD | NEW |