| 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 <fstream> | 7 #include <fstream> |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 13 #include "base/process/process_handle.h" | 13 #include "base/process/process_handle.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 17 #include "tools/gn/build_settings.h" | 17 #include "tools/gn/build_settings.h" |
| 18 #include "tools/gn/escape.h" | 18 #include "tools/gn/escape.h" |
| 19 #include "tools/gn/filesystem_utils.h" | 19 #include "tools/gn/filesystem_utils.h" |
| 20 #include "tools/gn/input_file_manager.h" | 20 #include "tools/gn/input_file_manager.h" |
| 21 #include "tools/gn/ninja_utils.h" |
| 21 #include "tools/gn/scheduler.h" | 22 #include "tools/gn/scheduler.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 #if defined(OS_WIN) | 26 #if defined(OS_WIN) |
| 26 #include <windows.h> | 27 #include <windows.h> |
| 27 #endif | 28 #endif |
| 28 | 29 |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 const BuildSettings* build_settings, | 76 const BuildSettings* build_settings, |
| 76 const std::vector<const Settings*>& all_settings, | 77 const std::vector<const Settings*>& all_settings, |
| 77 const std::vector<const Target*>& default_toolchain_targets, | 78 const std::vector<const Target*>& default_toolchain_targets, |
| 78 std::ostream& out, | 79 std::ostream& out, |
| 79 std::ostream& dep_out) | 80 std::ostream& dep_out) |
| 80 : build_settings_(build_settings), | 81 : build_settings_(build_settings), |
| 81 all_settings_(all_settings), | 82 all_settings_(all_settings), |
| 82 default_toolchain_targets_(default_toolchain_targets), | 83 default_toolchain_targets_(default_toolchain_targets), |
| 83 out_(out), | 84 out_(out), |
| 84 dep_out_(dep_out), | 85 dep_out_(dep_out), |
| 85 path_output_(build_settings->build_dir(), ESCAPE_NINJA), | 86 path_output_(build_settings->build_dir(), ESCAPE_NINJA) { |
| 86 helper_(build_settings) { | |
| 87 } | 87 } |
| 88 | 88 |
| 89 NinjaBuildWriter::~NinjaBuildWriter() { | 89 NinjaBuildWriter::~NinjaBuildWriter() { |
| 90 } | 90 } |
| 91 | 91 |
| 92 void NinjaBuildWriter::Run() { | 92 void NinjaBuildWriter::Run() { |
| 93 WriteNinjaRules(); | 93 WriteNinjaRules(); |
| 94 WriteSubninjas(); | 94 WriteSubninjas(); |
| 95 WritePhonyAndAllRules(); | 95 WritePhonyAndAllRules(); |
| 96 } | 96 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 std::vector<base::FilePath> other_files = g_scheduler->GetGenDependencies(); | 148 std::vector<base::FilePath> other_files = g_scheduler->GetGenDependencies(); |
| 149 for (size_t i = 0; i < other_files.size(); i++) | 149 for (size_t i = 0; i < other_files.size(); i++) |
| 150 dep_out_ << " " << FilePathToUTF8(other_files[i]); | 150 dep_out_ << " " << FilePathToUTF8(other_files[i]); |
| 151 | 151 |
| 152 out_ << std::endl; | 152 out_ << std::endl; |
| 153 } | 153 } |
| 154 | 154 |
| 155 void NinjaBuildWriter::WriteSubninjas() { | 155 void NinjaBuildWriter::WriteSubninjas() { |
| 156 for (size_t i = 0; i < all_settings_.size(); i++) { | 156 for (size_t i = 0; i < all_settings_.size(); i++) { |
| 157 out_ << "subninja "; | 157 out_ << "subninja "; |
| 158 path_output_.WriteFile(out_, | 158 path_output_.WriteFile(out_, GetNinjaFileForToolchain(all_settings_[i])); |
| 159 helper_.GetNinjaFileForToolchain(all_settings_[i])); | |
| 160 out_ << std::endl; | 159 out_ << std::endl; |
| 161 } | 160 } |
| 162 out_ << std::endl; | 161 out_ << std::endl; |
| 163 } | 162 } |
| 164 | 163 |
| 165 void NinjaBuildWriter::WritePhonyAndAllRules() { | 164 void NinjaBuildWriter::WritePhonyAndAllRules() { |
| 166 std::string all_rules; | 165 std::string all_rules; |
| 167 | 166 |
| 168 // Write phony rules for all uniquely-named targets in the default toolchain. | 167 // Write phony rules for all uniquely-named targets in the default toolchain. |
| 169 // Don't do other toolchains or we'll get naming conflicts, and if the name | 168 // Don't do other toolchains or we'll get naming conflicts, and if the name |
| (...skipping 15 matching lines...) Expand all Loading... |
| 185 if (dir_string.size() == label.name().size() + 3 && // Size matches. | 184 if (dir_string.size() == label.name().size() + 3 && // Size matches. |
| 186 dir_string[0] == '/' && dir_string[1] == '/' && // "//" at beginning. | 185 dir_string[0] == '/' && dir_string[1] == '/' && // "//" at beginning. |
| 187 dir_string[dir_string.size() - 1] == '/' && // "/" at end. | 186 dir_string[dir_string.size() - 1] == '/' && // "/" at end. |
| 188 dir_string.compare(2, label.name().size(), label.name()) == 0) | 187 dir_string.compare(2, label.name().size(), label.name()) == 0) |
| 189 toplevel_targets.push_back(target); | 188 toplevel_targets.push_back(target); |
| 190 } | 189 } |
| 191 | 190 |
| 192 for (size_t i = 0; i < default_toolchain_targets_.size(); i++) { | 191 for (size_t i = 0; i < default_toolchain_targets_.size(); i++) { |
| 193 const Target* target = default_toolchain_targets_[i]; | 192 const Target* target = default_toolchain_targets_[i]; |
| 194 const Label& label = target->label(); | 193 const Label& label = target->label(); |
| 195 OutputFile target_file = helper_.GetTargetOutputFile(target); | 194 const OutputFile& target_file = target->dependency_output_file(); |
| 196 | 195 |
| 197 // Write the long name "foo/bar:baz" for the target "//foo/bar:baz". | 196 // Write the long name "foo/bar:baz" for the target "//foo/bar:baz". |
| 198 std::string long_name = label.GetUserVisibleName(false); | 197 std::string long_name = label.GetUserVisibleName(false); |
| 199 base::TrimString(long_name, "/", &long_name); | 198 base::TrimString(long_name, "/", &long_name); |
| 200 WritePhonyRule(target, target_file, long_name); | 199 WritePhonyRule(target, target_file, long_name); |
| 201 | 200 |
| 202 // Write the directory name with no target name if they match | 201 // Write the directory name with no target name if they match |
| 203 // (e.g. "//foo/bar:bar" -> "foo/bar"). | 202 // (e.g. "//foo/bar:bar" -> "foo/bar"). |
| 204 if (FindLastDirComponent(label.dir()) == label.name()) { | 203 if (FindLastDirComponent(label.dir()) == label.name()) { |
| 205 std::string medium_name = DirectoryWithNoLastSlash(label.dir()); | 204 std::string medium_name = DirectoryWithNoLastSlash(label.dir()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 217 if (!all_rules.empty()) | 216 if (!all_rules.empty()) |
| 218 all_rules.append(" $\n "); | 217 all_rules.append(" $\n "); |
| 219 all_rules.append(target_file.value()); | 218 all_rules.append(target_file.value()); |
| 220 } | 219 } |
| 221 | 220 |
| 222 // Pick up phony rules for the toplevel targets with non-unique names (which | 221 // Pick up phony rules for the toplevel targets with non-unique names (which |
| 223 // would have been skipped in the above loop). | 222 // would have been skipped in the above loop). |
| 224 for (size_t i = 0; i < toplevel_targets.size(); i++) { | 223 for (size_t i = 0; i < toplevel_targets.size(); i++) { |
| 225 if (small_name_count[toplevel_targets[i]->label().name()] > 1) { | 224 if (small_name_count[toplevel_targets[i]->label().name()] > 1) { |
| 226 const Target* target = toplevel_targets[i]; | 225 const Target* target = toplevel_targets[i]; |
| 227 WritePhonyRule(target, helper_.GetTargetOutputFile(target), | 226 WritePhonyRule(target, target->dependency_output_file(), |
| 228 target->label().name()); | 227 target->label().name()); |
| 229 } | 228 } |
| 230 } | 229 } |
| 231 | 230 |
| 232 if (!all_rules.empty()) { | 231 if (!all_rules.empty()) { |
| 233 out_ << "\nbuild all: phony " << all_rules << std::endl; | 232 out_ << "\nbuild all: phony " << all_rules << std::endl; |
| 234 out_ << "default all" << std::endl; | 233 out_ << "default all" << std::endl; |
| 235 } | 234 } |
| 236 } | 235 } |
| 237 | 236 |
| 238 void NinjaBuildWriter::WritePhonyRule(const Target* target, | 237 void NinjaBuildWriter::WritePhonyRule(const Target* target, |
| 239 const OutputFile& target_file, | 238 const OutputFile& target_file, |
| 240 const std::string& phony_name) { | 239 const std::string& phony_name) { |
| 241 if (target_file.value() == phony_name) | 240 if (target_file.value() == phony_name) |
| 242 return; // No need for a phony rule. | 241 return; // No need for a phony rule. |
| 243 | 242 |
| 244 EscapeOptions ninja_escape; | 243 EscapeOptions ninja_escape; |
| 245 ninja_escape.mode = ESCAPE_NINJA; | 244 ninja_escape.mode = ESCAPE_NINJA; |
| 246 | 245 |
| 247 // Escape for special chars Ninja will handle. | 246 // Escape for special chars Ninja will handle. |
| 248 std::string escaped = EscapeString(phony_name, ninja_escape, NULL); | 247 std::string escaped = EscapeString(phony_name, ninja_escape, NULL); |
| 249 | 248 |
| 250 out_ << "build " << escaped << ": phony "; | 249 out_ << "build " << escaped << ": phony "; |
| 251 path_output_.WriteFile(out_, target_file); | 250 path_output_.WriteFile(out_, target_file); |
| 252 out_ << std::endl; | 251 out_ << std::endl; |
| 253 } | 252 } |
| OLD | NEW |