• 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 #ifndef TOOLS_GN_TOOLCHAIN_H_
6 #define TOOLS_GN_TOOLCHAIN_H_
7 
8 #include <memory>
9 #include <string_view>
10 
11 #include "base/logging.h"
12 #include "gn/item.h"
13 #include "gn/label_ptr.h"
14 #include "gn/scope.h"
15 #include "gn/substitution_type.h"
16 #include "gn/tool.h"
17 #include "gn/value.h"
18 
19 class BuiltinTool;
20 
21 // Holds information on a specific toolchain. This data is filled in when we
22 // encounter a toolchain definition.
23 //
24 // This class is an Item so it can participate in dependency management. In
25 // particular, when a target uses a toolchain, it should have a dependency on
26 // that toolchain's object so that we can be sure we loaded the toolchain
27 // before generating the build for that target.
28 //
29 // Note on threadsafety: The label of the toolchain never changes so can
30 // safely be accessed from any thread at any time (we do this when asking for
31 // the toolchain name). But the values in the toolchain do, so these can't
32 // be accessed until this Item is resolved.
33 class Toolchain : public Item {
34  public:
35   // The Settings of an Item is always the context in which the Item was
36   // defined. For a toolchain this is confusing because this is NOT the
37   // settings object that applies to the things in the toolchain.
38   //
39   // To get the Settings object corresponding to objects loaded in the context
40   // of this toolchain (probably what you want instead), see
41   // Loader::GetToolchainSettings(). Many toolchain objects may be created in a
42   // given build, but only a few might be used, and the Loader is in charge of
43   // this process.
44   //
45   // We also track the set of build files that may affect this target, please
46   // refer to Scope for how this is determined.
47   Toolchain(const Settings* settings,
48             const Label& label,
49             const SourceFileSet& build_dependency_files = {});
50   ~Toolchain() override;
51 
52   // Item overrides.
53   Toolchain* AsToolchain() override;
54   const Toolchain* AsToolchain() const override;
55 
56   // Returns null if the tool hasn't been defined.
57   Tool* GetTool(const char* name);
58   const Tool* GetTool(const char* name) const;
59 
60   // Returns null if the tool hasn't been defined or is not the correct type.
61   GeneralTool* GetToolAsGeneral(const char* name);
62   const GeneralTool* GetToolAsGeneral(const char* name) const;
63   CTool* GetToolAsC(const char* name);
64   const CTool* GetToolAsC(const char* name) const;
65   RustTool* GetToolAsRust(const char* name);
66   const RustTool* GetToolAsRust(const char* name) const;
67   BuiltinTool* GetToolAsBuiltin(const char* name);
68   const BuiltinTool* GetToolAsBuiltin(const char* name) const;
69 
70   // Set a tool. When all tools are configured, you should call
71   // ToolchainSetupComplete().
72   void SetTool(std::unique_ptr<Tool> t);
73 
74   // Does final setup on the toolchain once all tools are known.
75   void ToolchainSetupComplete();
76 
77   // Targets that must be resolved before compiling any targets.
deps()78   const LabelTargetVector& deps() const { return deps_; }
deps()79   LabelTargetVector& deps() { return deps_; }
80 
81   // Specifies build argument overrides that will be set on the base scope. It
82   // will be as if these arguments were passed in on the command line. This
83   // allows a toolchain to override the OS type of the default toolchain or
84   // pass in other settings.
args()85   Scope::KeyValueMap& args() { return args_; }
args()86   const Scope::KeyValueMap& args() const { return args_; }
87 
88   // Specifies whether public_configs and all_dependent_configs in this
89   // toolchain propagate to targets in other toolchains.
propagates_configs()90   bool propagates_configs() const { return propagates_configs_; }
set_propagates_configs(bool propagates_configs)91   void set_propagates_configs(bool propagates_configs) {
92     propagates_configs_ = propagates_configs;
93   }
94 
95   // Returns the tool for compiling the given source file type.
96   const Tool* GetToolForSourceType(SourceFile::Type type) const;
97   const CTool* GetToolForSourceTypeAsC(SourceFile::Type type) const;
98   const GeneralTool* GetToolForSourceTypeAsGeneral(SourceFile::Type type) const;
99   const RustTool* GetToolForSourceTypeAsRust(SourceFile::Type type) const;
100   const BuiltinTool* GetToolForSourceTypeAsBuiltin(SourceFile::Type type) const;
101 
102   // Returns the tool that produces the final output for the given target type.
103   // This isn't necessarily the tool you would expect. For copy target, this
104   // will return the stamp tool instead since the final output of a copy
105   // target is to stamp the set of copies done so there is one output.
106   const Tool* GetToolForTargetFinalOutput(const Target* target) const;
107   const CTool* GetToolForTargetFinalOutputAsC(const Target* target) const;
108   const GeneralTool* GetToolForTargetFinalOutputAsGeneral(
109       const Target* target) const;
110   const RustTool* GetToolForTargetFinalOutputAsRust(const Target* target) const;
111   const BuiltinTool* GetToolForTargetFinalOutputAsBuiltin(
112       const Target* target) const;
113 
substitution_bits()114   const SubstitutionBits& substitution_bits() const {
115     DCHECK(setup_complete_);
116     return substitution_bits_;
117   }
118 
tools()119   const std::map<const char*, std::unique_ptr<Tool>>& tools() const {
120     return tools_;
121   }
122 
123  private:
124   std::map<const char*, std::unique_ptr<Tool>> tools_;
125 
126   bool setup_complete_ = false;
127 
128   // Substitutions used by the tools in this toolchain.
129   SubstitutionBits substitution_bits_;
130 
131   LabelTargetVector deps_;
132   Scope::KeyValueMap args_;
133   bool propagates_configs_ = false;
134 };
135 
136 #endif  // TOOLS_GN_TOOLCHAIN_H_
137