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 3 // found in the LICENSE file. 4 5 #ifndef TOOLS_GN_NINJA_BINARY_TARGET_WRITER_H_ 6 #define TOOLS_GN_NINJA_BINARY_TARGET_WRITER_H_ 7 8 #include "gn/c_tool.h" 9 #include "gn/config_values.h" 10 #include "gn/ninja_target_writer.h" 11 #include "gn/toolchain.h" 12 #include "gn/unique_vector.h" 13 14 struct EscapeOptions; 15 16 // Writes a .ninja file for a binary target type (an executable, a shared 17 // library, or a static library). 18 class NinjaBinaryTargetWriter : public NinjaTargetWriter { 19 public: 20 NinjaBinaryTargetWriter(const Target* target, std::ostream& out); 21 ~NinjaBinaryTargetWriter() override; 22 23 void Run() override; 24 25 protected: 26 // Structure used to return the classified deps from |GetDeps| method. 27 struct ClassifiedDeps { 28 UniqueVector<OutputFile> extra_object_files; 29 UniqueVector<const Target*> linkable_deps; 30 UniqueVector<const Target*> non_linkable_deps; 31 UniqueVector<const Target*> framework_deps; 32 UniqueVector<const Target*> swiftmodule_deps; 33 }; 34 35 // Writes to the output stream a stamp rule for inputs, and 36 // returns the file to be appended to source rules that encodes the 37 // implicit dependencies for the current target. 38 // If num_stamp_uses is small, this might return all input dependencies 39 // directly, without writing a stamp file. 40 // If there are no implicit dependencies and no extra target dependencies 41 // are passed in, this returns an empty vector. 42 std::vector<OutputFile> WriteInputsStampAndGetDep( 43 size_t num_stamp_uses) const; 44 45 // Gets all target dependencies and classifies them, as well as accumulates 46 // object files from source sets we need to link. 47 ClassifiedDeps GetClassifiedDeps() const; 48 49 // Classifies the dependency as linkable or nonlinkable with the current 50 // target, adding it to the appropriate vector of |classified_deps|. If the 51 // dependency is a source set we should link in, the source set's object 52 // files will be appended to |classified_deps.extra_object_files|. 53 void ClassifyDependency(const Target* dep, 54 ClassifiedDeps* classified_deps) const; 55 56 void WriteCompilerBuildLine(const std::vector<SourceFile>& sources, 57 const std::vector<OutputFile>& extra_deps, 58 const std::vector<OutputFile>& order_only_deps, 59 const char* tool_name, 60 const std::vector<OutputFile>& outputs, 61 bool can_write_source_info = true, 62 bool restat_output_allowed = false); 63 64 void WriteLinkerFlags(std::ostream& out, 65 const Tool* tool, 66 const SourceFile* optional_def_file); 67 void WriteCustomLinkerFlags(std::ostream& out, const Tool* tool); 68 void WriteLibrarySearchPath(std::ostream& out, const Tool* tool); 69 void WriteLibs(std::ostream& out, const Tool* tool); 70 void WriteFrameworks(std::ostream& out, const Tool* tool); 71 void WriteSwiftModules(std::ostream& out, 72 const Tool* tool, 73 const std::vector<OutputFile>& swiftmodules); 74 void WritePool(std::ostream& out); 75 76 void AddSourceSetFiles(const Target* source_set, 77 UniqueVector<OutputFile>* obj_files) const; 78 79 // Cached version of the prefix used for rule types for this toolchain. 80 std::string rule_prefix_; 81 82 private: 83 NinjaBinaryTargetWriter(const NinjaBinaryTargetWriter&) = delete; 84 NinjaBinaryTargetWriter& operator=(const NinjaBinaryTargetWriter&) = delete; 85 }; 86 87 #endif // TOOLS_GN_NINJA_BINARY_TARGET_WRITER_H_ 88