Skip to content

Move header wrapping and compile_commands merging to CMake - #177644

Closed
zklaus wants to merge 29 commits into
gh/zklaus/38/basefrom
gh/zklaus/38/head
Closed

zklaus wants to merge 29 commits into
gh/zklaus/38/basefrom
gh/zklaus/38/head

Conversation

@zklaus

@zklaus zklaus commented Mar 17, 2026 •

Copy link
Copy Markdown
Collaborator

Stack from ghstack (oldest at bottom):

Adds cmake/PostBuildSteps.cmake with two post-build steps previously handled by setup.py's build_ext.run():

  • TORCH_STABLE_ONLY header wrapping — runs tools/wrap_headers.py at install time to add guards to installed headers. Headers wrapped this way error when included with TORCH_STABLE_ONLY or TORCH_TARGET_VERSION defined.
  • compile_commands.json merging — runs tools/merge_compile_commands.py as a build target to combine compile commands from cmake build subdirectories into a single file at the project root.

Both wrap_headers.py and merge_compile_commands.py are extracted from inline code in setup.py into standalone scripts under tools/. The corresponding code is removed from setup.py's build_ext class (97 lines removed).

This creates the PostBuildSteps.cmake module that subsequent PRs (#180237, #180238, #180239) extend with license bundling, Windows DLL bundling, and macOS OpenMP embedding.

Co-Generated with Claude Code

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Mar 17, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/177644

Note: Links to docs will display an error until the docs builds have been completed.

❌ 14 New Failures

As of commit d0964a4 with merge base 3659a40 (image):

NEW FAILURES - The following jobs have failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot

pytorch-bot Bot commented Mar 17, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

zklaus added a commit that referenced this pull request Mar 17, 2026
Handles post-build steps: TORCH_STABLE_ONLY header wrapping,
compile_commands.json merging, bundled license generation, Windows
export library installation, and macOS OpenMP embedding. Replaces
the equivalent logic from setup.py's build_ext.run().

Remove header wrapping, compile_commands merging, and Windows export
library copying from setup.py — these are now handled by
cmake/PostBuildSteps.cmake.

The macOS _embed_libomp method stays because the cmake version has
only a simplified stub. The concat_license_files class and
bdist_wheel/sdist overrides stay because setuptools uses a different
license bundling mechanism (temporarily concatenating the root LICENSE
file during wheel/sdist creation).

Co-Generated with [Claude Code](https://claude.com/claude-code)


ghstack-source-id: 61ce781
Pull-Request: #177644
pytorch-bot Bot pushed a commit that referenced this pull request Mar 21, 2026
Handles post-build steps: TORCH_STABLE_ONLY header wrapping,
compile_commands.json merging, bundled license generation, Windows
export library installation, and macOS OpenMP embedding. Replaces
the equivalent logic from setup.py's build_ext.run().

Remove header wrapping, compile_commands merging, and Windows export
library copying from setup.py — these are now handled by
cmake/PostBuildSteps.cmake.

The macOS _embed_libomp method stays because the cmake version has
only a simplified stub. The concat_license_files class and
bdist_wheel/sdist overrides stay because setuptools uses a different
license bundling mechanism (temporarily concatenating the root LICENSE
file during wheel/sdist creation).

Co-Generated with [Claude Code](https://claude.com/claude-code)


ghstack-source-id: 61ce781
Pull-Request: #177644
pytorch-bot Bot pushed a commit that referenced this pull request Mar 24, 2026
Handles post-build steps: TORCH_STABLE_ONLY header wrapping,
compile_commands.json merging, bundled license generation, Windows
export library installation, and macOS OpenMP embedding. Replaces
the equivalent logic from setup.py's build_ext.run().

Remove header wrapping, compile_commands merging, and Windows export
library copying from setup.py — these are now handled by
cmake/PostBuildSteps.cmake.

The macOS _embed_libomp method stays because the cmake version has
only a simplified stub. The concat_license_files class and
bdist_wheel/sdist overrides stay because setuptools uses a different
license bundling mechanism (temporarily concatenating the root LICENSE
file during wheel/sdist creation).

Co-Generated with [Claude Code](https://claude.com/claude-code)


ghstack-source-id: 61ce781
Pull-Request: #177644
zklaus added a commit to zklaus/pytorch that referenced this pull request Mar 24, 2026
Handles post-build steps: TORCH_STABLE_ONLY header wrapping,
compile_commands.json merging, bundled license generation, Windows
export library installation, and macOS OpenMP embedding. Replaces
the equivalent logic from setup.py's build_ext.run().

Remove header wrapping, compile_commands merging, and Windows export
library copying from setup.py — these are now handled by
cmake/PostBuildSteps.cmake.

The macOS _embed_libomp method stays because the cmake version has
only a simplified stub. The concat_license_files class and
bdist_wheel/sdist overrides stay because setuptools uses a different
license bundling mechanism (temporarily concatenating the root LICENSE
file during wheel/sdist creation).

Co-Generated with [Claude Code](https://claude.com/claude-code)


ghstack-source-id: 61ce781
Pull-Request: pytorch#177644
pytorch-bot Bot pushed a commit that referenced this pull request Mar 26, 2026
Handles post-build steps: TORCH_STABLE_ONLY header wrapping,
compile_commands.json merging, bundled license generation, Windows
export library installation, and macOS OpenMP embedding. Replaces
the equivalent logic from setup.py's build_ext.run().

Remove header wrapping, compile_commands merging, and Windows export
library copying from setup.py — these are now handled by
cmake/PostBuildSteps.cmake.

The macOS _embed_libomp method stays because the cmake version has
only a simplified stub. The concat_license_files class and
bdist_wheel/sdist overrides stay because setuptools uses a different
license bundling mechanism (temporarily concatenating the root LICENSE
file during wheel/sdist creation).

Co-Generated with [Claude Code](https://claude.com/claude-code)

ghstack-source-id: 61ce781
Pull-Request: #177644
[ghstack-poisoned]
zklaus added a commit that referenced this pull request Mar 28, 2026
Handles post-build steps: TORCH_STABLE_ONLY header wrapping,
compile_commands.json merging, bundled license generation, Windows
export library installation, and macOS OpenMP embedding. Replaces
the equivalent logic from setup.py's build_ext.run().

Remove header wrapping, compile_commands merging, and Windows export
library copying from setup.py — these are now handled by
cmake/PostBuildSteps.cmake.

The macOS _embed_libomp method stays because the cmake version has
only a simplified stub. The concat_license_files class and
bdist_wheel/sdist overrides stay because setuptools uses a different
license bundling mechanism (temporarily concatenating the root LICENSE
file during wheel/sdist creation).

Co-Generated with [Claude Code](https://claude.com/claude-code)

ghstack-source-id: b3adcba
Pull-Request: #177644
[ghstack-poisoned]
zklaus added a commit that referenced this pull request Mar 30, 2026
Handles post-build steps: TORCH_STABLE_ONLY header wrapping,
compile_commands.json merging, bundled license generation, Windows
export library installation, and macOS OpenMP embedding. Replaces
the equivalent logic from setup.py's build_ext.run().

Remove header wrapping, compile_commands merging, and Windows export
library copying from setup.py — these are now handled by
cmake/PostBuildSteps.cmake.

The macOS _embed_libomp method stays because the cmake version has
only a simplified stub. The concat_license_files class and
bdist_wheel/sdist overrides stay because setuptools uses a different
license bundling mechanism (temporarily concatenating the root LICENSE
file during wheel/sdist creation).

Co-Generated with [Claude Code](https://claude.com/claude-code)

ghstack-source-id: 630604c
Pull-Request: #177644
[ghstack-poisoned]
zklaus added a commit that referenced this pull request Mar 30, 2026
Handles post-build steps: TORCH_STABLE_ONLY header wrapping,
compile_commands.json merging, bundled license generation, Windows
export library installation, and macOS OpenMP embedding. Replaces
the equivalent logic from setup.py's build_ext.run().

Remove header wrapping, compile_commands merging, and Windows export
library copying from setup.py — these are now handled by
cmake/PostBuildSteps.cmake.

The macOS _embed_libomp method stays because the cmake version has
only a simplified stub. The concat_license_files class and
bdist_wheel/sdist overrides stay because setuptools uses a different
license bundling mechanism (temporarily concatenating the root LICENSE
file during wheel/sdist creation).

Co-Generated with [Claude Code](https://claude.com/claude-code)

ghstack-source-id: 354f998
Pull-Request: #177644
@zklaus zklaus added ciflow/binaries Trigger all binary build and upload jobs on the PR labels Mar 31, 2026
@zklaus
zklaus requested review from albanD, atalman and malfet March 31, 2026 22:50
[ghstack-poisoned]
zklaus added a commit that referenced this pull request Apr 1, 2026
Handles post-build steps: TORCH_STABLE_ONLY header wrapping,
compile_commands.json merging, bundled license generation, Windows
export library installation, and macOS OpenMP embedding. Replaces
the equivalent logic from setup.py's build_ext.run().

Remove header wrapping, compile_commands merging, and Windows export
library copying from setup.py — these are now handled by
cmake/PostBuildSteps.cmake.

The macOS _embed_libomp method stays because the cmake version has
only a simplified stub. The concat_license_files class and
bdist_wheel/sdist overrides stay because setuptools uses a different
license bundling mechanism (temporarily concatenating the root LICENSE
file during wheel/sdist creation).

Co-Generated with [Claude Code](https://claude.com/claude-code)

ghstack-source-id: 5b61f9f
Pull-Request: #177644
[ghstack-poisoned]
zklaus added a commit that referenced this pull request Apr 2, 2026
Handles post-build steps: TORCH_STABLE_ONLY header wrapping,
compile_commands.json merging, bundled license generation, Windows
export library installation, and macOS OpenMP embedding. Replaces
the equivalent logic from setup.py's build_ext.run().

Remove header wrapping, compile_commands merging, and Windows export
library copying from setup.py — these are now handled by
cmake/PostBuildSteps.cmake.

The macOS _embed_libomp method stays because the cmake version has
only a simplified stub. The concat_license_files class and
bdist_wheel/sdist overrides stay because setuptools uses a different
license bundling mechanism (temporarily concatenating the root LICENSE
file during wheel/sdist creation).

Co-Generated with [Claude Code](https://claude.com/claude-code)

ghstack-source-id: 6c24b56
Pull-Request: #177644
@atalman

atalman commented Apr 29, 2026 •

Copy link
Copy Markdown
Collaborator

From one more Claude iteration. Review findings — most → least important:

1. (Likely regression) setup.py no longer wraps headers. PR #177644 removes the
  _wrap_headers_with_macro call from build_ext.run() but only re-attaches the wrap step
  via cmake's install(CODE …). setuptools builds don't normally invoke cmake --install,
  so a python setup.py bdist_wheel flow ships un-wrapped headers — losing the
  TORCH_STABLE_ONLY / TORCH_TARGET_VERSION guards. PR #177643's body explicitly says
  setuptools is still maintained ("setup.py's package_data declarations remain
  necessary"), so this is asymmetric: data files keep dual paths, header wrapping
  doesn't. Either keep the call in setup.py.run() (importing/exec'ing
  tools/wrap_headers.py) or document scikit-build-core as the only supported path.

  2. (Subtle) Benchmark utils glob is broader than setup.py. Old package_data was
  specifically utils/benchmark/utils/*.cpp +
  utils/benchmark/utils/valgrind_wrapper/*.{cpp,h}. New install(DIRECTORY …
  FILES_MATCHING PATTERN "*.cpp" PATTERN "*.h") is recursive and unscoped — any future
  stray .h under utils/benchmark/utils/ ships in the wheel. Either split into two narrow
  rules or scope with explicit subdir patterns.

  3. (Build correctness) merge_compile_commands lacks DEPENDS. It's add_custom_target(...
   ALL) so it runs every build, but with no dependency on the underlying
  compile_commands.json files there's no guarantee about ordering relative to subbuild
  regen. Prefer add_custom_command(OUTPUT compile_commands.json DEPENDS …) + a target
  that depends on it.

  4. (Risk of CI break) annotated_fn_args.py install is not OPTIONAL. Other generated
  files (script.ld, graph_break_registry.json, AOTI model.h) get OPTIONAL, but this one
  doesn't. If codegen ever fails to run before install, the build hard-errors. Add
  OPTIONAL for symmetry, or assert the codegen is wired into the dependency graph.

[ghstack-poisoned]
zklaus added a commit to zklaus/pytorch that referenced this pull request May 1, 2026
Move TORCH_STABLE_ONLY header wrapping and compile_commands.json
merging from setup.py's build_ext to cmake/PostBuildSteps.cmake.

Co-Generated with [Claude Code](https://claude.com/claude-code)

ghstack-source-id: 237b93e
Pull-Request: pytorch#177644
zklaus added 2 commits May 1, 2026 16:45
[ghstack-poisoned]
[ghstack-poisoned]
@zklaus

zklaus commented May 5, 2026

Copy link
Copy Markdown
Collaborator Author

Walked both AI reviews. Addressed in fixups; the rest dismissed with reasoning:

Addressed

  • TORCH_INSTALL_LIB_DIR was indeed dead at this commit — moved the set block to Add Windows runtime DLL bundling to PostBuildSteps #180238, which is the first consumer.
  • install(CODE) for wrap_headers.py now has COMMAND_ERROR_IS_FATAL ANY so a script failure aborts install instead of silently shipping unwrapped headers.

Dismissed (parity with old behavior or non-issue)

  • Header-wrap "double newline before #else": the old setup.py code produced the same output for files ending in \n. No change.
  • "setup.py no longer wraps headers": setup.py's build path runs cmake --build --target install, which fires install(CODE). Headers still get wrapped on the setuptools track.
  • merge_compile_commands.py sub-build glob path: the old setup.py glob (<source>/torch/lib/build/) was already a dead path in modern PyTorch — sub-builds (NCCL, AOTriton, …) live under ${CMAKE_BINARY_DIR}. Net behavior unchanged.
  • merge_compile_commands lacks DEPENDS: same correctness guarantee as the old create_compile_commands call from build_extensions(). Long-term plan is to drop the merge entirely and trust cmake's native handling.

Mis-attributed to other PRs

[ghstack-poisoned]

@atalman atalman left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

lgtm

@zklaus zklaus added suppress-bc-linter Suppresses the failures of API backward-compatibility linter (Lint/bc_linter) labels May 6, 2026
[ghstack-poisoned]
@zklaus

zklaus commented May 6, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

zklaus added a commit to zklaus/pytorch that referenced this pull request May 6, 2026
Move TORCH_STABLE_ONLY header wrapping and compile_commands.json
merging from setup.py's build_ext to cmake/PostBuildSteps.cmake.

Co-Generated with [Claude Code](https://claude.com/claude-code)

ghstack-source-id: 8a46c7a
Pull-Request: pytorch#177644
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

[ghstack-poisoned]
@zklaus

zklaus commented May 7, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge -f "Failures unrelated"

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

Alokksinha00 pushed a commit to Alokksinha00/pytorch that referenced this pull request May 15, 2026
…77644)

Adds `cmake/PostBuildSteps.cmake` with two post-build steps previously handled by `setup.py`'s `build_ext.run()`:

- **`TORCH_STABLE_ONLY` header wrapping** — runs `tools/wrap_headers.py` at install time to add guards to installed headers. Headers wrapped this way error when included with `TORCH_STABLE_ONLY` or `TORCH_TARGET_VERSION` defined.
- **`compile_commands.json` merging** — runs `tools/merge_compile_commands.py` as a build target to combine compile commands from cmake build subdirectories into a single file at the project root.

Both `wrap_headers.py` and `merge_compile_commands.py` are extracted from inline code in `setup.py` into standalone scripts under `tools/`. The corresponding code is removed from `setup.py`'s `build_ext` class (97 lines removed).

This creates the `PostBuildSteps.cmake` module that subsequent PRs (pytorch#180237, pytorch#180238, pytorch#180239) extend with license bundling, Windows DLL bundling, and macOS OpenMP embedding.

Co-Generated with [Claude Code](https://claude.com/claude-code)

Pull Request resolved: pytorch#177644
Approved by: https://github.com/atalman
@github-actions
github-actions Bot deleted the gh/zklaus/38/head branch June 7, 2026 02:29
aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
…77644)

Adds `cmake/PostBuildSteps.cmake` with two post-build steps previously handled by `setup.py`'s `build_ext.run()`:

- **`TORCH_STABLE_ONLY` header wrapping** — runs `tools/wrap_headers.py` at install time to add guards to installed headers. Headers wrapped this way error when included with `TORCH_STABLE_ONLY` or `TORCH_TARGET_VERSION` defined.
- **`compile_commands.json` merging** — runs `tools/merge_compile_commands.py` as a build target to combine compile commands from cmake build subdirectories into a single file at the project root.

Both `wrap_headers.py` and `merge_compile_commands.py` are extracted from inline code in `setup.py` into standalone scripts under `tools/`. The corresponding code is removed from `setup.py`'s `build_ext` class (97 lines removed).

This creates the `PostBuildSteps.cmake` module that subsequent PRs (pytorch#180237, pytorch#180238, pytorch#180239) extend with license bundling, Windows DLL bundling, and macOS OpenMP embedding.

Co-Generated with [Claude Code](https://claude.com/claude-code)

Pull Request resolved: pytorch#177644
Approved by: https://github.com/atalman
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…77644)

Adds `cmake/PostBuildSteps.cmake` with two post-build steps previously handled by `setup.py`'s `build_ext.run()`:

- **`TORCH_STABLE_ONLY` header wrapping** — runs `tools/wrap_headers.py` at install time to add guards to installed headers. Headers wrapped this way error when included with `TORCH_STABLE_ONLY` or `TORCH_TARGET_VERSION` defined.
- **`compile_commands.json` merging** — runs `tools/merge_compile_commands.py` as a build target to combine compile commands from cmake build subdirectories into a single file at the project root.

Both `wrap_headers.py` and `merge_compile_commands.py` are extracted from inline code in `setup.py` into standalone scripts under `tools/`. The corresponding code is removed from `setup.py`'s `build_ext` class (97 lines removed).

This creates the `PostBuildSteps.cmake` module that subsequent PRs (pytorch#180237, pytorch#180238, pytorch#180239) extend with license bundling, Windows DLL bundling, and macOS OpenMP embedding.

Co-Generated with [Claude Code](https://claude.com/claude-code)

Pull Request resolved: pytorch#177644
Approved by: https://github.com/atalman
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…77644)

Adds `cmake/PostBuildSteps.cmake` with two post-build steps previously handled by `setup.py`'s `build_ext.run()`:

- **`TORCH_STABLE_ONLY` header wrapping** — runs `tools/wrap_headers.py` at install time to add guards to installed headers. Headers wrapped this way error when included with `TORCH_STABLE_ONLY` or `TORCH_TARGET_VERSION` defined.
- **`compile_commands.json` merging** — runs `tools/merge_compile_commands.py` as a build target to combine compile commands from cmake build subdirectories into a single file at the project root.

Both `wrap_headers.py` and `merge_compile_commands.py` are extracted from inline code in `setup.py` into standalone scripts under `tools/`. The corresponding code is removed from `setup.py`'s `build_ext` class (97 lines removed).

This creates the `PostBuildSteps.cmake` module that subsequent PRs (pytorch#180237, pytorch#180238, pytorch#180239) extend with license bundling, Windows DLL bundling, and macOS OpenMP embedding.

Co-Generated with [Claude Code](https://claude.com/claude-code)

Pull Request resolved: pytorch#177644
Approved by: https://github.com/atalman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/binaries Trigger all binary build and upload jobs on the PR ciflow/trunk Trigger trunk jobs on your pull request Merged open source release notes: releng release notes category suppress-bc-linter Suppresses the failures of API backward-compatibility linter (Lint/bc_linter)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants