Skip to content

[pull] main from bazel-contrib:main #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 8 additions & 4 deletions internal_dev_setup.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ def rules_python_internal_setup():
name = "pythons_hub",
minor_mapping = MINOR_MAPPING,
default_python_version = "",
toolchain_prefixes = [],
toolchain_python_versions = [],
toolchain_set_python_version_constraints = [],
toolchain_user_repository_names = [],
python_versions = sorted(TOOL_VERSIONS.keys()),
toolchain_names = [],
toolchain_repo_names = {},
toolchain_target_compatible_with_map = {},
toolchain_target_settings_map = {},
toolchain_platform_keys = {},
toolchain_python_versions = {},
toolchain_set_python_version_constraints = {},
base_toolchain_repo_names = [],
)

runtime_env_repo(name = "rules_python_runtime_env_tc_info")
Expand Down
29 changes: 28 additions & 1 deletion python/private/config_settings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ If the value is missing, then the default value is being used, see documentation
{docs_url}/python/config_settings
"""

# Indicates something needs public visibility so that other generated code can
# access it, but it's not intended for general public usage.
_NOT_ACTUALLY_PUBLIC = ["//visibility:public"]

def construct_config_settings(*, name, default_version, versions, minor_mapping, documented_flags): # buildifier: disable=function-docstring
"""Create a 'python_version' config flag and construct all config settings used in rules_python.

Expand Down Expand Up @@ -128,7 +132,30 @@ def construct_config_settings(*, name, default_version, versions, minor_mapping,
# `whl_library` in the hub repo created by `pip.parse`.
flag_values = {"current_config": "will-never-match"},
# Only public so that PyPI hub repo can access it
visibility = ["//visibility:public"],
visibility = _NOT_ACTUALLY_PUBLIC,
)

libc = Label("//python/config_settings:py_linux_libc")
native.config_setting(
name = "_is_py_linux_libc_glibc",
flag_values = {libc: "glibc"},
visibility = _NOT_ACTUALLY_PUBLIC,
)
native.config_setting(
name = "_is_py_linux_libc_musl",
flag_values = {libc: "glibc"},
visibility = _NOT_ACTUALLY_PUBLIC,
)
freethreaded = Label("//python/config_settings:py_freethreaded")
native.config_setting(
name = "_is_py_freethreaded_yes",
flag_values = {freethreaded: "yes"},
visibility = _NOT_ACTUALLY_PUBLIC,
)
native.config_setting(
name = "_is_py_freethreaded_no",
flag_values = {freethreaded: "no"},
visibility = _NOT_ACTUALLY_PUBLIC,
)

