From 31d0d7e37b4c63dcd3627ca6ba4dda3055bf2870 Mon Sep 17 00:00:00 2001 From: Zhongpeng Lin Date: Wed, 2 Mar 2022 09:34:22 -0800 Subject: [PATCH] Added support for Apple Silicon (#272) * 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 --- bazelisk.py | 35 ++++++++++++++++++++++++++++++----- bazelisk_test.sh | 12 ++++++------ 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/bazelisk.py b/bazelisk.py index b6fab96e..08c5929e 100755 --- a/bazelisk.py +++ b/bazelisk.py @@ -206,16 +206,16 @@ 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": @@ -223,6 +223,31 @@ def determine_bazel_filename(version): 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": diff --git a/bazelisk_test.sh b/bazelisk_test.sh index 9002979e..09733bab 100755 --- a/bazelisk_test.sh +++ b/bazelisk_test.sh @@ -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() { @@ -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() {