From e1349ec1f3151cf50005584dd3f2024ea49ecd67 Mon Sep 17 00:00:00 2001 From: Damien Martin-Guillerez Date: Thu, 12 Mar 2015 11:00:44 +0000 Subject: [PATCH] Defaulted cpu to the host cpu This should remove the needs for the --cpu=darwin flag under darwin. Bazel build are now using the host cpu too. This commit also fix the host cpu detection. The host cpu detection was too late and and some corner case was not really working with configurable attributes. It is now done as the default value for the --host_cpu flag. -- MOS_MIGRATED_REVID=88427551 --- bootstrap_test.sh | 8 ++-- .../analysis/config/BuildConfiguration.java | 46 +++++++++++++------ .../cpp/CrosstoolConfigurationIdentifier.java | 9 +++- tools/cpp/CROSSTOOL | 2 +- 4 files changed, 43 insertions(+), 22 deletions(-) diff --git a/bootstrap_test.sh b/bootstrap_test.sh index f97a249b424cc6..304814f880599b 100755 --- a/bootstrap_test.sh +++ b/bootstrap_test.sh @@ -24,9 +24,7 @@ function parse_options() { } PLATFORM="$(uname -s | tr 'A-Z' 'a-z')" -CPU_FLAG="" if [[ ${PLATFORM} == "darwin" ]]; then - CPU_FLAG="--cpu=darwin" function md5_file() { md5 $1 | sed 's|^MD5 (\(.*\)) =|\1|' } @@ -63,8 +61,8 @@ function bootstrap() { local BAZEL_BIN=$1 local BAZEL_SUM=$2 [ -x "${BAZEL_BIN}" ] || fail "syntax: bootstrap bazel-binary" - ${BAZEL_BIN} --blazerc=/dev/null clean ${CPU_FLAG} || return $? - ${BAZEL_BIN} --blazerc=/dev/null build ${CPU_FLAG} --nostamp //src:bazel //src:tools || return $? + ${BAZEL_BIN} --blazerc=/dev/null clean || return $? + ${BAZEL_BIN} --blazerc=/dev/null build --nostamp //src:bazel //src:tools || return $? if [ -n "${BAZEL_SUM}" ]; then get_outputs_sum > ${BAZEL_SUM} || return $? @@ -124,7 +122,7 @@ fi if [ $DO_TESTS ]; then start_test "test" - $BOOTSTRAP --blazerc=/dev/null test ${CPU_FLAG} -k --test_output=errors //src/... || fail "Tests failed" + $BOOTSTRAP --blazerc=/dev/null test -k --test_output=errors //src/... || fail "Tests failed" end_test "test" fi diff --git a/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java b/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java index 35edea00328630..cc7c81d99fc1a0 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java @@ -390,6 +390,33 @@ public StrictDepsConverter() { } } + /** + * Converter for default --host_cpu to the auto-detected host cpu. + * + *

This detects the host cpu of the Blaze's server but if the compilation happens in a + * compilation cluster then the host cpu of the compilation cluster might be different than + * the auto-detected one and the --host_cpu option must then be set explicitly. + */ + public static class HostCpuConverter implements Converter { + @Override + public String convert(String input) throws OptionsParsingException { + if (input.isEmpty()) { + switch (OS.getCurrent()) { + case DARWIN: + return "darwin"; + default: + return "k8"; + } + } + return input; + } + + @Override + public String getTypeDescription() { + return "a string"; + } + } + /** * Options that affect the value of a BuildConfiguration instance. * @@ -491,8 +518,9 @@ public String getCpu() { public boolean showCachedAnalysisResults; @Option(name = "host_cpu", - defaultValue = "null", + defaultValue = "", category = "semantics", + converter = HostCpuConverter.class, help = "The host CPU.") public String hostCpu; @@ -753,9 +781,9 @@ public FragmentOptions getHost(boolean fallback) { // In the fallback case, we have already tried the target options and they didn't work, so // now we try the default options; the hostCpu field has the default value, because we use // getDefault() above. - host.cpu = computeHostCpu(host.hostCpu); + host.cpu = host.hostCpu; } else { - host.cpu = computeHostCpu(hostCpu); + host.cpu = hostCpu; } // === Runfiles === @@ -784,18 +812,6 @@ public FragmentOptions getHost(boolean fallback) { return host; } - private static String computeHostCpu(String explicitHostCpu) { - if (explicitHostCpu != null) { - return explicitHostCpu; - } - switch (OS.getCurrent()) { - case DARWIN: - return "darwin"; - default: - return "k8"; - } - } - @Override public void addAllLabels(Multimap labelMap) { labelMap.putAll("action_listener", actionListeners); diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigurationIdentifier.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigurationIdentifier.java index 11111891012616..e4af3b17536878 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigurationIdentifier.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CrosstoolConfigurationIdentifier.java @@ -15,6 +15,7 @@ package com.google.devtools.build.lib.rules.cpp; import com.google.devtools.build.lib.analysis.config.BuildConfiguration; +import com.google.devtools.build.lib.analysis.config.BuildConfiguration.Options; import com.google.devtools.build.lib.analysis.config.BuildOptions; import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig; import com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig.CToolchain; @@ -30,6 +31,8 @@ */ public final class CrosstoolConfigurationIdentifier implements CrosstoolConfigurationOptions { + private static final String USE_HOST_CPU = "same_as_host"; + /** The CPU associated with this crosstool configuration. */ private final String cpu; @@ -51,9 +54,13 @@ private CrosstoolConfigurationIdentifier(String cpu, String compiler, String lib */ public static CrosstoolConfigurationIdentifier fromReleaseAndCrosstoolConfiguration( CrosstoolConfig.CrosstoolRelease release, BuildOptions buildOptions) { - String cpu = buildOptions.get(BuildConfiguration.Options.class).getCpu(); + Options options = buildOptions.get(BuildConfiguration.Options.class); + String cpu = options.getCpu(); if (cpu == null) { cpu = release.getDefaultTargetCpu(); + if (cpu.equals(USE_HOST_CPU)) { + cpu = options.hostCpu; + } } CppOptions cppOptions = buildOptions.get(CppOptions.class); return new CrosstoolConfigurationIdentifier(cpu, cppOptions.cppCompiler, cppOptions.glibc); diff --git a/tools/cpp/CROSSTOOL b/tools/cpp/CROSSTOOL index 7287bc49ba2ef9..87b81d36372dde 100644 --- a/tools/cpp/CROSSTOOL +++ b/tools/cpp/CROSSTOOL @@ -1,6 +1,6 @@ major_version: "local" minor_version: "" -default_target_cpu: "k8" +default_target_cpu: "same_as_host" default_toolchain { cpu: "k8" toolchain_identifier: "local_linux"