Skip to content

refactor: decouple build orchestration from BuildContext#3100

Merged
messense merged 13 commits into
PyO3:mainfrom
messense:refactor/clean-monolith
Mar 22, 2026
Merged

refactor: decouple build orchestration from BuildContext#3100
messense merged 13 commits into
PyO3:mainfrom
messense:refactor/clean-monolith

Conversation

@messense

@messense messense commented Mar 22, 2026

Copy link
Copy Markdown
Member

Summary

BuildContext had grown into a monolith that mixed data (configuration, interpreter lists, paths) with behavior (wheel building, PGO loops, SBOM generation, target filtering). This PR extracts the orchestration logic into dedicated types, leaving BuildContext as a pure data container.

What changed

  • New BuildOrchestrator — owns the high-level build flow (wheel building, PGO loops, sdist creation, tag computation). Takes a &BuildContext reference and coordinates the build pipeline without owning any state.

  • PgoContext narrowed to profiling concerns — manages the profdata directory, llvm-profdata resolution, instrumentation execution, and profile merging. The three-phase PGO build loops now live in BuildOrchestrator.

  • SbomData gained generate() and write() — SBOM generation and wheel-writing logic moved from the orchestrator into sbom.rs, keeping CycloneDX serialization details out of the build pipeline.

  • Target filtering moved to compile.rsfilter_cargo_targets() extracted as a free function, removing workspace/metadata logic from BuildContext.

Cleanup:

  • Removed dead rust-cpython/packages references from bridge detection
  • Tightened visibility: internal build methods (compile_cdylib, build_cffi_wheel, build_uniffi_wheel, build_bin_wheel, get_universal_tag) are now private or pub(crate)
  • Deleted src/build_context/wheels.rs (531 lines) — logic moved to BuildOrchestrator

Architecture

BuildContext (data)          BuildOrchestrator (behavior)
├── ProjectContext       ──▶ ├── build_wheels()
├── PythonContext             │   ├── PGO loops (single-pass / per-interpreter)
├── ArtifactContext           │   └── build_wheels_inner()
                              ├── build_source_distribution()
                              └── tags_from_bridge()

PgoContext (profiling env)   SbomData (SBOM lifecycle)
├── run_instrumentation()    ├── generate()
├── merge_profiles()         └── write()
└── find_llvm_profdata()

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors maturin’s build pipeline by moving the high-level “build verbs” (wheel/sdist orchestration, tagging, SBOM generation) out of BuildContext into a new BuildOrchestrator, keeping BuildContext primarily as a data container plus lower-level helpers.

Changes:

  • Added BuildOrchestrator and migrated wheel/sdist build orchestration (including SBOM generation/writing and tag selection) into it.
  • Updated CLI commands and test helpers to use BuildOrchestrator instead of calling orchestration methods on BuildContext.
  • Reshaped BuildContext to expose narrower pub(crate) repair helpers and moved filter_cargo_targets into compile.rs.

Reviewed changes

Copilot reviewed 22 out of 22 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/run/sdist.rs Switch sdist tests to build via BuildOrchestrator.
tests/common/other.rs Switch common test helpers to build wheels/sdists via BuildOrchestrator.
tests/common/integration.rs Switch integration tests to build wheels via BuildOrchestrator.
tests/common/errors.rs Switch error-path tests to build wheels via BuildOrchestrator.
src/sbom.rs Moves SBOM generation/writing out; keeps include-path resolution and SBOM data type.
src/pgo.rs Refactors PGO pipeline to orchestrate via BuildOrchestrator helpers.
src/module_writer/mock_writer.rs Uses BuildOrchestrator::tags_from_bridge() for dist-info generation in tests.
src/lib.rs Exposes BuildOrchestrator publicly and adds module.
src/develop/mod.rs Uses BuildOrchestrator for wheel builds in develop.
src/compile.rs Adds filter_cargo_targets here (moved from build_context builder).
src/commands/utils.rs Uses BuildOrchestrator for building sdists used by other commands.
src/commands/sdist.rs Uses BuildOrchestrator to build the sdist.
src/commands/publish.rs Uses BuildOrchestrator to build wheels before upload.
src/commands/pep517.rs Uses BuildOrchestrator for PEP517 operations; subcommand naming changed.
src/commands/mod.rs Narrows command module visibility to pub(crate) and adds tracing instrumentation.
src/commands/develop.rs Adds tracing instrumentation.
src/commands/build.rs Uses BuildOrchestrator for wheel builds; adds tracing instrumentation.
src/build_orchestrator.rs New orchestrator module containing the migrated build pipeline.
src/build_context/wheels.rs Deleted (logic migrated into BuildOrchestrator).
src/build_context/repair.rs Renames/opens up repair helpers for orchestrator use.
src/build_context/mod.rs Removes orchestration methods; keeps wrappers for repair helpers used by orchestrator.
src/build_context/builder.rs Uses compile::filter_cargo_targets after moving it.

Comment thread src/commands/pep517.rs
Comment thread src/build_orchestrator.rs Outdated
Comment thread src/build_orchestrator.rs

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 23 out of 23 changed files in this pull request and generated 1 comment.

Comment thread src/module_writer/mock_writer.rs
…GO orchestration to BuildOrchestrator

- Make internal build methods (compile_cdylib, build_cffi_wheel,
  build_uniffi_wheel, build_bin_wheel, get_universal_tag) private or
  pub(crate) to avoid leaking internals in the public API.
- Move SBOM generation and writing logic from BuildOrchestrator into
  SbomData::generate() and SbomData::write() in sbom.rs, keeping
  separation of concerns.
- Move PGO build loops into BuildOrchestrator and reduce PgoContext to
  profiling environment management (paths, instrumentation, merging).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 23 out of 23 changed files in this pull request and generated 4 comments.

Comment thread src/build_context/mod.rs
Comment thread src/commands/mod.rs
Comment thread src/bridge/detection.rs
Comment thread src/compile.rs
@messense messense marked this pull request as ready for review March 22, 2026 07:41
@messense messense merged commit 6419c9d into PyO3:main Mar 22, 2026
86 of 88 checks passed
@messense messense deleted the refactor/clean-monolith branch March 22, 2026 09:36
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.

2 participants