Chromium Code Reviews| Index: tools/gn/ninja_binary_target_writer.cc | 
| diff --git a/tools/gn/ninja_binary_target_writer.cc b/tools/gn/ninja_binary_target_writer.cc | 
| index 971f4d87f439bbb3ff4941a263a0603069dab438..3c7548306692a0b5c4d07e3845a4c6853aa53018 100644 | 
| --- a/tools/gn/ninja_binary_target_writer.cc | 
| +++ b/tools/gn/ninja_binary_target_writer.cc | 
| @@ -132,8 +132,8 @@ bool GetOutputFilesForSource(const Target* target, | 
| return !outputs->empty(); | 
| } | 
| -// Returns the language-specific prefix/suffix for precomiled header files. | 
| -const char* GetPCHLangForToolType(Toolchain::ToolType type) { | 
| +// Returns the language-specific suffix for precompiled header files. | 
| +const char* GetPCHLangSuffixForToolType(Toolchain::ToolType type) { | 
| switch (type) { | 
| case Toolchain::TYPE_CC: | 
| return "c"; | 
| @@ -144,16 +144,61 @@ const char* GetPCHLangForToolType(Toolchain::ToolType type) { | 
| case Toolchain::TYPE_OBJCXX: | 
| return "mm"; | 
| default: | 
| - NOTREACHED() << "Not a valid PCH tool type type"; | 
| + NOTREACHED() << "Not a valid PCH tool type: " << type; | 
| return ""; | 
| } | 
| } | 
| -// Returns the object files for the precompiled header of the given type (flag | 
| -// type and tool type must match). | 
| -void GetWindowsPCHObjectFiles(const Target* target, | 
| - Toolchain::ToolType tool_type, | 
| - std::vector<OutputFile>* outputs) { | 
| +// Returns the appropriate extension for precompiled header outputs. | 
| +std::string GetPCHOutputExtension(Toolchain::ToolType tool_type, | 
| + Tool::PrecompiledHeaderType header_type) { | 
| + const char* lang_suffix = GetPCHLangSuffixForToolType(tool_type); | 
| + std::string result = "."; | 
| + if (header_type == Tool::PCH_MSVC) { | 
| + // For MSVC, annotate the obj files with the language type. For example: | 
| + // obj/foo/target_name.precompile.o -> | 
| + // obj/foo/target_name.precompile.cc.o | 
| + result += lang_suffix; | 
| 
 
brettw
2015/09/16 22:41:13
This function is kind of confusing because it retu
 
Bons
2015/09/17 16:53:39
Done.
 
 | 
| + result += ".o"; | 
| + } else if (header_type == Tool::PCH_GCC) { | 
| + // For GCC, the output name must have a .gch suffix and be annotated with | 
| + // the language type. For example: | 
| + // obj/foo/target_name.header.h -> | 
| + // obj/foo/target_name.header.h-cc.gch | 
| + // In order for the compiler to pick it up, the output name (minus the .gch | 
| + // suffix MUST match whatever is passed to the -include flag). | 
| + result += "h-"; | 
| + result += lang_suffix; | 
| + result += ".gch"; | 
| + } else { | 
| + NOTREACHED() << "No outputs for no (or unknown) PCH type."; | 
| + } | 
| + return result; | 
| +} | 
| + | 
| +// Returns the language-specific lang recognized by gcc’s -x flag for | 
| +// precompiled header files. | 
| +const char* GetPCHLangForToolType(Toolchain::ToolType type) { | 
| + switch (type) { | 
| + case Toolchain::TYPE_CC: | 
| + return "c-header"; | 
| + case Toolchain::TYPE_CXX: | 
| + return "c++-header"; | 
| + case Toolchain::TYPE_OBJC: | 
| + return "objective-c-header"; | 
| + case Toolchain::TYPE_OBJCXX: | 
| + return "objective-c++-header"; | 
| + default: | 
| + NOTREACHED() << "Not a valid PCH tool type: " << type; | 
| + return ""; | 
| + } | 
| +} | 
| + | 
| +// Returns the object or gch files for the precompiled header of the given type | 
| +// (flag type and tool type must match). | 
| +void GetPCHOutputFiles(const Target* target, | 
| + Toolchain::ToolType tool_type, | 
| + std::vector<OutputFile>* outputs) { | 
| outputs->clear(); | 
| // Compute the tool. This must use the tool type passed in rather than the | 
| 
 
brettw
2015/09/16 22:41:14
If you take the above advice, this first part of t
 
Bons
2015/09/17 16:53:39
Done.
 
 | 
| @@ -171,20 +216,20 @@ void GetWindowsPCHObjectFiles(const Target* target, | 
| if (outputs->size() > 1) | 
| outputs->resize(1); // Only link the first output from the compiler tool. | 
| - // Need to annotate the obj files with the language type. For example: | 
| - // obj/foo/target_name.precompile.obj -> | 
| - // obj/foo/target_name.precompile.cc.obj | 
| - const char* lang_suffix = GetPCHLangForToolType(tool_type); | 
| std::string& output_value = (*outputs)[0].value(); | 
| size_t extension_offset = FindExtensionOffset(output_value); | 
| if (extension_offset == std::string::npos) { | 
| - NOTREACHED() << "No extension found"; | 
| - } else { | 
| - DCHECK(extension_offset >= 1); | 
| - DCHECK(output_value[extension_offset - 1] == '.'); | 
| - output_value.insert(extension_offset - 1, "."); | 
| - output_value.insert(extension_offset, lang_suffix); | 
| + // No extension found. Bail early. | 
| + return; | 
| } | 
| + DCHECK(extension_offset >= 1); | 
| + DCHECK(output_value[extension_offset - 1] == '.'); | 
| + | 
| + std::string output_extension = GetPCHOutputExtension(tool_type, | 
| + tool->precompiled_header_type()); | 
| + output_value.replace(extension_offset - 1, | 
| + std::string::npos, | 
| + output_extension); | 
| } | 
| // Appends the object files generated by the given source set to the given | 
| @@ -204,24 +249,37 @@ void AddSourceSetObjectFiles(const Target* source_set, | 
| used_types.Set(GetSourceFileType(source)); | 
| } | 
| - // Precompiled header object files. | 
| + // Add MSVC precompiled header object files. GCC .gch files are not object | 
| + // files so they are omitted. | 
| if (source_set->config_values().has_precompiled_headers()) { | 
| if (used_types.Get(SOURCE_C)) { | 
| - GetWindowsPCHObjectFiles(source_set, Toolchain::TYPE_CC, &tool_outputs); | 
| - obj_files->Append(tool_outputs.begin(), tool_outputs.end()); | 
| + const Tool* tool = source_set->toolchain()->GetTool(Toolchain::TYPE_CC); | 
| + if (tool && tool->precompiled_header_type() == Tool::PCH_MSVC) { | 
| + GetPCHOutputFiles(source_set, Toolchain::TYPE_CC, &tool_outputs); | 
| + obj_files->Append(tool_outputs.begin(), tool_outputs.end()); | 
| + } | 
| } | 
| if (used_types.Get(SOURCE_CPP)) { | 
| - GetWindowsPCHObjectFiles(source_set, Toolchain::TYPE_CXX, &tool_outputs); | 
| - obj_files->Append(tool_outputs.begin(), tool_outputs.end()); | 
| + const Tool* tool = source_set->toolchain()->GetTool(Toolchain::TYPE_CXX); | 
| + if (tool && tool->precompiled_header_type() == Tool::PCH_MSVC) { | 
| + GetPCHOutputFiles(source_set, Toolchain::TYPE_CXX, &tool_outputs); | 
| + obj_files->Append(tool_outputs.begin(), tool_outputs.end()); | 
| + } | 
| } | 
| if (used_types.Get(SOURCE_M)) { | 
| - GetWindowsPCHObjectFiles(source_set, Toolchain::TYPE_OBJC, &tool_outputs); | 
| - obj_files->Append(tool_outputs.begin(), tool_outputs.end()); | 
| + const Tool* tool = source_set->toolchain()->GetTool(Toolchain::TYPE_OBJC); | 
| + if (tool && tool->precompiled_header_type() == Tool::PCH_MSVC) { | 
| + GetPCHOutputFiles(source_set, Toolchain::TYPE_OBJC, &tool_outputs); | 
| + obj_files->Append(tool_outputs.begin(), tool_outputs.end()); | 
| + } | 
| } | 
| if (used_types.Get(SOURCE_MM)) { | 
| - GetWindowsPCHObjectFiles(source_set, Toolchain::TYPE_OBJCXX, | 
| - &tool_outputs); | 
| - obj_files->Append(tool_outputs.begin(), tool_outputs.end()); | 
| + const Tool* tool = source_set->toolchain()->GetTool( | 
| + Toolchain::TYPE_OBJCXX); | 
| + if (tool && tool->precompiled_header_type() == Tool::PCH_MSVC) { | 
| + GetPCHOutputFiles(source_set, Toolchain::TYPE_OBJCXX, &tool_outputs); | 
| + obj_files->Append(tool_outputs.begin(), tool_outputs.end()); | 
| + } | 
| } | 
| } | 
| } | 
| @@ -251,10 +309,10 @@ void NinjaBinaryTargetWriter::Run() { | 
| // but changes in the inputs deps won't cause the file to be recompiled. | 
| // | 
| // This is important to prevent changes in unrelated actions that are | 
| - // upstream of this target from causing everything to be recompiled | 
| + // upstream of this target from causing everything to be recompiled. | 
| // | 
| // Why can we get away with this rather than using implicit deps ("|", which | 
| - // will force rebuilds when the inputs change)? For source code, the | 
| + // will force rebuilds when the inputs change)? For source code, the | 
| // computed dependencies of all headers will be computed by the compiler, | 
| // which will cause source rebuilds if any "real" upstream dependencies | 
| // change. | 
| @@ -272,30 +330,41 @@ void NinjaBinaryTargetWriter::Run() { | 
| OutputFile order_only_dep = | 
| WriteInputDepsStampAndGetDep(std::vector<const Target*>()); | 
| + // For GCC builds, the .gch files are not object files, but still need to be | 
| + // added as explicit dependencies below. The .gch output files are placed in | 
| + // |pch_other_files|. This is to prevent linking against them. | 
| std::vector<OutputFile> pch_obj_files; | 
| - WritePrecompiledHeaderCommands(used_types, order_only_dep, &pch_obj_files); | 
| + std::vector<OutputFile> pch_other_files; | 
| + if (!WritePCHCommands(used_types, order_only_dep, | 
| + &pch_obj_files, &pch_other_files)) { | 
| + return; | 
| + } | 
| + std::vector<OutputFile>* pch_files = !pch_obj_files.empty() ? | 
| + &pch_obj_files : &pch_other_files; | 
| - // Treat all precompiled object files as explicit dependencies of all | 
| + // Treat all pch output files as explicit dependencies of all | 
| // compiles. Some notes: | 
| // | 
| - // - Technically only the language-specific one is required for any specific | 
| - // compile, but that's more difficult to express and the additional logic | 
| - // doesn't buy much reduced parallelism. Just list them all (there's | 
| - // usually only one anyway). | 
| + // - Only the language-specific one is required for any specific compile, but | 
| + // that's more difficult to express and the additional logic doesn't buy | 
| + // much reduced parallelism. Just list them all (there's usually only one | 
| + // anyway). | 
| // | 
| - // - Technically the .pch file is the input to the compile, not the | 
| + // - On Windows, the .pch file is the input to the compile, not the | 
| // precompiled header's corresponding object file that we're using here. | 
| // But Ninja's depslog doesn't support multiple outputs from the | 
| // precompiled header compile step (it outputs both the .pch file and a | 
| // corresponding .obj file). So we consistently list the .obj file and the | 
| // .pch file we really need comes along with it. | 
| + // | 
| + // - GCC .gch files are not object files, therefore they are not added to the | 
| + // object file list. | 
| std::vector<OutputFile> obj_files; | 
| std::vector<SourceFile> other_files; | 
| - WriteSources(pch_obj_files, order_only_dep, &obj_files, &other_files); | 
| + WriteSources(*pch_files, order_only_dep, &obj_files, &other_files); | 
| - // Also link all pch object files. | 
| + // Link all MSVC pch object files. The vector will be empty on GCC toolchains. | 
| obj_files.insert(obj_files.end(), pch_obj_files.begin(), pch_obj_files.end()); | 
| - | 
| if (!CheckForDuplicateObjectFiles(obj_files)) | 
| return; | 
| @@ -343,11 +412,7 @@ void NinjaBinaryTargetWriter::WriteCompilerVars( | 
| bool has_precompiled_headers = | 
| target_->config_values().has_precompiled_headers(); | 
| - // Some toolchains pass cflags to the assembler since it's the same command, | 
| - // and cflags_c might also be sent to the objective C compiler. | 
| - // | 
| - // TODO(brettw) remove the SOURCE_M from the CFLAGS_C writing once the Chrome | 
| - // Mac build is updated not to pass cflags_c to .m files. | 
| + // Some toolchains pass cflags to the assembler since it's the same command. | 
| EscapeOptions opts = GetFlagOptions(); | 
| if (used_types.Get(SOURCE_C) || used_types.Get(SOURCE_CPP) || | 
| used_types.Get(SOURCE_M) || used_types.Get(SOURCE_MM) || | 
| @@ -355,8 +420,8 @@ void NinjaBinaryTargetWriter::WriteCompilerVars( | 
| WriteOneFlag(SUBSTITUTION_CFLAGS, false, Toolchain::TYPE_NONE, | 
| &ConfigValues::cflags, opts); | 
| } | 
| - if (used_types.Get(SOURCE_C) || used_types.Get(SOURCE_M) || | 
| - used_types.Get(SOURCE_S) || used_types.Get(SOURCE_ASM)) { | 
| + if (used_types.Get(SOURCE_C) || used_types.Get(SOURCE_S) || | 
| + used_types.Get(SOURCE_ASM)) { | 
| WriteOneFlag(SUBSTITUTION_CFLAGS_C, has_precompiled_headers, | 
| Toolchain::TYPE_CC, &ConfigValues::cflags_c, opts); | 
| } | 
| @@ -387,6 +452,8 @@ void NinjaBinaryTargetWriter::WriteOneFlag( | 
| out_ << kSubstitutionNinjaNames[subst_enum] << " ="; | 
| + bool flag_values_written = false; | 
| + | 
| if (has_precompiled_headers) { | 
| const Tool* tool = target_->toolchain()->GetTool(tool_type); | 
| if (tool && tool->precompiled_header_type() == Tool::PCH_MSVC) { | 
| @@ -397,80 +464,207 @@ void NinjaBinaryTargetWriter::WriteOneFlag( | 
| // Enables precompiled headers and names the .h file. It's a string | 
| // rather than a file name (so no need to rebase or use path_output_). | 
| out_ << " /Yu" << target_->config_values().precompiled_header(); | 
| + } else if (tool && tool->precompiled_header_type() == Tool::PCH_GCC) { | 
| + // The targets to build the .gch files should omit the -include flag | 
| + // below. To accomplish this, each substitution flag is overwritten in the | 
| + // target rule and these values are repeated. The -include flag is omitted | 
| + // in place of the required -x <header lang> flag for .gch targets. | 
| + RecursiveTargetConfigStringsToStream(target_, getter, | 
| + flag_escape_options, out_); | 
| + flag_values_written = true; | 
| + | 
| + // Compute the gch file (it will be language-specific). | 
| + std::vector<OutputFile> outputs; | 
| + GetPCHOutputFiles(target_, tool_type, &outputs); | 
| + if (outputs.empty()) | 
| + return; | 
| 
 
brettw
2015/09/16 22:41:14
This flow here is a bit weird. This function goes
 
Bons
2015/09/17 16:53:39
Done.
 
 | 
| + | 
| + // Trim the .gch suffix for the -include flag. | 
| + // e.g. for gch file foo/bar/target.precompiled.h.gch: | 
| + // -include foo/bar/target.precompiled.h | 
| + std::string pch_file = outputs[0].value(); | 
| + pch_file.erase(pch_file.length() - 4); | 
| + out_ << " -include " << pch_file; | 
| } | 
| } | 
| - | 
| - RecursiveTargetConfigStringsToStream(target_, getter, | 
| - flag_escape_options, out_); | 
| + if (!flag_values_written) { | 
| + RecursiveTargetConfigStringsToStream(target_, getter, | 
| + flag_escape_options, out_); | 
| + } | 
| out_ << std::endl; | 
| } | 
| -void NinjaBinaryTargetWriter::WritePrecompiledHeaderCommands( | 
| +bool NinjaBinaryTargetWriter::WritePCHCommands( | 
| const SourceFileTypeSet& used_types, | 
| const OutputFile& order_only_dep, | 
| - std::vector<OutputFile>* object_files) { | 
| + std::vector<OutputFile>* object_files, | 
| + std::vector<OutputFile>* other_files) { | 
| if (!target_->config_values().has_precompiled_headers()) | 
| - return; | 
| + return true; | 
| const Tool* tool_c = target_->toolchain()->GetTool(Toolchain::TYPE_CC); | 
| if (tool_c && | 
| - tool_c->precompiled_header_type() == Tool::PCH_MSVC && | 
| + tool_c->precompiled_header_type() != Tool::PCH_NONE && | 
| used_types.Get(SOURCE_C)) { | 
| - WriteWindowsPCHCommand(SUBSTITUTION_CFLAGS_C, | 
| - Toolchain::TYPE_CC, | 
| - order_only_dep, object_files); | 
| + if (!WritePCHCommand(SUBSTITUTION_CFLAGS_C, | 
| + Toolchain::TYPE_CC, | 
| + tool_c->precompiled_header_type(), | 
| + order_only_dep, object_files, other_files)) { | 
| + return false; | 
| + } | 
| } | 
| const Tool* tool_cxx = target_->toolchain()->GetTool(Toolchain::TYPE_CXX); | 
| if (tool_cxx && | 
| - tool_cxx->precompiled_header_type() == Tool::PCH_MSVC && | 
| + tool_cxx->precompiled_header_type() != Tool::PCH_NONE && | 
| used_types.Get(SOURCE_CPP)) { | 
| - WriteWindowsPCHCommand(SUBSTITUTION_CFLAGS_CC, | 
| - Toolchain::TYPE_CXX, | 
| - order_only_dep, object_files); | 
| + if (!WritePCHCommand(SUBSTITUTION_CFLAGS_CC, | 
| + Toolchain::TYPE_CXX, | 
| + tool_cxx->precompiled_header_type(), | 
| + order_only_dep, object_files, other_files)) { | 
| + return false; | 
| + } | 
| } | 
| + | 
| + const Tool* tool_objc = target_->toolchain()->GetTool(Toolchain::TYPE_OBJC); | 
| + if (tool_objc && | 
| + tool_objc->precompiled_header_type() == Tool::PCH_GCC && | 
| + used_types.Get(SOURCE_M)) { | 
| + if (!WritePCHCommand(SUBSTITUTION_CFLAGS_OBJC, | 
| + Toolchain::TYPE_OBJC, | 
| + tool_objc->precompiled_header_type(), | 
| + order_only_dep, object_files, other_files)) { | 
| + return false; | 
| + } | 
| + } | 
| + | 
| + const Tool* tool_objcxx = | 
| + target_->toolchain()->GetTool(Toolchain::TYPE_OBJCXX); | 
| + if (tool_objcxx && | 
| + tool_objcxx->precompiled_header_type() == Tool::PCH_GCC && | 
| + used_types.Get(SOURCE_MM)) { | 
| + if (!WritePCHCommand(SUBSTITUTION_CFLAGS_OBJCC, | 
| + Toolchain::TYPE_OBJCXX, | 
| + tool_objcxx->precompiled_header_type(), | 
| + order_only_dep, object_files, other_files)) { | 
| + return false; | 
| + } | 
| + } | 
| + return true; | 
| } | 
| -void NinjaBinaryTargetWriter::WriteWindowsPCHCommand( | 
| +bool NinjaBinaryTargetWriter::WritePCHCommand( | 
| 
 
brettw
2015/09/16 22:41:13
I'm feeling like this function would be better spl
 
Bons
2015/09/17 16:53:39
Done.
 
 | 
| SubstitutionType flag_type, | 
| Toolchain::ToolType tool_type, | 
| + Tool::PrecompiledHeaderType header_type, | 
| const OutputFile& order_only_dep, | 
| - std::vector<OutputFile>* object_files) { | 
| - // Compute the object file (it will be language-specific). | 
| + std::vector<OutputFile>* object_files, | 
| + std::vector<OutputFile>* other_files) { | 
| + // With the GCC toolset, ensure the precompiled source and precompiled header | 
| + // are the same target. | 
| + if (header_type == Tool::PCH_GCC) { | 
| + SourceFile source = target_->config_values().precompiled_source(); | 
| + std::string header_str = target_->config_values().precompiled_header(); | 
| + header_str.insert(0, "//"); | 
| + SourceFile header = SourceFile(SourceFile::SWAP_IN, &header_str); | 
| + if (source != header) { | 
| + Err err( | 
| + target_->defined_from(), | 
| + "GCC precompiled source and header are different files", | 
| + "The target " + target_->label().GetUserVisibleName(false) + | 
| + "\nspecifies a precompiled source:\n " + | 
| + source.value() + "\n" | 
| + "that is different than the precompiled header:\n " + | 
| + header_str + "\n" | 
| + "\n" | 
| + "For GCC toolchains, the two must be equal, for instance:\n" | 
| + " precompiled_header = \"build/precompile.h\"" | 
| + " precompiled_source = \"//build/precompile.h\""); | 
| + g_scheduler->FailWithError(err); | 
| + return false; | 
| + } | 
| + } | 
| + | 
| + // Compute the pch output file (it will be language-specific). | 
| std::vector<OutputFile> outputs; | 
| - GetWindowsPCHObjectFiles(target_, tool_type, &outputs); | 
| + GetPCHOutputFiles(target_, tool_type, &outputs); | 
| if (outputs.empty()) | 
| - return; | 
| - object_files->insert(object_files->end(), outputs.begin(), outputs.end()); | 
| + return true; | 
| + switch (header_type) { | 
| + case Tool::PCH_MSVC: | 
| + object_files->insert(object_files->end(), outputs.begin(), outputs.end()); | 
| + break; | 
| + case Tool::PCH_GCC: | 
| + // .gch files are not object files. | 
| + other_files->insert(other_files->end(), outputs.begin(), outputs.end()); | 
| + break; | 
| + case Tool::PCH_NONE: | 
| + NOTREACHED() << "Cannot write a PCH command with no PCH header type"; | 
| + break; | 
| + } | 
| // Build line to compile the file. | 
| WriteCompilerBuildLine(target_->config_values().precompiled_source(), | 
| std::vector<OutputFile>(), order_only_dep, tool_type, | 
| outputs); | 
| - // This build line needs a custom language-specific flags value. It needs to | 
| - // include the switch to generate the .pch file in addition to the normal | 
| - // ones. Rule-specific variables are just indented underneath the rule line, | 
| - // and this defines the new one in terms of the old value. | 
| + // This build line needs a custom language-specific flags value. Rule-specific | 
| + // variables are just indented underneath the rule line. | 
| out_ << " " << kSubstitutionNinjaNames[flag_type] << " ="; | 
| - out_ << " ${" << kSubstitutionNinjaNames[flag_type] << "}"; | 
| - // Append the command to generate the .pch file. | 
| - out_ << " /Yc" << target_->config_values().precompiled_header(); | 
| + switch (header_type) { | 
| + case Tool::PCH_MSVC: | 
| + // Append the command to generate the .pch file. | 
| + // This adds the value to the existing flag instead of overwriting it. | 
| + out_ << " ${" << kSubstitutionNinjaNames[flag_type] << "}"; | 
| + out_ << " /Yc" << target_->config_values().precompiled_header(); | 
| + break; | 
| + case Tool::PCH_GCC: { | 
| + // Each substitution flag is overwritten in the target rule to replace the | 
| + // -include flag with the -x <header lang> flag required for .gch targets. | 
| + EscapeOptions opts = GetFlagOptions(); | 
| + if (header_type == Tool::PCH_GCC) { | 
| + if (tool_type == Toolchain::TYPE_CC) { | 
| + RecursiveTargetConfigStringsToStream(target_, | 
| + &ConfigValues::cflags_c, opts, out_); | 
| + } else if (tool_type == Toolchain::TYPE_CXX) { | 
| + RecursiveTargetConfigStringsToStream(target_, | 
| + &ConfigValues::cflags_cc, opts, out_); | 
| + } else if (tool_type == Toolchain::TYPE_OBJC) { | 
| + RecursiveTargetConfigStringsToStream(target_, | 
| + &ConfigValues::cflags_objc, opts, out_); | 
| + } else if (tool_type == Toolchain::TYPE_OBJCXX) { | 
| + RecursiveTargetConfigStringsToStream(target_, | 
| + &ConfigValues::cflags_objcc, opts, out_); | 
| + } | 
| + } | 
| + // Append the command to specify the language of the .gch file. | 
| + out_ << " -x " << GetPCHLangForToolType(tool_type); | 
| + break; | 
| + } | 
| + case Tool::PCH_NONE: | 
| + NOTREACHED() << "Cannot write a PCH command with no PCH header type"; | 
| + break; | 
| + } | 
| // Write two blank lines to help separate the PCH build lines from the | 
| // regular source build lines. | 
| out_ << std::endl << std::endl; | 
| + | 
| + return true; | 
| } | 
| void NinjaBinaryTargetWriter::WriteSources( | 
| - const std::vector<OutputFile>& extra_deps, | 
| + const std::vector<OutputFile>& pch_deps, | 
| const OutputFile& order_only_dep, | 
| std::vector<OutputFile>* object_files, | 
| std::vector<SourceFile>* other_files) { | 
| object_files->reserve(object_files->size() + target_->sources().size()); | 
| std::vector<OutputFile> tool_outputs; // Prevent reallocation in loop. | 
| + std::vector<OutputFile> deps; | 
| for (const auto& source : target_->sources()) { | 
| + // Clear the vector but maintain the max capacity to prevent reallocations. | 
| + deps.resize(0); | 
| Toolchain::ToolType tool_type = Toolchain::TYPE_NONE; | 
| if (!GetOutputFilesForSource(target_, source, &tool_type, &tool_outputs)) { | 
| if (GetSourceFileType(source) == SOURCE_DEF) | 
| @@ -479,7 +673,26 @@ void NinjaBinaryTargetWriter::WriteSources( | 
| } | 
| if (tool_type != Toolchain::TYPE_NONE) { | 
| - WriteCompilerBuildLine(source, extra_deps, order_only_dep, tool_type, | 
| + // Only include PCH deps that correspond to the tool type, for instance, | 
| + // do not specify target_name.precompile.cc.o (a CXX PCH file) as a dep | 
| + // for the output of a C tool type. | 
| + // | 
| + // This makes the assumption that pch_deps only contains pch output files | 
| + // with the naming scheme specified in GetPCHOutputExtension. | 
| + const Tool* tool = target_->toolchain()->GetTool(tool_type); | 
| + for (const auto& dep : pch_deps) { | 
| + const std::string& output_value = dep.value(); | 
| + std::string output_extension; | 
| + if (tool->precompiled_header_type() != Tool::PCH_NONE) { | 
| + output_extension = GetPCHOutputExtension(tool_type, | 
| + tool->precompiled_header_type()); | 
| + } | 
| + if (output_value.compare(output_value.size() - output_extension.size(), | 
| + output_extension.size(), output_extension) == 0) { | 
| + deps.push_back(dep); | 
| + } | 
| + } | 
| + WriteCompilerBuildLine(source, deps, order_only_dep, tool_type, | 
| tool_outputs); | 
| } | 
| @@ -788,11 +1001,11 @@ void NinjaBinaryTargetWriter::WriteOrderOnlyDependencies( | 
| OutputFile NinjaBinaryTargetWriter::GetWindowsPCHFile( | 
| Toolchain::ToolType tool_type) const { | 
| // Use "obj/{dir}/{target_name}_{lang}.pch" which ends up | 
| - // looking like "obj/chrome/browser/browser.cc.pch" | 
| + // looking like "obj/chrome/browser/browser_cc.pch" | 
| OutputFile ret = GetTargetOutputDirAsOutputFile(target_); | 
| ret.value().append(target_->label().name()); | 
| ret.value().push_back('_'); | 
| - ret.value().append(GetPCHLangForToolType(tool_type)); | 
| + ret.value().append(GetPCHLangSuffixForToolType(tool_type)); | 
| ret.value().append(".pch"); | 
| return ret; |