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} "$@"