diff options
author | Ralf Gommers <ralf.gommers@gmail.com> | 2024-05-27 22:55:39 +0200 |
---|---|---|
committer | Jussi Pakkanen <jpakkane@gmail.com> | 2024-06-23 13:23:31 +0300 |
commit | a111c28ecef721af960721c26b40f4e5108760c7 (patch) | |
tree | c6bbe61d3e0e5e8e4061d58bc5f8ae22d19e208a | |
parent | c0ca35c8fd796bea4bd92f88b03cb3d6abd2096c (diff) | |
download | meson-a111c28ecef721af960721c26b40f4e5108760c7.zip meson-a111c28ecef721af960721c26b40f4e5108760c7.tar.gz meson-a111c28ecef721af960721c26b40f4e5108760c7.tar.bz2 |
Add support for detecting free-threaded Python on Windows
This does a couple of things:
1. Scrape the `Py_GIL_DISABLED` sysconfig var, which is the best way as of today
to determine whether the target interpreter was built with `--disable-gil`
2. link against the correct libpython
3. On Windows, work around a known issue in the python.org installer with a
missing define in `pyconfig.h`, which the CPython devs have said is unlikely
to be fixed since headers are shared between the regular and free-threaded
builds in a single NSIS installer.
-rw-r--r-- | mesonbuild/dependencies/python.py | 14 | ||||
-rwxr-xr-x | mesonbuild/scripts/python_info.py | 4 |
2 files changed, 17 insertions, 1 deletions
diff --git a/mesonbuild/dependencies/python.py b/mesonbuild/dependencies/python.py index 17c807c..851c630 100644 --- a/mesonbuild/dependencies/python.py +++ b/mesonbuild/dependencies/python.py @@ -29,6 +29,7 @@ if T.TYPE_CHECKING: install_paths: T.Dict[str, str] is_pypy: bool is_venv: bool + is_freethreaded: bool link_libpython: bool sysconfig_paths: T.Dict[str, str] paths: T.Dict[str, str] @@ -91,6 +92,7 @@ class BasicPythonExternalProgram(ExternalProgram): 'install_paths': {}, 'is_pypy': False, 'is_venv': False, + 'is_freethreaded': False, 'link_libpython': False, 'sysconfig_paths': {}, 'paths': {}, @@ -146,6 +148,7 @@ class _PythonDependencyBase(_Base): self.variables = python_holder.info['variables'] self.paths = python_holder.info['paths'] self.is_pypy = python_holder.info['is_pypy'] + self.is_freethreaded = python_holder.info['is_freethreaded'] # The "-embed" version of python.pc / python-config was introduced in 3.8, # and distutils extension linking was changed to be considered a non embed # usage. Before then, this dependency always uses the embed=True handling @@ -161,6 +164,12 @@ class _PythonDependencyBase(_Base): else: self.major_version = 2 + # pyconfig.h is shared between regular and free-threaded builds in the + # Windows installer from python.org, and hence does not define + # Py_GIL_DISABLED correctly. So do it here: + if mesonlib.is_windows() and self.is_freethreaded: + self.compile_args += ['-DPy_GIL_DISABLED'] + def find_libpy(self, environment: 'Environment') -> None: if self.is_pypy: if self.major_version == 3: @@ -220,7 +229,10 @@ class _PythonDependencyBase(_Base): else: if limited_api: vernum = vernum[0] - libpath = Path('libs') / f'python{vernum}.lib' + if self.is_freethreaded: + libpath = Path('libs') / f'python{vernum}t.lib' + else: + libpath = Path('libs') / f'python{vernum}.lib' # For a debug build, pyconfig.h may force linking with # pythonX_d.lib (see meson#10776). This cannot be avoided # and won't work unless we also have a debug build of diff --git a/mesonbuild/scripts/python_info.py b/mesonbuild/scripts/python_info.py index 5b048ca..6aab380 100755 --- a/mesonbuild/scripts/python_info.py +++ b/mesonbuild/scripts/python_info.py @@ -106,6 +106,9 @@ if sys.version_info >= (3, 2): if is_pypy: limited_api_suffix = suffix +# Whether we're targeting a free-threaded CPython interpreter +is_freethreaded = bool(variables.get('Py_GIL_DISABLED', False)) + print(json.dumps({ 'variables': variables, 'paths': paths, @@ -118,4 +121,5 @@ print(json.dumps({ 'link_libpython': links_against_libpython(), 'suffix': suffix, 'limited_api_suffix': limited_api_suffix, + 'is_freethreaded': is_freethreaded, })) |