@@ -21,6 +21,8 @@ load(":glob_excludes.bzl", "glob_excludes")
2121load (":py_exec_tools_toolchain.bzl" , "py_exec_tools_toolchain" )
2222load (":semver.bzl" , "semver" )
2323
24+ _IS_FREETHREADED = Label ("//python/config_settings:is_py_freethreaded" )
25+
2426def define_hermetic_runtime_toolchain_impl (
2527 * ,
2628 name ,
@@ -45,7 +47,7 @@ def define_hermetic_runtime_toolchain_impl(
4547 python_version: {type}`str` The Python version, in `major.minor.micro`
4648 format.
4749 python_bin: {type}`str` The path to the Python binary within the
48- repositoroy .
50+ repository .
4951 coverage_tool: {type}`str` optional target to the coverage tool to
5052 use.
5153 """
@@ -67,19 +69,23 @@ def define_hermetic_runtime_toolchain_impl(
6769 exclude = [
6870 # Unused shared libraries. `python` executable and the `:libpython` target
6971 # depend on `libpython{python_version}.so.1.0`.
70- "lib/libpython{major}.{minor}.so" .format (** version_dict ),
72+ "lib/libpython{major}.{minor}* .so" .format (** version_dict ),
7173 # static libraries
7274 "lib/**/*.a" ,
7375 # tests for the standard libraries.
74- "lib/python{major}.{minor}/**/test/**" .format (** version_dict ),
75- "lib/python{major}.{minor}/**/tests/**" .format (** version_dict ),
76- "**/__pycache__/*.pyc.*" , # During pyc creation, temp files named *.pyc.NNN are created
76+ "lib/python{major}.{minor}*/**/test/**" .format (** version_dict ),
77+ "lib/python{major}.{minor}*/**/tests/**" .format (** version_dict ),
78+ # During pyc creation, temp files named *.pyc.NNN are created
79+ "**/__pycache__/*.pyc.*" ,
7780 ] + glob_excludes .version_dependent_exclusions () + extra_files_glob_exclude ,
7881 ),
7982 )
8083 cc_import (
8184 name = "interface" ,
82- interface_library = "libs/python{major}{minor}.lib" .format (** version_dict ),
85+ interface_library = select ({
86+ _IS_FREETHREADED : "libs/python{major}{minor}t.lib" .format (** version_dict ),
87+ "//conditions:default" : "libs/python{major}{minor}.lib" .format (** version_dict ),
88+ }),
8389 system_provided = True ,
8490 )
8591
@@ -96,14 +102,62 @@ def define_hermetic_runtime_toolchain_impl(
96102 hdrs = [":includes" ],
97103 includes = [
98104 "include" ,
99- "include/python{major}.{minor}" .format (** version_dict ),
100- "include/python{major}.{minor}m" .format (** version_dict ),
105+ ] + select ({
106+ _IS_FREETHREADED : [
107+ "include/python{major}.{minor}t" .format (** version_dict ),
108+ ],
109+ "//conditions:default" : [
110+ "include/python{major}.{minor}" .format (** version_dict ),
111+ "include/python{major}.{minor}m" .format (** version_dict ),
112+ ],
113+ }),
114+ )
115+ native .config_setting (
116+ name = "is_freethreaded_linux" ,
117+ flag_values = {
118+ Label ("//python/config_settings:py_freethreaded" ): "yes" ,
119+ },
120+ constraint_values = [
121+ "@platforms//os:linux" ,
101122 ],
123+ visibility = ["//visibility:private" ],
102124 )
125+ native .config_setting (
126+ name = "is_freethreaded_osx" ,
127+ flag_values = {
128+ Label ("//python/config_settings:py_freethreaded" ): "yes" ,
129+ },
130+ constraint_values = [
131+ "@platforms//os:osx" ,
132+ ],
133+ visibility = ["//visibility:private" ],
134+ )
135+ native .config_setting (
136+ name = "is_freethreaded_windows" ,
137+ flag_values = {
138+ Label ("//python/config_settings:py_freethreaded" ): "yes" ,
139+ },
140+ constraint_values = [
141+ "@platforms//os:windows" ,
142+ ],
143+ visibility = ["//visibility:private" ],
144+ )
145+
103146 cc_library (
104147 name = "libpython" ,
105148 hdrs = [":includes" ],
106149 srcs = select ({
150+ ":is_freethreaded_linux" : [
151+ "lib/libpython{major}.{minor}t.so" .format (** version_dict ),
152+ "lib/libpython{major}.{minor}t.so.1.0" .format (** version_dict ),
153+ ],
154+ ":is_freethreaded_osx" : [
155+ "lib/libpython{major}.{minor}t.dylib" .format (** version_dict ),
156+ ],
157+ ":is_freethreaded_windows" : [
158+ "python3.dll" ,
159+ "libs/python{major}{minor}t.lib" .format (** version_dict ),
160+ ],
107161 "@platforms//os:linux" : [
108162 "lib/libpython{major}.{minor}.so" .format (** version_dict ),
109163 "lib/libpython{major}.{minor}.so.1.0" .format (** version_dict ),
@@ -132,12 +186,18 @@ def define_hermetic_runtime_toolchain_impl(
132186 "micro" : str (version_info .patch ),
133187 "minor" : str (version_info .minor ),
134188 },
135- # Convert empty string to None
136- coverage_tool = coverage_tool or None ,
189+ coverage_tool = select ({
190+ # Convert empty string to None
191+ ":coverage_enabled" : coverage_tool or None ,
192+ "//conditions:default" : None ,
193+ }),
137194 python_version = "PY3" ,
138195 implementation_name = "cpython" ,
139196 # See https://peps.python.org/pep-3147/ for pyc tag infix format
140- pyc_tag = "cpython-{major}{minor}" .format (** version_dict ),
197+ pyc_tag = select ({
198+ _IS_FREETHREADED : "cpython-{major}{minor}t" .format (** version_dict ),
199+ "//conditions:default" : "cpython-{major}{minor}" .format (** version_dict ),
200+ }),
141201 )
142202
143203 py_runtime_pair (
0 commit comments