248 lines
6.4 KiB
Ruby
Raw Normal View History

require "hardware"
require "extend/ENV/shared"
2013-08-19 12:32:59 -05:00
# @private
2013-08-19 12:32:59 -05:00
module Stdenv
include SharedEnvExtension
# @private
2016-09-11 17:53:00 +01:00
SAFE_CFLAGS_FLAGS = "-w -pipe".freeze
DEFAULT_FLAGS = "-march=core2 -msse4".freeze
2013-08-19 12:32:59 -05:00
# @private
def setup_build_environment(formula = nil)
super
2013-08-19 12:32:59 -05:00
self["HOMEBREW_ENV"] = "std"
PATH.new(ENV["HOMEBREW_PATH"]).each { |p| prepend_path "PATH", p }
2013-08-19 12:32:59 -05:00
# Set the default pkg-config search path, overriding the built-in paths
# Anything in PKG_CONFIG_PATH is searched before paths in this variable
self["PKG_CONFIG_LIBDIR"] = determine_pkg_config_libdir
2013-08-19 12:32:59 -05:00
self["MAKEFLAGS"] = "-j#{make_jobs}"
2013-08-19 12:32:59 -05:00
unless HOMEBREW_PREFIX.to_s == "/usr/local"
2013-08-19 12:32:59 -05:00
# /usr/local is already an -isystem and -L directory so we skip it
self["CPPFLAGS"] = "-isystem#{HOMEBREW_PREFIX}/include"
self["LDFLAGS"] = "-L#{HOMEBREW_PREFIX}/lib"
2013-08-19 12:32:59 -05:00
# CMake ignores the variables above
self["CMAKE_PREFIX_PATH"] = HOMEBREW_PREFIX.to_s
2013-08-19 12:32:59 -05:00
end
frameworks = HOMEBREW_PREFIX.join("Frameworks")
if frameworks.directory?
append "CPPFLAGS", "-F#{frameworks}"
append "LDFLAGS", "-F#{frameworks}"
self["CMAKE_FRAMEWORK_PATH"] = frameworks.to_s
2013-08-19 12:32:59 -05:00
end
# Os is the default Apple uses for all its stuff so let's trust them
2016-09-24 17:59:14 +02:00
define_cflags "-Os #{SAFE_CFLAGS_FLAGS}"
2013-08-19 12:32:59 -05:00
append "LDFLAGS", "-Wl,-headerpad_max_install_names"
send(compiler)
2016-09-23 22:02:23 +02:00
return unless cc =~ GNU_GCC_REGEXP
gcc_formula = gcc_version_formula($&)
append_path "PATH", gcc_formula.opt_bin.to_s
2016-07-12 11:39:39 +01:00
end
2016-09-23 18:13:48 +02:00
alias generic_setup_build_environment setup_build_environment
2016-07-12 11:39:39 +01:00
def homebrew_extra_pkg_config_paths
[]
2013-08-19 12:32:59 -05:00
end
def determine_pkg_config_libdir
2017-04-27 09:56:16 +02:00
PATH.new(
HOMEBREW_PREFIX/"lib/pkgconfig",
HOMEBREW_PREFIX/"share/pkgconfig",
homebrew_extra_pkg_config_paths,
"/usr/lib/pkgconfig",
).existing
2013-08-19 12:32:59 -05:00
end
# Removes the MAKEFLAGS environment variable, causing make to use a single job.
# This is useful for makefiles with race conditions.
# When passed a block, MAKEFLAGS is removed only for the duration of the block and is restored after its completion.
2013-08-19 12:32:59 -05:00
def deparallelize
old = self["MAKEFLAGS"]
remove "MAKEFLAGS", /-j\d+/
if block_given?
begin
yield
ensure
self["MAKEFLAGS"] = old
end
end
old
2013-08-19 12:32:59 -05:00
end
2013-11-12 12:00:18 -06:00
%w[O3 O2 O1 O0 Os].each do |opt|
2013-11-12 12:00:18 -06:00
define_method opt do
remove_from_cflags(/-O./)
append_to_cflags "-#{opt}"
end
2013-11-12 12:00:18 -06:00
end
2013-08-19 12:32:59 -05:00
# @private
def determine_cc
s = super
DevelopmentTools.locate(s) || Pathname.new(s)
end
# @private
def determine_cxx
2014-06-22 18:35:18 -05:00
dir, base = determine_cc.split
2017-06-01 16:06:51 +02:00
dir/base.to_s.sub("gcc", "g++").sub("clang", "clang++")
end
def gcc_4_0
super
set_cpu_cflags "-march=nocona -mssse3"
2013-08-19 12:32:59 -05:00
end
def gcc_4_2
super
2013-08-19 12:32:59 -05:00
set_cpu_cflags
end
GNU_GCC_VERSIONS.each do |n|
define_method(:"gcc-#{n}") do
super()
set_cpu_cflags
end
end
2013-08-19 12:32:59 -05:00
def clang
super
2013-08-19 12:32:59 -05:00
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 "-march=native", map
2013-08-19 12:32:59 -05:00
end
def minimal_optimization
2016-09-24 17:59:14 +02:00
define_cflags "-Os #{SAFE_CFLAGS_FLAGS}"
2013-08-19 12:32:59 -05:00
end
2016-09-23 18:13:48 +02:00
alias generic_minimal_optimization minimal_optimization
2013-08-19 12:32:59 -05:00
def no_optimization
2016-09-24 17:59:14 +02:00
define_cflags SAFE_CFLAGS_FLAGS
2013-08-19 12:32:59 -05:00
end
2016-09-23 18:13:48 +02:00
alias generic_no_optimization no_optimization
2013-08-19 12:32:59 -05:00
# we've seen some packages fail to build when warnings are disabled!
def enable_warnings
remove_from_cflags "-w"
2013-08-19 12:32:59 -05:00
end
def m64
append_to_cflags "-m64"
append "LDFLAGS", "-arch #{Hardware::CPU.arch_64_bit}"
2013-08-19 12:32:59 -05:00
end
2013-08-19 12:32:59 -05:00
def m32
append_to_cflags "-m32"
append "LDFLAGS", "-arch #{Hardware::CPU.arch_32_bit}"
2013-08-19 12:32:59 -05:00
end
def universal_binary
check_for_compiler_universal_support
2013-08-19 12:32:59 -05:00
append_to_cflags Hardware::CPU.universal_archs.as_arch_flags
append "LDFLAGS", Hardware::CPU.universal_archs.as_arch_flags
2013-08-19 12:32:59 -05:00
2016-09-23 22:02:23 +02:00
return if compiler == :clang
return unless Hardware.is_32_bit?
# Can't mix "-march" for a 32-bit CPU with "-arch x86_64"
replace_in_cflags(/-march=\S*/, "-Xarch_#{Hardware::CPU.arch_32_bit} \\0")
2013-08-19 12:32:59 -05:00
end
def cxx11
if compiler == :clang
append "CXX", "-std=c++11"
append "CXX", "-stdlib=libc++"
elsif gcc_with_cxx11_support?(compiler)
append "CXX", "-std=c++11"
else
raise "The selected compiler doesn't support C++11: #{compiler}"
end
end
def libcxx
2016-09-23 11:01:40 +02:00
append "CXX", "-stdlib=libc++" if compiler == :clang
end
def libstdcxx
2016-09-23 11:01:40 +02:00
append "CXX", "-stdlib=libstdc++" if compiler == :clang
end
# @private
def replace_in_cflags(before, after)
2013-08-19 12:32:59 -05:00
CC_FLAG_VARS.each do |key|
self[key] = self[key].sub(before, after) if key?(key)
2013-08-19 12:32:59 -05:00
end
end
# Convenience method to set all C compiler flags in one shot.
2016-09-24 17:59:14 +02:00
def define_cflags(val)
2013-08-19 12:32:59 -05:00
CC_FLAG_VARS.each { |key| self[key] = val }
end
# 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)
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)?/
2013-08-19 12:32:59 -05:00
append flags, xarch unless xarch.empty?
append flags, map.fetch(effective_arch, default)
end
2016-09-23 18:13:48 +02:00
alias generic_set_cpu_flags set_cpu_flags
2013-08-19 12:32:59 -05:00
def x11; end
# @private
def effective_arch
2013-08-19 12:32:59 -05:00
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
2013-08-19 12:32:59 -05:00
else
Hardware::CPU.family
2013-08-19 12:32:59 -05:00
end
end
# @private
def set_cpu_cflags(default = DEFAULT_FLAGS, map = Hardware::CPU.optimization_flags)
2013-08-19 12:32:59 -05:00
set_cpu_flags CC_FLAG_VARS, default, map
end
def make_jobs
# '-j' requires a positive integral argument
2017-09-24 20:12:58 +01:00
if (jobs = self["HOMEBREW_MAKE_JOBS"].to_i).positive?
jobs
2013-08-19 12:32:59 -05:00
else
Hardware::CPU.cores
end
end
# This method does nothing in stdenv since there's no arg refurbishment
# @private
def refurbish_args; end
2013-08-19 12:32:59 -05:00
end
2016-07-12 11:39:39 +01:00
require "extend/os/extend/ENV/std"