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_C_TOOL_H_ 6 #define TOOLS_GN_C_TOOL_H_ 7 8 #include <string> 9 10 #include "base/logging.h" 11 #include "gn/label.h" 12 #include "gn/label_ptr.h" 13 #include "gn/scope.h" 14 #include "gn/substitution_list.h" 15 #include "gn/substitution_pattern.h" 16 #include "gn/tool.h" 17 #include "gn/toolchain.h" 18 19 class CTool : public Tool { 20 public: 21 // C compiler tools 22 static const char* kCToolCc; 23 static const char* kCToolCxx; 24 static const char* kCToolCxxModule; 25 static const char* kCToolObjC; 26 static const char* kCToolObjCxx; 27 static const char* kCToolRc; 28 static const char* kCToolAsm; 29 static const char* kCToolSwift; 30 31 // C linker tools 32 static const char* kCToolAlink; 33 static const char* kCToolSolink; 34 static const char* kCToolSolinkModule; 35 static const char* kCToolLink; 36 37 enum DepsFormat { DEPS_GCC = 0, DEPS_MSVC = 1 }; 38 39 enum PrecompiledHeaderType { PCH_NONE = 0, PCH_GCC = 1, PCH_MSVC = 2 }; 40 41 CTool(const char* n); 42 ~CTool(); 43 44 // Manual RTTI and required functions --------------------------------------- 45 46 bool InitTool(Scope* block_scope, Toolchain* toolchain, Err* err); 47 bool ValidateName(const char* name) const override; 48 void SetComplete() override; 49 bool ValidateSubstitution(const Substitution* sub_type) const override; 50 51 CTool* AsC() override; 52 const CTool* AsC() const override; 53 54 // Getters/setters ---------------------------------------------------------- 55 // 56 // After the tool has had its attributes set, the caller must call 57 // SetComplete(), at which point no other changes can be made. 58 depsformat() const59 DepsFormat depsformat() const { return depsformat_; } set_depsformat(DepsFormat f)60 void set_depsformat(DepsFormat f) { 61 DCHECK(!complete_); 62 depsformat_ = f; 63 } 64 precompiled_header_type() const65 PrecompiledHeaderType precompiled_header_type() const { 66 return precompiled_header_type_; 67 } set_precompiled_header_type(PrecompiledHeaderType pch_type)68 void set_precompiled_header_type(PrecompiledHeaderType pch_type) { 69 DCHECK(!complete_); 70 precompiled_header_type_ = pch_type; 71 } 72 73 // Should match files in the outputs() if nonempty. link_output() const74 const SubstitutionPattern& link_output() const { return link_output_; } set_link_output(SubstitutionPattern link_out)75 void set_link_output(SubstitutionPattern link_out) { 76 DCHECK(!complete_); 77 link_output_ = std::move(link_out); 78 } 79 depend_output() const80 const SubstitutionPattern& depend_output() const { return depend_output_; } set_depend_output(SubstitutionPattern dep_out)81 void set_depend_output(SubstitutionPattern dep_out) { 82 DCHECK(!complete_); 83 depend_output_ = std::move(dep_out); 84 } 85 86 // Other functions ---------------------------------------------------------- 87 88 // Returns true if this tool has separate outputs for dependency tracking 89 // and linking. has_separate_solink_files() const90 bool has_separate_solink_files() const { 91 return !link_output_.empty() || !depend_output_.empty(); 92 } 93 toolchain_whole_status() const94 int toolchain_whole_status() const { return toolchain_whole_status_; } 95 96 private: 97 // Initialization functions ------------------------------------------------- 98 // 99 // Initialization methods used by InitTool(). If successful, will set the 100 // field and return true, otherwise will return false. Must be called before 101 // SetComplete(). 102 bool ValidateOutputSubstitution(const Substitution* sub_type) const; 103 bool ValidateRuntimeOutputs(Err* err); 104 // Validates either link_output or depend_output. To generalize to either, 105 // pass 106 // the associated pattern, and the variable name that should appear in error 107 // messages. 108 bool ValidateLinkAndDependOutput(const SubstitutionPattern& pattern, 109 const char* variable_name, 110 Err* err); 111 bool ReadOutputsPatternList(Scope* scope, 112 const char* var, 113 bool required, 114 SubstitutionList* field, 115 Err* err); 116 bool ReadPrecompiledHeaderType(Scope* scope, Err* err); 117 bool ReadDepsFormat(Scope* scope, Err* err); 118 119 DepsFormat depsformat_; 120 PrecompiledHeaderType precompiled_header_type_; 121 SubstitutionPattern link_output_; 122 SubstitutionPattern depend_output_; 123 int toolchain_whole_status_ = -1; 124 125 CTool(const CTool&) = delete; 126 CTool& operator=(const CTool&) = delete; 127 }; 128 129 #endif // TOOLS_GN_C_TOOL_H_ 130