Skip to content

Commit

Permalink
Added support for Apple Silicon (#272)
Browse files Browse the repository at this point in the history
* Added support for Apple Sillicon

* support arm64 for released versions only

* Printing out Bazel version

* Checking operation system

* Fixed tests for darwin arm64

* Address comments
  • Loading branch information
linzhp authored Mar 2, 2022
1 parent 72e2fdb commit 31d0d7e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
35 changes: 30 additions & 5 deletions bazelisk.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,23 +206,48 @@ def determine_executable_filename_suffix():


def determine_bazel_filename(version):
operating_system = get_operating_system()
supported_machines = get_supported_machine_archs(version, operating_system)
machine = normalized_machine_arch_name()
if machine != "x86_64":
if machine not in supported_machines:
raise Exception(
'Unsupported machine architecture "{}". Bazel currently only supports x86_64.'.format(
machine
'Unsupported machine architecture "{}". Bazel {} only supports {} on {}.'.format(
machine, version, ", ".join(supported_machines), operating_system.capitalize()
)
)

operating_system = get_operating_system()

filename_suffix = determine_executable_filename_suffix()
bazel_flavor = "bazel"
if os.environ.get("BAZELISK_NOJDK", "0") != "0":
bazel_flavor = "bazel_nojdk"
return "{}-{}-{}-{}{}".format(bazel_flavor, version, operating_system, machine, filename_suffix)


def get_supported_machine_archs(version, operating_system):
supported_machines = ["x86_64"]
versions = version.split(".")[:2]
if len(versions) == 2:
# released version
major, minor = int(versions[0]), int(versions[1])
if (
operating_system == "darwin"
and (major > 4 or major == 4 and minor >= 1)
or operating_system == "linux"
and (major > 3 or major == 3 and minor >= 4)
):
# Linux arm64 was supported since 3.4.0.
# Darwin arm64 was supported since 4.1.0.
supported_machines.append("arm64")
elif operating_system in ("darwin", "linux"):
# This is needed to run bazelisk_test.sh on Linux and Darwin arm64 machines, which are
# becoming more and more popular.
# It works because all recent commits of Bazel support arm64 on Darwin and Linux.
# However, this would add arm64 by mistake if the commit is too old, which should be
# a rare scenario.
supported_machines.append("arm64")
return supported_machines


def normalized_machine_arch_name():
machine = platform.machine().lower()
if machine == "amd64":
Expand Down
12 changes: 6 additions & 6 deletions bazelisk_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ function test_bazel_version_go() {
function test_bazel_version_from_environment() {
setup

USE_BAZEL_VERSION="0.20.0" \
USE_BAZEL_VERSION="5.0.0" \
BAZELISK_HOME="$BAZELISK_HOME" \
bazelisk version 2>&1 | tee log

grep "Build label: 0.20.0" log || \
(echo "FAIL: Expected to find 'Build label: 0.20.0' in the output of 'bazelisk version'"; exit 1)
grep "Build label: 5.0.0" log || \
(echo "FAIL: Expected to find 'Build label: 5.0.0' in the output of 'bazelisk version'"; exit 1)
}

function test_bazel_version_prefer_environment_to_bazeliskrc() {
Expand Down Expand Up @@ -157,13 +157,13 @@ function test_bazel_version_prefer_bazeliskrc_to_bazelversion_file() {
function test_bazel_version_from_file() {
setup

echo "0.19.0" > .bazelversion
echo "5.0.0" > .bazelversion

BAZELISK_HOME="$BAZELISK_HOME" \
bazelisk version 2>&1 | tee log

grep "Build label: 0.19.0" log || \
(echo "FAIL: Expected to find 'Build label: 0.19.0' in the output of 'bazelisk version'"; exit 1)
grep "Build label: 5.0.0" log || \
(echo "FAIL: Expected to find 'Build label: 5.0.0' in the output of 'bazelisk version'"; exit 1)
}

function test_bazel_version_from_url() {
Expand Down

0 comments on commit 31d0d7e

Please sign in to comment.