Skip to content

Commit

Permalink
refactor: use usr flags instead of platforms in transition
Browse files Browse the repository at this point in the history
Signed-off-by: Thulio Ferraz Assis <3149049+f0rmiga@users.noreply.github.com>
  • Loading branch information
f0rmiga committed Nov 18, 2022
1 parent 059aa98 commit 1053ab0
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 142 deletions.
11 changes: 11 additions & 0 deletions examples/multi_python_versions/WORKSPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
workspace(name = "rules_python_multi_python_versions")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "bazel_skylib",
sha256 = "c6966ec828da198c5d9adbaa94c05e3a1c7f21bd012a0b29ba8ddbccb2c93b0d",
urls = [
"https://github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
"https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
],
)

local_repository(
name = "rules_python",
path = "../..",
Expand Down
2 changes: 1 addition & 1 deletion python/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ filegroup(
name = "distribution",
srcs = glob(["**"]) + [
"//python/constraints:distribution",
"//python/platforms:distribution",
"//python/config_settings:distribution",
"//python/private:distribution",
"//python/runfiles:distribution",
],
Expand Down
12 changes: 12 additions & 0 deletions python/config_settings/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("//python:versions.bzl", "TOOL_VERSIONS")
load(":config_settings.bzl", "construct_config_settings")

filegroup(
name = "distribution",
srcs = glob(["*.bzl"]) + [
"BUILD.bazel",
],
visibility = ["//python:__pkg__"],
)

construct_config_settings(python_versions = TOOL_VERSIONS.keys())
26 changes: 26 additions & 0 deletions python/config_settings/config_settings.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""This module is used to construct the config settings in the BUILD file in this same package.
"""

load("@bazel_skylib//rules:common_settings.bzl", "string_flag")

# buildifier: disable=unnamed-macro
def construct_config_settings(python_versions):
"""Constructs a set of configs for all Python versions.
Args:
python_versions: The Python versions supported by rules_python.
"""
string_flag(
name = "python_version",
build_setting_default = python_versions[0],
values = python_versions,
visibility = ["//visibility:public"],
)

for python_version in python_versions:
python_version_constraint_setting = "is_python_" + python_version
native.config_setting(
name = python_version_constraint_setting,
flag_values = {":python_version": python_version},
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ them to the desired target platform.

load("//python:defs.bzl", _py_binary = "py_binary", _py_test = "py_test")

def _transition_platform_impl(_, attr):
return {"//command_line_option:platforms": str(attr.target_platform)}
def _transition_python_version_impl(_, attr):
return {"//python/config_settings:python_version": str(attr.python_version)}

_transition_platform = transition(
implementation = _transition_platform_impl,
_transition_python_version = transition(
implementation = _transition_python_version_impl,
inputs = [],
outputs = ["//command_line_option:platforms"],
outputs = ["//python/config_settings:python_version"],
)

def _transition_py_impl(ctx):
Expand Down Expand Up @@ -61,15 +61,15 @@ _COMMON_ATTRS = {
"is_windows": attr.bool(
mandatory = True,
),
"python_version": attr.string(
mandatory = True,
),
"target": attr.label(
executable = True,
cfg = _transition_platform,
cfg = _transition_python_version,
mandatory = True,
providers = [PyInfo],
),
"target_platform": attr.label(
mandatory = True,
),
# "tools" is a hack here. It should be "data" but "data" is not included by default in the
# location expansion in the same way it is in the native Python rules. The difference on how
# the Bazel deals with those special attributes differ on the LocationExpander, e.g.:
Expand Down Expand Up @@ -102,7 +102,7 @@ _transition_py_test = rule(
test = True,
)

def _py_rule(rule, transition_rule, name, target_platform, **kwargs):
def _py_rule(rule, transition_rule, name, python_version, **kwargs):
args = kwargs.pop("args", None)
data = kwargs.pop("data", None)
env = kwargs.pop("env", None)
Expand Down Expand Up @@ -154,9 +154,9 @@ def _py_rule(rule, transition_rule, name, target_platform, **kwargs):
"//conditions:default": False,
}),
target = ":_" + name,
python_version = python_version,

# Attributes common to all build rules.
target_platform = target_platform,
compatible_with = compatible_with,
deprecation = deprecation,
distribs = distribs,
Expand All @@ -171,8 +171,8 @@ def _py_rule(rule, transition_rule, name, target_platform, **kwargs):
visibility = visibility,
)

def py_binary(name, target_platform, **kwargs):
return _py_rule(_py_binary, _transition_py_binary, name, target_platform, **kwargs)
def py_binary(name, python_version, **kwargs):
return _py_rule(_py_binary, _transition_py_binary, name, python_version, **kwargs)

def py_test(name, target_platform, **kwargs):
return _py_rule(_py_test, _transition_py_test, name, target_platform, **kwargs)
def py_test(name, python_version, **kwargs):
return _py_rule(_py_test, _transition_py_test, name, python_version, **kwargs)
26 changes: 12 additions & 14 deletions python/pip.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +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(":versions.bzl", "MINOR_MAPPING", "PLATFORMS")
load(":versions.bzl", "MINOR_MAPPING")

compile_pip_requirements = _compile_pip_requirements
package_annotation = _package_annotation
Expand Down Expand Up @@ -285,19 +285,17 @@ def _whl_library_render_alias_target(
alias(
name = "{alias_name}",
actual = select({{""".format(alias_name = alias_name)]
for [platform_name, meta] in PLATFORMS.items():
for [python_version, repo_prefix] in version_map:
alias.append("""\
"@{rules_python}//python/platforms:{platform_name}_{full_python_version}_config": "{actual}",""".format(
full_python_version = MINOR_MAPPING[python_version] if python_version in MINOR_MAPPING else python_version,
platform_name = platform_name,
actual = "@{repo_prefix}{wheel_name}//:{alias_name}".format(
repo_prefix = repo_prefix,
wheel_name = wheel_name,
alias_name = alias_name,
),
rules_python = rules_python,
))
for [python_version, repo_prefix] in version_map:
alias.append("""\
"@{rules_python}//python/config_settings:is_python_{full_python_version}": "{actual}",""".format(
full_python_version = MINOR_MAPPING[python_version] if python_version in MINOR_MAPPING else python_version,
actual = "@{repo_prefix}{wheel_name}//:{alias_name}".format(
repo_prefix = repo_prefix,
wheel_name = wheel_name,
alias_name = alias_name,
),
rules_python = rules_python,
))
alias.append("""\
"//conditions:default": "{default_actual}",
}}),
Expand Down
15 changes: 0 additions & 15 deletions python/platforms/BUILD.bazel

This file was deleted.

42 changes: 0 additions & 42 deletions python/platforms/platforms.bzl

This file was deleted.

46 changes: 31 additions & 15 deletions python/private/toolchains_repo.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ load(

def _toolchains_repo_impl(rctx):
rules_python_repository_name = rctx.attr._rules_python_workspace.workspace_name
python_version_constraint = "@{rules_python}//python/platforms:is_python_{python_version}".format(
python_version_constraint = "@{rules_python}//python/config_settings:is_python_{python_version}".format(
rules_python = rules_python_repository_name,
python_version = rctx.attr.python_version,
)
Expand All @@ -54,7 +54,8 @@ def _toolchains_repo_impl(rctx):
# for executing build actions.
toolchain(
name = "{platform}_toolchain",
target_compatible_with = {compatible_with} + (["{python_version_constraint}"] if {set_python_version_constraint} else []),
target_compatible_with = {compatible_with},
target_settings = ["{python_version_constraint}"] if {set_python_version_constraint} else [],
toolchain = "@{user_repository_name}_{platform}//:python_runtimes",
toolchain_type = "@bazel_tools//tools/python:toolchain_type",
)
Expand Down Expand Up @@ -103,13 +104,10 @@ alias(name = "py3_runtime", actual = "@{py_repository}_{host_platform}//:py3
alias(name = "python_headers", actual = "@{py_repository}_{host_platform}//:python_headers")
alias(name = "python_runtimes", actual = "@{py_repository}_{host_platform}//:python_runtimes")
alias(name = "python3", actual = "@{py_repository}_{host_platform}//:{python3_binary_path}")
alias(name = "platform", actual = "@{rules_python_repository_name}//:{host_platform}_{python_version}_platform")
""".format(
py_repository = rctx.attr.user_repository_name,
host_platform = host_platform,
python3_binary_path = python3_binary_path,
python_version = rctx.attr.python_version,
rules_python_repository_name = rules_python_repository_name,
)
if not is_windows:
build_contents += """\
Expand All @@ -125,22 +123,40 @@ alias(name = "pip", actual = "@{py_repository}_{host_platform}//:bin
rctx.file("defs.bzl", content = """\
# Generated by python/private/toolchains_repo.bzl
load(
"@{py_repository}_{host_platform}//:defs.bzl",
_compile_pip_requirements = "compile_pip_requirements",
_py_binary = "py_binary",
_py_test = "py_test",
)
load("@{rules_python}//python/config_settings:transition.bzl", _py_binary = "py_binary", _py_test = "py_test")
load("@{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
compile_pip_requirements = _compile_pip_requirements
host_platform = "{host_platform}"
interpreter = "@{py_repository}_{host_platform}//:{python3_binary_path}"
py_binary = _py_binary
py_test = _py_test
def py_binary(name, **kwargs):
return _py_binary(
name = name,
python_version = "{python_version}",
**kwargs
)
def py_test(name, **kwargs):
return _py_test(
name = name,
python_version = "{python_version}",
**kwargs
)
def compile_pip_requirements(name, **kwargs):
return _compile_pip_requirements(
name = name,
py_binary = py_binary,
py_test = py_test,
**kwargs
)
""".format(
py_repository = rctx.attr.user_repository_name,
host_platform = host_platform,
py_repository = rctx.attr.user_repository_name,
python_version = rctx.attr.python_version,
python3_binary_path = python3_binary_path,
rules_python = rules_python_repository_name,
))

toolchain_aliases = repository_rule(
Expand Down
40 changes: 0 additions & 40 deletions python/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -260,43 +260,6 @@ py_runtime_pair(
rctx.file(STANDALONE_INTERPRETER_FILENAME, "# File intentionally left blank. Indicates that this is an interpreter repo created by rules_python.")
rctx.file("BUILD.bazel", build_content)

rules_python_repository_name = rctx.attr._rules_python_workspace.workspace_name

defs_content = """\
# Generated by python/repositories.bzl
load("@{rules_python}//python/platforms:transition.bzl", _py_binary = "py_binary", _py_test = "py_test")
load("@{rules_python}//python:pip.bzl", _compile_pip_requirements = "compile_pip_requirements")
def py_binary(name, **kwargs):
return _py_binary(
name = name,
target_platform = "@{rules_python}//python/platforms:{platform}_{python_version}_platform",
**kwargs
)
def py_test(name, **kwargs):
return _py_test(
name = name,
target_platform = "@{rules_python}//python/platforms:{platform}_{python_version}_platform",
**kwargs
)
def compile_pip_requirements(name, **kwargs):
return _compile_pip_requirements(
name = name,
py_binary = py_binary,
py_test = py_test,
**kwargs
)
""".format(
platform = platform,
python_version = python_version,
rules_python = rules_python_repository_name,
)

rctx.file("defs.bzl", defs_content)

return {
"distutils": rctx.attr.distutils,
"distutils_content": rctx.attr.distutils_content,
Expand Down Expand Up @@ -364,9 +327,6 @@ python_repository = repository_rule(
"zstd_version": attr.string(
default = "1.5.2",
),
"_rules_python_workspace": attr.label(
default = Label("//:WORKSPACE"),
),
},
)

Expand Down

0 comments on commit 1053ab0

Please sign in to comment.