Add rustc wrapper shim to fix RUSTFLAGS conflicts

Fixes #18556 by using RUSTC_WRAPPER instead of setting RUSTFLAGS directly.
This allows Homebrew's optimization flags to coexist with .cargo/config.toml
settings, preventing build failures when projects have their own Rust
configuration.

- Add rustc_wrapper shim that clears RUSTFLAGS and prepends HOMEBREW_RUSTFLAGS
- Update both std and super environments to use RUSTC_WRAPPER
- Store Homebrew's rustflags in HOMEBREW_RUSTFLAGS instead of RUSTFLAGS

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Co-authored-by: Carlo Cabrera <github@carlo.cab>
This commit is contained in:
Mike McQuaid 2025-08-06 17:06:25 +01:00
parent 5e1fd26da0
commit e6ba71c5b1
No known key found for this signature in database
4 changed files with 24 additions and 2 deletions

View File

@ -113,6 +113,11 @@ module SharedEnvExtension
self[key] = PATH.new(self[key]).append(path) self[key] = PATH.new(self[key]).append(path)
end end
sig { params(rustflags: String).void }
def append_to_rustflags(rustflags)
append("HOMEBREW_RUSTFLAGS", rustflags)
end
# Prepends a directory to `PATH`. # Prepends a directory to `PATH`.
# Is the formula struggling to find the pkgconfig file? Point it to it. # Is the formula struggling to find the pkgconfig file? Point it to it.
# This is done automatically for keg-only formulae. # This is done automatically for keg-only formulae.

View File

@ -34,7 +34,8 @@ module Stdenv
self["PKG_CONFIG_LIBDIR"] = determine_pkg_config_libdir self["PKG_CONFIG_LIBDIR"] = determine_pkg_config_libdir
self["MAKEFLAGS"] = "-j#{make_jobs}" self["MAKEFLAGS"] = "-j#{make_jobs}"
self["RUSTFLAGS"] = Hardware.rustflags_target_cpu(effective_arch) self["RUSTC_WRAPPER"] = "#{HOMEBREW_SHIMS_PATH}/super/rustc_wrapper"
self["HOMEBREW_RUSTFLAGS"] = Hardware.rustflags_target_cpu(effective_arch)
if HOMEBREW_PREFIX.to_s != "/usr/local" if HOMEBREW_PREFIX.to_s != "/usr/local"
# /usr/local is already an -isystem and -L directory so we skip it # /usr/local is already an -isystem and -L directory so we skip it

View File

@ -62,7 +62,8 @@ module Superenv
self["HOMEBREW_ENV"] = "super" self["HOMEBREW_ENV"] = "super"
self["MAKEFLAGS"] ||= "-j#{determine_make_jobs}" self["MAKEFLAGS"] ||= "-j#{determine_make_jobs}"
self["RUSTFLAGS"] = Hardware.rustflags_target_cpu(effective_arch) self["RUSTC_WRAPPER"] = "#{HOMEBREW_SHIMS_PATH}/super/rustc_wrapper"
self["HOMEBREW_RUSTFLAGS"] = Hardware.rustflags_target_cpu(effective_arch)
self["PATH"] = determine_path self["PATH"] = determine_path
self["PKG_CONFIG_PATH"] = determine_pkg_config_path self["PKG_CONFIG_PATH"] = determine_pkg_config_path
self["PKG_CONFIG_LIBDIR"] = determine_pkg_config_libdir || "" self["PKG_CONFIG_LIBDIR"] = determine_pkg_config_libdir || ""

View File

@ -0,0 +1,15 @@
#!/bin/bash
# Prepend HOMEBREW_RUSTFLAGS to rustc arguments if set.
# This allows Homebrew to pass optimization flags while still respecting
# .cargo/config.toml (which sets RUSTFLAGS).
# rustc is passed as the first argument to RUSTC_WRAPPER
# https://doc.rust-lang.org/cargo/reference/environment-variables.html
RUSTC="${1}"
shift
# Prepend HOMEBREW_RUSTFLAGS to the arguments if set.
# HOMEBREW_RUSTFLAGS is set in extend/ENV/{std,super}.rb
# shellcheck disable=SC2086,SC2154
exec "${RUSTC}" ${HOMEBREW_RUSTFLAGS} "$@"