Chromium Code Reviews| 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 #include "tools/gn/args.h" | 5 #include "tools/gn/args.h" |
| 6 | 6 |
| 7 #include "base/sys_info.h" | 7 #include "base/sys_info.h" |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 #include "tools/gn/variables.h" | 9 #include "tools/gn/variables.h" |
| 10 | 10 |
| 11 const char kBuildArgs_Help[] = | 11 const char kBuildArgs_Help[] = |
| 12 "Build Arguments Overview\n" | 12 "Build Arguments Overview\n" |
| 13 "\n" | 13 "\n" |
| 14 " Build arguments are variables passed in from outside of the build\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" | 15 " that build files can query to determine how the build works.\n" |
| 16 "\n" | 16 "\n" |
| 17 "How build arguments are set\n" | 17 "How build arguments are set\n" |
| 18 "\n" | 18 "\n" |
| 19 " First, system default arguments are set based on the current system.\n" | 19 " First, system default arguments are set based on the current system.\n" |
| 20 " The built-in arguments are:\n" | 20 " The built-in arguments are:\n" |
| 21 " - cpu_arch (by default this is the same as \"default_cpu_arch\")\n" | 21 " - build_cpu_arch\n" |
| 22 " - default_cpu_arch\n" | 22 " - build_os\n" |
| 23 " - default_os\n" | 23 " - cpu_arch (by default this is the same as \"build_cpu_arch\")\n" |
| 24 " - os (by default this is the same as \"default_os\")\n" | 24 " - os (by default this is the same as \"build_os\")\n" |
| 25 "\n" | 25 "\n" |
| 26 " If specified, arguments from the --args command line flag are used. If\n" | 26 " If specified, arguments from the --args command line flag are used. If\n" |
| 27 " that flag is not specified, args from previous builds in the build\n" | 27 " that flag is not specified, args from previous builds in the build\n" |
| 28 " directory will be used (this is in the file args.gn in the build\n" | 28 " directory will be used (this is in the file args.gn in the build\n" |
| 29 " directory).\n" | 29 " directory).\n" |
| 30 "\n" | 30 "\n" |
| 31 " Last, for targets being compiled with a non-default toolchain, the\n" | 31 " Last, for targets being compiled with a non-default toolchain, the\n" |
| 32 " toolchain overrides are applied. These are specified in the\n" | 32 " toolchain overrides are applied. These are specified in the\n" |
| 33 " toolchain_args section of a toolchain definition. The use-case for\n" | 33 " toolchain_args section of a toolchain definition. The use-case for\n" |
| 34 " this is that a toolchain may be building code for a different\n" | 34 " this is that a toolchain may be building code for a different\n" |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 ApplyOverridesLocked(toolchain_overrides, dest); | 116 ApplyOverridesLocked(toolchain_overrides, dest); |
| 117 SaveOverrideRecordLocked(toolchain_overrides); | 117 SaveOverrideRecordLocked(toolchain_overrides); |
| 118 } | 118 } |
| 119 | 119 |
| 120 bool Args::DeclareArgs(const Scope::KeyValueMap& args, | 120 bool Args::DeclareArgs(const Scope::KeyValueMap& args, |
| 121 Scope* scope_to_set, | 121 Scope* scope_to_set, |
| 122 Err* err) const { | 122 Err* err) const { |
| 123 base::AutoLock lock(lock_); | 123 base::AutoLock lock(lock_); |
| 124 | 124 |
| 125 for (const auto& arg : args) { | 125 for (const auto& arg : args) { |
| 126 std::string arg_name = arg.first.as_string(); | |
| 127 | |
| 128 // TODO(dpranke): Figure out how to allow a declare_args() block | |
| 129 // to update the value of a built-in variable. I think we want | |
| 130 // something like: | |
| 131 // | |
| 132 // if (arg_name == variables::kBuildCpuArch || | |
| 133 // arg_name == variables::kBuildOs || | |
| 134 // arg_name == variables::kCpuArch || | |
| 135 // arg_name == variables::kOs || | |
| 136 // arg_name == variables::kTargetCpuArch || | |
| 137 // arg_name == variables::kTargetOs) { | |
| 138 // Scope::KeyValueMap::iterator previously_declared = | |
| 139 // declared_arguments_.find(arg.first); | |
| 140 // CHECK(previously_declared != declared_arguments_.end()); | |
| 141 // if (previously_declared->second.origin() == nullptr) { | |
| 142 // previously_declared->second.set_origin(arg.second.origin()); | |
| 143 // // TODO: update the built-in variable's value somehow. | |
| 144 // } | |
| 145 // } | |
| 146 | |
|
Dirk Pranke
2015/02/11 02:34:31
Brett: I couldn't figure out how to make this work
| |
| 126 // Verify that the value hasn't already been declared. We want each value | 147 // Verify that the value hasn't already been declared. We want each value |
| 127 // to be declared only once. | 148 // to be declared only once. |
| 128 // | 149 // |
| 129 // The tricky part is that a buildfile can be interpreted multiple times | 150 // The tricky part is that a buildfile can be interpreted multiple times |
| 130 // when used from different toolchains, so we can't just check that we've | 151 // when used from different toolchains, so we can't just check that we've |
| 131 // seen it before. Instead, we check that the location matches. | 152 // seen it before. Instead, we check that the location matches. |
| 132 Scope::KeyValueMap::iterator previously_declared = | 153 Scope::KeyValueMap::iterator previously_declared = |
| 133 declared_arguments_.find(arg.first); | 154 declared_arguments_.find(arg.first); |
| 134 if (previously_declared != declared_arguments_.end()) { | 155 if (previously_declared != declared_arguments_.end()) { |
| 135 if (previously_declared->second.origin() != arg.second.origin()) { | 156 if (previously_declared->second.origin() != arg.second.origin()) { |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 217 #elif defined(OS_LINUX) | 238 #elif defined(OS_LINUX) |
| 218 os = "linux"; | 239 os = "linux"; |
| 219 #elif defined(OS_ANDROID) | 240 #elif defined(OS_ANDROID) |
| 220 os = "android"; | 241 os = "android"; |
| 221 #else | 242 #else |
| 222 #error Unknown OS type. | 243 #error Unknown OS type. |
| 223 #endif | 244 #endif |
| 224 Value os_val(nullptr, std::string(os)); | 245 Value os_val(nullptr, std::string(os)); |
| 225 dest->SetValue(variables::kBuildOs, os_val, nullptr); | 246 dest->SetValue(variables::kBuildOs, os_val, nullptr); |
| 226 dest->SetValue(variables::kOs, os_val, nullptr); | 247 dest->SetValue(variables::kOs, os_val, nullptr); |
| 248 dest->SetValue(variables::kTargetOs, os_val, nullptr); | |
| 227 | 249 |
| 228 // Host architecture. | 250 // Host architecture. |
| 229 static const char kX86[] = "x86"; | 251 static const char kX86[] = "x86"; |
| 230 static const char kX64[] = "x64"; | 252 static const char kX64[] = "x64"; |
| 231 static const char kArm[] = "arm"; | 253 static const char kArm[] = "arm"; |
| 232 const char* arch = nullptr; | 254 const char* arch = nullptr; |
| 233 | 255 |
| 234 // Set the CPU architecture based on the underlying OS, not | 256 // Set the CPU architecture based on the underlying OS, not |
| 235 // whatever the current bit-tedness of the GN binary is. | 257 // whatever the current bit-tedness of the GN binary is. |
| 236 std::string os_arch = base::SysInfo::OperatingSystemArchitecture(); | 258 std::string os_arch = base::SysInfo::OperatingSystemArchitecture(); |
| 237 if (os_arch == "x86") | 259 if (os_arch == "x86") |
| 238 arch = kX86; | 260 arch = kX86; |
| 239 else if (os_arch == "x86_64") | 261 else if (os_arch == "x86_64") |
| 240 arch = kX64; | 262 arch = kX64; |
| 241 else if (os_arch.substr(3) == "arm") | 263 else if (os_arch.substr(3) == "arm") |
| 242 arch = kArm; | 264 arch = kArm; |
| 243 else | 265 else |
| 244 CHECK(false) << "OS architecture not handled."; | 266 CHECK(false) << "OS architecture not handled."; |
| 245 | 267 |
| 246 Value arch_val(nullptr, std::string(arch)); | 268 Value arch_val(nullptr, std::string(arch)); |
| 247 dest->SetValue(variables::kBuildCpuArch, arch_val, nullptr); | 269 dest->SetValue(variables::kBuildCpuArch, arch_val, nullptr); |
| 248 dest->SetValue(variables::kCpuArch, arch_val, nullptr); | 270 dest->SetValue(variables::kCpuArch, arch_val, nullptr); |
| 271 dest->SetValue(variables::kTargetCpuArch, arch_val, nullptr); | |
| 249 | 272 |
| 250 // Save the OS and architecture as build arguments that are implicitly | 273 // Save the OS and architecture as build arguments that are implicitly |
| 251 // declared. This is so they can be overridden in a toolchain build args | 274 // declared. This is so they can be overridden in a toolchain build args |
| 252 // override, and so that they will appear in the "gn args" output. | 275 // override, and so that they will appear in the "gn args" output. |
| 253 // | 276 // |
| 254 // Do not declare the build* variants since these shouldn't be changed. | |
| 255 // | |
| 256 // Mark these variables used so the build config file can override them | 277 // Mark these variables used so the build config file can override them |
| 257 // without geting a warning about overwriting an unused variable. | 278 // without geting a warning about overwriting an unused variable. |
| 279 declared_arguments_[variables::kBuildOs] = os_val; | |
| 258 declared_arguments_[variables::kOs] = os_val; | 280 declared_arguments_[variables::kOs] = os_val; |
| 281 declared_arguments_[variables::kTargetOs] = os_val; | |
| 282 declared_arguments_[variables::kBuildCpuArch] = arch_val; | |
| 259 declared_arguments_[variables::kCpuArch] = arch_val; | 283 declared_arguments_[variables::kCpuArch] = arch_val; |
| 284 declared_arguments_[variables::kTargetCpuArch] = arch_val; | |
| 285 dest->MarkUsed(variables::kBuildCpuArch); | |
| 260 dest->MarkUsed(variables::kCpuArch); | 286 dest->MarkUsed(variables::kCpuArch); |
| 287 dest->MarkUsed(variables::kTargetCpuArch); | |
| 288 dest->MarkUsed(variables::kBuildOs); | |
| 261 dest->MarkUsed(variables::kOs); | 289 dest->MarkUsed(variables::kOs); |
| 290 dest->MarkUsed(variables::kTargetOs); | |
| 262 } | 291 } |
| 263 | 292 |
| 264 void Args::ApplyOverridesLocked(const Scope::KeyValueMap& values, | 293 void Args::ApplyOverridesLocked(const Scope::KeyValueMap& values, |
| 265 Scope* scope) const { | 294 Scope* scope) const { |
| 266 lock_.AssertAcquired(); | 295 lock_.AssertAcquired(); |
| 267 for (const auto& val : values) | 296 for (const auto& val : values) |
| 268 scope->SetValue(val.first, val.second, val.second.origin()); | 297 scope->SetValue(val.first, val.second, val.second.origin()); |
| 269 } | 298 } |
| 270 | 299 |
| 271 void Args::SaveOverrideRecordLocked(const Scope::KeyValueMap& values) const { | 300 void Args::SaveOverrideRecordLocked(const Scope::KeyValueMap& values) const { |
| 272 lock_.AssertAcquired(); | 301 lock_.AssertAcquired(); |
| 273 for (const auto& val : values) | 302 for (const auto& val : values) |
| 274 all_overrides_[val.first] = val.second; | 303 all_overrides_[val.first] = val.second; |
| 275 } | 304 } |
| OLD | NEW |