Skip to content

Commit fd4f488

Browse files
authored
Merge pull request #15544 from Tokarak/cargo_cpu
Apply cpu-optimisation to Rust projects
2 parents 9cd4475 + 521fdcb commit fd4f488

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

Library/Homebrew/extend/ENV/shared.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module SharedEnvExtension
2828
CMAKE_PREFIX_PATH CMAKE_INCLUDE_PATH CMAKE_FRAMEWORK_PATH
2929
GOBIN GOPATH GOROOT PERL_MB_OPT PERL_MM_OPT
3030
LIBRARY_PATH LD_LIBRARY_PATH LD_PRELOAD LD_RUN_PATH
31+
RUSTFLAGS
3132
].freeze
3233
private_constant :SANITIZED_VARS
3334

Library/Homebrew/extend/ENV/std.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def setup_build_environment(formula: nil, cc: nil, build_bottle: false, bottle_a
3636
self["PKG_CONFIG_LIBDIR"] = determine_pkg_config_libdir
3737

3838
self["MAKEFLAGS"] = "-j#{make_jobs}"
39+
self["RUSTFLAGS"] = Hardware.rustflags_target_cpu
3940

4041
if HOMEBREW_PREFIX.to_s != "/usr/local"
4142
# /usr/local is already an -isystem and -L directory so we skip it

Library/Homebrew/extend/ENV/super.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def setup_build_environment(formula: nil, cc: nil, build_bottle: false, bottle_a
6363

6464
self["HOMEBREW_ENV"] = "super"
6565
self["MAKEFLAGS"] ||= "-j#{determine_make_jobs}"
66+
self["RUSTFLAGS"] = Hardware.rustflags_target_cpu
6667
self["PATH"] = determine_path
6768
self["PKG_CONFIG_PATH"] = determine_pkg_config_path
6869
self["PKG_CONFIG_LIBDIR"] = determine_pkg_config_libdir

Library/Homebrew/hardware.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,24 @@ def oldest_cpu(_version = nil)
214214
end
215215
end
216216
alias generic_oldest_cpu oldest_cpu
217+
218+
# Returns a Rust flag to set the target CPU if necessary.
219+
# Defaults to nil.
220+
sig { returns(T.nilable(String)) }
221+
def rustflags_target_cpu
222+
# Rust already defaults to the oldest supported cpu for each target-triplet
223+
# so it's safe to ignore generic archs such as :armv6 here.
224+
# Rust defaults to apple-m1 since Rust 1.71 for aarch64-apple-darwin.
225+
@target_cpu ||= case (cpu = oldest_cpu)
226+
when :core
227+
:prescott
228+
when :native, :ivybridge, :sandybridge, :nehalem, :core2
229+
cpu
230+
end
231+
return if @target_cpu.blank?
232+
233+
"--codegen target-cpu=#{@target_cpu}"
234+
end
217235
end
218236
end
219237

0 commit comments

Comments
 (0)