Skip to content

Commit

Permalink
fix: python-interpreter detection with rules_python >= 0.36.0
Browse files Browse the repository at this point in the history
  • Loading branch information
betaboon committed Jan 17, 2025
1 parent 4f86df5 commit ca9d5c3
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions pycross/private/interpreter_version.bzl
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
"""Provides a config flag that returns the micro-level version of the selected rules_python toolchain."""

load("@rules_python//python:versions.bzl", "TOOL_VERSIONS")
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")

def _rules_python_interpreter_version_impl(ctx):
return [
config_common.FeatureFlagInfo(value = ctx.attr.version),
]
value = _flag_value(ctx.attr._python_version_flag)

if not value:
value = ctx.attr.default_version

return [config_common.FeatureFlagInfo(value = value)]

_rules_python_interpreter_version = rule(
implementation = _rules_python_interpreter_version_impl,
attrs = {
"version": attr.string(mandatory = True),
"default_version": attr.string(mandatory = True),
"_python_version_flag": attr.label(
default = "@rules_python//python/config_settings:python_version",
),
},
)

def _flag_value(s):
if config_common.FeatureFlagInfo in s:
return s[config_common.FeatureFlagInfo].value
else:
return s[BuildSettingInfo].value

def rules_python_interpreter_version(name, default_version, **kwargs):
"""Builds a target that returns the currently-selected rules_pycross toolchain version.
Expand All @@ -26,14 +38,8 @@ def rules_python_interpreter_version(name, default_version, **kwargs):
)
"""

selects = {
"@rules_python//python/config_settings:is_python_%s" % version: version
for version in sorted(TOOL_VERSIONS)
}
selects["//conditions:default"] = default_version

_rules_python_interpreter_version(
name = name,
version = select(selects),
default_version = default_version,
**kwargs
)

0 comments on commit ca9d5c3

Please sign in to comment.