Skip to content

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

Merged
merged 2 commits into from
Mar 15, 2019
Merged

bpo-36235: Fix CFLAGS in distutils customize_compiler() #12236

merged 2 commits into from
Mar 15, 2019

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Mar 8, 2019

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

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>
@vstinner
Copy link
Member Author

vstinner commented Mar 8, 2019

cc @izbyshev @tirkarthi

@ericvw
Copy link
Contributor

ericvw commented Mar 8, 2019

@vstinner, I just ran into this exact issue today when needing to add to the CFLAGS captured during ./configure time.

There is also #7997 I have out there for address CPPFLAGS being passed through to distutils from ./configure.

@vstinner
Copy link
Member Author

@ned-deily @stratakis: Would you mind to review this change?

@vstinner
Copy link
Member Author

@ericvw @pablogsal: Would you mind to review this PR?

@ericvw
Copy link
Contributor

ericvw commented Mar 15, 2019

@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']
Copy link
Contributor

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:

Suggested change
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.

Copy link
Member Author

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()

Copy link
Contributor

@ericvw ericvw Mar 15, 2019

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']
Copy link
Contributor

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.

Copy link
Member Author

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.
Copy link
Contributor

@ericvw ericvw left a 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']
Copy link
Contributor

@ericvw ericvw Mar 15, 2019

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.

@vstinner vstinner merged commit 86082c2 into python:master Mar 15, 2019
@miss-islington
Copy link
Contributor

Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 2.7, 3.7.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-12348 is a backport of this pull request to the 3.7 branch.

@bedevere-bot
Copy link

GH-12349 is a backport of this pull request to the 2.7 branch.

vstinner added a commit that referenced this pull request Mar 15, 2019
…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)
vstinner added a commit that referenced this pull request Mar 15, 2019
…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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants