[Session restore] Rename group name Enabled to Restore.
[chromium-blink-merge.git] / tools / gn / args.h
blob345516cb8148f034474dc2f8138bc77701277220
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_ARGS_H_
6 #define TOOLS_GN_ARGS_H_
8 #include "base/basictypes.h"
9 #include "base/containers/hash_tables.h"
10 #include "base/synchronization/lock.h"
11 #include "tools/gn/err.h"
12 #include "tools/gn/scope.h"
14 extern const char kBuildArgs_Help[];
16 // Manages build arguments. It stores the global arguments specified on the
17 // command line, and sets up the root scope with the proper values.
19 // This class tracks accesses so we can report errors about unused variables.
20 // The use case is if the user specifies an override on the command line, but
21 // no buildfile actually uses that variable. We want to be able to report that
22 // the argument was unused.
23 class Args {
24 public:
25 Args();
26 Args(const Args& other);
27 ~Args();
29 // Specifies overrides of the build arguments. These are normally specified
30 // on the command line.
31 void AddArgOverride(const char* name, const Value& value);
32 void AddArgOverrides(const Scope::KeyValueMap& overrides);
34 // Returns the value corresponding to the given argument name, or NULL if no
35 // argument is set.
36 const Value* GetArgOverride(const char* name) const;
38 // Gets all overrides set on the build.
39 Scope::KeyValueMap GetAllOverrides() const;
41 // Sets up the root scope for a toolchain. This applies the default system
42 // flags, then any overrides stored in this object, then applies any
43 // toolchain overrides specified in the argument.
44 void SetupRootScope(Scope* dest,
45 const Scope::KeyValueMap& toolchain_overrides) const;
47 // Sets up the given scope with arguments passed in.
49 // If the values specified in the args are not already set, the values in
50 // the args list will be used (which are assumed to be the defaults), but
51 // they will not override the system defaults or the current overrides.
53 // All args specified in the input will be marked as "used".
55 // On failure, the err will be set and it will return false.
56 bool DeclareArgs(const Scope::KeyValueMap& args,
57 Scope* scope_to_set,
58 Err* err) const;
60 // Checks to see if any of the overrides ever used were never declared as
61 // arguments. If there are, this returns false and sets the error.
62 bool VerifyAllOverridesUsed(Err* err) const;
64 // Adds all declared arguments to the given output list. If the values exist
65 // in the list already, their values will be overwriten, but other values
66 // already in the list will remain.
67 void MergeDeclaredArguments(Scope::KeyValueMap* dest) const;
69 private:
70 using DeclaredArgumentsPerToolchain =
71 base::hash_map<const Settings*, Scope::KeyValueMap>;
73 // Sets the default config based on the current system.
74 void SetSystemVarsLocked(Scope* scope) const;
76 // Sets the given vars on the given scope.
77 void ApplyOverridesLocked(const Scope::KeyValueMap& values,
78 Scope* scope) const;
80 void SaveOverrideRecordLocked(const Scope::KeyValueMap& values) const;
82 // Returns the KeyValueMap used for arguments declared for the specified
83 // toolchain.
84 Scope::KeyValueMap& DeclaredArgumentsForToolchainLocked(Scope* scope) const;
86 // Since this is called during setup which we assume is single-threaded,
87 // this is not protected by the lock. It should be set only during init.
88 Scope::KeyValueMap overrides_;
90 mutable base::Lock lock_;
92 // Maintains a list of all overrides we've ever seen. This is the main
93 // |overrides_| as well as toolchain overrides. Tracking this allows us to
94 // check for overrides that were specified but never used.
95 mutable Scope::KeyValueMap all_overrides_;
97 // Maps from Settings (which corresponds to a toolchain) to the map of
98 // declared variables. This is used to tracks all variables declared in any
99 // buildfile. This is so we can see if the user set variables on the command
100 // line that are not used anywhere. Each map is toolchain specific as each
101 // toolchain may define variables in different locations.
102 mutable DeclaredArgumentsPerToolchain declared_arguments_per_toolchain_;
104 Args& operator=(const Args& other); // Disallow assignment.
107 #endif // TOOLS_GN_ARGS_H_