Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
799f9c3
wip: flag to evaluate dep specs
rickeylev Apr 27, 2025
a658e8f
add some todo, fake some more values
rickeylev Apr 27, 2025
e7ea75b
Merge branch 'main' into pep508.flag
rickeylev Apr 27, 2025
e65c973
source values from select and flags
rickeylev Apr 27, 2025
6181509
sorta fix platform_python_implementation
rickeylev Apr 27, 2025
bf2173c
wip: analysis tests basic
rickeylev Apr 28, 2025
1e23941
trying to get tests to work
rickeylev Apr 29, 2025
ef61724
got basic test passing
rickeylev Apr 29, 2025
d1b9c0d
Merge branch 'main' of https://github.com/bazel-contrib/rules_python …
rickeylev Apr 29, 2025
77decbc
parameterize test
rickeylev Apr 29, 2025
bda07f7
expand testing a bit
rickeylev Apr 29, 2025
5d37d7d
a few notes and links and continue with impl
aignas Apr 29, 2025
38e6f5b
rename to env_marker_setting
rickeylev Apr 29, 2025
be7e007
Merge remote-tracking branch 'origin/pep508.flag' into pep508.flag
rickeylev Apr 29, 2025
3f794c2
move select mappings to constants
rickeylev Apr 29, 2025
7dae5d0
remove extras_flag; various code/comment cleanup
rickeylev Apr 30, 2025
2f1b528
minor cleanup, doc
rickeylev Apr 30, 2025
7ff220e
fixup: forgot to pass args
rickeylev Apr 30, 2025
b340de7
code share with pep508_env.bzl, add config_setting,
rickeylev Apr 30, 2025
104c27b
Merge branch 'main' of https://github.com/bazel-contrib/rules_python …
rickeylev Apr 30, 2025
b2c73c0
more cleanup
rickeylev May 1, 2025
ba05d71
more cleanup
rickeylev May 1, 2025
5316f2a
remove platform_version and platform_release flags for now
rickeylev May 1, 2025
2b30b54
wire platform_release back to osx flag
rickeylev May 1, 2025
a6bc605
cleanup comment
rickeylev May 2, 2025
764879d
Merge branch 'main' of https://github.com/bazel-contrib/rules_python …
rickeylev May 2, 2025
72a8f1e
update dleted packages
rickeylev May 2, 2025
8a210d2
restore bazelrc
rickeylev May 2, 2025
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
18 changes: 18 additions & 0 deletions python/config_settings/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,21 @@ string_flag(
define_pypi_internal_flags(
name = "define_pypi_internal_flags",
)

# todo: have single --pypi_env_config flag instead? Under the hood, it
# could be driven more more discrete flags, which could be considered an
# implementation detail of our particular impl.
# A label is used to allow users to use select() to affect the value.
# A target providing BuildSettingInfo is used to allow users to define
# a flag as the target, which lets them CLI-override or transition it easily.
label_flag(
name = "pip_platform_release_config",
build_setting_default = ":_pip_platform_release_default_config",
)

# docs: points to a label that provides BuildSettingInfo for the value.
# A label and BuildSettingInfo are used for the same reason as platform_release
label_flag(
name = "pip_platform_version_config",
build_setting_default = ":_pip_platform_version_default_config",
)
141 changes: 141 additions & 0 deletions python/private/pypi/dependency_specifier_flag.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
load("//python/private:toolchain_types.bzl", "TARGET_TOOLCHAIN_TYPE")
load(":pep508_evaluate.bzl", "evaluate")

# todo: maybe put all the env into a single target and have a
# PyPiEnvMarkersInfo provider? Have --pypi_env=//some:target?
def _impl(ctx):
# todo: should unify with pep508_env.bzl
env = {}

runtime = ctx.toolchains[TARGET_TOOLCHAIN_TYPE].py3_runtime
if runtime.interpreter_version_info:
version_info = runtime.interpreter_version_info
env["python_version"] = "{v.major}.{v.minor}".format(v = version_info)
full_version = format_full_version(version_info)
env["python_full_version"] = full_version
env["implementation_version"] = full_version
else:
env["python_version"] = get_flag(ctx.attr._python_version)
full_version = get_flag(ctx.attr._python_full_version)
env["python_full_version"] = full_version
env["implementation_version"] = full_version

# We assume cpython if the toolchain doesn't specify because it's most
# likely to be true.
env["implementation_name"] = runtime.implementation_name or "cpython"
env["os_name"] = ctx.attr.os_name
env["sys_platform"] = ctx.attr.sys_platform
env["platform_machine"] = ctx.attr.platform_machine

# todo: maybe add PyRuntimeInfo.platform_python_implementation?
# The values are slightly different to implementation_name.
# However, digging through old PEPs, it looks like
# platform.python_implementation is legacy, and sys.implementation.name
# "replaced" it. Can probably just special case this.
platform_python_impl = runtime.implementation_name
if platform_python_impl == "cpython":
platform_python_impl = "CPython"
env["platform_python_implementation"] = platform_python_impl
env["platform_release"] = ctx.attr._platform_release_config_flag[BuildSettingInfo].value
env["platform_system"] = ctx.attr.platform_system
env["platform_version"] = ctx.attr._platform_version_config_flag[BuildSettingInfo].value

if evalute(ctx.attr.expression, env):
value = "yes"
else:
value = "no"
return [config_common.FeatureFlagInfo(value = value)]

# Adapted from spec code at:
# https://packaging.python.org/en/latest/specifications/dependency-specifiers/#environment-markers
def format_full_info(info):
kind = info.releaselevel
if kind == "final":
kind = ""
serial = ""
else:
kind = kind[0] if kind else ""
serial = str(info.serial) if info.serial else ""

return "{v.major}.{v.minor}.{v.micro}{kind}{serial}".format(
v = version_info,
kind = kind,
serial = serial,
)
return version

def pypa_dep_spec(**kwargs):
pypa_dependency_specification(
# todo: copied from pep508_env.bzl
os_name = select({
# The "java" value is documented, but with Jython defunct,
# shouldn't occur in practice.
# The osname value is technically a property of the runtime, not the
# targetted OS at runtime, but the distinction shouldn't matter in
# practice.
"@//platforms/os:windows": "nt",
"//conditions:default": "posix",
}),
# todo: copied from pep508_env.bzl
sys_platform = select({
"@//platforms/os:windows": "win32",
"@//platforms/os:linux": "linux",
"@//platforms/os:osx": "darwin",
# todo: what does spec say unknown value is?
"//conditions:default": "",
}),
# todo: copied from pep508_env.bzl
# todo: pep508_env and evaluate have an "aliases" thing that needs
# to be incorporated
# todo: there are many more cpus. Unfortunately, it doesn't look like
# the value is directly accessible to starlark. It might be possible to
# get it via CcToolchain.cpu though.
platform_machine = select({
"@platforms//cpu:x86_64": "x86_64",
"@platforms//cpu:aarch64": "aarch64",
# todo: what does spec say unknown value is?
"//conditions:default": "",
}),
# todo: copied from pep508_env.bzl
platform_system = select({
"@//platforms/os:windows": "Windows",
"@//platforms/os:linux": "Linux",
"@//platforms/os:osx": "Darwin",
# todo: what does spec say unknown value is?
"//conditions:default": "",
}),
)

pypa_dependency_specification = rule(
implementation = _impl,
attrs = {
"expression": attt.string(),
"os_name": attr.string(),
"sys_platform": attr.string(),
"platform_machine": attr.string(),
"platform_system": attr.string(),
"_platform_release_config_flag": attr.label(
default = "//python/config_settings:pip_platform_release_config",
),
"_platform_version_config_flag": attr.label(
default = "//python/config_settings:pip_platform_version_config",
),
"_python_version_flag": attr.label(
default = "//python/config_settings:_python_version_major_minor",
),
"_python_full_version_flag": attr.label(
default = "//python/config_settings:python_version",
),
"_extra_flag": attr.label(),
},
toolchains = [
TARGET_TOOLCHAIN_TYPE,
],
)

def _get_flag(t):
if config_common.FeatureFlagInfo in t:
return t[config_common.FeatureFlagInfo].value
if BuildSettingInfo in t:
return t[BuildSettingInfo].value
fail("Should not occur: {} does not have necessary providers")
46 changes: 46 additions & 0 deletions python/private/pypi/flags.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ def define_pypi_internal_flags(name):
name = "_internal_pip_whl",
visibility = ["//visibility:public"],
)
_platform_release_config(
name = "_pip_platform_release_default_config",
value = select({
"//conditions:osx": "USE_OSX_VERSION_FLAG",
"//conditions:default": "",
}),
)
_platform_version_config(
name = "_pip_platform_version_default_config",
value = select({
"//conditions:osx": "USE_OSX_VERSION_FLAG",
"//conditions:default": "",
}),
)

