Skip to content

[FR] [Docs] Return processing of environment variables to build_ext #5117

@MikhailRyazanov

Description

@MikhailRyazanov

What's the problem this feature will solve?

The documentation for Building Extension Modules / Compiler and linker options says:

The command build_ext builds C/C++ extension modules. It creates a command line for running the compiler and linker by combining compiler and linker options from various sources:

  • the sysconfig variables CC, CXX, CCSHARED, LDSHARED, and CFLAGS,
  • the environment variables CC, CPP, CXX, LDSHARED and CFLAGS, CPPFLAGS, LDFLAGS,
  • the Extension attributes include_dirs, library_dirs, extra_compile_args, extra_link_args, runtime_library_dirs.

However, in reality, at least building with MSVC completely ignores all environment variables, making customized installations (for example, with more specific optimizations through /favor:.../, /arch:..., /fp:...) impossible without editing project files.

Describe the solution you'd like

The code still has a misleading comment

# Two possible sources for extra compiler arguments:
# - 'extra_compile_args' in Extension object
# - CFLAGS environment variable (not particularly
# elegant, but people seem to expect it and I
# guess it's useful)
# The environment variable should take precedence, and
# any sensible compiler will give precedence to later
# command line args. Hence we combine them in order:
extra_args = ext.extra_compile_args or []

but the “we combine them” part was in fact removed in 17cca1e with an explanation that “sysconfig.py will do that now”. And the problem is that customize_compiler() from sysconfig.py, which is supposed to read environment variables, currently is used only in some cases (not including MSVC, maybe after bacd9c6, maybe even earlier).

So either sysconfig.py should indeed “do that” for all compilers, or merging the documented environment variables into compiler/linker options should be done somewhere else.

Alternative Solutions

An obvious option would be to forget about Setuptools and switch to some other build backend (for example, Meson claims to support all these ...FLAGS and also has equivalent command-line options), but this choice would not require creating this issue.

An alternative is to (re)introduce EXTRA_CFLAGS (previously added for "unux" in 1133a2e, then removed in a9aaf53) but for all compilers and appended after extra_compile_args and extra_link_args, thus allowing to override some options hard-coded there.

If Setuptools maintainers consider that using environment variables to add compiler and linker options is wrong and/or would be unfeasible to implement, the currently misleading documentation must be updated accordingly.

Additional context

I’m reporting this here instead of distutils because the documentation is in this repository.

Code of Conduct

  • I agree to follow the PSF Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs TriageIssues that need to be evaluated for severity and status.enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions