Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ END_UNRELEASED_TEMPLATE
* (pypi) Starlark-based evaluation of environment markers (requirements.txt conditionals)
available (not enabled by default) for improved multi-platform build support.
Set the `RULES_PYTHON_ENABLE_PIPSTAR=1` environment variable to enable it.
Work towards [#260](https://github.com/bazel-contrib/rules_python/issues/260).

{#v0-0-0-removed}
### Removed
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