-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Prepend Pybind11Extension flags rather than appending them. #2808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 @@ def __init__(self, *args, **kwargs): | |
# 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 @@ def cxx_std(self, level): | |
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 @@ def cxx_std(self, level): | |
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) | ||
Comment on lines
+206
to
+207
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason these are prepended? This may happen at the beginning (if it is manually specified), at an arbitrary point (if assigned to, in Python 3), or at the end (if left to "automatic"). The order of these particular flags shouldn't really matter, in theory, I'd expect? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I chose to just prepend everything to simplify the explanation ("pybind11 puts all its flags at the beginning") (also, perhaps someone really wants to override one of the -Wflags...), but admittedly the only thing I personally really care about is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is fine, it's the most consistent (I think) if this gets triggered at different times. |
||
|
||
|
||
# Just in case someone clever tries to multithread | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, I did not know this was valid! But it is, even back in 2.7!
Even can be used in the middle:
Maybe this is common knowledge, but it surprised me. :)