From e5ff6c4b9929aa99d5c989726bd55a036b069802 Mon Sep 17 00:00:00 2001 From: Claudio Bley Date: Tue, 10 Dec 2024 15:48:56 +0100 Subject: [PATCH 1/4] Use `abs` from utils instead of the global Fixes #3068 --- cargo/private/cargo_utils.bzl | 1 + rust/private/rustc.bzl | 2 +- rust/private/utils.bzl | 1 + rust/repositories.bzl | 1 + 4 files changed, 4 insertions(+), 1 deletion(-) diff --git a/cargo/private/cargo_utils.bzl b/cargo/private/cargo_utils.bzl index c4e943d612..549dd0eb25 100644 --- a/cargo/private/cargo_utils.bzl +++ b/cargo/private/cargo_utils.bzl @@ -1,6 +1,7 @@ """Utility functions for the cargo rules""" load("//rust/platform:triple_mappings.bzl", "system_to_binary_ext") +load("//rust/private:utils.bzl", "abs") def _resolve_repository_template( *, diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index fc8d34dbde..3a5307d24a 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -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..94edb2ee89 100644 --- a/rust/private/utils.bzl +++ b/rust/private/utils.bzl @@ -175,6 +175,7 @@ def get_lib_name_for_windows(lib): return libname +# TODO: remove after dropping support for Bazel < 7 when `abs` is a global def abs(value): """Returns the absolute value of a number. diff --git a/rust/repositories.bzl b/rust/repositories.bzl index 3122d326e2..61b006d9bb 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:utils.bzl", "abs") load( "//rust/private:repository_utils.bzl", "BUILD_for_rust_analyzer_proc_macro_srv", From 00289c7d40e9767c6ac6083c321369e7d6aa8621 Mon Sep 17 00:00:00 2001 From: Claudio Bley Date: Thu, 12 Dec 2024 10:04:30 +0100 Subject: [PATCH 2/4] Add `abs` to `cargo/private:cargo_utils.bzl` --- cargo/private/cargo_utils.bzl | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cargo/private/cargo_utils.bzl b/cargo/private/cargo_utils.bzl index 549dd0eb25..bf17fc224a 100644 --- a/cargo/private/cargo_utils.bzl +++ b/cargo/private/cargo_utils.bzl @@ -1,7 +1,20 @@ """Utility functions for the cargo rules""" load("//rust/platform:triple_mappings.bzl", "system_to_binary_ext") -load("//rust/private:utils.bzl", "abs") + +# 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( *, From 39a31eecd53879cad08e8a15cdbf1ac4c4d97f49 Mon Sep 17 00:00:00 2001 From: Claudio Bley Date: Thu, 12 Dec 2024 10:28:00 +0100 Subject: [PATCH 3/4] Separate `abs` into `rust/private:compat.bzl` Since repository.bzl cannot load `private:utils.bzl` because of a cyclic dependency for rules_cc. --- rust/private/compat.bzl | 29 +++++++++++++++++++++++++++++ rust/private/rustc.bzl | 2 +- rust/private/utils.bzl | 15 +-------------- rust/repositories.bzl | 2 +- 4 files changed, 32 insertions(+), 16 deletions(-) create mode 100644 rust/private/compat.bzl 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 3a5307d24a..fa0cea5142 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -27,7 +27,6 @@ load("//rust/private:providers.bzl", "RustcOutputDiagnosticsInfo", _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", @@ -39,6 +38,7 @@ load( "make_static_lib_symlink", "relativize", ) +load("//rust/private:compat.bzl", "abs") # 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 94edb2ee89..84222a4ff8 100644 --- a/rust/private/utils.bzl +++ b/rust/private/utils.bzl @@ -17,6 +17,7 @@ load("@bazel_skylib//lib:paths.bzl", "paths") load("@bazel_tools//tools/cpp:toolchain_utils.bzl", find_rules_cc_toolchain = "find_cpp_toolchain") load(":providers.bzl", "BuildInfo", "CrateGroupInfo", "CrateInfo", "DepInfo", "DepVariantInfo", "RustcOutputDiagnosticsInfo") +load(":compat.bzl", "abs") UNSUPPORTED_FEATURES = [ "thin_lto", @@ -175,20 +176,6 @@ def get_lib_name_for_windows(lib): return libname -# 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 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 61b006d9bb..7a9b437b56 100644 --- a/rust/repositories.bzl +++ b/rust/repositories.bzl @@ -5,7 +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:utils.bzl", "abs") +load("//rust/private:compat.bzl", "abs") load( "//rust/private:repository_utils.bzl", "BUILD_for_rust_analyzer_proc_macro_srv", From 365dd21468395a4de38b168de04adbe9ef410d0d Mon Sep 17 00:00:00 2001 From: Claudio Bley Date: Thu, 12 Dec 2024 10:45:56 +0100 Subject: [PATCH 4/4] Please buildifier --- rust/private/rustc.bzl | 2 +- rust/private/utils.bzl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index fa0cea5142..8731cc64ad 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -23,6 +23,7 @@ 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( @@ -38,7 +39,6 @@ load( "make_static_lib_symlink", "relativize", ) -load("//rust/private:compat.bzl", "abs") # 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 84222a4ff8..7b11c10da6 100644 --- a/rust/private/utils.bzl +++ b/rust/private/utils.bzl @@ -16,8 +16,8 @@ load("@bazel_skylib//lib:paths.bzl", "paths") load("@bazel_tools//tools/cpp:toolchain_utils.bzl", find_rules_cc_toolchain = "find_cpp_toolchain") -load(":providers.bzl", "BuildInfo", "CrateGroupInfo", "CrateInfo", "DepInfo", "DepVariantInfo", "RustcOutputDiagnosticsInfo") load(":compat.bzl", "abs") +load(":providers.bzl", "BuildInfo", "CrateGroupInfo", "CrateInfo", "DepInfo", "DepVariantInfo", "RustcOutputDiagnosticsInfo") UNSUPPORTED_FEATURES = [ "thin_lto",