def _python_version_flag_impl(ctx):
Expand Down
12 changes: 8 additions & 4 deletions python/private/py_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ def py_repositories():
name = "pythons_hub",
minor_mapping = MINOR_MAPPING,
default_python_version = "",
toolchain_prefixes = [],
toolchain_python_versions = [],
toolchain_set_python_version_constraints = [],
toolchain_user_repository_names = [],
python_versions = sorted(TOOL_VERSIONS.keys()),
toolchain_names = [],
toolchain_repo_names = {},
toolchain_target_compatible_with_map = {},
toolchain_target_settings_map = {},
toolchain_platform_keys = {},
toolchain_python_versions = {},
toolchain_set_python_version_constraints = {},
base_toolchain_repo_names = [],
)
http_archive(
name = "bazel_skylib",
Expand Down
11 changes: 8 additions & 3 deletions python/private/py_toolchain_suite.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@ def py_toolchain_suite(
python_version,
set_python_version_constraint,
flag_values,
target_settings = [],
target_compatible_with = []):
"""For internal use only.

Args:
prefix: Prefix for toolchain target names.
user_repository_name: The name of the user repository.
user_repository_name: The name of the repository with the toolchain
implementation (it's assumed to have particular target names within
it). Does not include the leading "@".
python_version: The full (X.Y.Z) version of the interpreter.
set_python_version_constraint: True or False as a string.
flag_values: Extra flag values to match for this toolchain.
flag_values: Extra flag values to match for this toolchain. These
are prepended to target_settings.
target_settings: Extra target_settings to match for this toolchain.
target_compatible_with: list constraints the toolchains are compatible with.
"""

Expand Down Expand Up @@ -82,7 +87,7 @@ def py_toolchain_suite(
match_any = match_any,
visibility = ["//visibility:private"],
)
target_settings = [name]
target_settings = [name] + target_settings
else:
fail(("Invalid set_python_version_constraint value: got {} {}, wanted " +
"either the string 'True' or the string 'False'; " +
Expand Down
4 changes: 0 additions & 4 deletions python/private/pypi/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,9 @@ bzl_library(
name = "pep508_deps_bzl",
srcs = ["pep508_deps.bzl"],
deps = [
":pep508_env_bzl",
":pep508_evaluate_bzl",
":pep508_platform_bzl",
":pep508_requirement_bzl",
"//python/private:full_version_bzl",
"//python/private:normalize_name_bzl",
"@pythons_hub//:versions_bzl",
],
)

Expand Down
2 changes: 1 addition & 1 deletion python/private/pypi/config.bzl.tmpl.bzlmod
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ with your usecase. This may change in between rules_python versions without any
@generated by rules_python pip.parse bzlmod extension.
"""

target_platforms = %%TARGET_PLATFORMS%%
whl_map = %%WHL_MAP%%
35 changes: 23 additions & 12 deletions python/private/pypi/extension.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
load("@bazel_features//:features.bzl", "bazel_features")
load("@pythons_hub//:interpreters.bzl", "INTERPRETER_LABELS")
load("@pythons_hub//:versions.bzl", "MINOR_MAPPING")
load("@rules_python_internal//:rules_python_config.bzl", rp_config = "config")
load("//python/private:auth.bzl", "AUTH_ATTRS")
load("//python/private:full_version.bzl", "full_version")
load("//python/private:normalize_name.bzl", "normalize_name")
Expand Down Expand Up @@ -72,7 +73,8 @@ def _create_whl_repos(
available_interpreters = INTERPRETER_LABELS,
minor_mapping = MINOR_MAPPING,
evaluate_markers = evaluate_markers_py,
get_index_urls = None):
get_index_urls = None,
enable_pipstar = False):
"""create all of the whl repositories

Args:
Expand All @@ -87,6 +89,7 @@ def _create_whl_repos(
minor_mapping: {type}`dict[str, str]` The dictionary needed to resolve the full
python version used to parse package METADATA files.
evaluate_markers: the function used to evaluate the markers.
enable_pipstar: enable the pipstar feature.

Returns a {type}`struct` with the following attributes:
whl_map: {type}`dict[str, list[struct]]` the output is keyed by the
Expand Down Expand Up @@ -216,7 +219,6 @@ def _create_whl_repos(
enable_implicit_namespace_pkgs = pip_attr.enable_implicit_namespace_pkgs,
environment = pip_attr.environment,
envsubst = pip_attr.envsubst,
experimental_target_platforms = pip_attr.experimental_target_platforms,
group_deps = group_deps,
group_name = group_name,
pip_data_exclude = pip_attr.pip_data_exclude,
Expand All @@ -227,6 +229,9 @@ def _create_whl_repos(
for p, args in whl_overrides.get(whl_name, {}).items()
},
)
if not enable_pipstar:
maybe_args["experimental_target_platforms"] = pip_attr.experimental_target_platforms

whl_library_args.update({k: v for k, v in maybe_args.items() if v})
maybe_args_with_default = dict(
# The following values have defaults next to them
Expand All @@ -249,6 +254,7 @@ def _create_whl_repos(
auth_patterns = pip_attr.auth_patterns,
python_version = major_minor,
multiple_requirements_for_whl = len(requirements) > 1.,
enable_pipstar = enable_pipstar,
).items():
repo_name = "{}_{}".format(pip_name, repo_name)
if repo_name in whl_libraries:
Expand Down Expand Up @@ -277,7 +283,7 @@ def _create_whl_repos(
},
)

def _whl_repos(*, requirement, whl_library_args, download_only, netrc, auth_patterns, multiple_requirements_for_whl = False, python_version):
def _whl_repos(*, requirement, whl_library_args, download_only, netrc, auth_patterns, multiple_requirements_for_whl = False, python_version, enable_pipstar = False):
ret = {}

dists = requirement.whls
Expand Down Expand Up @@ -305,13 +311,14 @@ def _whl_repos(*, requirement, whl_library_args, download_only, netrc, auth_patt
args["urls"] = [distribution.url]
args["sha256"] = distribution.sha256
args["filename"] = distribution.filename
args["experimental_target_platforms"] = [
# Get rid of the version fot the target platforms because we are
# passing the interpreter any way. Ideally we should search of ways
# how to pass the target platforms through the hub repo.
p.partition("_")[2]
for p in requirement.target_platforms
]
if not enable_pipstar:
args["experimental_target_platforms"] = [
# Get rid of the version fot the target platforms because we are
# passing the interpreter any way. Ideally we should search of ways
# how to pass the target platforms through the hub repo.
p.partition("_")[2]
for p in requirement.target_platforms
]

# Pure python wheels or sdists may need to have a platform here
target_platforms = None
Expand Down Expand Up @@ -357,7 +364,11 @@ def _whl_repos(*, requirement, whl_library_args, download_only, netrc, auth_patt

return ret

def parse_modules(module_ctx, _fail = fail, simpleapi_download = simpleapi_download, **kwargs):
def parse_modules(
module_ctx,
_fail = fail,
simpleapi_download = simpleapi_download,
**kwargs):
"""Implementation of parsing the tag classes for the extension and return a struct for registering repositories.

Args:
Expand Down Expand Up @@ -639,7 +650,7 @@ def _pip_impl(module_ctx):
module_ctx: module contents
"""

mods = parse_modules(module_ctx)
mods = parse_modules(module_ctx, enable_pipstar = rp_config.enable_pipstar)

# Build all of the wheel modifications if the tag class is called.
_whl_mods_impl(mods.whl_mods)
Expand Down
27 changes: 22 additions & 5 deletions python/private/pypi/generate_whl_library_build_bazel.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ _RENDER = {
"entry_points": render.dict,
"extras": render.list,
"group_deps": render.list,
"include": str,
"requires_dist": render.list,
"srcs_exclude": render.list,
"tags": render.list,
"target_platforms": lambda x: render.list(x) if x else "target_platforms",
"target_platforms": render.list,
}

# NOTE @aignas 2024-10-25: We have to keep this so that files in
Expand Down Expand Up @@ -62,28 +63,44 @@ def generate_whl_library_build_bazel(
A complete BUILD file as a string
"""

fn = "whl_library_targets"
loads = []
if kwargs.get("tags"):
fn = "whl_library_targets"

# legacy path
unsupported_args = [
"requires",
"metadata_name",
"metadata_version",
"include",
]
else:
fn = "{}_from_requires".format(fn)
fn = "whl_library_targets_from_requires"
unsupported_args = [
"dependencies",
"dependencies_by_platform",
"target_platforms",
"default_python_version",
]
dep_template = kwargs.get("dep_template")
loads.append(
"""load("{}", "{}")""".format(
dep_template.format(
name = "",
target = "config.bzl",
),
"whl_map",
),
)
kwargs["include"] = "whl_map"

for arg in unsupported_args:
if kwargs.get(arg):
fail("BUG, unsupported arg: '{}'".format(arg))

loads = [
loads.extend([
"""load("@rules_python//python/private/pypi:whl_library_targets.bzl", "{}")""".format(fn),
]
])

additional_content = []
if annotation:
Expand Down
2 changes: 1 addition & 1 deletion python/private/pypi/hub_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def _impl(rctx):
"config.bzl",
rctx.attr._config_template,
substitutions = {
"%%TARGET_PLATFORMS%%": render.list(rctx.attr.target_platforms),
"%%WHL_MAP%%": render.dict(rctx.attr.whl_map, value_repr = lambda x: "None"),
},
)
rctx.template("requirements.bzl", rctx.attr._requirements_bzl_template, substitutions = {
Expand Down
Loading