OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef TOOLS_GN_ARGS_H_ | 5 #ifndef TOOLS_GN_ARGS_H_ |
6 #define TOOLS_GN_ARGS_H_ | 6 #define TOOLS_GN_ARGS_H_ |
7 | 7 |
| 8 #include <map> |
| 9 |
8 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
9 #include "base/macros.h" | 11 #include "base/macros.h" |
10 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
11 #include "tools/gn/scope.h" | 13 #include "tools/gn/scope.h" |
12 | 14 |
13 class Err; | 15 class Err; |
14 | 16 |
15 extern const char kBuildArgs_Help[]; | 17 extern const char kBuildArgs_Help[]; |
16 | 18 |
17 // Manages build arguments. It stores the global arguments specified on the | 19 // 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 // command line, and sets up the root scope with the proper values. |
19 // | 21 // |
20 // This class tracks accesses so we can report errors about unused variables. | 22 // 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 | 23 // 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 | 24 // no buildfile actually uses that variable. We want to be able to report that |
23 // the argument was unused. | 25 // the argument was unused. |
24 class Args { | 26 class Args { |
25 public: | 27 public: |
| 28 struct ValueWithOverride { |
| 29 ValueWithOverride(); |
| 30 ValueWithOverride(const Value& def_val); |
| 31 ~ValueWithOverride(); |
| 32 |
| 33 Value default_value; // Default value given in declare_args. |
| 34 |
| 35 bool has_override; // True indicates override_value is valid. |
| 36 Value override_value; // From .gn or the current build's "gn args". |
| 37 }; |
| 38 using ValueWithOverrideMap = std::map<base::StringPiece, ValueWithOverride>; |
| 39 |
26 Args(); | 40 Args(); |
27 Args(const Args& other); | 41 Args(const Args& other); |
28 ~Args(); | 42 ~Args(); |
29 | 43 |
30 // Specifies overrides of the build arguments. These are normally specified | 44 // Specifies overrides of the build arguments. These are normally specified |
31 // on the command line. | 45 // on the command line. |
32 void AddArgOverride(const char* name, const Value& value); | 46 void AddArgOverride(const char* name, const Value& value); |
33 void AddArgOverrides(const Scope::KeyValueMap& overrides); | 47 void AddArgOverrides(const Scope::KeyValueMap& overrides); |
34 | 48 |
35 // Returns the value corresponding to the given argument name, or NULL if no | 49 // Returns the value corresponding to the given argument name, or NULL if no |
36 // argument is set. | 50 // argument is set. |
37 const Value* GetArgOverride(const char* name) const; | 51 const Value* GetArgOverride(const char* name) const; |
38 | 52 |
39 // Gets all overrides set on the build. | |
40 Scope::KeyValueMap GetAllOverrides() const; | |
41 | |
42 // Sets up the root scope for a toolchain. This applies the default system | 53 // Sets up the root scope for a toolchain. This applies the default system |
43 // flags and saves the toolchain overrides so they can be applied to | 54 // flags and saves the toolchain overrides so they can be applied to |
44 // declare_args blocks that appear when loading files in that toolchain. | 55 // declare_args blocks that appear when loading files in that toolchain. |
45 void SetupRootScope(Scope* dest, | 56 void SetupRootScope(Scope* dest, |
46 const Scope::KeyValueMap& toolchain_overrides) const; | 57 const Scope::KeyValueMap& toolchain_overrides) const; |
47 | 58 |
48 // Sets up the given scope with arguments passed in. | 59 // Sets up the given scope with arguments passed in. |
49 // | 60 // |
50 // If the values specified in the args are not already set, the values in | 61 // 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 | 62 // 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. | 63 // they will not override the system defaults or the current overrides. |
53 // | 64 // |
54 // All args specified in the input will be marked as "used". | 65 // All args specified in the input will be marked as "used". |
55 // | 66 // |
56 // On failure, the err will be set and it will return false. | 67 // On failure, the err will be set and it will return false. |
57 bool DeclareArgs(const Scope::KeyValueMap& args, | 68 bool DeclareArgs(const Scope::KeyValueMap& args, |
58 Scope* scope_to_set, | 69 Scope* scope_to_set, |
59 Err* err) const; | 70 Err* err) const; |
60 | 71 |
61 // Checks to see if any of the overrides ever used were never declared as | 72 // 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. | 73 // arguments. If there are, this returns false and sets the error. |
63 bool VerifyAllOverridesUsed(Err* err) const; | 74 bool VerifyAllOverridesUsed(Err* err) const; |
64 | 75 |
65 // Adds all declared arguments to the given output list. If the values exist | 76 // Returns information about all arguements, both defaults and overrides. |
66 // in the list already, their values will be overwriten, but other values | 77 // This is used for the help system which is not performance critical. Use a |
67 // already in the list will remain. | 78 // map instead of a hash map so the arguements are sorted alphabetically. |
68 void MergeDeclaredArguments(Scope::KeyValueMap* dest) const; | 79 ValueWithOverrideMap GetAllArguments() const; |
69 | 80 |
70 private: | 81 private: |
71 using ArgumentsPerToolchain = | 82 using ArgumentsPerToolchain = |
72 base::hash_map<const Settings*, Scope::KeyValueMap>; | 83 base::hash_map<const Settings*, Scope::KeyValueMap>; |
73 | 84 |
74 // Sets the default config based on the current system. | 85 // Sets the default config based on the current system. |
75 void SetSystemVarsLocked(Scope* scope) const; | 86 void SetSystemVarsLocked(Scope* scope) const; |
76 | 87 |
77 // Sets the given already declared vars on the given scope. | 88 // Sets the given already declared vars on the given scope. |
78 void ApplyOverridesLocked(const Scope::KeyValueMap& values, | 89 void ApplyOverridesLocked(const Scope::KeyValueMap& values, |
(...skipping 29 matching lines...) Expand all Loading... |
108 | 119 |
109 // Overrides for individual toolchains. This is necessary so we | 120 // Overrides for individual toolchains. This is necessary so we |
110 // can apply the correct override for the current toolchain, once | 121 // can apply the correct override for the current toolchain, once |
111 // we see an argument declaration. | 122 // we see an argument declaration. |
112 mutable ArgumentsPerToolchain toolchain_overrides_; | 123 mutable ArgumentsPerToolchain toolchain_overrides_; |
113 | 124 |
114 DISALLOW_ASSIGN(Args); | 125 DISALLOW_ASSIGN(Args); |
115 }; | 126 }; |
116 | 127 |
117 #endif // TOOLS_GN_ARGS_H_ | 128 #endif // TOOLS_GN_ARGS_H_ |
OLD | NEW |