Skip to content

Commit b11ff91

Browse files
authored
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.
1 parent b322018 commit b11ff91

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

pybind11/setup_helpers.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import contextlib
4343
import os
4444
import platform
45+
import shlex
4546
import shutil
4647
import sys
4748
import sysconfig
@@ -143,7 +144,12 @@ def __init__(self, *args, **kwargs):
143144
if WIN:
144145
cflags += ["/EHsc", "/bigobj"]
145146
else:
146-
cflags += ["-fvisibility=hidden", "-g0"]
147+
cflags += ["-fvisibility=hidden"]
148+
env_cflags = os.environ.get("CFLAGS", "")
149+
env_cppflags = os.environ.get("CPPFLAGS", "")
150+
c_cpp_flags = shlex.split(env_cflags) + shlex.split(env_cppflags)
151+
if not any(opt.startswith("-g") for opt in c_cpp_flags):
152+
cflags += ["-g0"]
147153
if MACOS:
148154
cflags += ["-stdlib=libc++"]
149155
ldflags += ["-stdlib=libc++"]

tests/extra_setuptools/test_setuphelper.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
DIR = os.path.abspath(os.path.dirname(__file__))
1010
MAIN_DIR = os.path.dirname(os.path.dirname(DIR))
11+
WIN = sys.platform.startswith("win32") or sys.platform.startswith("cygwin")
1112

1213

1314
@pytest.mark.parametrize("parallel", [False, True])
@@ -71,13 +72,20 @@ def test_simple_setup_py(monkeypatch, tmpdir, parallel, std):
7172
encoding="ascii",
7273
)
7374

74-
subprocess.check_call(
75+
out = subprocess.check_output(
7576
[sys.executable, "setup.py", "build_ext", "--inplace"],
76-
stdout=sys.stdout,
77-
stderr=sys.stderr,
7877
)
78+
if not WIN:
79+
assert b"-g0" in out
80+
out = subprocess.check_output(
81+
[sys.executable, "setup.py", "build_ext", "--inplace", "--force"],
82+
env=dict(os.environ, CFLAGS="-g"),
83+
)
84+
if not WIN:
85+
assert b"-g0" not in out
7986

8087
# Debug helper printout, normally hidden
88+
print(out)
8189
for item in tmpdir.listdir():
8290
print(item.basename)
8391

0 commit comments

Comments
 (0)