blob: d83c6789793a541b46386d07e3eeeec941c6102f [file] [log] [blame]
[email protected]60749e1c2013-08-19 21:11:051// 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.
4
5#include "tools/gn/args.h"
6
thakis3dd547102014-12-17 20:05:027#include "base/sys_info.h"
[email protected]145e5a12013-10-18 21:57:138#include "build/build_config.h"
[email protected]60749e1c2013-08-19 21:11:059#include "tools/gn/variables.h"
10
11const char kBuildArgs_Help[] =
[email protected]ea3690c2013-09-23 17:59:2212 "Build Arguments Overview\n"
[email protected]60749e1c2013-08-19 21:11:0513 "\n"
14 " Build arguments are variables passed in from outside of the build\n"
15 " that build files can query to determine how the build works.\n"
16 "\n"
[email protected]ea3690c2013-09-23 17:59:2217 "How build arguments are set\n"
[email protected]60749e1c2013-08-19 21:11:0518 "\n"
19 " First, system default arguments are set based on the current system.\n"
20 " The built-in arguments are:\n"
dpranke74add9fc2015-02-19 20:21:0021 " - host_cpu\n"
22 " - host_os\n"
23 " - current_cpu\n"
24 " - current_os\n"
25 " - target_cpu\n"
26 " - target_os\n"
[email protected]60749e1c2013-08-19 21:11:0527 "\n"
[email protected]d5645f12014-05-03 04:32:1928 " If specified, arguments from the --args command line flag are used. If\n"
29 " that flag is not specified, args from previous builds in the build\n"
30 " directory will be used (this is in the file args.gn in the build\n"
31 " directory).\n"
[email protected]60749e1c2013-08-19 21:11:0532 "\n"
[email protected]d5645f12014-05-03 04:32:1933 " Last, for targets being compiled with a non-default toolchain, the\n"
34 " toolchain overrides are applied. These are specified in the\n"
[email protected]60749e1c2013-08-19 21:11:0535 " toolchain_args section of a toolchain definition. The use-case for\n"
36 " this is that a toolchain may be building code for a different\n"
37 " platform, and that it may want to always specify Posix, for example.\n"
38 " See \"gn help toolchain_args\" for more.\n"
39 "\n"
[email protected]d5645f12014-05-03 04:32:1940 " If you specify an override for a build argument that never appears in\n"
41 " a \"declare_args\" call, a nonfatal error will be displayed.\n"
42 "\n"
43 "Examples\n"
44 "\n"
45 " gn args out/FooBar\n"
46 " Create the directory out/FooBar and open an editor. You would type\n"
47 " something like this into that file:\n"
48 " enable_doom_melon=false\n"
49 " os=\"android\"\n"
50 "\n"
51 " gn gen out/FooBar --args=\"enable_doom_melon=true os=\\\"android\\\"\"\n"
52 " This will overwrite the build directory with the given arguments.\n"
53 " (Note that the quotes inside the args command will usually need to\n"
54 " be escaped for your shell to pass through strings values.)\n"
[email protected]60749e1c2013-08-19 21:11:0555 "\n"
[email protected]ea3690c2013-09-23 17:59:2256 "How build arguments are used\n"
[email protected]60749e1c2013-08-19 21:11:0557 "\n"
58 " If you want to use an argument, you use declare_args() and specify\n"
59 " default values. These default values will apply if none of the steps\n"
60 " listed in the \"How build arguments are set\" section above apply to\n"
61 " the given argument, but the defaults will not override any of these.\n"
62 "\n"
63 " Often, the root build config file will declare global arguments that\n"
64 " will be passed to all buildfiles. Individual build files can also\n"
[email protected]b8aa81242014-01-05 11:59:5165 " specify arguments that apply only to those files. It is also useful\n"
[email protected]60749e1c2013-08-19 21:11:0566 " to specify build args in an \"import\"-ed file if you want such\n"
67 " arguments to apply to multiple buildfiles.\n";
68
skyca5cccef2015-03-16 18:06:0069namespace {
70
71// Removes all entries in |overrides| that are in |declared_overrides|.
72void RemoveDeclaredOverrides(const Scope::KeyValueMap& declared_arguments,
73 Scope::KeyValueMap* overrides) {
74 for (Scope::KeyValueMap::iterator override = overrides->begin();
75 override != overrides->end();) {
76 if (declared_arguments.find(override->first) == declared_arguments.end())
77 ++override;
78 else
79 overrides->erase(override++);
80 }
81}
82
83} // namespace
84
[email protected]60749e1c2013-08-19 21:11:0585Args::Args() {
86}
87
[email protected]e3730f812013-10-16 16:46:1488Args::Args(const Args& other)
89 : overrides_(other.overrides_),
90 all_overrides_(other.all_overrides_),
skyca5cccef2015-03-16 18:06:0091 declared_arguments_per_toolchain_(
92 other.declared_arguments_per_toolchain_) {
[email protected]e3730f812013-10-16 16:46:1493}
94
[email protected]60749e1c2013-08-19 21:11:0595Args::~Args() {
96}
97
[email protected]e3730f812013-10-16 16:46:1498void Args::AddArgOverride(const char* name, const Value& value) {
[email protected]0aa183b2014-02-06 08:23:4099 base::AutoLock lock(lock_);
100
[email protected]e3730f812013-10-16 16:46:14101 overrides_[base::StringPiece(name)] = value;
102 all_overrides_[base::StringPiece(name)] = value;
103}
104
[email protected]0a79fe42013-08-29 21:06:26105void Args::AddArgOverrides(const Scope::KeyValueMap& overrides) {
[email protected]0aa183b2014-02-06 08:23:40106 base::AutoLock lock(lock_);
107
brettwd1033b62014-09-30 21:44:05108 for (const auto& cur_override : overrides) {
109 overrides_[cur_override.first] = cur_override.second;
110 all_overrides_[cur_override.first] = cur_override.second;
[email protected]0a79fe42013-08-29 21:06:26111 }
[email protected]60749e1c2013-08-19 21:11:05112}
113
[email protected]8d039c62014-02-03 12:04:33114const Value* Args::GetArgOverride(const char* name) const {
[email protected]0aa183b2014-02-06 08:23:40115 base::AutoLock lock(lock_);
116
[email protected]8d039c62014-02-03 12:04:33117 Scope::KeyValueMap::const_iterator found =
118 all_overrides_.find(base::StringPiece(name));
119 if (found == all_overrides_.end())
tfarina9b636af2014-12-23 00:52:07120 return nullptr;
[email protected]8d039c62014-02-03 12:04:33121 return &found->second;
122}
123
[email protected]60fe5092014-02-13 18:03:43124Scope::KeyValueMap Args::GetAllOverrides() const {
125 base::AutoLock lock(lock_);
126 return all_overrides_;
127}
128
[email protected]60749e1c2013-08-19 21:11:05129void Args::SetupRootScope(Scope* dest,
130 const Scope::KeyValueMap& toolchain_overrides) const {
[email protected]0aa183b2014-02-06 08:23:40131 base::AutoLock lock(lock_);
132
133 SetSystemVarsLocked(dest);
134 ApplyOverridesLocked(overrides_, dest);
135 ApplyOverridesLocked(toolchain_overrides, dest);
136 SaveOverrideRecordLocked(toolchain_overrides);
[email protected]60749e1c2013-08-19 21:11:05137}
138
139bool Args::DeclareArgs(const Scope::KeyValueMap& args,
140 Scope* scope_to_set,
141 Err* err) const {
142 base::AutoLock lock(lock_);
143
skyca5cccef2015-03-16 18:06:00144 Scope::KeyValueMap& declared_arguments(
145 DeclaredArgumentsForToolchainLocked(scope_to_set));
brettwd1033b62014-09-30 21:44:05146 for (const auto& arg : args) {
[email protected]60749e1c2013-08-19 21:11:05147 // Verify that the value hasn't already been declared. We want each value
148 // to be declared only once.
149 //
150 // The tricky part is that a buildfile can be interpreted multiple times
151 // when used from different toolchains, so we can't just check that we've
[email protected]55a3dd762014-02-18 20:24:40152 // seen it before. Instead, we check that the location matches.
[email protected]60749e1c2013-08-19 21:11:05153 Scope::KeyValueMap::iterator previously_declared =
skyca5cccef2015-03-16 18:06:00154 declared_arguments.find(arg.first);
155 if (previously_declared != declared_arguments.end()) {
brettwd1033b62014-09-30 21:44:05156 if (previously_declared->second.origin() != arg.second.origin()) {
[email protected]60749e1c2013-08-19 21:11:05157 // Declaration location mismatch.
brettwd1033b62014-09-30 21:44:05158 *err = Err(arg.second.origin(),
159 "Duplicate build argument declaration.",
[email protected]60749e1c2013-08-19 21:11:05160 "Here you're declaring an argument that was already declared "
161 "elsewhere.\nYou can only declare each argument once in the entire "
162 "build so there is one\ncanonical place for documentation and the "
163 "default value. Either move this\nargument to the build config "
164 "file (for visibility everywhere) or to a .gni file\nthat you "
165 "\"import\" from the files where you need it (preferred).");
166 err->AppendSubErr(Err(previously_declared->second.origin(),
167 "Previous declaration.",
168 "See also \"gn help buildargs\" for more on how "
[email protected]b8aa81242014-01-05 11:59:51169 "build arguments work."));
[email protected]60749e1c2013-08-19 21:11:05170 return false;
[email protected]60749e1c2013-08-19 21:11:05171 }
172 } else {
skyca5cccef2015-03-16 18:06:00173 declared_arguments.insert(arg);
[email protected]60749e1c2013-08-19 21:11:05174 }
175
176 // Only set on the current scope to the new value if it hasn't been already
[email protected]42b80ef2013-10-29 23:43:57177 // set. Mark the variable used so the build script can override it in
178 // certain cases without getting unused value errors.
brettwd1033b62014-09-30 21:44:05179 if (!scope_to_set->GetValue(arg.first)) {
180 scope_to_set->SetValue(arg.first, arg.second, arg.second.origin());
181 scope_to_set->MarkUsed(arg.first);
[email protected]42b80ef2013-10-29 23:43:57182 }
[email protected]60749e1c2013-08-19 21:11:05183 }
184
185 return true;
186}
187
188bool Args::VerifyAllOverridesUsed(Err* err) const {
189 base::AutoLock lock(lock_);
skyca5cccef2015-03-16 18:06:00190 Scope::KeyValueMap all_overrides(all_overrides_);
191 for (const auto& map_pair : declared_arguments_per_toolchain_)
192 RemoveDeclaredOverrides(map_pair.second, &all_overrides);
[email protected]60749e1c2013-08-19 21:11:05193
skyca5cccef2015-03-16 18:06:00194 if (all_overrides.empty())
195 return true;
[email protected]0aa183b2014-02-06 08:23:40196
skyca5cccef2015-03-16 18:06:00197 *err = Err(
198 all_overrides.begin()->second.origin(), "Build argument has no effect.",
199 "The variable \"" + all_overrides.begin()->first.as_string() +
200 "\" was set as a build argument\nbut never appeared in a " +
brettw12985c02016-04-24 19:03:22201 "declare_args() block in any buildfile.\n\n"
202 "To view possible args, run \"gn args --list <builddir>\"");
skyca5cccef2015-03-16 18:06:00203 return false;
[email protected]60749e1c2013-08-19 21:11:05204}
205
[email protected]60fe5092014-02-13 18:03:43206void Args::MergeDeclaredArguments(Scope::KeyValueMap* dest) const {
207 base::AutoLock lock(lock_);
skyca5cccef2015-03-16 18:06:00208 for (const auto& map_pair : declared_arguments_per_toolchain_) {
209 for (const auto& arg : map_pair.second)
210 (*dest)[arg.first] = arg.second;
211 }
[email protected]60fe5092014-02-13 18:03:43212}
213
[email protected]0aa183b2014-02-06 08:23:40214void Args::SetSystemVarsLocked(Scope* dest) const {
[email protected]60fe5092014-02-13 18:03:43215 lock_.AssertAcquired();
216
[email protected]145e5a12013-10-18 21:57:13217 // Host OS.
tfarina9b636af2014-12-23 00:52:07218 const char* os = nullptr;
[email protected]60749e1c2013-08-19 21:11:05219#if defined(OS_WIN)
[email protected]145e5a12013-10-18 21:57:13220 os = "win";
221#elif defined(OS_MACOSX)
222 os = "mac";
223#elif defined(OS_LINUX)
224 os = "linux";
[email protected]a74ddbc2014-05-13 17:41:06225#elif defined(OS_ANDROID)
226 os = "android";
[email protected]60749e1c2013-08-19 21:11:05227#else
[email protected]145e5a12013-10-18 21:57:13228 #error Unknown OS type.
[email protected]60749e1c2013-08-19 21:11:05229#endif
[email protected]60749e1c2013-08-19 21:11:05230
[email protected]145e5a12013-10-18 21:57:13231 // Host architecture.
[email protected]777ad7f2013-11-22 22:38:39232 static const char kX86[] = "x86";
233 static const char kX64[] = "x64";
thakis3dd547102014-12-17 20:05:02234 static const char kArm[] = "arm";
milko.leporis8ea3910c72016-04-18 07:51:18235 static const char kMips[] = "mipsel";
tfarina9b636af2014-12-23 00:52:07236 const char* arch = nullptr;
thakis3dd547102014-12-17 20:05:02237
dpranke74add9fc2015-02-19 20:21:00238 // Set the host CPU architecture based on the underlying OS, not
[email protected]145e5a12013-10-18 21:57:13239 // whatever the current bit-tedness of the GN binary is.
thakis3dd547102014-12-17 20:05:02240 std::string os_arch = base::SysInfo::OperatingSystemArchitecture();
241 if (os_arch == "x86")
[email protected]777ad7f2013-11-22 22:38:39242 arch = kX86;
thakis3dd547102014-12-17 20:05:02243 else if (os_arch == "x86_64")
244 arch = kX64;
saiarcot895b68180a2016-04-28 22:25:01245 else if (os_arch.substr(0, 3) == "arm")
[email protected]145e5a12013-10-18 21:57:13246 arch = kArm;
milko.leporis8ea3910c72016-04-18 07:51:18247 else if (os_arch == "mips")
248 arch = kMips;
thakis3dd547102014-12-17 20:05:02249 else
250 CHECK(false) << "OS architecture not handled.";
[email protected]60749e1c2013-08-19 21:11:05251
[email protected]dbd915b42013-10-25 22:23:17252 // Save the OS and architecture as build arguments that are implicitly
253 // declared. This is so they can be overridden in a toolchain build args
254 // override, and so that they will appear in the "gn args" output.
dpranke74add9fc2015-02-19 20:21:00255 Value empty_string(nullptr, std::string());
256
257 Value os_val(nullptr, std::string(os));
258 dest->SetValue(variables::kHostOs, os_val, nullptr);
259 dest->SetValue(variables::kTargetOs, empty_string, nullptr);
260 dest->SetValue(variables::kCurrentOs, empty_string, nullptr);
261
262 Value arch_val(nullptr, std::string(arch));
263 dest->SetValue(variables::kHostCpu, arch_val, nullptr);
264 dest->SetValue(variables::kTargetCpu, empty_string, nullptr);
265 dest->SetValue(variables::kCurrentCpu, empty_string, nullptr);
266
skyca5cccef2015-03-16 18:06:00267 Scope::KeyValueMap& declared_arguments(
268 DeclaredArgumentsForToolchainLocked(dest));
269 declared_arguments[variables::kHostOs] = os_val;
270 declared_arguments[variables::kCurrentOs] = empty_string;
271 declared_arguments[variables::kTargetOs] = empty_string;
272 declared_arguments[variables::kHostCpu] = arch_val;
273 declared_arguments[variables::kCurrentCpu] = empty_string;
274 declared_arguments[variables::kTargetCpu] = empty_string;
dpranke74add9fc2015-02-19 20:21:00275
[email protected]145e5a12013-10-18 21:57:13276 // Mark these variables used so the build config file can override them
277 // without geting a warning about overwriting an unused variable.
dpranke74add9fc2015-02-19 20:21:00278 dest->MarkUsed(variables::kHostCpu);
279 dest->MarkUsed(variables::kCurrentCpu);
280 dest->MarkUsed(variables::kTargetCpu);
281 dest->MarkUsed(variables::kHostOs);
282 dest->MarkUsed(variables::kCurrentOs);
283 dest->MarkUsed(variables::kTargetOs);
[email protected]60749e1c2013-08-19 21:11:05284}
285
[email protected]0aa183b2014-02-06 08:23:40286void Args::ApplyOverridesLocked(const Scope::KeyValueMap& values,
287 Scope* scope) const {
[email protected]60fe5092014-02-13 18:03:43288 lock_.AssertAcquired();
brettwd1033b62014-09-30 21:44:05289 for (const auto& val : values)
290 scope->SetValue(val.first, val.second, val.second.origin());
[email protected]60749e1c2013-08-19 21:11:05291}
292
[email protected]0aa183b2014-02-06 08:23:40293void Args::SaveOverrideRecordLocked(const Scope::KeyValueMap& values) const {
[email protected]60fe5092014-02-13 18:03:43294 lock_.AssertAcquired();
brettwd1033b62014-09-30 21:44:05295 for (const auto& val : values)
296 all_overrides_[val.first] = val.second;
[email protected]60749e1c2013-08-19 21:11:05297}
skyca5cccef2015-03-16 18:06:00298
299Scope::KeyValueMap& Args::DeclaredArgumentsForToolchainLocked(
300 Scope* scope) const {
301 lock_.AssertAcquired();
302 return declared_arguments_per_toolchain_[scope->settings()];
303}