From b11ff912a6b68edcd67770308ba4703e3a740e7b Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 8 Nov 2021 22:27:32 +0100 Subject: [PATCH] fix(setup =_helpers): don't add -g0 CFLAGS sets -g (#3436) On Unix, setuptools prepends $CFLAGS and $CPPFLAGS to the compiler flags (they always come before extra_compile_args and anything else; see distutils.sysconfig.customize_compiler). In practice, the environment variables are useful e.g. to quickly generate a debug build (e.g. by setting CFLAGS=-g), but Pybind11Extension currently unconditionally overwrites this with -g0. Instead, check the environment variables and only insert -g0 if not overridden by them. --- pybind11/setup_helpers.py | 8 +++++++- tests/extra_setuptools/test_setuphelper.py | 14 +++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/pybind11/setup_helpers.py b/pybind11/setup_helpers.py index 4ff1a0cb3..d6d84f672 100644 --- a/pybind11/setup_helpers.py +++ b/pybind11/setup_helpers.py @@ -42,6 +42,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import contextlib import os import platform +import shlex import shutil import sys import sysconfig @@ -143,7 +144,12 @@ class Pybind11Extension(_Extension): if WIN: cflags += ["/EHsc", "/bigobj"] else: - cflags += ["-fvisibility=hidden", "-g0"] + cflags += ["-fvisibility=hidden"] + env_cflags = os.environ.get("CFLAGS", "") + env_cppflags = os.environ.get("CPPFLAGS", "") + c_cpp_flags = shlex.split(env_cflags) + shlex.split(env_cppflags) + if not any(opt.startswith("-g") for opt in c_cpp_flags): + cflags += ["-g0"] if MACOS: cflags += ["-stdlib=libc++"] ldflags += ["-stdlib=libc++"] diff --git a/tests/extra_setuptools/test_setuphelper.py b/tests/extra_setuptools/test_setuphelper.py index c24f50af8..788f368b1 100644 --- a/tests/extra_setuptools/test_setuphelper.py +++ b/tests/extra_setuptools/test_setuphelper.py @@ -8,6 +8,7 @@ import pytest DIR = os.path.abspath(os.path.dirname(__file__)) MAIN_DIR = os.path.dirname(os.path.dirname(DIR)) +WIN = sys.platform.startswith("win32") or sys.platform.startswith("cygwin") @pytest.mark.parametrize("parallel", [False, True]) @@ -71,13 +72,20 @@ def test_simple_setup_py(monkeypatch, tmpdir, parallel, std): encoding="ascii", ) - subprocess.check_call( + out = subprocess.check_output( [sys.executable, "setup.py", "build_ext", "--inplace"], - stdout=sys.stdout, - stderr=sys.stderr, ) + if not WIN: + assert b"-g0" in out + out = subprocess.check_output( + [sys.executable, "setup.py", "build_ext", "--inplace", "--force"], + env=dict(os.environ, CFLAGS="-g"), + ) + if not WIN: + assert b"-g0" not in out # Debug helper printout, normally hidden + print(out) for item in tmpdir.listdir(): print(item.basename)