Skip to content

Commit

Permalink
Defaulted cpu to the host cpu
Browse files Browse the repository at this point in the history
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
  • Loading branch information
damienmg authored and hanwen committed Mar 13, 2015
1 parent a627291 commit e1349ec
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 22 deletions.
8 changes: 3 additions & 5 deletions bootstrap_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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|'
}
Expand Down Expand Up @@ -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 $?
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,33 @@ public StrictDepsConverter() {
}
}

/**
* Converter for default --host_cpu to the auto-detected host cpu.
*
* <p>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<String> {
@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.
*
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 ===
Expand Down Expand Up @@ -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<String, Label> labelMap) {
labelMap.putAll("action_listener", actionListeners);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion tools/cpp/CROSSTOOL
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit e1349ec

Please sign in to comment.