def _allow_wheels_flag_impl(ctx):
input = ctx.attr._setting[BuildSettingInfo].value
Expand All @@ -97,3 +111,35 @@ This rule allows us to greatly reduce the number of config setting targets at no
if we are duplicating some of the functionality of the `native.config_setting`.
""",
)

def _platform_release_config_impl(ctx):
value = ctx.attr.value
if value == "USE_OSX_VERSION_FLAG":
value = ctx.attr._osx_version[BuildSettingInfo].value

return [BuildSettingInfo(value = ctx.attr.value)]

# This value is loosely some version-value looking thing, but the format
# varies depending on the OS.
_platform_release_config = rule(
implementation = _platform_release_config_impl,
attrs = {
"value": attr.string(),
"_osx_version": attr.label(
default = "//python/config_settings:pip_whl_osx_version",
),
},
)

def _platform_version_config_impl(ctx):
value = ctx.attr.value
return [BuildSettingInfo(value = ctx.attr.value)]

# Despite its name, this "version" value is not a simple version value.
# It's a more detailed, arbitrary, description the OS gives about itself.
_platform_version_config = rule(
implementation = _platform_version_config_impl,
attrs = {
"value": attr.string(),
},
)
5 changes: 5 additions & 0 deletions tests/pypi/pep508/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
load(":deps_tests.bzl", "deps_test_suite")
load(":depspec_flag_tests.bzl", "depspec_flag_test_suite")
load(":evaluate_tests.bzl", "evaluate_test_suite")
load(":requirement_tests.bzl", "requirement_test_suite")

Expand All @@ -13,3 +14,7 @@ evaluate_test_suite(
requirement_test_suite(
name = "requirement_tests",
)

depspec_flag_test_suite(
name = "depspec_flag_tests",
)
28 changes: 28 additions & 0 deletions tests/pypi/pep508/depspec_flag_tests.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
load("@rules_testing//lib:test_suite.bzl", "test_suite")
load("//python/private/pypi:pep508_env.bzl", pep508_env = "env") # buildifier: disable=bzl-visibility
load("//python/private/pypi:pep508_evaluate.bzl", "evaluate", "tokenize") # buildifier: disable=bzl-visibility

_tests = []

def test_whatever(name):
def impl(env, target):
# todo: create FeatureFlagInfo subject
actual = target[config_common.FeatureFlagInfo].value
env.expect.that_string(actual).equals("yes")

depspec_flag(
name = name + "_subject",
)
analysis_test(
name = name,
impl = impl,
target = name + "_subject",
config_settings = {
},
)

def depspec_flag_tests(name):
test_suite(
name = name,
tests = _tests,
)