Move common ENV methods to a shared module

This commit is contained in:
Jack Nagel 2013-08-19 12:32:59 -05:00
parent cc18f5e0c4
commit 9a5b15bb61
2 changed files with 6 additions and 118 deletions

View File

@ -1,4 +1,5 @@
require 'hardware' require 'hardware'
require 'extend/ENV/shared'
require 'extend/ENV/super' require 'extend/ENV/super'
def superenv? def superenv?
@ -21,9 +22,9 @@ end
ENV.extend(EnvActivation) ENV.extend(EnvActivation)
module HomebrewEnvExtension module HomebrewEnvExtension
include SharedEnvExtension
SAFE_CFLAGS_FLAGS = "-w -pipe" SAFE_CFLAGS_FLAGS = "-w -pipe"
CC_FLAG_VARS = %w{CFLAGS CXXFLAGS OBJCFLAGS OBJCXXFLAGS}
FC_FLAG_VARS = %w{FCFLAGS FFLAGS}
DEFAULT_FLAGS = '-march=core2 -msse4' DEFAULT_FLAGS = '-march=core2 -msse4'
def self.extended(base) def self.extended(base)
@ -403,119 +404,3 @@ module HomebrewEnvExtension
append "LDFLAGS", "-B#{ld64.bin.to_s+"/"}" append "LDFLAGS", "-B#{ld64.bin.to_s+"/"}"
end end
end end
class << ENV
def remove_cc_etc
keys = %w{CC CXX OBJC OBJCXX LD CPP CFLAGS CXXFLAGS OBJCFLAGS OBJCXXFLAGS LDFLAGS CPPFLAGS}
removed = Hash[*keys.map{ |key| [key, self[key]] }.flatten]
keys.each do |key|
delete(key)
end
removed
end
def append_to_cflags newflags
append(HomebrewEnvExtension::CC_FLAG_VARS, newflags)
end
def remove_from_cflags val
remove HomebrewEnvExtension::CC_FLAG_VARS, val
end
def append keys, value, separator = ' '
value = value.to_s
Array(keys).each do |key|
unless self[key].to_s.empty?
self[key] = self[key] + separator + value.to_s
else
self[key] = value.to_s
end
end
end
def prepend keys, value, separator = ' '
Array(keys).each do |key|
unless self[key].to_s.empty?
self[key] = value.to_s + separator + self[key]
else
self[key] = value.to_s
end
end
end
def prepend_path key, path
prepend key, path, ':' if File.directory? path
end
def remove keys, value
Array(keys).each do |key|
next unless self[key]
self[key] = self[key].sub(value, '')
delete(key) if self[key].to_s.empty?
end if value
end
def cc; self['CC'] or "cc"; end
def cxx; self['CXX'] or "c++"; end
def cflags; self['CFLAGS']; end
def cxxflags; self['CXXFLAGS']; end
def cppflags; self['CPPFLAGS']; end
def ldflags; self['LDFLAGS']; end
def fc; self['FC']; end
def fflags; self['FFLAGS']; end
def fcflags; self['FCFLAGS']; 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
def ncurses_define
append 'CPPFLAGS', "-DNCURSES_OPAQUE=0"
end
def userpaths!
paths = ORIGINAL_PATHS.map { |p| p.realpath.to_s rescue nil } - %w{/usr/X11/bin /opt/X11/bin}
self['PATH'] = paths.unshift(*self['PATH'].split(":")).uniq.join(":")
# XXX hot fix to prefer brewed stuff (e.g. python) over /usr/bin.
prepend 'PATH', HOMEBREW_PREFIX/'bin', ':'
end
def with_build_environment
old_env = to_hash
setup_build_environment
yield
ensure
replace(old_env)
end
def fortran
# superenv removes these PATHs, but this option needs them
# TODO fix better, probably by making a super-fc
self['PATH'] += ":#{HOMEBREW_PREFIX}/bin:/usr/local/bin"
if self['FC']
ohai "Building with an alternative Fortran compiler"
puts "This is unsupported."
self['F77'] ||= self['FC']
if ARGV.include? '--default-fortran-flags'
flags_to_set = []
flags_to_set << 'FCFLAGS' unless self['FCFLAGS']
flags_to_set << 'FFLAGS' unless self['FFLAGS']
flags_to_set.each {|key| self[key] = cflags}
set_cpu_flags(flags_to_set)
elsif not self['FCFLAGS'] or self['FFLAGS']
opoo <<-EOS.undent
No Fortran optimization information was provided. You may want to consider
setting FCFLAGS and FFLAGS or pass the `--default-fortran-flags` option to
`brew install` if your compiler is compatible with GCC.
If you like the default optimization level of your compiler, ignore this
warning.
EOS
end
elsif which 'gfortran'
ohai "Using Homebrew-provided fortran compiler."
puts "This may be changed by setting the FC environment variable."
self['FC'] = which 'gfortran'
self['F77'] = self['FC']
HomebrewEnvExtension::FC_FLAG_VARS.each {|key| self[key] = cflags}
set_cpu_flags(HomebrewEnvExtension::FC_FLAG_VARS)
end
end
end

View File

@ -1,4 +1,5 @@
require 'macos' require 'macos'
require 'extend/ENV/shared'
### Why `superenv`? ### Why `superenv`?
# 1) Only specify the environment we need (NO LDFLAGS for cmake) # 1) Only specify the environment we need (NO LDFLAGS for cmake)
@ -11,6 +12,8 @@ require 'macos'
# 8) Build-system agnostic configuration of the tool-chain # 8) Build-system agnostic configuration of the tool-chain
module Superenv module Superenv
include SharedEnvExtension
attr_accessor :keg_only_deps, :deps, :x11 attr_accessor :keg_only_deps, :deps, :x11
alias_method :x11?, :x11 alias_method :x11?, :x11