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