Skip to content

Commit

Permalink
Merge branch 'main' into updated-pip-packaging-versions
Browse files Browse the repository at this point in the history
  • Loading branch information
vfdev-5 authored Dec 24, 2024
2 parents 033bd41 + 922929b commit 0f921dc
Show file tree
Hide file tree
Showing 31 changed files with 1,265 additions and 1,036 deletions.
1 change: 1 addition & 0 deletions .bazelignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ examples/bzlmod/other_module/bazel-bin
examples/bzlmod/other_module/bazel-other_module
examples/bzlmod/other_module/bazel-out
examples/bzlmod/other_module/bazel-testlogs
examples/bzlmod/py_proto_library/foo_external
examples/bzlmod_build_file_generation/bazel-bzlmod_build_file_generation
examples/multi_python_versions/bazel-multi_python_versions
examples/pip_parse/bazel-pip_parse
Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Unreleased changes template.

{#v0-0-0-changed}
### Changed
* (toolchains) 3.13 means 3.13.1 (previously 3.13.0)
* Bazel 6 support is dropped and Bazel 7.4.1 is the minimum supported
version, per our Bazel support matrix. Earlier versions are not
tested by CI, so functionality cannot be guaranteed.
Expand All @@ -70,6 +71,10 @@ Unreleased changes template.
* (pypi) Using {bzl:obj}`pip_parse.experimental_requirement_cycles` and
{bzl:obj}`pip_parse.use_hub_alias_dependencies` together now works when
using WORKSPACE files.
* (pypi) The error messages when the wheel distributions do not match anything
are now printing more details and include the currently active flag
values. Fixes [#2466](https://github.com/bazelbuild/rules_python/issues/2466).
* (py_proto_library) Fix import paths in Bazel 8.

[pep-695]: https://peps.python.org/pep-0695/

Expand All @@ -84,6 +89,14 @@ Unreleased changes template.
To select the free-threaded interpreter in the repo phase, please use
the documented [env](/environment-variables.html) variables.
Fixes [#2386](https://github.com/bazelbuild/rules_python/issues/2386).
* (toolchains) Use the latest astrahl-sh toolchain release [20241206] for Python versions:
* 3.9.21
* 3.10.16
* 3.11.11
* 3.12.8
* 3.13.1

[20241206]: https://github.com/astral-sh/python-build-standalone/releases/tag/20241206

{#v0-0-0-removed}
### Removed
Expand Down
18 changes: 18 additions & 0 deletions docs/api/rules_python/python/config_settings/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,21 @@ instead.
:::

::::

::::{bzl:flag} current_config
Fail the build if the current build configuration does not match the
{obj}`pip.parse` defined wheels.

Values:
* `fail`: Will fail in the build action ensuring that we get the error
message no matter the action cache.
* ``: (empty string) The default value, that will just print a warning.

:::{seealso}
{obj}`pip.parse`
:::

:::{versionadded} 1.1.0
:::

::::
1 change: 1 addition & 0 deletions examples/bzlmod/.bazelignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
other_module
py_proto_library/foo_external
7 changes: 7 additions & 0 deletions examples/bzlmod/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module(
)

bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "platforms", version = "0.0.4")
bazel_dep(name = "rules_python", version = "0.0.0")
local_path_override(
module_name = "rules_python",
Expand Down Expand Up @@ -272,5 +273,11 @@ local_path_override(
path = "other_module",
)

bazel_dep(name = "foo_external", version = "")
local_path_override(
module_name = "foo_external",
path = "py_proto_library/foo_external",
)

# example test dependencies
bazel_dep(name = "rules_shell", version = "0.3.0", dev_dependency = True)
16 changes: 16 additions & 0 deletions examples/bzlmod/py_proto_library/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@bazel_skylib//rules:native_binary.bzl", "native_test")
load("@rules_python//python:py_test.bzl", "py_test")

py_test(
Expand All @@ -16,3 +17,18 @@ py_test(
"//py_proto_library/example.com/another_proto:message_proto_py_pb2",
],
)

# Regression test for https://github.com/bazelbuild/rules_python/issues/2515
#
# This test failed before https://github.com/bazelbuild/rules_python/pull/2516
# when ran with --legacy_external_runfiles=False (default in Bazel 8.0.0).
native_test(
name = "external_import_test",
src = "@foo_external//:py_binary_with_proto",
# Incompatible with Windows: native_test wrapping a py_binary doesn't work
# on Windows.
target_compatible_with = select({
"@platforms//os:windows": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)
22 changes: 22 additions & 0 deletions examples/bzlmod/py_proto_library/foo_external/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
load("@rules_proto//proto:defs.bzl", "proto_library")
load("@rules_python//python:proto.bzl", "py_proto_library")
load("@rules_python//python:py_binary.bzl", "py_binary")

package(default_visibility = ["//visibility:public"])

proto_library(
name = "proto_lib",
srcs = ["nested/foo/my_proto.proto"],
strip_import_prefix = "/nested/foo",
)

py_proto_library(
name = "a_proto",
deps = [":proto_lib"],
)

py_binary(
name = "py_binary_with_proto",
srcs = ["py_binary_with_proto.py"],
deps = [":a_proto"],
)
8 changes: 8 additions & 0 deletions examples/bzlmod/py_proto_library/foo_external/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module(
name = "foo_external",
version = "0.0.1",
)

bazel_dep(name = "rules_python", version = "1.0.0")
bazel_dep(name = "protobuf", version = "28.2", repo_name = "com_google_protobuf")
bazel_dep(name = "rules_proto", version = "7.0.2")
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
syntax = "proto3";

package my_proto;

message MyMessage {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sys

if __name__ == "__main__":
import my_proto_pb2
sys.exit(0)
9 changes: 9 additions & 0 deletions python/config_settings/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ filegroup(
construct_config_settings(
name = "construct_config_settings",
default_version = DEFAULT_PYTHON_VERSION,
documented_flags = [
":pip_whl",
":pip_whl_glibc_version",
":pip_whl_muslc_version",
":pip_whl_osx_arch",
":pip_whl_osx_version",
":py_freethreaded",
":py_linux_libc",
],
minor_mapping = MINOR_MAPPING,
versions = PYTHON_VERSIONS,
)
Expand Down
54 changes: 21 additions & 33 deletions python/private/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -104,30 +104,18 @@ bzl_library(
deps = [":py_internal_bzl"],
)

bzl_library(
name = "common_bazel_bzl",
srcs = ["common_bazel.bzl"],
deps = [
":attributes_bzl",
":common_bzl",
":py_cc_link_params_info_bzl",
":py_internal_bzl",
":py_interpreter_program_bzl",
":toolchain_types_bzl",
"@bazel_skylib//lib:paths",
],
)

bzl_library(
name = "common_bzl",
srcs = ["common.bzl"],
deps = [
":cc_helper_bzl",
":py_cc_link_params_info_bzl",
":py_info_bzl",
":py_internal_bzl",
":reexports_bzl",
":rules_cc_srcs_bzl",
":semantics_bzl",
"@bazel_skylib//lib:paths",
],
)

Expand Down Expand Up @@ -199,6 +187,18 @@ bzl_library(
srcs = ["normalize_name.bzl"],
)

bzl_library(
name = "precompile_bzl",
srcs = ["precompile.bzl"],
deps = [
":attributes_bzl",
":py_internal_bzl",
":py_interpreter_program_bzl",
":toolchain_types_bzl",
"@bazel_skylib//lib:paths",
],
)

bzl_library(
name = "python_bzl",
srcs = ["python.bzl"],
Expand Down Expand Up @@ -265,8 +265,8 @@ bzl_library(
name = "py_binary_macro_bzl",
srcs = ["py_binary_macro.bzl"],
deps = [
":common_bzl",
":py_binary_rule_bzl",
":py_executable_bzl",
],
)

Expand All @@ -275,7 +275,7 @@ bzl_library(
srcs = ["py_binary_rule.bzl"],
deps = [
":attributes_bzl",
":py_executable_bazel_bzl",
":py_executable_bzl",
":semantics_bzl",
"@bazel_skylib//lib:dicts",
],
Expand Down Expand Up @@ -343,20 +343,6 @@ bzl_library(
],
)

bzl_library(
name = "py_executable_bazel_bzl",
srcs = ["py_executable_bazel.bzl"],
deps = [
":attributes_bzl",
":common_bazel_bzl",
":common_bzl",
":py_executable_bzl",
":py_internal_bzl",
":py_runtime_info_bzl",
":semantics_bzl",
],
)

bzl_library(
name = "py_executable_bzl",
srcs = ["py_executable.bzl"],
Expand All @@ -365,6 +351,7 @@ bzl_library(
":cc_helper_bzl",
":common_bzl",
":flags_bzl",
":precompile_bzl",
":py_cc_link_params_info_bzl",
":py_executable_info_bzl",
":py_info_bzl",
Expand All @@ -373,6 +360,7 @@ bzl_library(
":rules_cc_srcs_bzl",
":toolchain_types_bzl",
"@bazel_skylib//lib:dicts",
"@bazel_skylib//lib:paths",
"@bazel_skylib//lib:structs",
"@bazel_skylib//rules:common_settings",
],
Expand Down Expand Up @@ -431,8 +419,8 @@ bzl_library(
name = "py_library_rule_bzl",
srcs = ["py_library_rule.bzl"],
deps = [
":common_bazel_bzl",
":common_bzl",
":precompile_bzl",
":py_library_bzl",
],
)
Expand Down Expand Up @@ -508,7 +496,7 @@ bzl_library(
name = "py_test_macro_bzl",
srcs = ["py_test_macro.bzl"],
deps = [
":common_bazel_bzl",
":py_executable_bzl",
":py_test_rule_bzl",
],
)
Expand All @@ -519,7 +507,7 @@ bzl_library(
deps = [
":attributes_bzl",
":common_bzl",
":py_executable_bazel_bzl",
":py_executable_bzl",
":semantics_bzl",
"@bazel_skylib//lib:dicts",
],
Expand Down
61 changes: 60 additions & 1 deletion python/private/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
# 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.
"""Various things common to Bazel and Google rule implementations."""
"""Various things common to rule implementations."""

load("@bazel_skylib//lib:paths.bzl", "paths")
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
load(":cc_helper.bzl", "cc_helper")
load(":py_cc_link_params_info.bzl", "PyCcLinkParamsInfo")
load(":py_info.bzl", "PyInfo", "PyInfoBuilder")
load(":py_internal.bzl", "py_internal")
load(":reexports.bzl", "BuiltinPyInfo")
Expand Down Expand Up @@ -262,6 +266,30 @@ def filter_to_py_srcs(srcs):
# as a valid extension.
return [f for f in srcs if f.extension == "py"]

def collect_cc_info(ctx, extra_deps = []):
"""Collect C++ information from dependencies for Bazel.
Args:
ctx: Rule ctx; must have `deps` attribute.
extra_deps: list of Target to also collect C+ information from.
Returns:
CcInfo provider of merged information.
"""
deps = ctx.attr.deps
if extra_deps:
deps = list(deps)
deps.extend(extra_deps)
cc_infos = []
for dep in deps:
if CcInfo in dep:
cc_infos.append(dep[CcInfo])

if PyCcLinkParamsInfo in dep:
cc_infos.append(dep[PyCcLinkParamsInfo].cc_info)

return cc_common.merge_cc_infos(cc_infos = cc_infos)

def collect_imports(ctx, semantics):
"""Collect the direct and transitive `imports` strings.
Expand All @@ -280,6 +308,37 @@ def collect_imports(ctx, semantics):
transitive.append(dep[BuiltinPyInfo].imports)
return depset(direct = semantics.get_imports(ctx), transitive = transitive)

def get_imports(ctx):
"""Gets the imports from a rule's `imports` attribute.
See create_binary_semantics_struct for details about this function.
Args:
ctx: Rule ctx.
Returns:
List of strings.
"""
prefix = "{}/{}".format(
ctx.workspace_name,
py_internal.get_label_repo_runfiles_path(ctx.label),
)
result = []
for import_str in ctx.attr.imports:
import_str = ctx.expand_make_variables("imports", import_str, {})
if import_str.startswith("/"):
continue

# To prevent "escaping" out of the runfiles tree, we normalize
# the path and ensure it doesn't have up-level references.
import_path = paths.normalize("{}/{}".format(prefix, import_str))
if import_path.startswith("../") or import_path == "..":
fail("Path '{}' references a path above the execution root".format(
import_str,
))
result.append(import_path)
return result

def collect_runfiles(ctx, files = depset()):
"""Collects the necessary files from the rule's context.
Expand Down
Loading

0 comments on commit 0f921dc

Please sign in to comment.