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.
5 #ifndef TOOLS_GN_TOOLCHAIN_MANAGER_H_
6 #define TOOLS_GN_TOOLCHAIN_MANAGER_H_
10 #include "base/basictypes.h"
11 #include "base/synchronization/lock.h"
12 #include "tools/gn/label.h"
13 #include "tools/gn/location.h"
14 #include "tools/gn/source_file.h"
15 #include "tools/gn/toolchain.h"
22 // The toolchain manager manages the mapping of toolchain names to the
23 // settings and toolchain object. It also loads build files in the context of a
24 // toolchain context of a toolchain, and manages running the build config
25 // script when necessary.
27 // This class uses the lock from the item tree to manage threadsafety. The
28 // functions requiring this lock to be held are named "Locked" to make this
29 // more clear. The "Unlocked" versions will acquire the lock themselves so will
30 // break if you call it while locked. (The rationale behind which is which is
31 // just based on the needs of the callers, so it can be changed.) There are two
34 // The first is that when resolving a target, we do a bunch of script
35 // stuff (slow) and then lookup the target, config, and toolchain dependencies
36 // based on that. The options are to do a lock around each dependency lookup
37 // or do a lock around the entire operation. Given that there's not a huge
38 // amount of work, the "big lock" approach is likely a bit better since it
39 // avoids lots of locking overhead.
41 // The second reason is that if we had a separate lock here, we would need to
42 // lock around creating a new toolchain. But creating a new toolchain involves
43 // adding it to the item tree, and this needs to be done atomically to prevent
44 // other threads from seeing a partially initialized toolchain. This sets up
45 // having deadlock do to acquiring multiple locks, or recursive locking
47 class ToolchainManager {
49 ToolchainManager(const BuildSettings* build_settings);
52 // At the very beginning of processing, this begins loading build files.
53 // This will scheduler loadin the default build config and the given build
54 // file in that context, going out from there.
56 // This returns immediately, you need to run the Scheduler to actually
57 // process anything. It's assumed this function is called on the main thread
58 // before doing anything, so it does not need locking.
59 void StartLoadingUnlocked(const SourceFile& build_file_name);
61 // Returns the settings object for a given toolchain. This does not
62 // schedule loading the given toolchain if it's not loaded yet: you actually
63 // need to invoke a target with that toolchain to get that.
65 // On error, returns NULL and sets the error.
66 const Settings* GetSettingsForToolchainLocked(const LocationRange& from_here,
67 const Label& toolchain_name,
70 // Returns the toolchain definition or NULL if the toolchain hasn't been
72 const Toolchain* GetToolchainDefinitionUnlocked(const Label& toolchain_name);
74 // Sets the default toolchain. If the default toolchain is already set,
75 // this function will return false and fill in the given err.
76 bool SetDefaultToolchainUnlocked(const Label& dt,
77 const LocationRange& defined_from,
80 // Returns the default toolchain name. This will be empty if it hasn't been
82 Label GetDefaultToolchainUnlocked() const;
84 // Saves the given named toolchain (the name will be taken from the toolchain
85 // parameter). This will fail and return false if the given toolchain was
86 // already defined. In this case, the given error will be set.
87 bool SetToolchainDefinitionLocked(const Toolchain& tc,
88 const LocationRange& defined_from,
91 // Schedules an invocation of the given file under the given toolchain. The
92 // toolchain file will be loaded if necessary. If the toolchain is an is_null
93 // label, the default toolchain will be used.
95 // The origin should be the node that will be blamed for this invocation if
96 // an error occurs. If a synchronous error occurs, the given error will be
97 // set and it will return false. If an async error occurs, the error will be
98 // sent to the scheduler.
99 bool ScheduleInvocationLocked(const LocationRange& origin,
100 const Label& toolchain_name,
101 const SourceDir& dir,
107 static std::string ToolchainToOutputSubdir(const Label& toolchain_name);
109 // Creates a new info struct and saves it in the map. A pointer to the
110 // struct is returned. No loads are scheduled.
112 // If the label is non-empty, the toolchain will be added to the ItemTree
113 // so that other nodes can depend on it. THe empty label case is for the
114 // default build config file (when the toolchain name isn't known yet). It
115 // will be added later.
117 // On error, will return NULL and the error will be set.
118 Info* LoadNewToolchainLocked(const LocationRange& specified_from,
119 const Label& toolchain_name,
122 // Fixes up the default toolchain names once they're known when processing
123 // the default build config, or throw an error if the default toolchain
124 // hasn't been set. See the StartLoading() implementation for more.
125 void FixupDefaultToolchainLocked();
127 // Schedules the buildfile to be invoked for the given toolchain info. The
128 // toolchain must be already loaded. Can be called with the lock held or not
129 // (it does not use any class vars).
130 bool ScheduleBackgroundInvoke(Info* info,
131 const LocationRange& specified_from,
132 const SourceFile& build_file,
135 // Schedules the build config file for the given info to be loaded and
136 // invoked. The toolchain definition (for non-default toolchains) should
137 // already be loaded.
138 bool ScheduleBuildConfigLoadLocked(Info* info, bool is_default, Err* err);
140 // Invokes the given file for a toolchain with loaded settings. Run on a
141 // background thread asynchronously.
142 void BackgroundInvoke(const Info* info,
143 const SourceFile& file_name,
144 const ParseNode* root);
146 // Loads the build config file for the given Info.
147 void BackgroundLoadBuildConfig(Info* info,
149 const ParseNode* root);
151 // Returns the lock to use.
152 base::Lock& GetLock() const;
154 const BuildSettings* build_settings_;
156 // We own the info pointers.
157 typedef std::map<Label, Info*> ToolchainMap;
158 ToolchainMap toolchains_;
160 Label default_toolchain_;
161 LocationRange default_toolchain_defined_here_;
163 typedef std::map<std::pair<SourceFile, const Info*>, Info*>
164 BuildConfigInvokeMap;
165 BuildConfigInvokeMap pending_build_config_map_;
167 DISALLOW_COPY_AND_ASSIGN(ToolchainManager);
170 #endif // TOOLS_GN_TOOLCHAIN_MANAGER_H_