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 // Writes the stamp line for a source set. These are not linked. 46 void WriteSourceSetStamp(const std::vector<OutputFile>& object_files); 47 48 // Gets all target dependencies and classifies them, as well as accumulates 49 // object files from source sets we need to link. 50 ClassifiedDeps GetClassifiedDeps() const; 51 52 // Classifies the dependency as linkable or nonlinkable with the current 53 // target, adding it to the appropriate vector of |classified_deps|. If the 54 // dependency is a source set we should link in, the source set's object 55 // files will be appended to |classified_deps.extra_object_files|. 56 void ClassifyDependency(const Target* dep, 57 ClassifiedDeps* classified_deps) const; 58 59 OutputFile WriteStampAndGetDep(const UniqueVector<const SourceFile*>& files, 60 const std::string& stamp_ext) const; 61 62 void WriteCompilerBuildLine(const std::vector<SourceFile>& sources, 63 const std::vector<OutputFile>& extra_deps, 64 const std::vector<OutputFile>& order_only_deps, 65 const char* tool_name, 66 const std::vector<OutputFile>& outputs, 67 bool can_write_source_info = true); 68 69 void WriteLinkerFlags(std::ostream& out, 70 const Tool* tool, 71 const SourceFile* optional_def_file); 72 void WriteCustomLinkerFlags(std::ostream& out, const Tool* tool); 73 void WriteLibrarySearchPath(std::ostream& out, const Tool* tool); 74 void WriteLibs(std::ostream& out, const Tool* tool); 75 void WriteFrameworks(std::ostream& out, const Tool* tool); 76 void WriteSwiftModules(std::ostream& out, 77 const Tool* tool, 78 const std::vector<OutputFile>& swiftmodules); 79 80 void AddSourceSetFiles(const Target* source_set, 81 UniqueVector<OutputFile>* obj_files) const; 82 83 // Cached version of the prefix used for rule types for this toolchain. 84 std::string rule_prefix_; 85 86 private: 87 NinjaBinaryTargetWriter(const NinjaBinaryTargetWriter&) = delete; 88 NinjaBinaryTargetWriter& operator=(const NinjaBinaryTargetWriter&) = delete; 89 }; 90 91 #endif // TOOLS_GN_NINJA_BINARY_TARGET_WRITER_H_ 92