Skip to content

Move environment variable handling from setup.py to CMake - #177640

Closed
zklaus wants to merge 10 commits into
gh/zklaus/34/basefrom
gh/zklaus/34/head
Closed

zklaus wants to merge 10 commits into
gh/zklaus/34/basefrom
gh/zklaus/34/head

Conversation

@zklaus

@zklaus zklaus commented Mar 17, 2026 •

Copy link
Copy Markdown
Collaborator

Migration PR on top of stack:

Stack from ghstack (oldest at bottom):

Forward BUILD_/USE_/CMAKE_* and other environment variables to CMake
variables, replicating the behavior of setup.py's cmake.py generate
method.

Also update the project() call to read the version from version.txt
and set Torch_SOURCE_DIR for compatibility.

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/177640

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

❌ 21 New Failures, 3 Unrelated Failures

As of commit 931b93c with merge base f063873 (image):

NEW FAILURES - The following jobs have failed:

FLAKY - The following jobs failed but were likely due to flakiness present on trunk:

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 to zklaus/pytorch that referenced this pull request Mar 18, 2026
Forward BUILD_*/USE_*/CMAKE_* and other environment variables to CMake
variables, replicating the behavior of setup.py's cmake.py generate
method.

Also update the project() call to read the version from version.txt
and set Torch_SOURCE_DIR for compatibility.

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


ghstack-source-id: b825e1b
Pull-Request: pytorch#177640
pytorch-bot Bot pushed a commit that referenced this pull request Mar 21, 2026
Forward BUILD_*/USE_*/CMAKE_* and other environment variables to CMake
variables, replicating the behavior of setup.py's cmake.py generate
method.

Also update the project() call to read the version from version.txt
and set Torch_SOURCE_DIR for compatibility.

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


ghstack-source-id: b825e1b
Pull-Request: #177640
pytorch-bot Bot pushed a commit that referenced this pull request Mar 24, 2026
Forward BUILD_*/USE_*/CMAKE_* and other environment variables to CMake
variables, replicating the behavior of setup.py's cmake.py generate
method.

Also update the project() call to read the version from version.txt
and set Torch_SOURCE_DIR for compatibility.

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


ghstack-source-id: b825e1b
Pull-Request: #177640
zklaus added a commit to zklaus/pytorch that referenced this pull request Mar 24, 2026
Forward BUILD_*/USE_*/CMAKE_* and other environment variables to CMake
variables, replicating the behavior of setup.py's cmake.py generate
method.

Also update the project() call to read the version from version.txt
and set Torch_SOURCE_DIR for compatibility.

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


ghstack-source-id: b825e1b
Pull-Request: pytorch#177640
pytorch-bot Bot pushed a commit that referenced this pull request Mar 26, 2026
Forward BUILD_*/USE_*/CMAKE_* and other environment variables to CMake
variables, replicating the behavior of setup.py's cmake.py generate
method.

Also update the project() call to read the version from version.txt
and set Torch_SOURCE_DIR for compatibility.

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

ghstack-source-id: b825e1b
Pull-Request: #177640
[ghstack-poisoned]
zklaus added a commit to zklaus/pytorch that referenced this pull request Mar 28, 2026
Forward BUILD_*/USE_*/CMAKE_* and other environment variables to CMake
variables, replicating the behavior of setup.py's cmake.py generate
method.

Also update the project() call to read the version from version.txt
and set Torch_SOURCE_DIR for compatibility.

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

ghstack-source-id: b7fb6b7
Pull-Request: pytorch#177640
[ghstack-poisoned]
@zklaus
zklaus marked this pull request as ready for review March 30, 2026 19:38
[ghstack-poisoned]
zklaus added a commit to zklaus/pytorch that referenced this pull request Mar 30, 2026
Forward BUILD_*/USE_*/CMAKE_* and other environment variables to CMake
variables, replicating the behavior of setup.py's cmake.py generate
method.

Also update the project() call to read the version from version.txt
and set Torch_SOURCE_DIR for compatibility.

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

ghstack-source-id: 5c1a291
Pull-Request: pytorch#177640
@zklaus
zklaus requested review from albanD, atalman and malfet March 31, 2026 02:35
@zklaus zklaus added ciflow/binaries Trigger all binary build and upload jobs on the PR labels Mar 31, 2026
@zklaus

