diff --git a/python/cc/BUILD.bazel b/python/cc/BUILD.bazel index d4a6bb8f6d..461fc19788 100644 --- a/python/cc/BUILD.bazel +++ b/python/cc/BUILD.bazel @@ -2,7 +2,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library") load("//python/private:current_py_cc_headers.bzl", "current_py_cc_headers") -load("//python/private:util.bzl", "BZLMOD_ENABLED") +load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") package( default_visibility = ["//:__subpackages__"], diff --git a/python/pip.bzl b/python/pip.bzl index 941c1e05b8..cae15919b0 100644 --- a/python/pip.bzl +++ b/python/pip.bzl @@ -16,6 +16,7 @@ load("//python/pip_install:pip_repository.bzl", "pip_repository", _package_annotation = "package_annotation") load("//python/pip_install:repositories.bzl", "pip_install_dependencies") load("//python/pip_install:requirements.bzl", _compile_pip_requirements = "compile_pip_requirements") +load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") load(":versions.bzl", "MINOR_MAPPING") compile_pip_requirements = _compile_pip_requirements @@ -286,7 +287,7 @@ def _whl_library_render_alias_target( wheel_name): # The template below adds one @, but under bzlmod, the name # is canonical, so we have to add a second @. - if str(Label("//:unused")).startswith("@@"): + if BZLMOD_ENABLED: rules_python = "@" + rules_python alias = ["""\ alias( diff --git a/python/pip_install/pip_repository.bzl b/python/pip_install/pip_repository.bzl index 866a834a72..88dedf091b 100644 --- a/python/pip_install/pip_repository.bzl +++ b/python/pip_install/pip_repository.bzl @@ -19,6 +19,7 @@ load("//python:versions.bzl", "WINDOWS_NAME") load("//python/pip_install:repositories.bzl", "all_requirements") load("//python/pip_install:requirements_parser.bzl", parse_requirements = "parse") load("//python/pip_install/private:srcs.bzl", "PIP_INSTALL_PY_SRCS") +load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") load("//python/private:toolchains_repo.bzl", "get_host_os_arch") CPPFLAGS = "CPPFLAGS" @@ -76,8 +77,7 @@ def _resolve_python_interpreter(rctx): if rctx.attr.python_interpreter_target != None: python_interpreter = rctx.path(rctx.attr.python_interpreter_target) - # If we have @@ we have bzlmod so we need to hand Windows differently. - if str(Label("//:unused")).startswith("@@"): + if BZLMOD_ENABLED: (os, _) = get_host_os_arch(rctx) # On Windows, the symlink doesn't work because Windows attempts to find diff --git a/python/private/bzlmod_enabled.bzl b/python/private/bzlmod_enabled.bzl new file mode 100644 index 0000000000..84839981a0 --- /dev/null +++ b/python/private/bzlmod_enabled.bzl @@ -0,0 +1,18 @@ +# +# 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. + +"""Variable to check if bzlmod is enabled""" + +# When bzlmod is enabled, canonical repos names have @@ in them, while under +# workspace builds, there is never a @@ in labels. +BZLMOD_ENABLED = "@@" in str(Label("//:unused")) diff --git a/python/private/util.bzl b/python/private/util.bzl index 4c4b8fcf69..6c8761d5b5 100644 --- a/python/private/util.bzl +++ b/python/private/util.bzl @@ -16,10 +16,6 @@ load("@bazel_skylib//lib:types.bzl", "types") -# When bzlmod is enabled, canonical repos names have @@ in them, while under -# workspace builds, there is never a @@ in labels. -BZLMOD_ENABLED = "@@" in str(Label("//:unused")) - def copy_propagating_kwargs(from_kwargs, into_kwargs = None): """Copies args that must be compatible between two targets with a dependency relationship. diff --git a/python/repositories.bzl b/python/repositories.bzl index 38a580e3a8..62d94210e0 100644 --- a/python/repositories.bzl +++ b/python/repositories.bzl @@ -19,6 +19,7 @@ For historic reasons, pip_repositories() is defined in //python:pip.bzl. load("@bazel_tools//tools/build_defs/repo:http.bzl", _http_archive = "http_archive") load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") +load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED") load("//python/private:coverage_deps.bzl", "coverage_dep") load( "//python/private:toolchains_repo.bzl", @@ -498,9 +499,7 @@ def python_register_toolchains( **kwargs: passed to each python_repositories call. """ - # If we have @@ we have bzlmod - bzlmod = str(Label("//:unused")).startswith("@@") - if bzlmod: + if BZLMOD_ENABLED: # you cannot used native.register_toolchains when using bzlmod. register_toolchains = False @@ -580,7 +579,7 @@ def python_register_toolchains( ) # in bzlmod we write out our own toolchain repos - if bzlmod: + if BZLMOD_ENABLED: return toolchains_repo(