diff --git a/Library/Homebrew/extend/ENV/shared.rb b/Library/Homebrew/extend/ENV/shared.rb index bef56363bc21d..fb25328cf7945 100644 --- a/Library/Homebrew/extend/ENV/shared.rb +++ b/Library/Homebrew/extend/ENV/shared.rb @@ -28,6 +28,7 @@ module SharedEnvExtension CMAKE_PREFIX_PATH CMAKE_INCLUDE_PATH CMAKE_FRAMEWORK_PATH GOBIN GOPATH GOROOT PERL_MB_OPT PERL_MM_OPT LIBRARY_PATH LD_LIBRARY_PATH LD_PRELOAD LD_RUN_PATH + RUSTFLAGS ].freeze private_constant :SANITIZED_VARS diff --git a/Library/Homebrew/extend/ENV/std.rb b/Library/Homebrew/extend/ENV/std.rb index b1bdb5424da55..19cec7ad88e50 100644 --- a/Library/Homebrew/extend/ENV/std.rb +++ b/Library/Homebrew/extend/ENV/std.rb @@ -36,6 +36,7 @@ def setup_build_environment(formula: nil, cc: nil, build_bottle: false, bottle_a self["PKG_CONFIG_LIBDIR"] = determine_pkg_config_libdir self["MAKEFLAGS"] = "-j#{make_jobs}" + self["RUSTFLAGS"] = Hardware.rustflags_target_cpu 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 734eccb95d266..c5bbe02caecb8 100644 --- a/Library/Homebrew/extend/ENV/super.rb +++ b/Library/Homebrew/extend/ENV/super.rb @@ -63,6 +63,7 @@ def setup_build_environment(formula: nil, cc: nil, build_bottle: false, bottle_a self["HOMEBREW_ENV"] = "super" self["MAKEFLAGS"] ||= "-j#{determine_make_jobs}" + self["RUSTFLAGS"] = Hardware.rustflags_target_cpu 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/hardware.rb b/Library/Homebrew/hardware.rb index 22bebf626b9bd..ed4034c48ad43 100644 --- a/Library/Homebrew/hardware.rb +++ b/Library/Homebrew/hardware.rb @@ -214,6 +214,24 @@ def oldest_cpu(_version = nil) end end alias generic_oldest_cpu oldest_cpu + + # Returns a Rust flag to set the target CPU if necessary. + # Defaults to nil. + sig { returns(T.nilable(String)) } + def rustflags_target_cpu + # Rust already defaults to the oldest supported cpu for each target-triplet + # so it's safe to ignore generic archs such as :armv6 here. + # Rust defaults to apple-m1 since Rust 1.71 for aarch64-apple-darwin. + @target_cpu ||= case (cpu = oldest_cpu) + when :core + :prescott + when :native, :ivybridge, :sandybridge, :nehalem, :core2 + cpu + end + return if @target_cpu.blank? + + "--codegen target-cpu=#{@target_cpu}" + end end end