13551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
23551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
33551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// found in the LICENSE file.
43551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
53551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "tools/gn/args.h"
63551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
78bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "build/build_config.h"
83551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "tools/gn/variables.h"
93551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#if defined(OS_WIN)
118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#include "base/win/windows_version.h"
128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#endif
138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)const char kBuildArgs_Help[] =
1568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    "Build Arguments Overview\n"
163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "\n"
173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  Build arguments are variables passed in from outside of the build\n"
183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  that build files can query to determine how the build works.\n"
193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "\n"
2068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    "How build arguments are set\n"
213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "\n"
223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  First, system default arguments are set based on the current system.\n"
233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  The built-in arguments are:\n"
248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    "   - cpu_arch (by default this is the same as \"default_cpu_arch\")\n"
258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    "   - default_cpu_arch\n"
268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    "   - default_os\n"
278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    "   - os (by default this is the same as \"default_os\")\n"
283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "\n"
293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  Second, arguments specified on the command-line via \"--args\" are\n"
303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  applied. These can override the system default ones, and add new ones.\n"
313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  These are whitespace-separated. For example:\n"
323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "\n"
338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    "    gn --args=\"enable_doom_melon=false\" os=\\\"beos\\\"\n"
343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "\n"
353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  Third, toolchain overrides are applied. These are specified in the\n"
363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  toolchain_args section of a toolchain definition. The use-case for\n"
373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  this is that a toolchain may be building code for a different\n"
383551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  platform, and that it may want to always specify Posix, for example.\n"
393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  See \"gn help toolchain_args\" for more.\n"
403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "\n"
413551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  It is an error to specify an override for a build argument that never\n"
423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  appears in a \"declare_args\" call.\n"
433551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "\n"
4468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)    "How build arguments are used\n"
453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "\n"
463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  If you want to use an argument, you use declare_args() and specify\n"
473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  default values. These default values will apply if none of the steps\n"
483551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  listed in the \"How build arguments are set\" section above apply to\n"
493551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  the given argument, but the defaults will not override any of these.\n"
503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "\n"
513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  Often, the root build config file will declare global arguments that\n"
523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  will be passed to all buildfiles. Individual build files can also\n"
533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  specify arguments that apply only to those files. It is also usedful\n"
543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  to specify build args in an \"import\"-ed file if you want such\n"
553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    "  arguments to apply to multiple buildfiles.\n";
563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)Args::Args() {
583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
593551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)Args::Args(const Args& other)
614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    : overrides_(other.overrides_),
624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      all_overrides_(other.all_overrides_),
634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      declared_arguments_(other.declared_arguments_) {
644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)Args::~Args() {
673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void Args::AddArgOverride(const char* name, const Value& value) {
704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  overrides_[base::StringPiece(name)] = value;
714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  all_overrides_[base::StringPiece(name)] = value;
724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
74424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void Args::AddArgOverrides(const Scope::KeyValueMap& overrides) {
75424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  for (Scope::KeyValueMap::const_iterator i = overrides.begin();
76424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)       i != overrides.end(); ++i) {
774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    overrides_[i->first] = i->second;
784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    all_overrides_[i->first] = i->second;
79424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
803551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
813551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
823551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void Args::SetupRootScope(Scope* dest,
833551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                          const Scope::KeyValueMap& toolchain_overrides) const {
843551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  SetSystemVars(dest);
853551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ApplyOverrides(overrides_, dest);
863551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  ApplyOverrides(toolchain_overrides, dest);
873551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  SaveOverrideRecord(toolchain_overrides);
883551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
893551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
903551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)bool Args::DeclareArgs(const Scope::KeyValueMap& args,
913551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                       Scope* scope_to_set,
923551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                       Err* err) const {
933551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::AutoLock lock(lock_);
943551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
953551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  for (Scope::KeyValueMap::const_iterator i = args.begin();
963551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)       i != args.end(); ++i) {
973551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // Verify that the value hasn't already been declared. We want each value
983551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // to be declared only once.
993551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    //
1003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // The tricky part is that a buildfile can be interpreted multiple times
1013551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // when used from different toolchains, so we can't just check that we've
1023551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // seen it before. Instead, we check that the location matches. We
1033551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // additionally check that the value matches to prevent people from
1043551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // declaring defaults based on other parameters that may change. The
1053551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // rationale is that you should have exactly one default value for each
1063551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // argument that we can display in the help.
1073551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    Scope::KeyValueMap::iterator previously_declared =
1083551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        declared_arguments_.find(i->first);
1093551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (previously_declared != declared_arguments_.end()) {
1103551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      if (previously_declared->second.origin() != i->second.origin()) {
1113551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        // Declaration location mismatch.
1123551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        *err = Err(i->second.origin(), "Duplicate build arg declaration.",
1133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            "Here you're declaring an argument that was already declared "
1143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            "elsewhere.\nYou can only declare each argument once in the entire "
1153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            "build so there is one\ncanonical place for documentation and the "
1163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            "default value. Either move this\nargument to the build config "
1173551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            "file (for visibility everywhere) or to a .gni file\nthat you "
1183551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            "\"import\" from the files where you need it (preferred).");
1193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        err->AppendSubErr(Err(previously_declared->second.origin(),
1203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                              "Previous declaration.",
1213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                              "See also \"gn help buildargs\" for more on how "
1223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                              "build args work."));
1233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        return false;
1243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      } else if (previously_declared->second != i->second) {
1253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        // Default value mismatch.
1263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        *err = Err(i->second.origin(),
1273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            "Non-constant default value for build arg.",
1283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            "Each build arg should have one default value so we report it "
1293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            "nicely in the\n\"gn args\" command. Please make this value "
1303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)            "constant.");
1313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        return false;
1323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      }
1333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    } else {
1343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      declared_arguments_.insert(*i);
1353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    }
1363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    // Only set on the current scope to the new value if it hasn't been already
1381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // set. Mark the variable used so the build script can override it in
1391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    // certain cases without getting unused value errors.
1401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    if (!scope_to_set->GetValue(i->first)) {
1413551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      scope_to_set->SetValue(i->first, i->second, i->second.origin());
1421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      scope_to_set->MarkUsed(i->first);
1431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    }
1443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return true;
1473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
1483551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1493551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)bool Args::VerifyAllOverridesUsed(Err* err) const {
1503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::AutoLock lock(lock_);
1513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  for (Scope::KeyValueMap::const_iterator i = all_overrides_.begin();
1533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)       i != all_overrides_.end(); ++i) {
1543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (declared_arguments_.find(i->first) == declared_arguments_.end()) {
1553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      *err = Err(i->second.origin(), "Build arg has no effect.",
1563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          "The value \"" + i->first.as_string() + "\" was set a build "
1573551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          "argument\nbut never appeared in a declare_args() block in any "
1583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)          "buildfile.");
1593551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      return false;
1603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    }
1613551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
1623551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  return true;
1633551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
1643551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
1653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void Args::SetSystemVars(Scope* dest) const {
1668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Host OS.
1678bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const char* os = NULL;
1683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#if defined(OS_WIN)
1698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  os = "win";
1708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#elif defined(OS_MACOSX)
1718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  os = "mac";
1728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#elif defined(OS_LINUX)
1738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  os = "linux";
1743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#else
1758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  #error Unknown OS type.
1763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#endif
1778bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  Value os_val(NULL, std::string(os));
1788bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  dest->SetValue(variables::kBuildOs, os_val, NULL);
1798bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  dest->SetValue(variables::kOs, os_val, NULL);
1808bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1818bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Host architecture.
182f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  static const char kX86[] = "x86";
183f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  static const char kX64[] = "x64";
1848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const char* arch = NULL;
1858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)#if defined(OS_WIN)
1868bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // ...on Windows, set the CPU architecture based on the underlying OS, not
1878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // whatever the current bit-tedness of the GN binary is.
1888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  const base::win::OSInfo* os_info = base::win::OSInfo::GetInstance();
1898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  switch (os_info->architecture()) {
1908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    case base::win::OSInfo::X86_ARCHITECTURE:
191f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      arch = kX86;
1928bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      break;
1938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    case base::win::OSInfo::X64_ARCHITECTURE:
194f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      arch = kX64;
1958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      break;
1968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    default:
1978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      CHECK(false) << "Windows architecture not handled.";
1988bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      break;
1998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  }
2003551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#else
2018bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // ...on all other platforms, just use the bit-tedness of the current
2028bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // process.
2038bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  #if defined(ARCH_CPU_X86_64)
204f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    arch = kX64;
2058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  #elif defined(ARCH_CPU_X86)
206f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    arch = kX86;
2078bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  #elif defined(ARCH_CPU_ARMEL)
2088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    static const char kArm[] = "arm";
2098bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    arch = kArm;
2108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  #else
2118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    #error Unknown architecture.
2128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  #endif
2133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#endif
2148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Avoid unused var warning.
215f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  (void)kX86;
216f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  (void)kX64;
2178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  Value arch_val(NULL, std::string(arch));
2198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  dest->SetValue(variables::kBuildCpuArch, arch_val, NULL);
2208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  dest->SetValue(variables::kCpuArch, arch_val, NULL);
2218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Save the OS and architecture as build arguments that are implicitly
2231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // declared. This is so they can be overridden in a toolchain build args
2241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // override, and so that they will appear in the "gn args" output.
2251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  //
2261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Do not declare the build* variants since these shouldn't be changed.
2271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  //
2288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Mark these variables used so the build config file can override them
2298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // without geting a warning about overwriting an unused variable.
2301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  declared_arguments_[variables::kOs] = os_val;
2311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  declared_arguments_[variables::kCpuArch] = arch_val;
2328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  dest->MarkUsed(variables::kCpuArch);
2338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  dest->MarkUsed(variables::kOs);
2343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
2353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
2368bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void Args::ApplyOverrides(const Scope::KeyValueMap& values,
2383551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                          Scope* scope) const {
2393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  for (Scope::KeyValueMap::const_iterator i = values.begin();
2403551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)       i != values.end(); ++i)
2413551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    scope->SetValue(i->first, i->second, i->second.origin());
2423551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
2433551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
2443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void Args::SaveOverrideRecord(const Scope::KeyValueMap& values) const {
2453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::AutoLock lock(lock_);
2463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  for (Scope::KeyValueMap::const_iterator i = values.begin();
2473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)       i != values.end(); ++i)
2483551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    all_overrides_[i->first] = i->second;
2493551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
250