-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
bpo-36235: Fix CFLAGS in distutils customize_compiler() #12236
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
Conversation
Fix CFLAGS in customize_compiler() of distutils.sysconfig: when the CFLAGS environment variable is defined, don't override CFLAGS variable with the OPT variable anymore. Initial patch written by David Malcolm. Co-Authored-By: David Malcolm <dmalcolm@redhat.com>
@ned-deily @stratakis: Would you mind to review this change? |
@ericvw @pablogsal: Would you mind to review this PR? |
Sure — will review soon. |
@@ -205,7 +205,7 @@ def customize_compiler(compiler): | |||
if 'LDFLAGS' in os.environ: | |||
ldshared = ldshared + ' ' + os.environ['LDFLAGS'] | |||
if 'CFLAGS' in os.environ: | |||
cflags = opt + ' ' + os.environ['CFLAGS'] | |||
cflags = cflags + ' ' + os.environ['CFLAGS'] |
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.
I believe opt
should be retained since there are compiler flags captured during ./configure
which would get dropped when the CFLAGS
environment variable is set invoking a build via distutils
.
For example, on my macOS machine, the following compiler flags would be lost if CFLAGS
was set:
>>> sysconfig.get_config_var('OPT')
'-DNDEBUG -g -fwrapv -O3 -Wall'
To the best of my understanding, it appears the goal is to have the compiler flags used when building the interpreter to be passed along to C/C++ extensions with the ability to add additional user-specified flags via the CFLAGS
environment variable.
Given that the intent appears to be appending to the flags already captured during the configuration phase of building the interpreter, I would suggest:
cflags = cflags + ' ' + os.environ['CFLAGS'] | |
cflags = opt + ' ' + cflags + ' ' + os.environ['CFLAGS'] |
If the propose change is accepted, then my follow-up feedback would be updating the commit message and news announcement accordingly.
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.
sysconfig reads PY_FLAGS from Makefile and expose it as "CFLAGS". In the Makefile, PY_CFLAGS is composed from other variables:
PY_CFLAGS= $(BASECFLAGS) $(OPT) $(CONFIGURE_CFLAGS) $(CFLAGS) $(EXTRA_CFLAGS)
So "OPT" sysconfig variable is a subset of "CFLAGS" sysconfig variable.
Note: I'm talking about the specific case of sysconfig variables.
Python compiled manually:
$ ./python
Python 3.8.0a2+ (heads/common_config2-dirty:3fe7fa316f, Mar 14 2019, 23:17:45)
>>> import shlex, sysconfig
>>> cflags = set(shlex.split(sysconfig.get_config_var('CFLAGS')))
>>> opt = set(shlex.split(sysconfig.get_config_var('OPT')))
>>> opt.issubset(cflags)
True
>>> cflags - opt
{'-Wno-unused-result', '-O0', '-Wsign-compare'}
>>> opt - cflags
set()
/usr/bin/python3:
vstinner@apu$ python3
Python 3.7.2 (default, Jan 16 2019, 19:49:22)
>>> import shlex, sysconfig
>>> cflags = set(shlex.split(sysconfig.get_config_var('CFLAGS')))
>>> opt = set(shlex.split(sysconfig.get_config_var('OPT')))
>>> opt.issubset(cflags)
True
>>> cflags - opt
{'-Wsign-compare', '-Wno-unused-result'}
>>> opt - cflags
set()
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.
@vstinner, thanks for the walkthrough and I have arrived at the same conclusion.
@@ -205,7 +205,7 @@ def customize_compiler(compiler): | |||
if 'LDFLAGS' in os.environ: | |||
ldshared = ldshared + ' ' + os.environ['LDFLAGS'] | |||
if 'CFLAGS' in os.environ: | |||
cflags = opt + ' ' + os.environ['CFLAGS'] | |||
cflags = cflags + ' ' + os.environ['CFLAGS'] |
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.
If my concern about opt
turns out to not be valid and the decision is to move forward with the original proposed changed, the opt
variable, and it's acquired value fromget_config_vars([...], 'OPT', [...])
, can be removed since it would be unused code.
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.
Right. Fixed.
get_config_var('OPT') is no longer used.
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.
lgtm.
Excited for this fix :)
@@ -205,7 +205,7 @@ def customize_compiler(compiler): | |||
if 'LDFLAGS' in os.environ: | |||
ldshared = ldshared + ' ' + os.environ['LDFLAGS'] | |||
if 'CFLAGS' in os.environ: | |||
cflags = opt + ' ' + os.environ['CFLAGS'] | |||
cflags = cflags + ' ' + os.environ['CFLAGS'] |
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.
@vstinner, thanks for the walkthrough and I have arrived at the same conclusion.
Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 2.7, 3.7. |
GH-12348 is a backport of this pull request to the 3.7 branch. |
GH-12349 is a backport of this pull request to the 2.7 branch. |
…H-12348) Fix CFLAGS in customize_compiler() of distutils.sysconfig: when the CFLAGS environment variable is defined, don't override CFLAGS variable with the OPT variable anymore. Initial patch written by David Malcolm. Co-Authored-By: David Malcolm <dmalcolm@redhat.com> (cherry picked from commit 86082c2)
…H-12349) Fix CFLAGS in customize_compiler() of distutils.sysconfig: when the CFLAGS environment variable is defined, don't override CFLAGS variable with the OPT variable anymore. Initial patch written by David Malcolm. Co-Authored-By: David Malcolm <dmalcolm@redhat.com> (cherry picked from commit 86082c2)
Fix CFLAGS in customize_compiler() of distutils.sysconfig: when the
CFLAGS environment variable is defined, don't override CFLAGS variable with
the OPT variable anymore.
Initial patch written by David Malcolm.
Co-Authored-By: David Malcolm dmalcolm@redhat.com
https://bugs.python.org/issue36235