Skip to content

Commit 1ffcd4d

Browse files
build: in setup.py set -std=c++17 only for the C++ compiler, not for C
This is an attempt to fix the MacOS build, where clang is used. Clang is more strict about not allowing -std=c++17 for C code.
1 parent bbb54f0 commit 1ffcd4d

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

setup.py

+23-8
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,35 @@ def _pack_std_jsonnet(self):
6363
f.write(",".join(str(x) for x in stdlib))
6464
f.write(",0\n\n")
6565

66-
def build_extension(self, ext):
66+
def build_extensions(self):
6767
# At this point, the compiler has been chosen so we add compiler-specific flags.
6868
# There is unfortunately no built in support for this in setuptools.
6969
# Feature request: https://github.com/pypa/setuptools/issues/1819
70-
71-
print("Setting compile flags for compiler type " + self.compiler.compiler_type)
70+
print("Adjusting compiler for compiler type " + self.compiler.compiler_type)
7271
# This is quite hacky as we're modifying the Extension object itself.
7372
if self.compiler.compiler_type == "msvc":
74-
ext.extra_compile_args.append("/std:c++17")
73+
for ext in self.extensions:
74+
ext.extra_compile_args.append("/std:c++17")
7575
else:
76-
ext.extra_compile_args.append("-std=c++17")
77-
78-
# Actually build.
79-
super().build_extension(ext)
76+
# -std=c++17 should only be applied to C++ build,
77+
# not when compiling C source code. Unfortunately,
78+
# the extra_compile_args applies to both. Instead,
79+
# patch the CC/CXX commands in the compiler object.
80+
#
81+
# Note that older versions of distutils/setuptools do not
82+
# have the necessary separation between C and C++ compilers.
83+
# This requires setuptools 72.2.
84+
for v in ("compiler_cxx", "compiler_so_cxx"):
85+
if not hasattr(self.compiler, v):
86+
print(
87+
f"WARNING: cannot adjust flag {v}, "
88+
f"compiler type {self.compiler.compiler_type}, "
89+
f"compiler class {type(self.compiler).__name__}"
90+
)
91+
continue
92+
current = getattr(self.compiler, v)
93+
self.compiler.set_executable(v, current + ["-std=c++17"])
94+
super().build_extensions()
8095

8196
def run(self):
8297
self._pack_std_jsonnet()

0 commit comments

Comments
 (0)