pypa / pypa/setuptools

[FR] Add the ability for an extension to be compiled to a .o but not linked into a .so

Open
#2,905 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement Needs Triage
Dominant language
Python
Stars
2.9k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
1

Description

What's the problem this feature will solve?

I'm attempting to build a single extension module that's a combination of Rust code and CFFI-generated C code. This is described in some detail in https://github.com/pyca/cryptography/issues/6634

But in short, the first objective is to get a .o with the compiled C code, which I will then be responsible for linking into the Rust extension module.

Unfortunately setuptools/distutils doesn't have an easy way to do only this. The core logic that compiles things to .o files and then links them is at: https://github.com/pypa/setuptools/blob/main/setuptools/_distutils/command/build_ext.py#L487-L561 Removing the link_shared_object invocation can only be done by copy-pasting the majority of this code.

Describe the solution you'd like

An option on Extension, which is then used in build_ext, which instructs setuptools/distutils to compile the .o files, but not build a .so (and not try to install the .so).

Alternative Solutions

The alternate solution is to override build_ext with a copy-paste of the majority of build_extension's code. This is sub-optimal due to the large amounts of copy-paste required, and also because of the complexity of introducing it into the right place in the class heirarchy, because cffi itself relies on overriding build_ext (https://foss.heptapod.net/pypy/cffi/-/blob/branch/default/cffi/setuptools_ext.py#L145)

Additional context

No response

Code of Conduct
  • I agree to follow the PSF Code of Conduct

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in setuptools/_distutils/command/build_ext.py, especially the build_extension logic around lines 487–561, and trace how Extension objects reach compilation, link_shared_object, and installation. Define the Extension option and build_ext behavior so the C sources produce .o files without creating or installing a .so; the issue’s Rust/CFFI use case is the completion check.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python, rust
Domain
build-system
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
43/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.