1 // Copyright 2019 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef TOOLS_GN_NINJA_C_BINARY_TARGET_WRITER_H_ 6 #define TOOLS_GN_NINJA_C_BINARY_TARGET_WRITER_H_ 7 8 #include "gn/config_values.h" 9 #include "gn/ninja_binary_target_writer.h" 10 #include "gn/toolchain.h" 11 #include "gn/unique_vector.h" 12 13 struct EscapeOptions; 14 struct ModuleDep; 15 16 // Writes a .ninja file for a binary target type (an executable, a shared 17 // library, or a static library). 18 class NinjaCBinaryTargetWriter : public NinjaBinaryTargetWriter { 19 public: 20 NinjaCBinaryTargetWriter(const Target* target, std::ostream& out); 21 ~NinjaCBinaryTargetWriter() override; 22 23 void Run() override; 24 25 private: 26 using OutputFileSet = std::set<OutputFile>; 27 28 // Writes all flags for the compiler: includes, defines, cflags, etc. 29 void WriteCompilerVars(const std::vector<ModuleDep>& module_dep_info); 30 31 // Write module_deps or module_deps_no_self flags for clang modulemaps. 32 void WriteModuleDepsSubstitution( 33 const Substitution* substitution, 34 const std::vector<ModuleDep>& module_dep_info, 35 bool include_self); 36 37 // Writes build lines required for precompiled headers. Any generated 38 // object files will be appended to the |object_files|. Any generated 39 // non-object files (for instance, .gch files from a GCC toolchain, are 40 // appended to |other_files|). 41 // 42 // input_deps is the stamp file collecting the dependencies required before 43 // compiling this target. It will be empty if there are no input deps. 44 void WritePCHCommands(const std::vector<OutputFile>& input_deps, 45 const std::vector<OutputFile>& order_only_deps, 46 std::vector<OutputFile>* object_files, 47 std::vector<OutputFile>* other_files); 48 49 // Writes a .pch compile build line for a language type. 50 void WritePCHCommand(const Substitution* flag_type, 51 const char* tool_name, 52 CTool::PrecompiledHeaderType header_type, 53 const std::vector<OutputFile>& input_deps, 54 const std::vector<OutputFile>& order_only_deps, 55 std::vector<OutputFile>* object_files, 56 std::vector<OutputFile>* other_files); 57 58 void WriteGCCPCHCommand(const Substitution* flag_type, 59 const char* tool_name, 60 const std::vector<OutputFile>& input_deps, 61 const std::vector<OutputFile>& order_only_deps, 62 std::vector<OutputFile>* gch_files); 63 64 void WriteWindowsPCHCommand(const Substitution* flag_type, 65 const char* tool_name, 66 const std::vector<OutputFile>& input_deps, 67 const std::vector<OutputFile>& order_only_deps, 68 std::vector<OutputFile>* object_files); 69 70 // pch_deps are additional dependencies to run before the rule. They are 71 // expected to abide by the naming conventions specified by GetPCHOutputFiles. 72 // 73 // order_only_dep are the dependencies that must be run before doing any 74 // compiles. 75 // 76 // The files produced by the compiler will be added to two output vectors. 77 void WriteSources(const std::vector<OutputFile>& pch_deps, 78 const std::vector<OutputFile>& input_deps, 79 const std::vector<OutputFile>& order_only_deps, 80 const std::vector<ModuleDep>& module_dep_info, 81 std::vector<OutputFile>* object_files, 82 std::vector<SourceFile>* other_files); 83 void WriteSwiftSources(const std::vector<OutputFile>& input_deps, 84 const std::vector<OutputFile>& order_only_deps, 85 std::vector<OutputFile>* object_files); 86 87 void WriteLinkerStuff(const std::vector<OutputFile>& object_files, 88 const std::vector<SourceFile>& other_files, 89 const std::vector<OutputFile>& input_deps); 90 void WriteOutputSubstitutions(); 91 void WriteLibsList(const std::string& label, 92 const std::vector<OutputFile>& libs); 93 94 // Writes the implicit dependencies for the link or stamp line. This is 95 // the "||" and everything following it on the ninja line. 96 // 97 // The order-only dependencies are the non-linkable deps passed in as an 98 // argument, plus the data file dependencies in the target. 99 void WriteOrderOnlyDependencies( 100 const UniqueVector<const Target*>& non_linkable_deps); 101 102 // Checks for duplicates in the given list of output files. If any duplicates 103 // are found, throws an error and return false. 104 bool CheckForDuplicateObjectFiles(const std::vector<OutputFile>& files) const; 105 106 const CTool* tool_; 107 108 NinjaCBinaryTargetWriter(const NinjaCBinaryTargetWriter&) = delete; 109 NinjaCBinaryTargetWriter& operator=(const NinjaCBinaryTargetWriter&) = delete; 110 }; 111 112 #endif // TOOLS_GN_NINJA_C_BINARY_TARGET_WRITER_H_ 113