Commit 74add9fc authored by dpranke's avatar dpranke Committed by Commit bot

Rework handling of os and cpu_arch in GN.

NaCl and V8 have more complicated requirements for handling architectures
than GN could previously handle. This CL reworks things to meet their
needs.

NaCl occasionally has the need to be able to override build_cpu_arch. This
comes up when they want to use a 32-bit host toolchain (i.e., build
32-bit "host" objects) on a 64-bit kernel. In addition, it is conceivable
that other projects will need this functionality on systems capable of
building for running programs for multiple architectures (any system
that can run both 32-bit and 64-bit binaries), and there is no way that
GN can always correctly guess which arch to use by default.

V8 has the need to be able to run a host toolchain that knows what the
requested target cpu_arch is; we can not use the existing "cpu_arch" for
this because that needs to be the cpu_arch of the current toolchain.
The concrete example is running a host binary on Linux x86 that needs to
get specific flags to target arm (rather than x86), for example. We could
solve this in the build configs by passing custom variables across the
toolchain, but this suggests that we should have a general solution to
track these things, which is what this CL does.

This CL introduces two new predefined variables -- target_cpu and
target_os -- and renames cpu_arch and os to to 'current_cpu' and
'current_os', and renames build_cpu_arch and build_os to
host_cpu and host_os for consistency.

current_cpu and target_cpu default to the same value as
host_cpu, and current_os and target_os default to the same value
as host_os.

Any of these variables is (and should be) overridable on the command line
or in a build config file. We want them to be overridable because (a) it's conceivable
that some projects might always want fixed values for target_os and
target_cpu_arch regardless of what platform GN is running on, and (b) we might
want to set the values based on other values (i.e., have target_cpu_arch
default to "arm" if target_os == "android").

Due to the renaming of "os" and "cpu_arch", this CL is likely to break any and
all existing project builds; projects will need to update their build configs when
rolling in the new binary.

R=brettw@chromium.org
BUG=344767

Review URL: https://codereview.chromium.org/914873002

