Skip to content

Commit 2f8cfec

Browse files
committed
Remove C++ flags when compiling C files in qhull extension
1 parent f8226d8 commit 2f8cfec

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

setup.py

+20
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ def has_flag(self, flagname):
6666
return True
6767

6868

69+
# Wrapper for distutils.ccompiler.CCompiler._compile to remove C++-specific
70+
# flags when compiling C files.
71+
def compile_wrapper(compiler, obj, src, ext, cc_args, extra_postargs, pp_opts):
72+
if src.lower().endswith(".c"):
73+
extra_postargs = list(filter(lambda x: x[1:4] != "std",
74+
extra_postargs))
75+
compiler._compile_old(obj, src, ext, cc_args, extra_postargs, pp_opts)
76+
77+
6978
class BuildExtraLibraries(setuptools.command.build_ext.build_ext):
7079
def finalize_options(self):
7180
self.distribution.ext_modules[:] = [
@@ -184,9 +193,20 @@ def build_extension(self, ext):
184193
orig_build_temp = self.build_temp
185194
self.build_temp = os.path.join(self.build_temp, ext.name)
186195
try:
196+
if ext.name == "matplotlib._qhull":
197+
# For qhull extension some C++ flags must be removed before
198+
# compiling C files.
199+
from distutils.ccompiler import CCompiler
200+
self.compiler._compile_old = self.compiler._compile
201+
self.compiler._compile = compile_wrapper.__get__(
202+
self.compiler, CCompiler)
187203
super().build_extension(ext)
188204
finally:
189205
self.build_temp = orig_build_temp
206+
if ext.name == "matplotlib._qhull" and hasattr(
207+
self.compiler, "_compile_old"):
208+
self.compiler._compile = self.compiler._compile_old
209+
delattr(self.compiler, "_compile_old")
190210

191211

192212
def update_matplotlibrc(path):

setupext.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ def get_extensions(self):
435435
# qhull
436436
ext = Pybind11Extension(
437437
"matplotlib._qhull", ["src/_qhull_wrapper.cpp"],
438-
# Do not set cxx_std as C compilers do not recognise it.
438+
cxx_std=11,
439439
define_macros=[("MPL_DEVNULL", os.devnull)])
440440
Qhull.add_flags(ext)
441441
yield ext

0 commit comments

Comments
 (0)