From e6ba71c5b118132f9bad5c3eff4839e4d0b782df Mon Sep 17 00:00:00 2001 From: Mike McQuaid Date: Wed, 6 Aug 2025 17:06:25 +0100 Subject: [PATCH] Add rustc wrapper shim to fix RUSTFLAGS conflicts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Co-authored-by: Carlo Cabrera --- Library/Homebrew/extend/ENV/shared.rb | 5 +++++ Library/Homebrew/extend/ENV/std.rb | 3 ++- Library/Homebrew/extend/ENV/super.rb | 3 ++- Library/Homebrew/shims/super/rustc_wrapper | 15 +++++++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) create mode 100755 Library/Homebrew/shims/super/rustc_wrapper diff --git a/Library/Homebrew/extend/ENV/shared.rb b/Library/Homebrew/extend/ENV/shared.rb index 38b7c16f4e..28e66d480e 100644 --- a/Library/Homebrew/extend/ENV/shared.rb +++ b/Library/Homebrew/extend/ENV/shared.rb @@ -113,6 +113,11 @@ module SharedEnvExtension self[key] = PATH.new(self[key]).append(path) end + sig { params(rustflags: String).void } + def append_to_rustflags(rustflags) + append("HOMEBREW_RUSTFLAGS", rustflags) + end + # Prepends a directory to `PATH`. # Is the formula struggling to find the pkgconfig file? Point it to it. # This is done automatically for keg-only formulae. diff --git a/Library/Homebrew/extend/ENV/std.rb b/Library/Homebrew/extend/ENV/std.rb index 22f5cb4056..f4b5f34f11 100644 --- a/Library/Homebrew/extend/ENV/std.rb +++ b/Library/Homebrew/extend/ENV/std.rb @@ -34,7 +34,8 @@ module Stdenv self["PKG_CONFIG_LIBDIR"] = determine_pkg_config_libdir 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" # /usr/local is already an -isystem and -L directory so we skip it diff --git a/Library/Homebrew/extend/ENV/super.rb b/Library/Homebrew/extend/ENV/super.rb index b419561736..bf083f3165 100644 --- a/Library/Homebrew/extend/ENV/super.rb +++ b/Library/Homebrew/extend/ENV/super.rb @@ -62,7 +62,8 @@ module Superenv self["HOMEBREW_ENV"] = "super" 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["PKG_CONFIG_PATH"] = determine_pkg_config_path self["PKG_CONFIG_LIBDIR"] = determine_pkg_config_libdir || "" diff --git a/Library/Homebrew/shims/super/rustc_wrapper b/Library/Homebrew/shims/super/rustc_wrapper new file mode 100755 index 0000000000..ad2c97c7a1 --- /dev/null +++ b/Library/Homebrew/shims/super/rustc_wrapper @@ -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} "$@"