From c948612ba307b1e35f3e07070178e3449b06a929 Mon Sep 17 00:00:00 2001 From: Claudio Bley Date: Thu, 28 Nov 2024 14:05:45 +0100 Subject: [PATCH 1/6] Use a vendored copy of `get_cpu_value` for Bazel 8 compatiblity --- sh/experimental/posix_hermetic.bzl | 1 - sh/posix.bzl | 2 +- sh/private/BUILD.bazel | 1 + sh/private/get_cpu_value.bzl | 60 ++++++++++++++++++++++++++++++ tests/import/get_cpu_value.bzl | 1 + tests/import/import_test.bzl | 2 +- 6 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 sh/private/get_cpu_value.bzl create mode 120000 tests/import/get_cpu_value.bzl diff --git a/sh/experimental/posix_hermetic.bzl b/sh/experimental/posix_hermetic.bzl index 504215e..6afded5 100644 --- a/sh/experimental/posix_hermetic.bzl +++ b/sh/experimental/posix_hermetic.bzl @@ -6,7 +6,6 @@ less hermetic version of the POSIX toolchain. """ load("@bazel_skylib//lib:paths.bzl", "paths") -load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_cpu_value") load("//sh/private:posix.bzl", _commands = "commands") load("//sh:posix.bzl", "MAKE_VARIABLES", "TOOLCHAIN_TYPE") load("//sh:sh.bzl", "ShBinariesInfo") diff --git a/sh/posix.bzl b/sh/posix.bzl index e41543f..706baed 100644 --- a/sh/posix.bzl +++ b/sh/posix.bzl @@ -8,7 +8,7 @@ available in `posix.commands`. """ load("@bazel_skylib//lib:paths.bzl", "paths") -load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_cpu_value") +load("//sh/private:get_cpu_value.bzl", "get_cpu_value") load( "//sh/private:defs.bzl", "mk_default_info_with_files_to_run", diff --git a/sh/private/BUILD.bazel b/sh/private/BUILD.bazel index d138dd5..c8f7fcc 100644 --- a/sh/private/BUILD.bazel +++ b/sh/private/BUILD.bazel @@ -14,6 +14,7 @@ bzl_library( name = "defs", srcs = [ "defs.bzl", + "get_cpu_value.bzl", ], deps = [ "@bazel_skylib//lib:dicts", diff --git a/sh/private/get_cpu_value.bzl b/sh/private/get_cpu_value.bzl new file mode 100644 index 0000000..6f271a6 --- /dev/null +++ b/sh/private/get_cpu_value.bzl @@ -0,0 +1,60 @@ + +# taken from https://github.com/bazelbuild/rules_cc/blob/8395ec0172270f3bf92cd7b06c9b5b3f1f679e88/cc/private/toolchain/lib_cc_configure.bzl#L225 +# TODO(cb): remove when using rules_cc > 0.0.17 +def get_cpu_value(repository_ctx): + """Compute the cpu_value based on the OS name. Doesn't %-escape the result! + + Args: + repository_ctx: The repository context. + Returns: + One of (darwin, freebsd, x64_windows, ppc, s390x, arm, aarch64, k8, piii) + """ + os_name = repository_ctx.os.name + arch = repository_ctx.os.arch + if os_name.startswith("mac os"): + # Check if we are on x86_64 or arm64 and return the corresponding cpu value. + return "darwin_" + ("arm64" if arch == "aarch64" else "x86_64") + if os_name.find("freebsd") != -1: + return "freebsd" + if os_name.find("openbsd") != -1: + return "openbsd" + if os_name.find("windows") != -1: + if arch == "aarch64": + return "arm64_windows" + else: + return "x64_windows" + + if arch in ["power", "ppc64le", "ppc", "ppc64"]: + return "ppc" + if arch in ["s390x"]: + return "s390x" + if arch in ["mips64"]: + return "mips64" + if arch in ["riscv64"]: + return "riscv64" + if arch in ["arm", "armv7l"]: + return "arm" + if arch in ["aarch64"]: + return "aarch64" + return "k8" if arch in ["amd64", "x86_64", "x64"] else "piii" + +def fail_on_err(return_value, prefix = None): + """Fail if the given return value indicates an error. + + Args: + return_value: Pair; If the second element is not `None` this indicates an error. + prefix: optional, String; A prefix for the error message contained in `return_value`. + + Returns: + The first element of `return_value` if no error was indicated. + """ + result, err = return_value + + if err: + if prefix: + msg = prefix + err + else: + msg = err + fail(msg) + + return result diff --git a/tests/import/get_cpu_value.bzl b/tests/import/get_cpu_value.bzl new file mode 120000 index 0000000..bde6024 --- /dev/null +++ b/tests/import/get_cpu_value.bzl @@ -0,0 +1 @@ +../../sh/private/get_cpu_value.bzl \ No newline at end of file diff --git a/tests/import/import_test.bzl b/tests/import/import_test.bzl index a9e27dc..c78437c 100644 --- a/tests/import/import_test.bzl +++ b/tests/import/import_test.bzl @@ -1,5 +1,5 @@ -load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_cpu_value") load("@rules_sh//sh:import.bzl", "create_shim") +load(":get_cpu_value.bzl", "get_cpu_value") # create shim ######################################################## From 3373dc75dffdedd4f39a29d2084a7a0d276d7aec Mon Sep 17 00:00:00 2001 From: Claudio Bley Date: Thu, 28 Nov 2024 14:21:54 +0100 Subject: [PATCH 2/6] CI: Test using Bazel 8.0.0rc6 --- .github/workflows/workflow.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/workflow.yaml b/.github/workflows/workflow.yaml index 26b0114..6efe67f 100644 --- a/.github/workflows/workflow.yaml +++ b/.github/workflows/workflow.yaml @@ -20,7 +20,7 @@ jobs: matrix: os: [ubuntu-latest, macos-latest, windows-latest] bazel_mode: [workspace, module] - version: ["5.4.1", "6.4.0", "7.0.0"] + version: ["5.4.1", "6.4.0", "7.0.0", "8.0.0rc6"] include: # Bazel 5.4.1 does not find Visual Studio on windows-2022. So, we # test it on windows-2019. From d02b7a3d974115c69a92ec0a7e550913b9525a17 Mon Sep 17 00:00:00 2001 From: Claudio Bley Date: Thu, 28 Nov 2024 16:54:24 +0100 Subject: [PATCH 3/6] Add `tools/bazel` script which enables per Bazel config --- .bazelrc | 11 +++++++++++ tests/tools | 1 + tools/bazel | 29 +++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 120000 tests/tools create mode 100755 tools/bazel diff --git a/.bazelrc b/.bazelrc index bd285c8..2142e94 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,3 +1,14 @@ +# Compatibility options (per bazel version) +common:bazel8 --noincompatible_disallow_ctx_resolve_tools + +common:bazel7 --config=common + +common:bazel6 --config=common + +common:bazel5 --config=common + +common:common --noverbose_failures + # Remote Cache Configuration build:remote-cache --bes_results_url=https://app.buildbuddy.io/invocation/ build:remote-cache --bes_backend=grpcs://remote.buildbuddy.io diff --git a/tests/tools b/tests/tools new file mode 120000 index 0000000..0b246f7 --- /dev/null +++ b/tests/tools @@ -0,0 +1 @@ +../tools/ \ No newline at end of file diff --git a/tools/bazel b/tools/bazel new file mode 100755 index 0000000..48e89d4 --- /dev/null +++ b/tools/bazel @@ -0,0 +1,29 @@ +#!/usr/bin/env bash + +set -euo pipefail + +bazel_version=$( + "${BAZEL_REAL}" --version | ( + read -r -a v + echo "${v[1]}" + ) +) + +# enable config per each major version, i.e. bazel6, bazel7, bazel8, ... +config="--config=bazel${bazel_version%%.*}" + +declare -a startup_options=() + +while [ "$#" -gt 0 ]; do + option="$1" + if [[ "$option" = -* ]]; then + startup_options+=( "$option" ) + shift + else + break + fi +done + +command=$1 ; shift + +exec "$BAZEL_REAL" "${startup_options[@]}" "$command" "$config" "$@" From 1c5b01b2e1c4b155f6d8cf1f4993cee0ddbe7f11 Mon Sep 17 00:00:00 2001 From: Claudio Bley Date: Thu, 28 Nov 2024 17:37:51 +0100 Subject: [PATCH 4/6] Add powershell wrapper --- tools/bazel.ps1 | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 tools/bazel.ps1 diff --git a/tools/bazel.ps1 b/tools/bazel.ps1 new file mode 100644 index 0000000..1a15798 --- /dev/null +++ b/tools/bazel.ps1 @@ -0,0 +1,30 @@ +# Exit immediately if a command fails, if a variable is used without being defined, or if a pipeline fails +Set-StrictMode -Version Latest +$ErrorActionPreference = "Stop" + +# Get the Bazel version +$bazel_version = & $Env:BAZEL_REAL --version | ForEach-Object { ($_ -split ' ')[1] } + +# Construct the config flag based on the major version +$config = "--config=bazel$($bazel_version -split '\.' | Select-Object -First 1)" + +# Initialize an array for startup options +$startup_options = @() + +# Parse command-line arguments for options +while ($args.Count -gt 0) { + $option = $args[0] + if ($option -match '^-') { + $startup_options += $option + $args = $args[1..$($args.Count - 1)] + } else { + break + } +} + +# Get the command and remaining arguments +$command = $args[0] +$args = $args[1..$($args.Count - 1)] + +# Execute Bazel with the collected options and arguments +& $Env:BAZEL_REAL @startup_options $command $config @args From 0f2d32ce634e19bca6c750c100bc1c7a43a32d5f Mon Sep 17 00:00:00 2001 From: Claudio Bley Date: Thu, 28 Nov 2024 18:04:15 +0100 Subject: [PATCH 5/6] Use a bat wrapper wrapper See https://github.com/bazelbuild/bazelisk/issues/643 --- tools/bazel.bat | 1 + tools/{bazel.ps1 => bazel_wrapper.ps1} | 0 2 files changed, 1 insertion(+) create mode 100644 tools/bazel.bat rename tools/{bazel.ps1 => bazel_wrapper.ps1} (100%) diff --git a/tools/bazel.bat b/tools/bazel.bat new file mode 100644 index 0000000..dc4b2d2 --- /dev/null +++ b/tools/bazel.bat @@ -0,0 +1 @@ +@pwsh -File "%~dp0bazel_wrapper.ps1" %* diff --git a/tools/bazel.ps1 b/tools/bazel_wrapper.ps1 similarity index 100% rename from tools/bazel.ps1 rename to tools/bazel_wrapper.ps1 From 215255130c86ad7f3d3af369ccc0fb90269a7f33 Mon Sep 17 00:00:00 2001 From: Claudio Bley Date: Fri, 29 Nov 2024 09:55:49 +0100 Subject: [PATCH 6/6] Workaround for bash 3 (on macOS) --- tools/bazel | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tools/bazel b/tools/bazel index 48e89d4..3a516f2 100755 --- a/tools/bazel +++ b/tools/bazel @@ -26,4 +26,6 @@ done command=$1 ; shift -exec "$BAZEL_REAL" "${startup_options[@]}" "$command" "$config" "$@" +# N.B. using a default value if unset for `startup_options` is needed for compatiblity with Bash 3 +# which treats an empty array as unset +exec "$BAZEL_REAL" "${startup_options[@]-}" "$command" "$config" "$@"