Move CompilerSelector logic into build env setup

This moves the CompilerSelector fails_with logic into the build
environment setup, making the compiler selection available before
performing actions that depends on knowing what the compiler is, e.g.
setting up PATH.

ENV.setup_build_environment now optionally takes a Formula argument
to provide the information necessary to do the fails_with, and the new
ENV.validate_cc! extracts the fails_with logic from Build.install.
This commit is contained in:
Misty De Meo 2013-09-10 23:08:17 -07:00
parent 91e6c993f8
commit 1ae81f0bf7
4 changed files with 45 additions and 30 deletions

View File

@ -120,12 +120,12 @@ class Build
ENV.keg_only_deps = keg_only_deps.map(&:to_s)
ENV.deps = deps.map { |d| d.to_formula.to_s }
ENV.x11 = reqs.any? { |rq| rq.kind_of?(X11Dependency) }
ENV.setup_build_environment
ENV.setup_build_environment(f)
post_superenv_hacks
reqs.each(&:modify_build_environment)
deps.each(&:modify_build_environment)
else
ENV.setup_build_environment
ENV.setup_build_environment(f)
reqs.each(&:modify_build_environment)
deps.each(&:modify_build_environment)
@ -141,14 +141,6 @@ class Build
end
end
if f.fails_with? ENV.compiler
begin
ENV.send CompilerSelector.new(f).compiler
rescue CompilerSelectionError => e
raise e.message
end
end
# We only support libstdc++ right now
stdlib_in_use = CxxStdlib.new(:libstdcxx, ENV.compiler)

View File

@ -111,6 +111,19 @@ module SharedEnvExtension
end
end
# If the given compiler isn't compatible, will try to select
# an alternate compiler, altering the value of environment variables.
# If no valid compiler is found, raises an exception.
def validate_cc!(formula)
if formula.fails_with? ENV.compiler
begin
send CompilerSelector.new(formula).compiler
rescue CompilerSelectionError => e
raise e.message
end
end
end
# Snow Leopard defines an NCURSES value the opposite of most distros
# See: http://bugs.python.org/issue6848
# Currently only used by aalib in core
@ -162,7 +175,7 @@ module SharedEnvExtension
begin
gcc_name = 'gcc' + gcc.delete('.')
gcc = Formula.factory(gcc_name)
gcc = Formulary.factory(gcc_name)
if !gcc.installed?
raise <<-EOS.undent
The requested Homebrew GCC, #{gcc_name}, was not installed.
@ -171,8 +184,6 @@ module SharedEnvExtension
brew install #{gcc_name}
EOS
end
ENV.append('PATH', gcc.opt_prefix/'bin', ':')
rescue FormulaUnavailableError
raise <<-EOS.undent
Homebrew GCC requested, but formula #{gcc_name} not found!

View File

@ -14,7 +14,7 @@ module Stdenv
end
end
def setup_build_environment
def setup_build_environment(formula=nil)
# Clear CDPATH to avoid make issues that depend on changing directories
delete('CDPATH')
delete('GREP_OPTIONS') # can break CMake (lol)
@ -68,8 +68,13 @@ module Stdenv
self.cxx = MacOS.locate("c++")
end
validate_cc!(formula) unless formula.nil?
if cc =~ GNU_GCC_REGEXP
warn_about_non_apple_gcc($1)
gcc_name = 'gcc' + $1.delete('.')
gcc = Formulary.factory(gcc_name)
self.append_path('PATH', gcc.opt_prefix/'bin')
end
# Add lib and include etc. from the current macosxsdk to compiler flags:

View File

@ -54,16 +54,18 @@ module Superenv
delete('CLICOLOR_FORCE') # autotools doesn't like this
end
def setup_build_environment
def setup_build_environment(formula=nil)
reset
self.cc = 'cc'
self.cxx = 'c++'
self['HOMEBREW_CC'] = determine_cc
validate_cc!(formula) unless formula.nil?
self['DEVELOPER_DIR'] = determine_developer_dir
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_CC'] = determine_cc
self['HOMEBREW_CCCFG'] = determine_cccfg
self['HOMEBREW_BREW_FILE'] = HOMEBREW_BREW_FILE
self['HOMEBREW_SDKROOT'] = "#{MacOS.sdk_path}" if MacOS::Xcode.without_clt?
@ -99,20 +101,7 @@ module Superenv
# s - apply fix for sed's Unicode support
# a - apply fix for apr-1-config path
# Homebrew's apple-gcc42 will be outside the PATH in superenv,
# so xcrun may not be able to find it
if self['HOMEBREW_CC'] == 'gcc-4.2'
apple_gcc42 = begin
Formulary.factory('apple-gcc42')
rescue Exception # in --debug, catch bare exceptions too
nil
end
append_path('PATH', apple_gcc42.opt_prefix/'bin') if apple_gcc42
end
if ENV['HOMEBREW_CC'] =~ GNU_GCC_REGEXP
warn_about_non_apple_gcc($1)
end
warn_about_non_apple_gcc($1) if ENV['HOMEBREW_CC'] =~ GNU_GCC_REGEXP
end
def universal_binary
@ -141,6 +130,24 @@ module Superenv
paths += deps.map{|dep| "#{HOMEBREW_PREFIX}/opt/#{dep}/bin" }
paths << MacOS::X11.bin 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
if self['HOMEBREW_CC'] == 'gcc-4.2'
apple_gcc42 = begin
Formulary.factory('apple-gcc42')
rescue Exception # in --debug, catch bare exceptions too
nil
end
paths << apple_gcc42.opt_prefix/'bin' if apple_gcc42
end
if self['HOMEBREW_CC'] =~ GNU_GCC_REGEXP
gcc_name = 'gcc' + $1.delete('.')
gcc = Formulary.factory(gcc_name)
paths << gcc.opt_prefix/'bin'
end
paths.to_path_s
end