diff --git a/CHANGELOG.md b/CHANGELOG.md index dc3b079d9d..1a4f3e7324 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -94,6 +94,8 @@ Breaking changes: * (utils) Added a `pip_utils` struct with a `normalize_name` function to allow users to find out how `rules_python` would normalize a PyPI distribution name. +* (config_settings) The `python_version` flag now accepts minor Python versions. + ## [0.26.0] - 2023-10-06 ### Changed diff --git a/python/config_settings/BUILD.bazel b/python/config_settings/BUILD.bazel index ab4ee8d880..04e820bb3f 100644 --- a/python/config_settings/BUILD.bazel +++ b/python/config_settings/BUILD.bazel @@ -1,4 +1,4 @@ -load("//python:versions.bzl", "TOOL_VERSIONS") +load("//python:versions.bzl", "MINOR_MAPPING", "TOOL_VERSIONS") load(":config_settings.bzl", "construct_config_settings") filegroup( @@ -10,4 +10,7 @@ filegroup( visibility = ["//python:__pkg__"], ) -construct_config_settings(python_versions = TOOL_VERSIONS.keys()) +construct_config_settings( + minor_mapping = MINOR_MAPPING, + python_versions = TOOL_VERSIONS.keys(), +) diff --git a/python/config_settings/config_settings.bzl b/python/config_settings/config_settings.bzl index 21e477e644..363205f048 100644 --- a/python/config_settings/config_settings.bzl +++ b/python/config_settings/config_settings.bzl @@ -15,26 +15,52 @@ """This module is used to construct the config settings in the BUILD file in this same package. """ +load("@bazel_skylib//lib:selects.bzl", "selects") load("@bazel_skylib//rules:common_settings.bzl", "string_flag") # buildifier: disable=unnamed-macro -def construct_config_settings(python_versions): +def construct_config_settings(minor_mapping, python_versions): """Constructs a set of configs for all Python versions. Args: - python_versions: The Python versions supported by rules_python. + minor_mapping: mapping from minor (x.y) versions to the corresponding full (x.y.z) versions. + python_versions: list of full (x.y.z) Python versions supported by rules_python. """ + + minor_versions = list(minor_mapping.keys()) + allowed_flag_values = python_versions + minor_versions + string_flag( name = "python_version", build_setting_default = python_versions[0], - values = python_versions, + values = allowed_flag_values, visibility = ["//visibility:public"], ) - for python_version in python_versions: - python_version_constraint_setting = "is_python_" + python_version + for flag_value in allowed_flag_values: + flag_value_constraint_setting = "python_version_flag_equals_" + flag_value native.config_setting( + name = flag_value_constraint_setting, + flag_values = {":python_version": flag_value}, + visibility = ["//visibility:public"], + ) + + flag_values_that_enable_version = { + full_version: [full_version] + for full_version in python_versions + } + + for minor_version, full_version in minor_mapping.items(): + flag_values_that_enable_version[full_version].append(minor_version) + + for full_version, flag_values in flag_values_that_enable_version.items(): + python_version_constraint_setting = "is_python_" + full_version + flag_value_constraints = [ + ":python_version_flag_equals_" + flag_value + for flag_value in flag_values + ] + selects.config_setting_group( name = python_version_constraint_setting, - flag_values = {":python_version": python_version}, + match_any = flag_value_constraints, visibility = ["//visibility:public"], )