aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAntony Lee <anntzer.lee@gmail.com>2021-01-20 00:20:30 +0100
committerGitHub <noreply@github.com>2021-01-19 18:20:30 -0500
commite8c4f5431620a6ec859c4a0e80cba68531d58c1c (patch)
tree6caa99ebc1fd93639e5853ffe456aebe6ae96936
parent48534089f7f1b0229bc7ae2e4d0f21dc9ad169b5 (diff)
downloadpybind11-e8c4f5431620a6ec859c4a0e80cba68531d58c1c.tar.gz
fix: prepend Pybind11Extension flags rather than appending them. (#2808)
This allows users to set e.g. `extra_compile_args=["-g"]` (temporarily, for debugging purposes) and not have that get overridden by Pybind11Extension's default `-g0`. In order to minimize "order inversion" (not that it really matters, though) I chose to collect all flags and add them at once (hence the dropping of `*args`). I also dropped flag deduplication, which seems unneeded.
-rw-r--r--pybind11/setup_helpers.py43
1 files changed, 25 insertions, 18 deletions
diff --git a/pybind11/setup_helpers.py b/pybind11/setup_helpers.py
index 83e0bce1..c69064ca 100644
--- a/pybind11/setup_helpers.py
+++ b/pybind11/setup_helpers.py
@@ -99,15 +99,14 @@ class Pybind11Extension(_Extension):
this is an ugly old-style class due to Distutils.
"""
- def _add_cflags(self, *flags):
- for flag in flags:
- if flag not in self.extra_compile_args:
- self.extra_compile_args.append(flag)
+ # flags are prepended, so that they can be further overridden, e.g. by
+ # ``extra_compile_args=["-g"]``.
- def _add_lflags(self, *flags):
- for flag in flags:
- if flag not in self.extra_link_args:
- self.extra_link_args.append(flag)
+ def _add_cflags(self, flags):
+ self.extra_compile_args[:0] = flags
+
+ def _add_ldflags(self, flags):
+ self.extra_link_args[:0] = flags
def __init__(self, *args, **kwargs):
@@ -139,13 +138,17 @@ class Pybind11Extension(_Extension):
# Have to use the accessor manually to support Python 2 distutils
Pybind11Extension.cxx_std.__set__(self, cxx_std)
+ cflags = []
+ ldflags = []
if WIN:
- self._add_cflags("/EHsc", "/bigobj")
+ cflags += ["/EHsc", "/bigobj"]
else:
- self._add_cflags("-fvisibility=hidden", "-g0")
+ cflags += ["-fvisibility=hidden", "-g0"]
if MACOS:
- self._add_cflags("-stdlib=libc++")
- self._add_lflags("-stdlib=libc++")
+ cflags += ["-stdlib=libc++"]
+ ldflags += ["-stdlib=libc++"]
+ self._add_cflags(cflags)
+ self._add_ldflags(ldflags)
@property
def cxx_std(self):
@@ -174,7 +177,8 @@ class Pybind11Extension(_Extension):
if not level:
return
- self.extra_compile_args.append(STD_TMPL.format(level))
+ cflags = [STD_TMPL.format(level)]
+ ldflags = []
if MACOS and "MACOSX_DEPLOYMENT_TARGET" not in os.environ:
# C++17 requires a higher min version of macOS. An earlier version
@@ -186,18 +190,21 @@ class Pybind11Extension(_Extension):
desired_macos = (10, 9) if level < 17 else (10, 14)
macos_string = ".".join(str(x) for x in min(current_macos, desired_macos))
macosx_min = "-mmacosx-version-min=" + macos_string
- self.extra_compile_args.append(macosx_min)
- self.extra_link_args.append(macosx_min)
+ cflags += [macosx_min]
+ ldflags += [macosx_min]
if PY2:
if WIN:
# Will be ignored on MSVC 2015, where C++17 is not supported so
# this flag is not valid.
- self.extra_compile_args.append("/wd5033")
+ cflags += ["/wd5033"]
elif level >= 17:
- self.extra_compile_args.append("-Wno-register")
+ cflags += ["-Wno-register"]
elif level >= 14:
- self.extra_compile_args.append("-Wno-deprecated-register")
+ cflags += ["-Wno-deprecated-register"]
+
+ self._add_cflags(cflags)
+ self._add_ldflags(ldflags)
# Just in case someone clever tries to multithread