[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / tools / gn / args.h
blob1f9cb52f7ded8f52c5be389b594e0abfbff307e4
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/containers/hash_tables.h"
9 #include "base/macros.h"
10 #include "base/synchronization/lock.h"
11 #include "tools/gn/scope.h"
13 class Err;
15 extern const char kBuildArgs_Help[];
17 // Manages build arguments. It stores the global arguments specified on the
18 // command line, and sets up the root scope with the proper values.
20 // This class tracks accesses so we can report errors about unused variables.
21 // The use case is if the user specifies an override on the command line, but
22 // no buildfile actually uses that variable. We want to be able to report that
23 // the argument was unused.
24 class Args {
25 public:
26 Args();
27 Args(const Args& other);
28 ~Args();
30 // Specifies overrides of the build arguments. These are normally specified
31 // on the command line.
32 void AddArgOverride(const char* name, const Value& value);
33 void AddArgOverrides(const Scope::KeyValueMap& overrides);
35 // Returns the value corresponding to the given argument name, or NULL if no
36 // argument is set.
37 const Value* GetArgOverride(const char* name) const;
39 // Gets all overrides set on the build.
40 Scope::KeyValueMap GetAllOverrides() const;
42 // Sets up the root scope for a toolchain. This applies the default system
43 // flags, then any overrides stored in this object, then applies any
44 // toolchain overrides specified in the argument.
45 void SetupRootScope(Scope* dest,
46 const Scope::KeyValueMap& toolchain_overrides) const;
48 // Sets up the given scope with arguments passed in.
50 // If the values specified in the args are not already set, the values in
51 // the args list will be used (which are assumed to be the defaults), but
52 // they will not override the system defaults or the current overrides.
54 // All args specified in the input will be marked as "used".
56 // On failure, the err will be set and it will return false.
57 bool DeclareArgs(const Scope::KeyValueMap& args,
58 Scope* scope_to_set,
59 Err* err) const;
61 // Checks to see if any of the overrides ever used were never declared as
62 // arguments. If there are, this returns false and sets the error.
63 bool VerifyAllOverridesUsed(Err* err) const;
65 // Adds all declared arguments to the given output list. If the values exist
66 // in the list already, their values will be overwriten, but other values
67 // already in the list will remain.
68 void MergeDeclaredArguments(Scope::KeyValueMap* dest) const;
70 private:
71 using DeclaredArgumentsPerToolchain =
72 base::hash_map<const Settings*, Scope::KeyValueMap>;
74 // Sets the default config based on the current system.
75 void SetSystemVarsLocked(Scope* scope) const;
77 // Sets the given vars on the given scope.
78 void ApplyOverridesLocked(const Scope::KeyValueMap& values,
79 Scope* scope) const;
81 void SaveOverrideRecordLocked(const Scope::KeyValueMap& values) const;
83 // Returns the KeyValueMap used for arguments declared for the specified
84 // toolchain.
85 Scope::KeyValueMap& DeclaredArgumentsForToolchainLocked(Scope* scope) const;
87 // Since this is called during setup which we assume is single-threaded,
88 // this is not protected by the lock. It should be set only during init.
89 Scope::KeyValueMap overrides_;
91 mutable base::Lock lock_;
93 // Maintains a list of all overrides we've ever seen. This is the main
94 // |overrides_| as well as toolchain overrides. Tracking this allows us to
95 // check for overrides that were specified but never used.
96 mutable Scope::KeyValueMap all_overrides_;
98 // Maps from Settings (which corresponds to a toolchain) to the map of
99 // declared variables. This is used to tracks all variables declared in any
100 // buildfile. This is so we can see if the user set variables on the command
101 // line that are not used anywhere. Each map is toolchain specific as each
102 // toolchain may define variables in different locations.
103 mutable DeclaredArgumentsPerToolchain declared_arguments_per_toolchain_;
105 DISALLOW_ASSIGN(Args);
108 #endif // TOOLS_GN_ARGS_H_