@@ -403,6 +403,10 @@ def _python_impl(module_ctx):
403403 # the PLATFORMS global for this toolchain
404404 toolchain_platform_keys = {}
405405
406+ # Extra target_settings to add to every registered toolchain, e.g. for
407+ # gating the default toolchains behind a custom config_setting.
408+ global_add_target_settings = py .config .add_target_settings
409+
406410 # Split the toolchain info into separate objects so they can be passed onto
407411 # the repository rule.
408412 for entry in toolchain_impls :
@@ -414,7 +418,7 @@ def _python_impl(module_ctx):
414418
415419 # The target_settings attribute may not be present for users
416420 # patching python/versions.bzl.
417- toolchain_ts_map [key ] = getattr (entry .platform , "target_settings" , [])
421+ toolchain_ts_map [key ] = getattr (entry .platform , "target_settings" , []) + global_add_target_settings
418422 toolchain_platform_keys [key ] = entry .platform_name
419423 toolchain_python_versions [key ] = entry .full_python_version
420424
@@ -702,6 +706,9 @@ def _process_global_overrides(*, tag, default, _fail = fail):
702706
703707 default ["minor_mapping" ] = tag .minor_mapping
704708
709+ if tag .add_target_settings :
710+ default ["add_target_settings" ] = list (tag .add_target_settings )
711+
705712 forwarded_attrs = sorted (AUTH_ATTRS ) + [
706713 "base_url" ,
707714 "register_all_versions" ,
@@ -809,6 +816,7 @@ def _get_toolchain_config(*, modules, _fail = fail):
809816 )
810817
811818 register_all_versions = default .pop ("register_all_versions" , False )
819+ add_target_settings = default .pop ("add_target_settings" , [])
812820 kwargs = default .pop ("kwargs" , {})
813821
814822 versions = {}
@@ -834,6 +842,7 @@ def _get_toolchain_config(*, modules, _fail = fail):
834842 minor_mapping = minor_mapping ,
835843 default = default ,
836844 register_all_versions = register_all_versions ,
845+ add_target_settings = add_target_settings ,
837846 )
838847
839848def _compute_default_python_version (mctx ):
@@ -1099,6 +1108,30 @@ _override = tag_class(
10991108:::
11001109""" ,
11011110 attrs = {
1111+ "add_target_settings" : attr .string_list (
1112+ mandatory = False ,
1113+ doc = """\
1114+ A list of `config_setting` labels to add to the `target_settings` of every
1115+ toolchain registered by this module extension. This is useful for creating
1116+ separate "families" of toolchains gated behind custom build settings.
1117+
1118+ For example, to ensure the default prebuilt toolchains are only resolved when
1119+ a `prebuilt` config setting is active:
1120+
1121+ ```starlark
1122+ python.override(
1123+ add_target_settings = ["@@//:python_toolchain_family_prebuilt"],
1124+ )
1125+ ```
1126+
1127+ These settings are appended to the `target_settings` of all toolchains
1128+ registered by the extension, including any that already have settings
1129+ from `python.single_version_platform_override`.
1130+
1131+ :::{versionadded} VERSION_NEXT_FEATURE
1132+ :::
1133+ """ ,
1134+ ),
11021135 "available_python_versions" : attr .string_list (
11031136 mandatory = False ,
11041137 doc = """\
0 commit comments