Cr-Commit-Position: refs/heads/master@{#317120}
parent bcadd727
......@@ -18,10 +18,12 @@ const char kBuildArgs_Help[] =
"\n"
" First, system default arguments are set based on the current system.\n"
" The built-in arguments are:\n"
" - cpu_arch (by default this is the same as \"default_cpu_arch\")\n"
" - default_cpu_arch\n"
" - default_os\n"
" - os (by default this is the same as \"default_os\")\n"
" - host_cpu\n"
" - host_os\n"
" - current_cpu\n"
" - current_os\n"
" - target_cpu\n"
" - target_os\n"
"\n"
" If specified, arguments from the --args command line flag are used. If\n"
" that flag is not specified, args from previous builds in the build\n"
......@@ -221,9 +223,6 @@ void Args::SetSystemVarsLocked(Scope* dest) const {
#else
#error Unknown OS type.
#endif
Value os_val(nullptr, std::string(os));
dest->SetValue(variables::kBuildOs, os_val, nullptr);
dest->SetValue(variables::kOs, os_val, nullptr);
// Host architecture.
static const char kX86[] = "x86";
......@@ -231,7 +230,7 @@ void Args::SetSystemVarsLocked(Scope* dest) const {
static const char kArm[] = "arm";
const char* arch = nullptr;
// Set the CPU architecture based on the underlying OS, not
// Set the host CPU architecture based on the underlying OS, not
// whatever the current bit-tedness of the GN binary is.
std::string os_arch = base::SysInfo::OperatingSystemArchitecture();
if (os_arch == "x86")
......@@ -243,22 +242,36 @@ void Args::SetSystemVarsLocked(Scope* dest) const {
else
CHECK(false) << "OS architecture not handled.";
Value arch_val(nullptr, std::string(arch));
dest->SetValue(variables::kBuildCpuArch, arch_val, nullptr);
dest->SetValue(variables::kCpuArch, arch_val, nullptr);
// Save the OS and architecture as build arguments that are implicitly
// declared. This is so they can be overridden in a toolchain build args
// override, and so that they will appear in the "gn args" output.
//
// Do not declare the build* variants since these shouldn't be changed.
//
Value empty_string(nullptr, std::string());
Value os_val(nullptr, std::string(os));
dest->SetValue(variables::kHostOs, os_val, nullptr);
dest->SetValue(variables::kTargetOs, empty_string, nullptr);
dest->SetValue(variables::kCurrentOs, empty_string, nullptr);
Value arch_val(nullptr, std::string(arch));
dest->SetValue(variables::kHostCpu, arch_val, nullptr);
dest->SetValue(variables::kTargetCpu, empty_string, nullptr);
dest->SetValue(variables::kCurrentCpu, empty_string, nullptr);
declared_arguments_[variables::kHostOs] = os_val;
declared_arguments_[variables::kCurrentOs] = empty_string;
declared_arguments_[variables::kTargetOs] = empty_string;
declared_arguments_[variables::kHostCpu] = arch_val;
declared_arguments_[variables::kCurrentCpu] = empty_string;
declared_arguments_[variables::kTargetCpu] = empty_string;
// Mark these variables used so the build config file can override them
// without geting a warning about overwriting an unused variable.
declared_arguments_[variables::kOs] = os_val;
declared_arguments_[variables::kCpuArch] = arch_val;
dest->MarkUsed(variables::kCpuArch);
dest->MarkUsed(variables::kOs);
dest->MarkUsed(variables::kHostCpu);
dest->MarkUsed(variables::kCurrentCpu);
dest->MarkUsed(variables::kTargetCpu);
dest->MarkUsed(variables::kHostOs);
dest->MarkUsed(variables::kCurrentOs);
dest->MarkUsed(variables::kTargetOs);
}
void Args::ApplyOverridesLocked(const Scope::KeyValueMap& values,
......
......@@ -322,8 +322,9 @@ extern const char kArgs_Help[] =
" Prints all arguments with their default values for the out/Debug\n"
" build.\n"
"\n"
" gn args out/Debug --list=cpu_arch\n"
" Prints information about the \"cpu_arch\" argument for the out/Debug\n"
" gn args out/Debug --list=target_cpu\n"
" Prints information about the \"target_cpu\" argument for the "
"out/Debug\n"
" build.\n"
"\n"
" gn args --list --args=\"os=\\\"android\\\" enable_doom_melon=true\"\n"
......
......@@ -3,7 +3,7 @@
# found in the LICENSE file.
config("compiler_defaults") {
if (os == "linux") {
if (current_os == "linux") {
cflags = [
"-fPIC",
"-pthread",
......
# Make sure continued conditions are aligned.
if (something) {
if (false) {
} else if (is_linux && !is_android && cpu_arch == "x64" && !disable_iterator_debugging) {
} else if (is_linux && !is_android && current_cpu == "x64" && !disable_iterator_debugging) {
}
}
# Make sure continued conditions are aligned.
if (something) {
if (false) {
} else if (is_linux && !is_android && cpu_arch == "x64" &&
} else if (is_linux && !is_android && current_cpu == "x64" &&
!disable_iterator_debugging) {
}
}
......@@ -16,10 +16,11 @@ syn keyword gnConditional if else
hi def link gnConditional Conditional
" Predefined variables
syn keyword gnPredefVar build_cpu_arch build_os cpu_arch current_toolchain
syn keyword gnPredefVar default_toolchain os python_path root_build_dir
syn keyword gnPredefVar root_gen_dir root_out_dir target_gen_dir
syn keyword gnPredefVar target_out_dir
syn keyword gnPredefVar current_cpu current_os current_toolchain
syn keyword gnPredefVar default_toolchain host_cpu host_os
syn keyword gnPredefVar root_build_dir root_gen_dir root_out_dir
syn keyword gnPredefVar target_cpu target_gen_dir target_out_dir
syn keyword gnPredefVar target_os
syn keyword gnPredefVar true false
hi def link gnPredefVar Constant
......
This diff is collapsed.
......@@ -13,17 +13,21 @@ namespace variables {
// Builtin vars ----------------------------------------------------------------
extern const char kBuildCpuArch[];
extern const char kBuildCpuArch_HelpShort[];
extern const char kBuildCpuArch_Help[];
extern const char kHostCpu[];
extern const char kHostCpu_HelpShort[];
extern const char kHostCpu_Help[];
extern const char kBuildOs[];
extern const char kBuildOs_HelpShort[];
extern const char kBuildOs_Help[];
extern const char kHostOs[];
extern const char kHostOs_HelpShort[];
extern const char kHostOs_Help[];
extern const char kCpuArch[];
extern const char kCpuArch_HelpShort[];
extern const char kCpuArch_Help[];
extern const char kCurrentCpu[];
extern const char kCurrentCpu_HelpShort[];
extern const char kCurrentCpu_Help[];
extern const char kCurrentOs[];
extern const char kCurrentOs_HelpShort[];
extern const char kCurrentOs_Help[];
extern const char kCurrentToolchain[];
extern const char kCurrentToolchain_HelpShort[];
......@@ -33,10 +37,6 @@ extern const char kDefaultToolchain[];
extern const char kDefaultToolchain_HelpShort[];
extern const char kDefaultToolchain_Help[];
extern const char kOs[];
extern const char kOs_HelpShort[];
extern const char kOs_Help[];
extern const char kPythonPath[];
extern const char kPythonPath_HelpShort[];
extern const char kPythonPath_Help[];
......@@ -53,6 +53,14 @@ extern const char kRootOutDir[];
extern const char kRootOutDir_HelpShort[];
extern const char kRootOutDir_Help[];
extern const char kTargetCpu[];
extern const char kTargetCpu_HelpShort[];
extern const char kTargetCpu_Help[];
extern const char kTargetOs[];
extern const char kTargetOs_HelpShort[];
extern const char kTargetOs_Help[];
extern const char kTargetGenDir[];
extern const char kTargetGenDir_HelpShort[];
extern const char kTargetGenDir_Help[];
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment