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()59 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()65 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()74 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()80 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()90 bool has_separate_solink_files() const { 91 return !link_output_.empty() || !depend_output_.empty(); 92 } 93 94 private: 95 // Initialization functions ------------------------------------------------- 96 // 97 // Initialization methods used by InitTool(). If successful, will set the 98 // field and return true, otherwise will return false. Must be called before 99 // SetComplete(). 100 bool ValidateOutputSubstitution(const Substitution* sub_type) const; 101 bool ValidateRuntimeOutputs(Err* err); 102 // Validates either link_output or depend_output. To generalize to either, 103 // pass 104 // the associated pattern, and the variable name that should appear in error 105 // messages. 106 bool ValidateLinkAndDependOutput(const SubstitutionPattern& pattern, 107 const char* variable_name, 108 Err* err); 109 bool ReadOutputsPatternList(Scope* scope, 110 const char* var, 111 bool required, 112 SubstitutionList* field, 113 Err* err); 114 bool ReadPrecompiledHeaderType(Scope* scope, Err* err); 115 bool ReadDepsFormat(Scope* scope, Err* err); 116 117 DepsFormat depsformat_; 118 PrecompiledHeaderType precompiled_header_type_; 119 SubstitutionPattern link_output_; 120 SubstitutionPattern depend_output_; 121 122 CTool(const CTool&) = delete; 123 CTool& operator=(const CTool&) = delete; 124 }; 125 126 #endif // TOOLS_GN_C_TOOL_H_ 127