From e4f6eeb7d77db7938588e5dd758c73291b40e4ee Mon Sep 17 00:00:00 2001 From: Ralf Gommers Date: Fri, 21 Apr 2023 23:39:27 +0100 Subject: Python module: emit warning for debug buildtypes with MSVC and no debug Python CPython adds a hard to avoid hardcoded link flag to look for a debug libpython. This results in a very puzzling error, so emit a warning with a clear message. Note that pybind11 has a workaround for this, which undefines `_DEBUG`. So users who use only pybind11 can use non-release buildtypes, but they won't get debug symbols on Windows unless they have a debug build. --- mesonbuild/dependencies/python.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'mesonbuild') diff --git a/mesonbuild/dependencies/python.py b/mesonbuild/dependencies/python.py index 65fef7f..4546e88 100644 --- a/mesonbuild/dependencies/python.py +++ b/mesonbuild/dependencies/python.py @@ -13,7 +13,7 @@ # limitations under the License. from __future__ import annotations -import functools, json, os +import functools, json, os, textwrap from pathlib import Path import typing as T @@ -275,6 +275,29 @@ class PythonSystemDependency(SystemDependency, _PythonDependencyBase): libpath = Path(f'python{vernum}.dll') 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 + # Python itself (except with pybind11, which has an ugly + # hack to work around this) - so emit a warning to explain + # the cause of the expected link error. + buildtype = self.env.coredata.get_option(mesonlib.OptionKey('buildtype')) + assert isinstance(buildtype, str) + debug = self.env.coredata.get_option(mesonlib.OptionKey('debug')) + # `debugoptimized` buildtype may not set debug=True currently, see gh-11645 + is_debug_build = debug or buildtype == 'debug' + vscrt_debug = False + if mesonlib.OptionKey('b_vscrt') in self.env.coredata.options: + vscrt = self.env.coredata.options[mesonlib.OptionKey('b_vscrt')].value + if vscrt in {'mdd', 'mtd', 'from_buildtype', 'static_from_buildtype'}: + vscrt_debug = True + if is_debug_build and vscrt_debug and not self.variables.get('Py_DEBUG'): + mlog.warning(textwrap.dedent('''\ + Using a debug build type with MSVC or an MSVC-compatible compiler + when the Python interpreter is not also a debug build will almost + certainly result in a failed build. Prefer using a release build + type or a debug Python interpreter. + ''')) # base_prefix to allow for virtualenvs. lib = Path(self.variables.get('base_prefix')) / libpath elif self.platform == 'mingw': -- cgit v1.1