16d528ed9Sopenharmony_ci// Copyright 2019 The Chromium Authors. All rights reserved. 26d528ed9Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 36d528ed9Sopenharmony_ci// found in the LICENSE file. 46d528ed9Sopenharmony_ci 56d528ed9Sopenharmony_ci#ifndef TOOLS_GN_C_TOOL_H_ 66d528ed9Sopenharmony_ci#define TOOLS_GN_C_TOOL_H_ 76d528ed9Sopenharmony_ci 86d528ed9Sopenharmony_ci#include <string> 96d528ed9Sopenharmony_ci 106d528ed9Sopenharmony_ci#include "base/logging.h" 116d528ed9Sopenharmony_ci#include "gn/label.h" 126d528ed9Sopenharmony_ci#include "gn/label_ptr.h" 136d528ed9Sopenharmony_ci#include "gn/scope.h" 146d528ed9Sopenharmony_ci#include "gn/substitution_list.h" 156d528ed9Sopenharmony_ci#include "gn/substitution_pattern.h" 166d528ed9Sopenharmony_ci#include "gn/tool.h" 176d528ed9Sopenharmony_ci#include "gn/toolchain.h" 186d528ed9Sopenharmony_ci 196d528ed9Sopenharmony_ciclass CTool : public Tool { 206d528ed9Sopenharmony_ci public: 216d528ed9Sopenharmony_ci // C compiler tools 226d528ed9Sopenharmony_ci static const char* kCToolCc; 236d528ed9Sopenharmony_ci static const char* kCToolCxx; 246d528ed9Sopenharmony_ci static const char* kCToolCxxModule; 256d528ed9Sopenharmony_ci static const char* kCToolObjC; 266d528ed9Sopenharmony_ci static const char* kCToolObjCxx; 276d528ed9Sopenharmony_ci static const char* kCToolRc; 286d528ed9Sopenharmony_ci static const char* kCToolAsm; 296d528ed9Sopenharmony_ci static const char* kCToolSwift; 306d528ed9Sopenharmony_ci 316d528ed9Sopenharmony_ci // C linker tools 326d528ed9Sopenharmony_ci static const char* kCToolAlink; 336d528ed9Sopenharmony_ci static const char* kCToolSolink; 346d528ed9Sopenharmony_ci static const char* kCToolSolinkModule; 356d528ed9Sopenharmony_ci static const char* kCToolLink; 366d528ed9Sopenharmony_ci 376d528ed9Sopenharmony_ci enum DepsFormat { DEPS_GCC = 0, DEPS_MSVC = 1 }; 386d528ed9Sopenharmony_ci 396d528ed9Sopenharmony_ci enum PrecompiledHeaderType { PCH_NONE = 0, PCH_GCC = 1, PCH_MSVC = 2 }; 406d528ed9Sopenharmony_ci 416d528ed9Sopenharmony_ci CTool(const char* n); 426d528ed9Sopenharmony_ci ~CTool(); 436d528ed9Sopenharmony_ci 446d528ed9Sopenharmony_ci // Manual RTTI and required functions --------------------------------------- 456d528ed9Sopenharmony_ci 466d528ed9Sopenharmony_ci bool InitTool(Scope* block_scope, Toolchain* toolchain, Err* err); 476d528ed9Sopenharmony_ci bool ValidateName(const char* name) const override; 486d528ed9Sopenharmony_ci void SetComplete() override; 496d528ed9Sopenharmony_ci bool ValidateSubstitution(const Substitution* sub_type) const override; 506d528ed9Sopenharmony_ci 516d528ed9Sopenharmony_ci CTool* AsC() override; 526d528ed9Sopenharmony_ci const CTool* AsC() const override; 536d528ed9Sopenharmony_ci 546d528ed9Sopenharmony_ci // Getters/setters ---------------------------------------------------------- 556d528ed9Sopenharmony_ci // 566d528ed9Sopenharmony_ci // After the tool has had its attributes set, the caller must call 576d528ed9Sopenharmony_ci // SetComplete(), at which point no other changes can be made. 586d528ed9Sopenharmony_ci 596d528ed9Sopenharmony_ci DepsFormat depsformat() const { return depsformat_; } 606d528ed9Sopenharmony_ci void set_depsformat(DepsFormat f) { 616d528ed9Sopenharmony_ci DCHECK(!complete_); 626d528ed9Sopenharmony_ci depsformat_ = f; 636d528ed9Sopenharmony_ci } 646d528ed9Sopenharmony_ci 656d528ed9Sopenharmony_ci PrecompiledHeaderType precompiled_header_type() const { 666d528ed9Sopenharmony_ci return precompiled_header_type_; 676d528ed9Sopenharmony_ci } 686d528ed9Sopenharmony_ci void set_precompiled_header_type(PrecompiledHeaderType pch_type) { 696d528ed9Sopenharmony_ci DCHECK(!complete_); 706d528ed9Sopenharmony_ci precompiled_header_type_ = pch_type; 716d528ed9Sopenharmony_ci } 726d528ed9Sopenharmony_ci 736d528ed9Sopenharmony_ci // Should match files in the outputs() if nonempty. 746d528ed9Sopenharmony_ci const SubstitutionPattern& link_output() const { return link_output_; } 756d528ed9Sopenharmony_ci void set_link_output(SubstitutionPattern link_out) { 766d528ed9Sopenharmony_ci DCHECK(!complete_); 776d528ed9Sopenharmony_ci link_output_ = std::move(link_out); 786d528ed9Sopenharmony_ci } 796d528ed9Sopenharmony_ci 806d528ed9Sopenharmony_ci const SubstitutionPattern& depend_output() const { return depend_output_; } 816d528ed9Sopenharmony_ci void set_depend_output(SubstitutionPattern dep_out) { 826d528ed9Sopenharmony_ci DCHECK(!complete_); 836d528ed9Sopenharmony_ci depend_output_ = std::move(dep_out); 846d528ed9Sopenharmony_ci } 856d528ed9Sopenharmony_ci 866d528ed9Sopenharmony_ci // Other functions ---------------------------------------------------------- 876d528ed9Sopenharmony_ci 886d528ed9Sopenharmony_ci // Returns true if this tool has separate outputs for dependency tracking 896d528ed9Sopenharmony_ci // and linking. 906d528ed9Sopenharmony_ci bool has_separate_solink_files() const { 916d528ed9Sopenharmony_ci return !link_output_.empty() || !depend_output_.empty(); 926d528ed9Sopenharmony_ci } 936d528ed9Sopenharmony_ci 946d528ed9Sopenharmony_ci int toolchain_whole_status() const { return toolchain_whole_status_; } 956d528ed9Sopenharmony_ci 966d528ed9Sopenharmony_ci private: 976d528ed9Sopenharmony_ci // Initialization functions ------------------------------------------------- 986d528ed9Sopenharmony_ci // 996d528ed9Sopenharmony_ci // Initialization methods used by InitTool(). If successful, will set the 1006d528ed9Sopenharmony_ci // field and return true, otherwise will return false. Must be called before 1016d528ed9Sopenharmony_ci // SetComplete(). 1026d528ed9Sopenharmony_ci bool ValidateOutputSubstitution(const Substitution* sub_type) const; 1036d528ed9Sopenharmony_ci bool ValidateRuntimeOutputs(Err* err); 1046d528ed9Sopenharmony_ci // Validates either link_output or depend_output. To generalize to either, 1056d528ed9Sopenharmony_ci // pass 1066d528ed9Sopenharmony_ci // the associated pattern, and the variable name that should appear in error 1076d528ed9Sopenharmony_ci // messages. 1086d528ed9Sopenharmony_ci bool ValidateLinkAndDependOutput(const SubstitutionPattern& pattern, 1096d528ed9Sopenharmony_ci const char* variable_name, 1106d528ed9Sopenharmony_ci Err* err); 1116d528ed9Sopenharmony_ci bool ReadOutputsPatternList(Scope* scope, 1126d528ed9Sopenharmony_ci const char* var, 1136d528ed9Sopenharmony_ci bool required, 1146d528ed9Sopenharmony_ci SubstitutionList* field, 1156d528ed9Sopenharmony_ci Err* err); 1166d528ed9Sopenharmony_ci bool ReadPrecompiledHeaderType(Scope* scope, Err* err); 1176d528ed9Sopenharmony_ci bool ReadDepsFormat(Scope* scope, Err* err); 1186d528ed9Sopenharmony_ci 1196d528ed9Sopenharmony_ci DepsFormat depsformat_; 1206d528ed9Sopenharmony_ci PrecompiledHeaderType precompiled_header_type_; 1216d528ed9Sopenharmony_ci SubstitutionPattern link_output_; 1226d528ed9Sopenharmony_ci SubstitutionPattern depend_output_; 1236d528ed9Sopenharmony_ci int toolchain_whole_status_ = -1; 1246d528ed9Sopenharmony_ci 1256d528ed9Sopenharmony_ci CTool(const CTool&) = delete; 1266d528ed9Sopenharmony_ci CTool& operator=(const CTool&) = delete; 1276d528ed9Sopenharmony_ci}; 1286d528ed9Sopenharmony_ci 1296d528ed9Sopenharmony_ci#endif // TOOLS_GN_C_TOOL_H_ 130