Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module(
compatibility_level = 1,
)

bazel_dep(name = "bazel_features", version = "1.9.1")
bazel_dep(name = "bazel_features", version = "1.18.0")
bazel_dep(name = "bazel_skylib", version = "1.6.1")
bazel_dep(name = "rules_cc", version = "0.0.9")
bazel_dep(name = "platforms", version = "0.0.4")
Expand Down
4 changes: 4 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ load("//:internal_deps.bzl", "rules_python_internal_deps")

rules_python_internal_deps()

load("//python:repositories_deps.bzl", "py_repositories_deps")

py_repositories_deps()

load("@rules_jvm_external//:repositories.bzl", "rules_jvm_external_deps")

rules_jvm_external_deps()
Expand Down
4 changes: 4 additions & 0 deletions examples/build_file_generation/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ load("@rules_python//python:repositories.bzl", "py_repositories", "python_regist
# Perform general setup
py_repositories()

load("@rules_python//python:repositories_deps.bzl", "py_repositories_deps")

py_repositories_deps()

# We now register a hermetic Python interpreter rather than relying on a system-installed interpreter.
# This toolchain will allow bazel to download a specific python version, and use that version
# for compilation.
Expand Down
9 changes: 4 additions & 5 deletions examples/bzlmod/MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions examples/multi_python_versions/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ load("@rules_python//python:repositories.bzl", "py_repositories", "python_regist

py_repositories()

load("@rules_python//python:repositories_deps.bzl", "py_repositories_deps")

py_repositories_deps()

default_python_version = "3.9"

python_register_multi_toolchains(
Expand Down
4 changes: 4 additions & 0 deletions examples/pip_parse/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ load("@rules_python//python:repositories.bzl", "py_repositories", "python_regist

py_repositories()

load("@rules_python//python:repositories_deps.bzl", "py_repositories_deps")

py_repositories_deps()

python_register_toolchains(
name = "python_3_9",
python_version = "3.9.13",
Expand Down
4 changes: 4 additions & 0 deletions examples/pip_parse_vendored/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ load("@rules_python//python:repositories.bzl", "py_repositories", "python_regist

py_repositories()

load("@rules_python//python:repositories_deps.bzl", "py_repositories_deps")

py_repositories_deps()

python_register_toolchains(
name = "python39",
python_version = "3.9",
Expand Down
4 changes: 4 additions & 0 deletions examples/pip_repository_annotations/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ load("@rules_python//python:repositories.bzl", "py_repositories", "python_regist

py_repositories()

load("@rules_python//python:repositories_deps.bzl", "py_repositories_deps")

py_repositories_deps()

python_register_toolchains(
name = "python39",
python_version = "3.9",
Expand Down
4 changes: 4 additions & 0 deletions examples/py_proto_library/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ load("@rules_python//python:repositories.bzl", "py_repositories", "python_regist
# We install the rules_python dependencies using the function below.
py_repositories()

load("@rules_python//python:repositories_deps.bzl", "py_repositories_deps")

py_repositories_deps()

python_register_toolchains(
name = "python39",
python_version = "3.9",
Expand Down
4 changes: 4 additions & 0 deletions gazelle/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ load("@rules_python//python:repositories.bzl", "py_repositories")

py_repositories()

load("@rules_python//python:repositories_deps.bzl", "py_repositories_deps")

py_repositories_deps()

load("//:deps.bzl", _py_gazelle_deps = "gazelle_deps")

# gazelle:repository_macro deps.bzl%go_deps
Expand Down
6 changes: 3 additions & 3 deletions internal_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,9 @@ def rules_python_internal_deps():

http_archive(
name = "bazel_features",
sha256 = "d7787da289a7fb497352211ad200ec9f698822a9e0757a4976fd9f713ff372b3",
strip_prefix = "bazel_features-1.9.1",
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.9.1/bazel_features-v1.9.1.tar.gz",
sha256 = "b4b145c19e08fd48337f53c383db46398d0a810002907ff0c590762d926e05be",
strip_prefix = "bazel_features-1.18.0",
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.18.0/bazel_features-v1.18.0.tar.gz",
)

http_archive(
Expand Down
1 change: 1 addition & 0 deletions python/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ bzl_library(
srcs = ["py_cc_link_params_info.bzl"],
deps = [
"//python/private/common:providers_bzl",
"@bazel_features//:features",
"@rules_python_internal//:rules_python_config_bzl",
],
)
Expand Down
4 changes: 2 additions & 2 deletions python/config_settings/transition.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ def _transition_py_impl(ctx):
]
if PyInfo in target:
providers.append(target[PyInfo])
if BuiltinPyInfo in target and PyInfo != BuiltinPyInfo:
if BuiltinPyInfo != None and BuiltinPyInfo in target and PyInfo != BuiltinPyInfo:
providers.append(target[BuiltinPyInfo])

if PyRuntimeInfo in target:
providers.append(target[PyRuntimeInfo])
if BuiltinPyRuntimeInfo in target and PyRuntimeInfo != BuiltinPyRuntimeInfo:
if BuiltinPyRuntimeInfo != None and BuiltinPyRuntimeInfo in target and PyRuntimeInfo != BuiltinPyRuntimeInfo:
providers.append(target[BuiltinPyRuntimeInfo])
return providers

Expand Down
5 changes: 4 additions & 1 deletion python/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,10 @@ bzl_library(
visibility = [
"//:__subpackages__",
],
deps = [":bazel_tools_bzl"],
deps = [
":bazel_tools_bzl",
"@bazel_features//:features",
],
)

bzl_library(
Expand Down
5 changes: 3 additions & 2 deletions python/private/common/attributes.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,16 @@ COMMON_ATTRS = union_attrs(
allow_none = True,
)

_MaybeBuiltinPyInfo = [[BuiltinPyInfo]] if BuiltinPyInfo != None else []

# Attributes common to rules accepting Python sources and deps.
PY_SRCS_ATTRS = union_attrs(
{
"deps": attr.label_list(
providers = [
[PyInfo],
[CcInfo],
[BuiltinPyInfo],
],
] + _MaybeBuiltinPyInfo,
# TODO(b/228692666): Google-specific; remove these allowances once
# the depot is cleaned up.
allow_rules = DEPS_ATTR_ALLOW_RULES,
Expand Down
8 changes: 4 additions & 4 deletions python/private/common/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def collect_imports(ctx, semantics):
dep[BuiltinPyInfo].imports
for dep in ctx.attr.deps
if BuiltinPyInfo in dep
])
] if BuiltinPyInfo != None else [])

def collect_runfiles(ctx, files = depset()):
"""Collects the necessary files from the rule's context.
Expand Down Expand Up @@ -374,7 +374,7 @@ def create_py_info(ctx, *, direct_sources, direct_pyc_files, imports):

for target in ctx.attr.deps:
# PyInfo may not be present e.g. cc_library rules.
if PyInfo in target or BuiltinPyInfo in target:
if PyInfo in target or (BuiltinPyInfo != None and BuiltinPyInfo in target):
py_info.merge(_get_py_info(target))
else:
# TODO(b/228692666): Remove this once non-PyInfo targets are no
Expand All @@ -395,7 +395,7 @@ def create_py_info(ctx, *, direct_sources, direct_pyc_files, imports):
for target in ctx.attr.data:
# TODO(b/234730058): Remove checking for PyInfo in data once depot
# cleaned up.
if PyInfo in target or BuiltinPyInfo in target:
if PyInfo in target or (BuiltinPyInfo != None and BuiltinPyInfo in target):
info = _get_py_info(target)
py_info.merge_uses_shared_libraries(info.uses_shared_libraries)
else:
Expand All @@ -410,7 +410,7 @@ def create_py_info(ctx, *, direct_sources, direct_pyc_files, imports):
return py_info.build(), deps_transitive_sources, py_info.build_builtin_py_info()

def _get_py_info(target):
return target[PyInfo] if PyInfo in target else target[BuiltinPyInfo]
return target[PyInfo] if PyInfo in target or BuiltinPyInfo == None else target[BuiltinPyInfo]

def create_instrumented_files_info(ctx):
return _coverage_common.instrumented_files_info(
Expand Down
2 changes: 1 addition & 1 deletion python/private/common/py_executable.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ def _create_providers(
# builtin py_runtime rule or defined their own. We can't directly detect
# the type of the provider object, but the rules_python PyRuntimeInfo
# object has an extra attribute that the builtin one doesn't.
if hasattr(py_runtime_info, "interpreter_version_info"):
if hasattr(py_runtime_info, "interpreter_version_info") and BuiltinPyRuntimeInfo != None:
providers.append(BuiltinPyRuntimeInfo(
interpreter_path = py_runtime_info.interpreter_path,
interpreter = py_runtime_info.interpreter,
Expand Down
14 changes: 8 additions & 6 deletions python/private/common/py_runtime_rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,20 @@ def _py_runtime_impl(ctx):
if not IS_BAZEL_7_OR_HIGHER:
builtin_py_runtime_info_kwargs.pop("bootstrap_template")

return [
providers = [
PyRuntimeInfo(**py_runtime_info_kwargs),
# Return the builtin provider for better compatibility.
# 1. There is a legacy code path in py_binary that
# checks for the provider when toolchains aren't used
# 2. It makes it easier to transition from builtins to rules_python
BuiltinPyRuntimeInfo(**builtin_py_runtime_info_kwargs),
DefaultInfo(
files = runtime_files,
runfiles = runfiles,
),
]
if BuiltinPyRuntimeInfo != None:
# Return the builtin provider for better compatibility.
# 1. There is a legacy code path in py_binary that
# checks for the provider when toolchains aren't used
# 2. It makes it easier to transition from builtins to rules_python
providers.append(BuiltinPyRuntimeInfo(**builtin_py_runtime_info_kwargs))
return providers

# Bind to the name "py_runtime" to preserve the kind/rule_class it shows up
# as elsewhere.
Expand Down
4 changes: 2 additions & 2 deletions python/private/py_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ This field is currently unused in Bazel and may go away in the future.
)

# The "effective" PyInfo is what the canonical //python:py_info.bzl%PyInfo symbol refers to
_EffectivePyInfo = PyInfo if config.enable_pystar else BuiltinPyInfo
_EffectivePyInfo = PyInfo if config.enable_pystar or BuiltinPyInfo == None else BuiltinPyInfo

def PyInfoBuilder():
# buildifier: disable=uninitialized
Expand Down Expand Up @@ -201,7 +201,7 @@ def _PyInfoBuilder_merge_all(self, py_infos):
def _PyInfoBuilder_merge_target(self, target):
if PyInfo in target:
self.merge(target[PyInfo])
elif BuiltinPyInfo in target:
elif BuiltinPyInfo != None and BuiltinPyInfo in target:
self.merge(target[BuiltinPyInfo])
return self

Expand Down
6 changes: 6 additions & 0 deletions python/private/py_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ def py_repositories():
sha256 = "2037875b9a4456dce4a79d112a8ae885bbc4aad968e6587dca6e64f3a0900cdf",
strip_prefix = "rules_cc-0.0.9",
)
http_archive(
name = "bazel_features",
sha256 = "b4b145c19e08fd48337f53c383db46398d0a810002907ff0c590762d926e05be",
strip_prefix = "bazel_features-1.18.0",
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.18.0/bazel_features-v1.18.0.tar.gz",
)
pypi_deps()
8 changes: 5 additions & 3 deletions python/private/py_runtime_pair_rule.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def _get_py_runtime_info(target):
# py_binary (implemented in Java) performs a type check on the provider
# value to verify it is an instance of the Java-implemented PyRuntimeInfo
# class.
if IS_BAZEL_7_OR_HIGHER and PyRuntimeInfo in target:
if (IS_BAZEL_7_OR_HIGHER and PyRuntimeInfo in target) or BuiltinPyRuntimeInfo == None:
return target[PyRuntimeInfo]
else:
return target[BuiltinPyRuntimeInfo]
Expand All @@ -70,21 +70,23 @@ def _is_py2_disabled(ctx):
return False
return ctx.fragments.py.disable_py2

_MaybeBuiltinPyRuntimeInfo = [[BuiltinPyRuntimeInfo]] if BuiltinPyRuntimeInfo != None else []

py_runtime_pair = rule(
implementation = _py_runtime_pair_impl,
attrs = {
# The two runtimes are used by the py_binary at runtime, and so need to
# be built for the target platform.
"py2_runtime": attr.label(
providers = [[PyRuntimeInfo], [BuiltinPyRuntimeInfo]],
providers = [[PyRuntimeInfo]] + _MaybeBuiltinPyRuntimeInfo,
cfg = "target",
doc = """\
The runtime to use for Python 2 targets. Must have `python_version` set to
`PY2`.
""",
),
"py3_runtime": attr.label(
providers = [[PyRuntimeInfo], [BuiltinPyRuntimeInfo]],
providers = [[PyRuntimeInfo]] + _MaybeBuiltinPyRuntimeInfo,
cfg = "target",
doc = """\
The runtime to use for Python 3 targets. Must have `python_version` set to
Expand Down
6 changes: 4 additions & 2 deletions python/private/reexports.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ inaccessible. So instead we access the builtin here and export it under a
different name. Then we can load it from elsewhere.
"""

load("@bazel_features//:features.bzl", "bazel_features")

# Don't use underscore prefix, since that would make the symbol local to this
# file only. Use a non-conventional name to emphasize that this is not a public
# symbol.
# buildifier: disable=name-conventions
BuiltinPyInfo = PyInfo
BuiltinPyInfo = getattr(bazel_features.globals, "PyInfo", None)

# buildifier: disable=name-conventions
BuiltinPyRuntimeInfo = PyRuntimeInfo
BuiltinPyRuntimeInfo = getattr(bazel_features.globals, "PyRuntimeInfo", None)
4 changes: 3 additions & 1 deletion python/py_cc_link_params_info.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Public entry point for PyCcLinkParamsInfo."""

load("@bazel_features//:features.bzl", "bazel_features")
load("@rules_python_internal//:rules_python_config.bzl", "config")
load("//python/private/common:providers.bzl", _starlark_PyCcLinkParamsProvider = "PyCcLinkParamsProvider")

PyCcLinkParamsInfo = _starlark_PyCcLinkParamsProvider if config.enable_pystar else PyCcLinkParamsProvider
_PyCcLinkParamsProvider = getattr(bazel_features.globals, "PyCcLinkParamsProvider", None) # buildifier: disable=name-conventions
PyCcLinkParamsInfo = _starlark_PyCcLinkParamsProvider if config.enable_pystar or _PyCcLinkParamsProvider == None else PyCcLinkParamsProvider
2 changes: 1 addition & 1 deletion python/py_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ load("@rules_python_internal//:rules_python_config.bzl", "config")
load("//python/private:py_info.bzl", _starlark_PyInfo = "PyInfo")
load("//python/private:reexports.bzl", "BuiltinPyInfo")

PyInfo = _starlark_PyInfo if config.enable_pystar else BuiltinPyInfo
PyInfo = _starlark_PyInfo if config.enable_pystar or BuiltinPyInfo == None else BuiltinPyInfo
6 changes: 6 additions & 0 deletions python/repositories_deps.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Sets up rules_python repository deps for WORKSPACE users"""

load("@bazel_features//:deps.bzl", "bazel_features_deps")

def py_repositories_deps():
bazel_features_deps()
4 changes: 4 additions & 0 deletions tests/integration/compile_pip_requirements/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ load("@rules_python//python:repositories.bzl", "py_repositories", "python_regist

py_repositories()

load("@rules_python//python:repositories_deps.bzl", "py_repositories_deps")

py_repositories_deps()

python_register_toolchains(
name = "python39",
python_version = "3.9",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ load("@rules_python//python:repositories.bzl", "py_repositories", "python_regist

py_repositories()

load("@rules_python//python:repositories_deps.bzl", "py_repositories_deps")

py_repositories_deps()

python_register_toolchains(
name = "python39",
python_version = "3.9",
Expand Down
Loading