Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove C++ flags when compiling C files in qhull extension
  • Loading branch information
ianthomas23 committed Sep 4, 2022
commit 2f8cfec1e0ed01e46c16bea057ae9b4e8eed1dcd
20 changes: 20 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ def has_flag(self, flagname):
return True


# Wrapper for distutils.ccompiler.CCompiler._compile to remove C++-specific
# flags when compiling C files.
def compile_wrapper(compiler, obj, src, ext, cc_args, extra_postargs, pp_opts):
if src.lower().endswith(".c"):
extra_postargs = list(filter(lambda x: x[1:4] != "std",
extra_postargs))
compiler._compile_old(obj, src, ext, cc_args, extra_postargs, pp_opts)


class BuildExtraLibraries(setuptools.command.build_ext.build_ext):
def finalize_options(self):
self.distribution.ext_modules[:] = [
Expand Down Expand Up @@ -184,9 +193,20 @@ def build_extension(self, ext):
orig_build_temp = self.build_temp
self.build_temp = os.path.join(self.build_temp, ext.name)
try:
if ext.name == "matplotlib._qhull":
# For qhull extension some C++ flags must be removed before
# compiling C files.
from distutils.ccompiler import CCompiler
self.compiler._compile_old = self.compiler._compile
self.compiler._compile = compile_wrapper.__get__(
self.compiler, CCompiler)
super().build_extension(ext)
finally:
self.build_temp = orig_build_temp
if ext.name == "matplotlib._qhull" and hasattr(
self.compiler, "_compile_old"):
self.compiler._compile = self.compiler._compile_old
delattr(self.compiler, "_compile_old")


def update_matplotlibrc(path):
Expand Down
2 changes: 1 addition & 1 deletion setupext.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ def get_extensions(self):
# qhull
ext = Pybind11Extension(
"matplotlib._qhull", ["src/_qhull_wrapper.cpp"],
# Do not set cxx_std as C compilers do not recognise it.
cxx_std=11,

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can specify the C++ standard to follow, so here we are explicitly restricting to C++11. This would probably be a configuration param at the top of the file to make it clear what C++ standard the project is currently working to.

define_macros=[("MPL_DEVNULL", os.devnull)])
Qhull.add_flags(ext)
yield ext
Expand Down