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_LOADER_H_
6 #define TOOLS_GN_LOADER_H_
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "tools/gn/label.h"
14 #include "tools/gn/scope.h"
26 // The loader manages execution of the different build files. It receives
27 // requests (normally from the Builder) when new references are found, and also
28 // manages loading the build config files.
30 // This loader class is abstract so it can be mocked out for testing the
32 class Loader
: public base::RefCountedThreadSafe
<Loader
> {
36 // Loads the given file in the conext of the given toolchain. The initial
37 // call to this (the one that actually starts the generation) should have an
38 // empty toolchain name, which will trigger the load of the default build
40 virtual void Load(const SourceFile
& file
,
41 const LocationRange
& origin
,
42 const Label
& toolchain_name
) = 0;
44 // Notification that the given toolchain has loaded. This will unblock files
45 // waiting on this definition.
46 virtual void ToolchainLoaded(const Toolchain
* toolchain
) = 0;
48 // Returns the label of the default toolchain.
49 virtual Label
GetDefaultToolchain() const = 0;
51 // Returns information about the toolchain with the given label. Will return
52 // false if we haven't processed this toolchain yet.
53 virtual const Settings
* GetToolchainSettings(const Label
& label
) const = 0;
55 // Helper function that extracts the file and toolchain name from the given
56 // label, and calls Load().
57 void Load(const Label
& label
, const LocationRange
& origin
);
59 // Returns the build file that the given label references.
60 static SourceFile
BuildFileForLabel(const Label
& label
);
62 // When processing the default build config, we want to capture the argument
63 // of set_default_build_config. The implementation of that function uses this
64 // constant as a property key to get the Label* out of the scope where the
65 // label should be stored.
66 static const void* kDefaultToolchainKey
;
69 friend class base::RefCountedThreadSafe
<Loader
>;
73 class LoaderImpl
: public Loader
{
75 // Callback to emulate InputFileManager::AsyncLoadFile.
76 typedef base::Callback
<bool(const LocationRange
&,
79 const base::Callback
<void(const ParseNode
*)>&,
80 Err
*)> AsyncLoadFileCallback
;
82 explicit LoaderImpl(const BuildSettings
* build_settings
);
84 // Loader implementation.
85 void Load(const SourceFile
& file
,
86 const LocationRange
& origin
,
87 const Label
& toolchain_name
) override
;
88 void ToolchainLoaded(const Toolchain
* toolchain
) override
;
89 Label
GetDefaultToolchain() const override
;
90 const Settings
* GetToolchainSettings(const Label
& label
) const override
;
92 // Sets the message loop corresponding to the main thread. By default this
93 // class will use the thread active during construction, but there is not
94 // a message loop active during construction all the time.
95 void set_main_loop(base::MessageLoop
* loop
) { main_loop_
= loop
; }
97 // The complete callback is called whenever there are no more pending loads.
98 // Called on the main thread only. This may be called more than once if the
99 // queue is drained, but then more stuff gets added.
100 void set_complete_callback(const base::Closure
& cb
) {
101 complete_callback_
= cb
;
104 // This callback is used when the loader finds it wants to load a file.
105 void set_async_load_file(const AsyncLoadFileCallback
& cb
) {
106 async_load_file_
= cb
;
109 const Label
& default_toolchain_label() const {
110 return default_toolchain_label_
;
115 struct ToolchainRecord
;
117 ~LoaderImpl() override
;
119 // Schedules the input file manager to load the given file.
120 void ScheduleLoadFile(const Settings
* settings
,
121 const LocationRange
& origin
,
122 const SourceFile
& file
);
123 void ScheduleLoadBuildConfig(
125 const Scope::KeyValueMap
& toolchain_overrides
);
127 // Runs the given file on the background thread. These are called by the
128 // input file manager.
129 void BackgroundLoadFile(const Settings
* settings
,
130 const SourceFile
& file_name
,
131 const ParseNode
* root
);
132 void BackgroundLoadBuildConfig(
134 const Scope::KeyValueMap
& toolchain_overrides
,
135 const ParseNode
* root
);
137 // Posted to the main thread when any file other than a build config file
138 // file has completed running.
141 // Posted to the main thread when any build config file has completed
142 // running. The label should be the name of the toolchain.
144 // If there is no defauled toolchain loaded yet, we'll assume that the first
145 // call to this indicates to the default toolchain, and this function will
146 // set the default toolchain name to the given label.
147 void DidLoadBuildConfig(const Label
& label
);
149 // Decrements the pending_loads_ variable and issues the complete callback if
151 void DecrementPendingLoads();
153 // Forwards to the appropriate location to load the file.
154 bool AsyncLoadFile(const LocationRange
& origin
,
155 const BuildSettings
* build_settings
,
156 const SourceFile
& file_name
,
157 const base::Callback
<void(const ParseNode
*)>& callback
,
160 base::MessageLoop
* main_loop_
;
163 base::Closure complete_callback_
;
165 // When non-null, use this callback instead of the InputFileManager for
167 AsyncLoadFileCallback async_load_file_
;
169 typedef std::set
<LoadID
> LoadIDSet
;
170 LoadIDSet invocations_
;
172 const BuildSettings
* build_settings_
;
173 Label default_toolchain_label_
;
175 // Records for the build config file loads.
177 typedef std::map
<Label
, ToolchainRecord
*> ToolchainRecordMap
;
178 ToolchainRecordMap toolchain_records_
;
181 #endif // TOOLS_GN_LOADER_H_