Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a vendored copy of get_cpu_value for Bazel 8 compatiblity #91

Merged
merged 6 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion sh/experimental/posix_hermetic.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion sh/posix.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions sh/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ bzl_library(
name = "defs",
srcs = [
"defs.bzl",
"get_cpu_value.bzl",
],
deps = [
"@bazel_skylib//lib:dicts",
Expand Down
60 changes: 60 additions & 0 deletions sh/private/get_cpu_value.bzl
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions tests/import/get_cpu_value.bzl
2 changes: 1 addition & 1 deletion tests/import/import_test.bzl
Original file line number Diff line number Diff line change
@@ -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 ########################################################

Expand Down
1 change: 1 addition & 0 deletions tests/tools
31 changes: 31 additions & 0 deletions tools/bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/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

# 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" "$@"
1 change: 1 addition & 0 deletions tools/bazel.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@pwsh -File "%~dp0bazel_wrapper.ps1" %*
30 changes: 30 additions & 0 deletions tools/bazel_wrapper.ps1
Original file line number Diff line number Diff line change
@@ -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