• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "gn/toolchain.h"
6 
7 #include <stddef.h>
8 #include <string.h>
9 #include <utility>
10 
11 #include "base/logging.h"
12 #include "gn/builtin_tool.h"
13 #include "gn/settings.h"
14 #include "gn/target.h"
15 #include "gn/value.h"
16 
Toolchain(const Settings * settings,const Label & label,const SourceFileSet & build_dependency_files)17 Toolchain::Toolchain(const Settings* settings,
18                      const Label& label,
19                      const SourceFileSet& build_dependency_files)
20     : Item(settings, label, build_dependency_files) {
21   // Ensure "phony" tool is part of all toolchains by default.
22   const char* phony_name = BuiltinTool::kBuiltinToolPhony;
23   tools_.emplace(phony_name, std::make_unique<BuiltinTool>(phony_name));
24 }
25 
26 Toolchain::~Toolchain() = default;
27 
AsToolchain()28 Toolchain* Toolchain::AsToolchain() {
29   return this;
30 }
31 
AsToolchain() const32 const Toolchain* Toolchain::AsToolchain() const {
33   return this;
34 }
35 
GetTool(const char * name)36 Tool* Toolchain::GetTool(const char* name) {
37   DCHECK(name != Tool::kToolNone);
38   auto pair = tools_.find(name);
39   if (pair != tools_.end()) {
40     return pair->second.get();
41   }
42   return nullptr;
43 }
44 
GetTool(const char * name) const45 const Tool* Toolchain::GetTool(const char* name) const {
46   DCHECK(name != Tool::kToolNone);
47   auto pair = tools_.find(name);
48   if (pair != tools_.end()) {
49     return pair->second.get();
50   }
51   return nullptr;
52 }
53 
GetToolAsGeneral(const char * name)54 GeneralTool* Toolchain::GetToolAsGeneral(const char* name) {
55   if (Tool* tool = GetTool(name)) {
56     return tool->AsGeneral();
57   }
58   return nullptr;
59 }
60 
GetToolAsGeneral(const char * name) const61 const GeneralTool* Toolchain::GetToolAsGeneral(const char* name) const {
62   if (const Tool* tool = GetTool(name)) {
63     return tool->AsGeneral();
64   }
65   return nullptr;
66 }
67 
GetToolAsC(const char * name)68 CTool* Toolchain::GetToolAsC(const char* name) {
69   if (Tool* tool = GetTool(name)) {
70     return tool->AsC();
71   }
72   return nullptr;
73 }
74 
GetToolAsC(const char * name) const75 const CTool* Toolchain::GetToolAsC(const char* name) const {
76   if (const Tool* tool = GetTool(name)) {
77     return tool->AsC();
78   }
79   return nullptr;
80 }
81 
GetToolAsRust(const char * name)82 RustTool* Toolchain::GetToolAsRust(const char* name) {
83   if (Tool* tool = GetTool(name)) {
84     return tool->AsRust();
85   }
86   return nullptr;
87 }
88 
GetToolAsRust(const char * name) const89 const RustTool* Toolchain::GetToolAsRust(const char* name) const {
90   if (const Tool* tool = GetTool(name)) {
91     return tool->AsRust();
92   }
93   return nullptr;
94 }
95 
GetToolAsBuiltin(const char * name)96 BuiltinTool* Toolchain::GetToolAsBuiltin(const char* name) {
97   if (Tool* tool = GetTool(name)) {
98     return tool->AsBuiltin();
99   }
100   return nullptr;
101 }
102 
GetToolAsBuiltin(const char * name) const103 const BuiltinTool* Toolchain::GetToolAsBuiltin(const char* name) const {
104   if (const Tool* tool = GetTool(name)) {
105     return tool->AsBuiltin();
106   }
107   return nullptr;
108 }
109 
SetTool(std::unique_ptr<Tool> t)110 void Toolchain::SetTool(std::unique_ptr<Tool> t) {
111   DCHECK(t->name() != Tool::kToolNone);
112   DCHECK(tools_.find(t->name()) == tools_.end());
113   t->SetComplete();
114   tools_[t->name()] = std::move(t);
115 }
116 
ToolchainSetupComplete()117 void Toolchain::ToolchainSetupComplete() {
118   // Collect required bits from all tools.
119   for (const auto& tool : tools_) {
120     substitution_bits_.MergeFrom(tool.second->substitution_bits());
121   }
122   setup_complete_ = true;
123 }
124 
GetToolForSourceType(SourceFile::Type type) const125 const Tool* Toolchain::GetToolForSourceType(SourceFile::Type type) const {
126   return GetTool(Tool::GetToolTypeForSourceType(type));
127 }
128 
GetToolForSourceTypeAsC(SourceFile::Type type) const129 const CTool* Toolchain::GetToolForSourceTypeAsC(SourceFile::Type type) const {
130   return GetToolAsC(Tool::GetToolTypeForSourceType(type));
131 }
132 
GetToolForSourceTypeAsGeneral(SourceFile::Type type) const133 const GeneralTool* Toolchain::GetToolForSourceTypeAsGeneral(
134     SourceFile::Type type) const {
135   return GetToolAsGeneral(Tool::GetToolTypeForSourceType(type));
136 }
137 
GetToolForSourceTypeAsRust(SourceFile::Type type) const138 const RustTool* Toolchain::GetToolForSourceTypeAsRust(
139     SourceFile::Type type) const {
140   return GetToolAsRust(Tool::GetToolTypeForSourceType(type));
141 }
142 
GetToolForSourceTypeAsBuiltin(SourceFile::Type type) const143 const BuiltinTool* Toolchain::GetToolForSourceTypeAsBuiltin(
144     SourceFile::Type type) const {
145   return GetToolAsBuiltin(Tool::GetToolTypeForSourceType(type));
146 }
147 
GetToolForTargetFinalOutput(const Target * target) const148 const Tool* Toolchain::GetToolForTargetFinalOutput(const Target* target) const {
149   return GetTool(Tool::GetToolTypeForTargetFinalOutput(target));
150 }
151 
GetToolForTargetFinalOutputAsC(const Target * target) const152 const CTool* Toolchain::GetToolForTargetFinalOutputAsC(
153     const Target* target) const {
154   return GetToolAsC(Tool::GetToolTypeForTargetFinalOutput(target));
155 }
156 
GetToolForTargetFinalOutputAsGeneral(const Target * target) const157 const GeneralTool* Toolchain::GetToolForTargetFinalOutputAsGeneral(
158     const Target* target) const {
159   return GetToolAsGeneral(Tool::GetToolTypeForTargetFinalOutput(target));
160 }
161 
GetToolForTargetFinalOutputAsRust(const Target * target) const162 const RustTool* Toolchain::GetToolForTargetFinalOutputAsRust(
163     const Target* target) const {
164   return GetToolAsRust(Tool::GetToolTypeForTargetFinalOutput(target));
165 }
166 
GetToolForTargetFinalOutputAsBuiltin(const Target * target) const167 const BuiltinTool* Toolchain::GetToolForTargetFinalOutputAsBuiltin(
168     const Target* target) const {
169   return GetToolAsBuiltin(Tool::GetToolTypeForTargetFinalOutput(target));
170 }
171