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_TOOLCHAIN_H_ 6 #define TOOLS_GN_TOOLCHAIN_H_ 7 8 #include "base/compiler_specific.h" 9 #include "base/logging.h" 10 #include "base/memory/scoped_ptr.h" 11 #include "base/strings/string_piece.h" 12 #include "tools/gn/item.h" 13 #include "tools/gn/label_ptr.h" 14 #include "tools/gn/scope.h" 15 #include "tools/gn/source_file_type.h" 16 #include "tools/gn/substitution_type.h" 17 #include "tools/gn/tool.h" 18 #include "tools/gn/value.h" 19 20 // Holds information on a specific toolchain. This data is filled in when we 21 // encounter a toolchain definition. 22 // 23 // This class is an Item so it can participate in dependency management. In 24 // particular, when a target uses a toolchain, it should have a dependency on 25 // that toolchain's object so that we can be sure we loaded the toolchain 26 // before generating the build for that target. 27 // 28 // Note on threadsafety: The label of the toolchain never changes so can 29 // safely be accessed from any thread at any time (we do this when asking for 30 // the toolchain name). But the values in the toolchain do, so these can't 31 // be accessed until this Item is resolved. 32 class Toolchain : public Item { 33 public: 34 enum ToolType { 35 TYPE_NONE = 0, 36 TYPE_CC, 37 TYPE_CXX, 38 TYPE_OBJC, 39 TYPE_OBJCXX, 40 TYPE_RC, 41 TYPE_ASM, 42 TYPE_ALINK, 43 TYPE_SOLINK, 44 TYPE_LINK, 45 TYPE_STAMP, 46 TYPE_COPY, 47 48 TYPE_NUMTYPES // Must be last. 49 }; 50 51 static const char* kToolCc; 52 static const char* kToolCxx; 53 static const char* kToolObjC; 54 static const char* kToolObjCxx; 55 static const char* kToolRc; 56 static const char* kToolAsm; 57 static const char* kToolAlink; 58 static const char* kToolSolink; 59 static const char* kToolLink; 60 static const char* kToolStamp; 61 static const char* kToolCopy; 62 63 Toolchain(const Settings* settings, const Label& label); 64 virtual ~Toolchain(); 65 66 // Item overrides. 67 virtual Toolchain* AsToolchain() OVERRIDE; 68 virtual const Toolchain* AsToolchain() const OVERRIDE; 69 70 // Returns TYPE_NONE on failure. 71 static ToolType ToolNameToType(const base::StringPiece& str); 72 static std::string ToolTypeToName(ToolType type); 73 74 // Returns null if the tool hasn't been defined. 75 const Tool* GetTool(ToolType type) const; 76 77 // Set a tool. When all tools are configured, you should call 78 // ToolchainSetupComplete(). 79 void SetTool(ToolType type, scoped_ptr<Tool> t); 80 81 // Does final setup on the toolchain once all tools are known. 82 void ToolchainSetupComplete(); 83 84 // Targets that must be resolved before compiling any targets. deps()85 const LabelTargetVector& deps() const { return deps_; } deps()86 LabelTargetVector& deps() { return deps_; } 87 88 // Specifies build argument overrides that will be set on the base scope. It 89 // will be as if these arguments were passed in on the command line. This 90 // allows a toolchain to override the OS type of the default toolchain or 91 // pass in other settings. args()92 Scope::KeyValueMap& args() { return args_; } args()93 const Scope::KeyValueMap& args() const { return args_; } 94 95 // Returns the tool for compiling the given source file type. 96 static ToolType GetToolTypeForSourceType(SourceFileType type); 97 const Tool* GetToolForSourceType(SourceFileType type); 98 99 // Returns the tool that produces the final output for the given target type. 100 // This isn't necessarily the tool you would expect. For copy target, this 101 // will return the stamp tool ionstead since the final output of a copy 102 // target is to stamp the set of copies done so there is one output. 103 static ToolType GetToolTypeForTargetFinalOutput(const Target* target); 104 const Tool* GetToolForTargetFinalOutput(const Target* target) const; 105 substitution_bits()106 const SubstitutionBits& substitution_bits() const { 107 DCHECK(setup_complete_); 108 return substitution_bits_; 109 } 110 set_concurrent_links(int cl)111 void set_concurrent_links(int cl) { concurrent_links_ = cl; } concurrent_links()112 int concurrent_links() const { return concurrent_links_; } 113 114 private: 115 scoped_ptr<Tool> tools_[TYPE_NUMTYPES]; 116 117 // How many links to run in parallel. Only the default toolchain's version of 118 // this variable applies. 119 int concurrent_links_; 120 121 bool setup_complete_; 122 123 // Substitutions used by the tools in this toolchain. 124 SubstitutionBits substitution_bits_; 125 126 LabelTargetVector deps_; 127 Scope::KeyValueMap args_; 128 }; 129 130 #endif // TOOLS_GN_TOOLCHAIN_H_ 131