aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Love <335402+chrislovecnm@users.noreply.github.com>2023-07-10 13:41:33 -0600
committerGitHub <noreply@github.com>2023-07-10 19:41:33 +0000
commita068d1bf6545fa74d52f6d73c2d79ec37f8ab6b9 (patch)
tree817fa9761510f03fac4f449d4c3d9afa8c4f32a3
parent3ffdf01edac7be32e229e7cd08100370e35348a0 (diff)
downloadbazelbuild-rules_python-a068d1bf6545fa74d52f6d73c2d79ec37f8ab6b9.tar.gz
feat(bzlmod): Use a common constant for detecting bzlmod being enabled (#1302)
Various parts of the codebase detect whether bzlmod is enabled or not. Most of them copy/paste the same if @ in Label(..) trick and use a comment to explain what they're doing. Rather than copy/paste that everywhere, this commit uses a constant defined that does this once and reuses the constant value to determine if bzlmod is enabled. Closes: https://github.com/bazelbuild/rules_python/issues/1295
-rw-r--r--python/cc/BUILD.bazel2
-rw-r--r--python/pip.bzl3
-rw-r--r--python/pip_install/pip_repository.bzl4
-rw-r--r--python/private/bzlmod_enabled.bzl18
-rw-r--r--python/private/util.bzl4
-rw-r--r--python/repositories.bzl7
6 files changed, 26 insertions, 12 deletions
diff --git a/python/cc/BUILD.bazel b/python/cc/BUILD.bazel
index d4a6bb8..0d90e15 100644
--- a/python/cc/BUILD.bazel
+++ b/python/cc/BUILD.bazel
@@ -1,8 +1,8 @@
# Package for C/C++ specific functionality of the Python rules.
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
+load("//python/private:bzlmod_enabled.bzl", "BZLMOD_ENABLED")
load("//python/private:current_py_cc_headers.bzl", "current_py_cc_headers")
-load("//python/private:util.bzl", "BZLMOD_ENABLED")
package(
default_visibility = ["//:__subpackages__"],
diff --git a/python/pip.bzl b/python/pip.bzl
index 941c1e0..cae1591 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 866a834..88dedf0 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 0000000..8483998
--- /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 4c4b8fc..6c8761d 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 38a580e..62d9421 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(