Skip to content

Commit

Permalink
ENV: default CFLAGS to bottle values.
Browse files Browse the repository at this point in the history
This reduces the deviation in compilation flags on a given OS between
users and between bottling/not bottling to provide a consistent
behaviour for debugging and support.
  • Loading branch information
MikeMcQuaid committed Jan 6, 2019
1 parent 28e34d8 commit 7e13b37
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 39 deletions.
9 changes: 9 additions & 0 deletions Library/Homebrew/extend/ENV/shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,15 @@ def ld64
append "LDFLAGS", "-B#{ld64.bin}/"
end

# @private
def effective_arch
if ARGV.build_bottle? && ARGV.bottle_arch
ARGV.bottle_arch
else
Hardware.oldest_cpu
end
end

# @private
def gcc_version_formula(name)
version = name[GNU_GCC_REGEXP, 1]
Expand Down
31 changes: 8 additions & 23 deletions Library/Homebrew/extend/ENV/std.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module Stdenv

# @private
SAFE_CFLAGS_FLAGS = "-w -pipe".freeze
DEFAULT_FLAGS = "-march=native".freeze

# @private
def setup_build_environment(formula = nil)
Expand Down Expand Up @@ -103,12 +102,12 @@ def determine_cxx

def gcc_4_0
super
set_cpu_cflags "-march=nocona -mssse3"
set_cpu_cflags
end

def gcc_4_2
super
set_cpu_cflags "-march=core2 -msse4"
set_cpu_cflags
end

GNU_GCC_VERSIONS.each do |n|
Expand All @@ -123,8 +122,8 @@ def clang
replace_in_cflags(/-Xarch_#{Hardware::CPU.arch_32_bit} (-march=\S*)/, '\1')
# Clang mistakenly enables AES-NI on plain Nehalem
map = Hardware::CPU.optimization_flags
map = map.merge(nehalem: "-march=native -Xclang -target-feature -Xclang -aes")
set_cpu_cflags DEFAULT_FLAGS, map
.merge(nehalem: "-march=nehalem -Xclang -target-feature -Xclang -aes")
set_cpu_cflags map
end

def minimal_optimization
Expand Down Expand Up @@ -199,37 +198,23 @@ def define_cflags(val)
# Sets architecture-specific flags for every environment variable
# given in the list `flags`.
# @private
def set_cpu_flags(flags, default = DEFAULT_FLAGS, map = Hardware::CPU.optimization_flags)
def set_cpu_flags(flags, map = Hardware::CPU.optimization_flags)
cflags =~ /(-Xarch_#{Hardware::CPU.arch_32_bit} )-march=/
xarch = Regexp.last_match(1).to_s
remove flags, /(-Xarch_#{Hardware::CPU.arch_32_bit} )?-march=\S*/
remove flags, /( -Xclang \S+)+/
remove flags, /-mssse3/
remove flags, /-msse4(\.\d)?/
append flags, xarch unless xarch.empty?
append flags, map.fetch(effective_arch, default)
append flags, map.fetch(effective_arch)
end
alias generic_set_cpu_flags set_cpu_flags

def x11; end

# @private
def effective_arch
if ARGV.build_bottle?
ARGV.bottle_arch || Hardware.oldest_cpu
elsif Hardware::CPU.intel? && !Hardware::CPU.sse4?
# If the CPU doesn't support SSE4, we cannot trust -march=native or
# -march=<cpu family> to do the right thing because we might be running
# in a VM or on a Hackintosh.
Hardware.oldest_cpu
else
Hardware::CPU.family
end
end

# @private
def set_cpu_cflags(default = DEFAULT_FLAGS, map = Hardware::CPU.optimization_flags)
set_cpu_flags CC_FLAG_VARS, default, map
def set_cpu_cflags(map = Hardware::CPU.optimization_flags)
set_cpu_flags CC_FLAG_VARS, map
end

def make_jobs
Expand Down
15 changes: 1 addition & 14 deletions Library/Homebrew/extend/ENV/super.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,20 +230,7 @@ def determine_make_jobs
end

def determine_optflags
if ARGV.build_bottle?
arch = ARGV.bottle_arch || Hardware.oldest_cpu
Hardware::CPU.optimization_flags.fetch(arch)
# If the CPU doesn't support SSE4, we cannot trust -march=native or
# -march=<cpu family> to do the right thing because we might be running
# in a VM or on a Hackintosh.
elsif Hardware::CPU.intel? && !Hardware::CPU.sse4?
Hardware::CPU.optimization_flags.fetch(Hardware.oldest_cpu)
elsif ![:gcc_4_0, :gcc_4_2].include?(compiler)
"-march=native"
# This is mutated elsewhere, so return an empty string in this case
else
""
end
Hardware::CPU.optimization_flags.fetch(effective_arch)
end

def determine_cccfg
Expand Down
4 changes: 2 additions & 2 deletions Library/Homebrew/extend/os/mac/extend/ENV/std.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def setup_build_environment(formula = nil)
# Sets architecture-specific flags for every environment variable
# given in the list `flags`.
# @private
def set_cpu_flags(flags, default = DEFAULT_FLAGS, map = Hardware::CPU.optimization_flags)
generic_set_cpu_flags(flags, default, map)
def set_cpu_flags(flags, map = Hardware::CPU.optimization_flags)
generic_set_cpu_flags(flags, map)

# Works around a buggy system header on Tiger
append flags, "-faltivec" if MacOS.version == :tiger
Expand Down

0 comments on commit 7e13b37

Please sign in to comment.