Developer Tools

trunk/81c9d238dd6078ff6ef9ba3cab8c86d20ee420ad: Fix --cmake-only and CMAKE_ONLY with setup.py develop/install (#176052)

A subtle bug in PyTorch's setup script was breaking builds for developers using the --cmake-only flag.

Deep Dive

The PyTorch development team has resolved a significant but subtle build system bug in commit 81c9d238dd. The issue affected developers using the `--cmake-only` flag or the `CMAKE_ONLY` environment variable with PyTorch's `setup.py develop` or `setup.py install` commands. These commands are commonly used by researchers and engineers who need to build PyTorch from source, often to enable specific hardware optimizations or to work on custom branches. The bug was particularly insidious because it involved an internal redirect: when a user ran `setup.py develop`, the script would hand off execution to pip, which would then re-invoke `setup.py` in a subprocess. This handoff broke in two specific ways, causing the build to fail silently or with confusing errors.

The technical root cause was twofold. First, the `CMAKE_ONLY` environment variable was being removed (`popped`) from `os.environ` before the redirect to pip, so the subprocess never received it. Second, command-line arguments like `--cmake-only` were parsed in the order they appeared in `sys.argv`. If `--cmake-only` came after the `develop` command, the redirect to pip would fire before the flag was ever processed, making it ineffective. The fix, detailed in pull request #176052, restructures the logic to parse these critical build flags before the main argument loop begins. Furthermore, when `CMAKE_ONLY` is detected, the script now completely skips the problematic pip redirect, substituting the command with a simple `build` action. This allows the existing, correct exit path within the `build_deps()` function to handle the CMake-only build process directly, ensuring a reliable and predictable outcome for developers compiling the framework.

Key Points
  • Bug broke `--cmake-only` flag due to pip subprocess redirect losing environment variables and argument order.
  • Fix restructures argument parsing to handle flags before command loop and skips pip redirect for CMAKE_ONLY builds.
  • Critical for developers and researchers who compile PyTorch from source for custom hardware or experimental features.

Why It Matters

This fix is essential for AI engineers and researchers who compile PyTorch from source to optimize for specific hardware or test custom modifications.