From ca827dd8a12400e226af2417068ceadecd9ec53f Mon Sep 17 00:00:00 2001 From: Claudio Bley Date: Fri, 13 Dec 2024 00:52:43 +0100 Subject: [PATCH] Use `abs` from utils instead of the global (#3075) Fixes #3068 --- cargo/private/cargo_utils.bzl | 14 ++++++++++++++ rust/private/compat.bzl | 29 +++++++++++++++++++++++++++++ rust/private/rustc.bzl | 4 ++-- rust/private/utils.bzl | 14 +------------- rust/repositories.bzl | 1 + 5 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 rust/private/compat.bzl diff --git a/cargo/private/cargo_utils.bzl b/cargo/private/cargo_utils.bzl index c4e943d612..bf17fc224a 100644 --- a/cargo/private/cargo_utils.bzl +++ b/cargo/private/cargo_utils.bzl @@ -2,6 +2,20 @@ load("//rust/platform:triple_mappings.bzl", "system_to_binary_ext") +# TODO: remove after dropping support for Bazel < 7 when `abs` is a global +def abs(value): + """Returns the absolute value of a number. + + Args: + value (int): A number. + + Returns: + int: The absolute value of the number. + """ + if value < 0: + return -value + return value + def _resolve_repository_template( *, template, diff --git a/rust/private/compat.bzl b/rust/private/compat.bzl new file mode 100644 index 0000000000..b335e9c08a --- /dev/null +++ b/rust/private/compat.bzl @@ -0,0 +1,29 @@ +# Copyright 2015 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Compatibility functions for older Bazel versions.""" + +# TODO: remove after dropping support for Bazel < 7 when `abs` is a global +def abs(value): + """Returns the absolute value of a number. + + Args: + value (int): A number. + + Returns: + int: The absolute value of the number. + """ + if value < 0: + return -value + return value diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index fc8d34dbde..8731cc64ad 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -23,11 +23,11 @@ load( "CPP_LINK_STATIC_LIBRARY_ACTION_NAME", ) load("//rust/private:common.bzl", "rust_common") +load("//rust/private:compat.bzl", "abs") load("//rust/private:providers.bzl", "RustcOutputDiagnosticsInfo", _BuildInfo = "BuildInfo") load("//rust/private:stamp.bzl", "is_stamping_enabled") load( "//rust/private:utils.bzl", - "abs", "expand_dict_value_locations", "expand_list_element_locations", "find_cc_toolchain", @@ -35,10 +35,10 @@ load( "get_lib_name_for_windows", "get_preferred_artifact", "is_exec_configuration", + "is_std_dylib", "make_static_lib_symlink", "relativize", ) -load(":utils.bzl", "is_std_dylib") # This feature is disabled unless one of the dependencies is a cc_library. # Authors of C++ toolchains can place linker flags that should only be applied diff --git a/rust/private/utils.bzl b/rust/private/utils.bzl index e8dfe8d099..7b11c10da6 100644 --- a/rust/private/utils.bzl +++ b/rust/private/utils.bzl @@ -16,6 +16,7 @@ load("@bazel_skylib//lib:paths.bzl", "paths") load("@bazel_tools//tools/cpp:toolchain_utils.bzl", find_rules_cc_toolchain = "find_cpp_toolchain") +load(":compat.bzl", "abs") load(":providers.bzl", "BuildInfo", "CrateGroupInfo", "CrateInfo", "DepInfo", "DepVariantInfo", "RustcOutputDiagnosticsInfo") UNSUPPORTED_FEATURES = [ @@ -175,19 +176,6 @@ def get_lib_name_for_windows(lib): return libname -def abs(value): - """Returns the absolute value of a number. - - Args: - value (int): A number. - - Returns: - int: The absolute value of the number. - """ - if value < 0: - return -value - return value - def determine_output_hash(crate_root, label): """Generates a hash of the crate root file's path. diff --git a/rust/repositories.bzl b/rust/repositories.bzl index 048bf3f011..f158f4bb61 100644 --- a/rust/repositories.bzl +++ b/rust/repositories.bzl @@ -5,6 +5,7 @@ load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") load("//rust/platform:triple.bzl", "get_host_triple", "triple") load("//rust/platform:triple_mappings.bzl", "triple_to_constraint_set") load("//rust/private:common.bzl", "rust_common") +load("//rust/private:compat.bzl", "abs") load( "//rust/private:repository_utils.bzl", "BUILD_for_rust_analyzer_proc_macro_srv",