319 lines
9.8 KiB
Ruby
Raw Normal View History

2013-10-18 12:56:51 -05:00
require 'os/mac'
require 'extend/ENV/shared'
### Why `superenv`?
# 1) Only specify the environment we need (NO LDFLAGS for cmake)
# 2) Only apply compiler specific options when we are calling that compiler
# 3) Force all incpaths and libpaths into the cc instantiation (less bugs)
# 4) Cater toolchain usage to specific Xcode versions
# 5) Remove flags that we don't want or that will break builds
# 6) Simpler code
# 7) Simpler formula that *just work*
# 8) Build-system agnostic configuration of the tool-chain
module Superenv
include SharedEnvExtension
2013-03-09 16:56:32 -06:00
attr_accessor :keg_only_deps, :deps, :x11
alias_method :x11?, :x11
2013-08-19 12:32:56 -05:00
def self.extended(base)
base.keg_only_deps = []
base.deps = []
end
2013-08-19 12:32:56 -05:00
def self.bin
@bin ||= (HOMEBREW_REPOSITORY/"Library/ENV").subdirs.reject { |d| d.basename.to_s > MacOS::Xcode.version }.max
2013-08-19 12:32:56 -05:00
end
def reset
%w{CC CXX OBJC OBJCXX CPP MAKE LD LDSHARED
CFLAGS CXXFLAGS OBJCFLAGS OBJCXXFLAGS LDFLAGS CPPFLAGS
MACOSX_DEPLOYMENT_TARGET SDKROOT DEVELOPER_DIR
CMAKE_PREFIX_PATH CMAKE_INCLUDE_PATH CMAKE_FRAMEWORK_PATH
CPATH C_INCLUDE_PATH CPLUS_INCLUDE_PATH OBJC_INCLUDE_PATH}.
each{ |x| delete(x) }
delete('CDPATH') # avoid make issues that depend on changing directories
delete('GREP_OPTIONS') # can break CMake
delete('CLICOLOR_FORCE') # autotools doesn't like this
# Configure scripts generated by autoconf 2.61 or later export as_nl, which
# we use as a heuristic for running under configure
delete('as_nl')
end
def setup_build_environment(formula=nil)
reset
self.cc = self['HOMEBREW_CC'] = determine_cc
self.cxx = self['HOMEBREW_CXX'] = determine_cxx
validate_cc!(formula) unless formula.nil?
self['MAKEFLAGS'] ||= "-j#{determine_make_jobs}"
self['PATH'] = determine_path
self['PKG_CONFIG_PATH'] = determine_pkg_config_path
self['PKG_CONFIG_LIBDIR'] = determine_pkg_config_libdir
self['HOMEBREW_CCCFG'] = determine_cccfg
self['HOMEBREW_OPTIMIZATION_LEVEL'] = 'Os'
self['HOMEBREW_BREW_FILE'] = HOMEBREW_BREW_FILE
self['HOMEBREW_PREFIX'] = HOMEBREW_PREFIX
self['HOMEBREW_TEMP'] = HOMEBREW_TEMP
self['HOMEBREW_SDKROOT'] = "#{MacOS.sdk_path}" if MacOS::Xcode.without_clt?
self['HOMEBREW_OPTFLAGS'] = determine_optflags
self['HOMEBREW_ARCHFLAGS'] = ''
self['CMAKE_PREFIX_PATH'] = determine_cmake_prefix_path
self['CMAKE_FRAMEWORK_PATH'] = determine_cmake_frameworks_path
self['CMAKE_INCLUDE_PATH'] = determine_cmake_include_path
self['CMAKE_LIBRARY_PATH'] = determine_cmake_library_path
self['ACLOCAL_PATH'] = determine_aclocal_path
self['M4'] = MacOS.locate("m4") if deps.include? "autoconf"
# On 10.9, the tools in /usr/bin proxy to the active developer directory.
# This means we can use them for any combination of CLT and Xcode.
2014-05-13 11:22:30 -05:00
self["HOMEBREW_PREFER_CLT_PROXIES"] = "1" if MacOS.version >= "10.9"
2013-08-01 11:36:44 -07:00
# The HOMEBREW_CCCFG ENV variable is used by the ENV/cc tool to control
# compiler flag stripping. It consists of a string of characters which act
# as flags. Some of these flags are mutually exclusive.
#
# 3 - A 32-bit build was requested
2013-08-04 22:37:52 -07:00
# O - Enables argument refurbishing. Only active under the
# make/bsdmake wrappers currently.
# x - Enable C++11 mode.
# g - Enable "-stdlib=libc++" for clang.
# h - Enable "-stdlib=libstdc++" for clang.
2013-08-01 11:36:44 -07:00
#
# On 10.8 and newer, these flags will also be present:
# s - apply fix for sed's Unicode support
# a - apply fix for apr-1-config path
2014-05-13 12:48:27 -05:00
warn_about_non_apple_gcc($1) if self["HOMEBREW_CC"] =~ GNU_GCC_REGEXP
end
private
def determine_cc
cc = compiler
COMPILER_SYMBOL_MAP.invert.fetch(cc, cc)
end
def determine_cxx
determine_cc.to_s.gsub('gcc', 'g++').gsub('clang', 'clang++')
end
def determine_path
2013-08-19 12:32:56 -05:00
paths = [Superenv.bin]
# On 10.9, there are shims for all tools in /usr/bin.
# On 10.7 and 10.8 we need to add these directories ourselves.
if MacOS::Xcode.without_clt? && MacOS.version <= "10.8"
paths << "#{MacOS::Xcode.prefix}/usr/bin"
paths << "#{MacOS::Xcode.toolchain_path}/usr/bin"
end
paths += deps.map { |dep| "#{HOMEBREW_PREFIX}/opt/#{dep}/bin" }
paths << MacOS::X11.bin.to_s if x11?
paths += %w{/usr/bin /bin /usr/sbin /sbin}
# Homebrew's apple-gcc42 will be outside the PATH in superenv,
# so xcrun may not be able to find it
2014-05-13 12:13:05 -05:00
case self["HOMEBREW_CC"]
when "gcc-4.2"
begin
apple_gcc42 = Formulary.factory('apple-gcc42')
rescue Exception # in --debug, catch bare exceptions too
end
paths << apple_gcc42.opt_bin.to_s if apple_gcc42
2014-05-13 12:13:05 -05:00
when GNU_GCC_REGEXP
gcc_formula = gcc_version_formula($1)
paths << gcc_formula.opt_bin.to_s
end
paths.to_path_s
end
def determine_pkg_config_path
2013-03-09 16:56:32 -06:00
paths = deps.map{|dep| "#{HOMEBREW_PREFIX}/opt/#{dep}/lib/pkgconfig" }
paths += deps.map{|dep| "#{HOMEBREW_PREFIX}/opt/#{dep}/share/pkgconfig" }
paths.to_path_s
end
def determine_pkg_config_libdir
2013-10-30 13:20:48 -07:00
paths = %W{/usr/lib/pkgconfig #{HOMEBREW_LIBRARY}/ENV/pkgconfig/#{MacOS.version}}
2013-08-06 19:24:19 -05:00
paths << "#{MacOS::X11.lib}/pkgconfig" << "#{MacOS::X11.share}/pkgconfig" if x11?
paths.to_path_s
end
def determine_aclocal_path
paths = keg_only_deps.map{|dep| "#{HOMEBREW_PREFIX}/opt/#{dep}/share/aclocal" }
paths << "#{HOMEBREW_PREFIX}/share/aclocal"
paths << "#{MacOS::X11.share}/aclocal" if x11?
paths.to_path_s
end
def determine_cmake_prefix_path
2013-03-09 16:56:32 -06:00
paths = keg_only_deps.map{|dep| "#{HOMEBREW_PREFIX}/opt/#{dep}" }
paths << HOMEBREW_PREFIX.to_s # put ourselves ahead of everything else
paths << "#{MacOS.sdk_path}/usr" if MacOS::Xcode.without_clt?
paths.to_path_s
end
def determine_cmake_frameworks_path
paths = deps.map{|dep| "#{HOMEBREW_PREFIX}/opt/#{dep}/Frameworks" }
paths << "#{MacOS.sdk_path}/System/Library/Frameworks" if MacOS::Xcode.without_clt?
paths.to_path_s
end
def determine_cmake_include_path
sdk = MacOS.sdk_path if MacOS::Xcode.without_clt?
paths = []
paths << "#{sdk}/usr/include/libxml2" unless deps.include? 'libxml2'
paths << "#{sdk}/usr/include/apache2" if MacOS::Xcode.without_clt?
2014-05-12 11:45:50 -05:00
if x11?
paths << MacOS::X11.include << "#{MacOS::X11.include}/freetype2"
else
paths << "#{sdk}/System/Library/Frameworks/OpenGL.framework/Versions/Current/Headers"
end
paths.to_path_s
end
def determine_cmake_library_path
sdk = MacOS.sdk_path if MacOS::Xcode.without_clt?
paths = []
2014-05-12 11:45:50 -05:00
if x11?
paths << MacOS::X11.lib
else
paths << "#{sdk}/System/Library/Frameworks/OpenGL.framework/Versions/Current/Libraries"
end
paths.to_path_s
end
2012-08-23 16:15:26 -04:00
def determine_make_jobs
if (j = self['HOMEBREW_MAKE_JOBS'].to_i) < 1
2013-06-13 12:04:58 -05:00
Hardware::CPU.cores
2012-08-23 16:15:26 -04:00
else
j
end
end
def determine_optflags
if ARGV.build_bottle?
arch = ARGV.bottle_arch || Hardware.oldest_cpu
Hardware::CPU.optimization_flags.fetch(arch)
elsif Hardware::CPU.intel? && !Hardware::CPU.sse4?
Hardware::CPU.optimization_flags.fetch(Hardware.oldest_cpu)
elsif compiler == :clang
"-march=native"
# This is mutated elsewhere, so return an empty string in this case
else
""
end
end
def determine_cccfg
s = ""
# Fix issue with sed barfing on unicode characters on Mountain Lion
s << 's' if MacOS.version >= :mountain_lion
# Fix issue with >= 10.8 apr-1-config having broken paths
s << 'a' if MacOS.version >= :mountain_lion
s
end
public
def deparallelize
delete('MAKEFLAGS')
end
alias_method :j1, :deparallelize
2013-11-11 18:33:47 -06:00
COMPILER_SYMBOL_MAP.values.each do |compiler|
define_method compiler do
@compiler = compiler
self.cc = self['HOMEBREW_CC'] = determine_cc
self.cxx = self['HOMEBREW_CXX'] = determine_cxx
end
end
2013-11-11 18:33:47 -06:00
GNU_GCC_VERSIONS.each do |n|
define_method(:"gcc-4.#{n}") do
@compiler = "gcc-4.#{n}"
self.cc = self['HOMEBREW_CC'] = determine_cc
self.cxx = self['HOMEBREW_CXX'] = determine_cxx
end
end
2013-11-11 18:33:47 -06:00
def make_jobs
self['MAKEFLAGS'] =~ /-\w*j(\d)+/
[$1.to_i, 1].max
end
2013-11-11 18:33:47 -06:00
def universal_binary
self['HOMEBREW_ARCHFLAGS'] = Hardware::CPU.universal_archs.as_arch_flags
# GCC doesn't accept "-march" for a 32-bit CPU with "-arch x86_64"
if compiler != :clang && Hardware.is_32_bit?
self['HOMEBREW_OPTFLAGS'] = self['HOMEBREW_OPTFLAGS'].sub(
/-march=\S*/,
"-Xarch_#{Hardware::CPU.arch_32_bit} \\0"
)
end
2013-11-11 18:33:47 -06:00
end
def cxx11
if self['HOMEBREW_CC'] == 'clang'
append 'HOMEBREW_CCCFG', "x", ''
append 'HOMEBREW_CCCFG', "g", ''
elsif self['HOMEBREW_CC'] =~ /gcc-4\.(8|9)/
append 'HOMEBREW_CCCFG', "x", ''
else
raise "The selected compiler doesn't support C++11: #{self['HOMEBREW_CC']}"
end
end
def libcxx
if self['HOMEBREW_CC'] == 'clang'
append 'HOMEBREW_CCCFG', "g", ''
end
end
def libstdcxx
if self['HOMEBREW_CC'] == 'clang'
append 'HOMEBREW_CCCFG', "h", ''
end
end
def refurbish_args
append 'HOMEBREW_CCCFG', "O", ''
end
2013-11-11 18:33:47 -06:00
# m32 on superenv does not add any CC flags. It prevents "-m32" from being erased.
def m32
append 'HOMEBREW_CCCFG', "3", ''
end
%w{O3 O2 O1 O0 Os}.each do |opt|
define_method opt do
self['HOMEBREW_OPTIMIZATION_LEVEL'] = opt
end
end
def noop(*args); end
noops = []
# These methods are no longer necessary under superenv, but are needed to
# maintain an interface compatible with stdenv.
noops.concat %w{fast O4 Og libxml2 x11 set_cpu_flags macosxsdk remove_macosxsdk}
# These methods provide functionality that has not yet been ported to
# superenv.
noops.concat %w{m64 gcc_4_0_1 minimal_optimization no_optimization enable_warnings}
noops.each { |m| alias_method m, :noop }
end
class Array
def to_path_s
map(&:to_s).uniq.select{|s| File.directory? s }.join(File::PATH_SEPARATOR).chuzzle
end
end