zklaus commented Apr 1, 2026

Copy link
Copy Markdown
Collaborator Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Apr 1, 2026
@henryiii

henryiii commented Apr 4, 2026

Copy link
Copy Markdown
Contributor

FWIW, you also can pass custom defines on the CLI using -Ccmake.define.<VAR>=<VALUE>.

I've had some design ideas for custom CLI flags somewhere, by the way, if it's something you are interested in. You'd be able to make new -C<STUFF>=<VALUE flags that way in pyproject.toml.

@Skylion007

Copy link
Copy Markdown
Collaborator

Maybe the better way to start this conversion is to go through scikit-build first, because it's way closer to the current setup.py setup and then convert it to the newer core API?

@henryiii

henryiii commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

I think you'd want to use the scikit-build-core setuptools plugin. Though I think it would be fine to try to just go all the way to the native one if possible.

[ghstack-poisoned]
@zklaus

zklaus commented Apr 6, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for all the feedback!

@malfet, I have refocused this PR to make only the move described in the title. I also have addressed your individual suggestions and put the finding of the Python interpreter as an unconditional find_package(Python... in the main CMakeLists.txt file.

@albanD, I have updated the forwarding pattern to include CMAKE_* variables and also the missing *EXITCODE postfix pattern. The r-path logic is moved to a later commit and the Windows path separator issue is addressed.

@Skylion007 and @henryiii, I did look at the override mechanism and the cmake.define table (which allows setting cmake variables from environment variables, but there are two main issues with that here:

  • it doesn't support wildcards (iiuc), but we don't have an explicit list of all supported variables per this comment.
  • These preparatory PRs are meant to move a functioning setup.py based system towards scikit-build-core, but only Cleaned up switch to scikit-build-core #177031 scikit-build-core itself will be available.

My hope is that mid- to long-term we can do away with environment variables (almost) all-together and instead use the cmake.define table in the scikit-build-core configuration and possibly overrides to group related options.

Regarding the possible interims use of scikit-build or the setuptools plugin, I'll follow @henryiii here and say that the price of doing essentially two migrations doesn't seem worthwhile to me at the moment, as the full transition seems very feasible.

zklaus added a commit to zklaus/pytorch that referenced this pull request Apr 6, 2026
Forward BUILD_*/USE_*/CMAKE_* and other environment variables to CMake
variables, replicating the behavior of setup.py's cmake.py generate
method.

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

ghstack-source-id: 5f3c995
Pull-Request: pytorch#177640
[ghstack-poisoned]
zklaus added a commit to zklaus/pytorch that referenced this pull request Apr 6, 2026
Forward BUILD_*/USE_*/CMAKE_* and other environment variables to CMake
variables, replicating the behavior of setup.py's cmake.py generate
method.

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

ghstack-source-id: dcb0361
Pull-Request: pytorch#177640

@malfet malfet 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, though I have to say I'm much more comfortable at reading Python files that CMake ones

@atalman

atalman commented Apr 7, 2026

Copy link
Copy Markdown
Collaborator

@pytorchmergebot merge -f "looks good to land"

@pytorch pytorch deleted a comment from pytorch-bot Bot Apr 7, 2026
@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

nklshy-aws pushed a commit to nklshy-aws/pytorch that referenced this pull request Apr 7, 2026
…7640)

Migration PR on top of stack:
* pytorch#177031

Forward BUILD_*/USE_*/CMAKE_* and other environment variables to CMake
variables, replicating the behavior of setup.py's cmake.py generate
method.

Also update the project() call to read the version from version.txt
and set Torch_SOURCE_DIR for compatibility.

Co-Generated with [Claude Code](https://claude.com/claude-code)
Pull Request resolved: pytorch#177640
Approved by: https://github.com/atalman, https://github.com/malfet
@github-actions
github-actions Bot deleted the gh/zklaus/34/head branch May 8, 2026 02:26
CodersAcademy006 added a commit to CodersAcademy006/pytorch that referenced this pull request Jul 10, 2026
…takes effect

CMAKE_SYSTEM_NAME/CMAKE_SYSTEM_PROCESSOR must be set before project() for CMake
to enter cross-compile mode. Since pytorch#177640 moved env-var forwarding into
cmake/EnvVarForwarding.cmake (included after project(), skipping already-defined
vars), the target values were dropped and CMAKE_SYSTEM_PROCESSOR stayed at the
x86 host value -- so arch guards (e.g. NNPACK) selected x86 kernels for riscv64.
Forward the toolchain-determining vars before project() to restore correct
cross configuration.
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…7640)

Migration PR on top of stack:
* pytorch#177031

Forward BUILD_*/USE_*/CMAKE_* and other environment variables to CMake
variables, replicating the behavior of setup.py's cmake.py generate
method.

Also update the project() call to read the version from version.txt
and set Torch_SOURCE_DIR for compatibility.

Co-Generated with [Claude Code](https://claude.com/claude-code)
Pull Request resolved: pytorch#177640
Approved by: https://github.com/atalman, https://github.com/malfet
pytorchmergebot pushed a commit that referenced this pull request Jul 22, 2026
Fixes a cross-compilation regression: target `CMAKE_SYSTEM_NAME` / `CMAKE_SYSTEM_PROCESSOR` passed via the environment are silently dropped, so the build configures for the host architecture.

## Root cause

CMake requires `CMAKE_SYSTEM_NAME` and `CMAKE_SYSTEM_PROCESSOR` to be set **before** `project()` in order to enter cross-compile mode and report the target architecture.

#177640 moved environment variable handling out of `setup.py` into `cmake/EnvVarForwarding.cmake`. That file is included at `CMakeLists.txt:24`, which is **after** `project(Torch CXX C)` at `CMakeLists.txt:13`. It forwards a variable only when it is not already defined (`EnvVarForwarding.cmake:61,68`):

```cmake
if(DEFINED ENV{${_var}} AND NOT DEFINED ${_var})
```

By line 24 `project()` has already defined the toolchain variables to their **host** values, so the guard skips them and the target values from the environment are lost. Architecture guards downstream (for example the `{Q,X}NNPACK` selection in `cmake/Dependencies.cmake`) then evaluate against the host and build host kernels while cross-compiling.

Before #177640 these were passed as `-D` arguments from `setup.py`, which took effect at `project()` time. This restores that behavior.

## Why it went unnoticed

This only affects **true** cross-compilation. Architectures that build natively under emulation never rely on the forwarded triplet, so their CI stays green.

## Reproduction

Minimal, isolated, mirroring the include order and the skip-if-defined guard. Host is `arm64`, environment sets `CMAKE_SYSTEM_PROCESSOR=riscv64 CMAKE_SYSTEM_NAME=Linux`:

| forwarding position | `CMAKE_SYSTEM_PROCESSOR` | `CMAKE_CROSSCOMPILING` |
| --- | --- | --- |
| after `project()` (current) | `arm64` (env dropped) | `FALSE` |
| before `project()` (this PR) | `riscv64` | `TRUE` |
| before `project()`, no env set | `arm64` | `FALSE` |

## The change

Forward only the toolchain-determining variables before `project()`. Every assignment is guarded by `DEFINED ENV{...} AND NOT DEFINED ...`, so this is a **no-op for native builds** (third row above) and does not override anything already passed via `-D` or a toolchain file. `EnvVarForwarding.cmake` keeps handling everything else, unchanged.

## Validation

Verified against current `main` that the ordering issue is still present and that no other change forwards these variables before `project()`. Mechanism verified with the isolated reproduction above (cmake 4.3.4), including the native no-op case. Downstream effect previously observed on a riscv64 cross configure: with the fix cmake correctly reports `Target architecture "riscv64" is not supported in {Q/X}NNPACK` and disables them, instead of silently selecting host kernels. A full riscv64 cross build is gated on separate, unrelated blockers, so CI here is the build gate.

Refs #180975

Pull Request resolved: #190003
Approved by: https://github.com/ezyang

Co-authored-by: Edward Z. Yang via mergedog <[email protected]>
youlituao pushed a commit to youlituao/pytorch that referenced this pull request Jul 28, 2026
…ch#190003)

Fixes a cross-compilation regression: target `CMAKE_SYSTEM_NAME` / `CMAKE_SYSTEM_PROCESSOR` passed via the environment are silently dropped, so the build configures for the host architecture.

## Root cause

CMake requires `CMAKE_SYSTEM_NAME` and `CMAKE_SYSTEM_PROCESSOR` to be set **before** `project()` in order to enter cross-compile mode and report the target architecture.

pytorch#177640 moved environment variable handling out of `setup.py` into `cmake/EnvVarForwarding.cmake`. That file is included at `CMakeLists.txt:24`, which is **after** `project(Torch CXX C)` at `CMakeLists.txt:13`. It forwards a variable only when it is not already defined (`EnvVarForwarding.cmake:61,68`):

```cmake
if(DEFINED ENV{${_var}} AND NOT DEFINED ${_var})
```

By line 24 `project()` has already defined the toolchain variables to their **host** values, so the guard skips them and the target values from the environment are lost. Architecture guards downstream (for example the `{Q,X}NNPACK` selection in `cmake/Dependencies.cmake`) then evaluate against the host and build host kernels while cross-compiling.

Before pytorch#177640 these were passed as `-D` arguments from `setup.py`, which took effect at `project()` time. This restores that behavior.

## Why it went unnoticed

This only affects **true** cross-compilation. Architectures that build natively under emulation never rely on the forwarded triplet, so their CI stays green.

## Reproduction

Minimal, isolated, mirroring the include order and the skip-if-defined guard. Host is `arm64`, environment sets `CMAKE_SYSTEM_PROCESSOR=riscv64 CMAKE_SYSTEM_NAME=Linux`:

| forwarding position | `CMAKE_SYSTEM_PROCESSOR` | `CMAKE_CROSSCOMPILING` |
| --- | --- | --- |
| after `project()` (current) | `arm64` (env dropped) | `FALSE` |
| before `project()` (this PR) | `riscv64` | `TRUE` |
| before `project()`, no env set | `arm64` | `FALSE` |

## The change

Forward only the toolchain-determining variables before `project()`. Every assignment is guarded by `DEFINED ENV{...} AND NOT DEFINED ...`, so this is a **no-op for native builds** (third row above) and does not override anything already passed via `-D` or a toolchain file. `EnvVarForwarding.cmake` keeps handling everything else, unchanged.

## Validation

Verified against current `main` that the ordering issue is still present and that no other change forwards these variables before `project()`. Mechanism verified with the isolated reproduction above (cmake 4.3.4), including the native no-op case. Downstream effect previously observed on a riscv64 cross configure: with the fix cmake correctly reports `Target architecture "riscv64" is not supported in {Q/X}NNPACK` and disables them, instead of silently selecting host kernels. A full riscv64 cross build is gated on separate, unrelated blockers, so CI here is the build gate.

Refs pytorch#180975

Pull Request resolved: pytorch#190003
Approved by: https://github.com/ezyang

Co-authored-by: Edward Z. Yang via mergedog <[email protected]>
youlituao pushed a commit to youlituao/pytorch that referenced this pull request Jul 28, 2026
…ch#190003)

Fixes a cross-compilation regression: target `CMAKE_SYSTEM_NAME` / `CMAKE_SYSTEM_PROCESSOR` passed via the environment are silently dropped, so the build configures for the host architecture.

## Root cause

CMake requires `CMAKE_SYSTEM_NAME` and `CMAKE_SYSTEM_PROCESSOR` to be set **before** `project()` in order to enter cross-compile mode and report the target architecture.

pytorch#177640 moved environment variable handling out of `setup.py` into `cmake/EnvVarForwarding.cmake`. That file is included at `CMakeLists.txt:24`, which is **after** `project(Torch CXX C)` at `CMakeLists.txt:13`. It forwards a variable only when it is not already defined (`EnvVarForwarding.cmake:61,68`):

```cmake
if(DEFINED ENV{${_var}} AND NOT DEFINED ${_var})
```

By line 24 `project()` has already defined the toolchain variables to their **host** values, so the guard skips them and the target values from the environment are lost. Architecture guards downstream (for example the `{Q,X}NNPACK` selection in `cmake/Dependencies.cmake`) then evaluate against the host and build host kernels while cross-compiling.

Before pytorch#177640 these were passed as `-D` arguments from `setup.py`, which took effect at `project()` time. This restores that behavior.

## Why it went unnoticed

This only affects **true** cross-compilation. Architectures that build natively under emulation never rely on the forwarded triplet, so their CI stays green.

## Reproduction

Minimal, isolated, mirroring the include order and the skip-if-defined guard. Host is `arm64`, environment sets `CMAKE_SYSTEM_PROCESSOR=riscv64 CMAKE_SYSTEM_NAME=Linux`:

| forwarding position | `CMAKE_SYSTEM_PROCESSOR` | `CMAKE_CROSSCOMPILING` |
| --- | --- | --- |
| after `project()` (current) | `arm64` (env dropped) | `FALSE` |
| before `project()` (this PR) | `riscv64` | `TRUE` |
| before `project()`, no env set | `arm64` | `FALSE` |

## The change

Forward only the toolchain-determining variables before `project()`. Every assignment is guarded by `DEFINED ENV{...} AND NOT DEFINED ...`, so this is a **no-op for native builds** (third row above) and does not override anything already passed via `-D` or a toolchain file. `EnvVarForwarding.cmake` keeps handling everything else, unchanged.

## Validation

Verified against current `main` that the ordering issue is still present and that no other change forwards these variables before `project()`. Mechanism verified with the isolated reproduction above (cmake 4.3.4), including the native no-op case. Downstream effect previously observed on a riscv64 cross configure: with the fix cmake correctly reports `Target architecture "riscv64" is not supported in {Q/X}NNPACK` and disables them, instead of silently selecting host kernels. A full riscv64 cross build is gated on separate, unrelated blockers, so CI here is the build gate.

Refs pytorch#180975

Pull Request resolved: pytorch#190003
Approved by: https://github.com/ezyang

Co-authored-by: Edward Z. Yang via mergedog <[email protected]>
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…7640)

Migration PR on top of stack:
* pytorch#177031

Forward BUILD_*/USE_*/CMAKE_* and other environment variables to CMake
variables, replicating the behavior of setup.py's cmake.py generate
method.

Also update the project() call to read the version from version.txt
and set Torch_SOURCE_DIR for compatibility.

Co-Generated with [Claude Code](https://claude.com/claude-code)
Pull Request resolved: pytorch#177640
Approved by: https://github.com/atalman, https://github.com/malfet
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…ch#190003)

Fixes a cross-compilation regression: target `CMAKE_SYSTEM_NAME` / `CMAKE_SYSTEM_PROCESSOR` passed via the environment are silently dropped, so the build configures for the host architecture.

## Root cause

CMake requires `CMAKE_SYSTEM_NAME` and `CMAKE_SYSTEM_PROCESSOR` to be set **before** `project()` in order to enter cross-compile mode and report the target architecture.

pytorch#177640 moved environment variable handling out of `setup.py` into `cmake/EnvVarForwarding.cmake`. That file is included at `CMakeLists.txt:24`, which is **after** `project(Torch CXX C)` at `CMakeLists.txt:13`. It forwards a variable only when it is not already defined (`EnvVarForwarding.cmake:61,68`):

```cmake
if(DEFINED ENV{${_var}} AND NOT DEFINED ${_var})
```

By line 24 `project()` has already defined the toolchain variables to their **host** values, so the guard skips them and the target values from the environment are lost. Architecture guards downstream (for example the `{Q,X}NNPACK` selection in `cmake/Dependencies.cmake`) then evaluate against the host and build host kernels while cross-compiling.

Before pytorch#177640 these were passed as `-D` arguments from `setup.py`, which took effect at `project()` time. This restores that behavior.

## Why it went unnoticed

This only affects **true** cross-compilation. Architectures that build natively under emulation never rely on the forwarded triplet, so their CI stays green.

## Reproduction

Minimal, isolated, mirroring the include order and the skip-if-defined guard. Host is `arm64`, environment sets `CMAKE_SYSTEM_PROCESSOR=riscv64 CMAKE_SYSTEM_NAME=Linux`:

| forwarding position | `CMAKE_SYSTEM_PROCESSOR` | `CMAKE_CROSSCOMPILING` |
| --- | --- | --- |
| after `project()` (current) | `arm64` (env dropped) | `FALSE` |
| before `project()` (this PR) | `riscv64` | `TRUE` |
| before `project()`, no env set | `arm64` | `FALSE` |

## The change

Forward only the toolchain-determining variables before `project()`. Every assignment is guarded by `DEFINED ENV{...} AND NOT DEFINED ...`, so this is a **no-op for native builds** (third row above) and does not override anything already passed via `-D` or a toolchain file. `EnvVarForwarding.cmake` keeps handling everything else, unchanged.

## Validation

Verified against current `main` that the ordering issue is still present and that no other change forwards these variables before `project()`. Mechanism verified with the isolated reproduction above (cmake 4.3.4), including the native no-op case. Downstream effect previously observed on a riscv64 cross configure: with the fix cmake correctly reports `Target architecture "riscv64" is not supported in {Q/X}NNPACK` and disables them, instead of silently selecting host kernels. A full riscv64 cross build is gated on separate, unrelated blockers, so CI here is the build gate.

Refs pytorch#180975

Pull Request resolved: pytorch#190003
Approved by: https://github.com/ezyang

Co-authored-by: Edward Z. Yang via mergedog <[email protected]>
MelindaTan pushed a commit to MelindaTan/pytorch that referenced this pull request Jul 31, 2026
…ch#190003)

Fixes a cross-compilation regression: target `CMAKE_SYSTEM_NAME` / `CMAKE_SYSTEM_PROCESSOR` passed via the environment are silently dropped, so the build configures for the host architecture.

## Root cause

CMake requires `CMAKE_SYSTEM_NAME` and `CMAKE_SYSTEM_PROCESSOR` to be set **before** `project()` in order to enter cross-compile mode and report the target architecture.

pytorch#177640 moved environment variable handling out of `setup.py` into `cmake/EnvVarForwarding.cmake`. That file is included at `CMakeLists.txt:24`, which is **after** `project(Torch CXX C)` at `CMakeLists.txt:13`. It forwards a variable only when it is not already defined (`EnvVarForwarding.cmake:61,68`):

```cmake
if(DEFINED ENV{${_var}} AND NOT DEFINED ${_var})
```

By line 24 `project()` has already defined the toolchain variables to their **host** values, so the guard skips them and the target values from the environment are lost. Architecture guards downstream (for example the `{Q,X}NNPACK` selection in `cmake/Dependencies.cmake`) then evaluate against the host and build host kernels while cross-compiling.

Before pytorch#177640 these were passed as `-D` arguments from `setup.py`, which took effect at `project()` time. This restores that behavior.

## Why it went unnoticed

This only affects **true** cross-compilation. Architectures that build natively under emulation never rely on the forwarded triplet, so their CI stays green.

## Reproduction

Minimal, isolated, mirroring the include order and the skip-if-defined guard. Host is `arm64`, environment sets `CMAKE_SYSTEM_PROCESSOR=riscv64 CMAKE_SYSTEM_NAME=Linux`:

| forwarding position | `CMAKE_SYSTEM_PROCESSOR` | `CMAKE_CROSSCOMPILING` |
| --- | --- | --- |
| after `project()` (current) | `arm64` (env dropped) | `FALSE` |
| before `project()` (this PR) | `riscv64` | `TRUE` |
| before `project()`, no env set | `arm64` | `FALSE` |

## The change

Forward only the toolchain-determining variables before `project()`. Every assignment is guarded by `DEFINED ENV{...} AND NOT DEFINED ...`, so this is a **no-op for native builds** (third row above) and does not override anything already passed via `-D` or a toolchain file. `EnvVarForwarding.cmake` keeps handling everything else, unchanged.

## Validation

Verified against current `main` that the ordering issue is still present and that no other change forwards these variables before `project()`. Mechanism verified with the isolated reproduction above (cmake 4.3.4), including the native no-op case. Downstream effect previously observed on a riscv64 cross configure: with the fix cmake correctly reports `Target architecture "riscv64" is not supported in {Q/X}NNPACK` and disables them, instead of silently selecting host kernels. A full riscv64 cross build is gated on separate, unrelated blockers, so CI here is the build gate.

Refs pytorch#180975

Pull Request resolved: pytorch#190003
Approved by: https://github.com/ezyang

Co-authored-by: Edward Z. Yang via mergedog <[email protected]>
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: devx

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants