354 lines
8.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "compilers"
require "development_tools"
2013-11-27 17:29:06 -06:00
# Homebrew extends Ruby's `ENV` to make our code more readable.
# Implemented in {SharedEnvExtension} and either {Superenv} or
# {Stdenv} (depending on the build mode).
# @see Superenv
# @see Stdenv
2018-10-03 21:03:22 +00:00
# @see https://www.rubydoc.info/stdlib/Env Ruby's ENV API
2013-08-19 12:32:59 -05:00
module SharedEnvExtension
include CompilerConstants
# @private
CC_FLAG_VARS = %w[CFLAGS CXXFLAGS OBJCFLAGS OBJCXXFLAGS].freeze
# @private
FC_FLAG_VARS = %w[FCFLAGS FFLAGS].freeze
# @private
SANITIZED_VARS = %w[
CDPATH CLICOLOR_FORCE
CPATH C_INCLUDE_PATH CPLUS_INCLUDE_PATH OBJC_INCLUDE_PATH
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
GOBIN GOPATH GOROOT PERL_MB_OPT PERL_MM_OPT
LIBRARY_PATH LD_LIBRARY_PATH LD_PRELOAD LD_RUN_PATH
].freeze
# @private
def setup_build_environment(formula = nil)
@formula = formula
reset
end
# @private
def reset
SANITIZED_VARS.each { |k| delete(k) }
end
2013-08-19 12:32:59 -05:00
def remove_cc_etc
keys = %w[CC CXX OBJC OBJCXX LD CPP CFLAGS CXXFLAGS OBJCFLAGS OBJCXXFLAGS LDFLAGS CPPFLAGS]
2015-08-06 17:12:35 +08:00
removed = Hash[*keys.flat_map { |key| [key, self[key]] }]
2013-08-19 12:32:59 -05:00
keys.each do |key|
delete(key)
end
removed
end
def append_to_cflags(newflags)
2013-08-19 12:32:59 -05:00
append(CC_FLAG_VARS, newflags)
end
def remove_from_cflags(val)
2013-08-19 12:32:59 -05:00
remove CC_FLAG_VARS, val
end
def append_to_cccfg(value)
2018-07-12 19:59:06 +01:00
append("HOMEBREW_CCCFG", value, "")
end
def append(keys, value, separator = " ")
2013-08-19 12:32:59 -05:00
value = value.to_s
Array(keys).each do |key|
old = self[key]
if old.nil? || old.empty?
self[key] = value
else
self[key] += separator + value
2013-08-19 12:32:59 -05:00
end
end
end
def prepend(keys, value, separator = " ")
value = value.to_s
2013-08-19 12:32:59 -05:00
Array(keys).each do |key|
old = self[key]
if old.nil? || old.empty?
self[key] = value
else
self[key] = value + separator + old
2013-08-19 12:32:59 -05:00
end
end
end
2013-08-19 17:21:13 -05:00
def append_path(key, path)
2017-04-27 10:44:44 +02:00
self[key] = PATH.new(self[key]).append(path)
2013-08-19 17:21:13 -05:00
end
# Prepends a directory to `PATH`.
# Is the formula struggling to find the pkgconfig file? Point it to it.
# This is done automatically for keg-only formulae.
# <pre>ENV.prepend_path "PKG_CONFIG_PATH", "#{Formula["glib"].opt_lib}/pkgconfig"</pre>
# Prepending a system path such as /usr/bin is a no-op so that requirements
# don't accidentally override superenv shims or formulae's `bin` directories, e.g.
# <pre>ENV.prepend_path "PATH", which("emacs").dirname</pre>
def prepend_path(key, path)
return if %w[/usr/bin /bin /usr/sbin /sbin].include? path.to_s
2018-09-17 02:45:00 +02:00
2017-04-27 10:44:44 +02:00
self[key] = PATH.new(self[key]).prepend(path)
2013-08-19 12:32:59 -05:00
end
2013-08-19 17:21:13 -05:00
def prepend_create_path(key, path)
path = Pathname.new(path) unless path.is_a? Pathname
path.mkpath
prepend_path key, path
end
def remove(keys, value)
return if value.nil?
2018-09-17 02:45:00 +02:00
2013-08-19 12:32:59 -05:00
Array(keys).each do |key|
next unless self[key]
2018-09-17 02:45:00 +02:00
self[key] = self[key].sub(value, "")
2014-07-18 11:43:37 -05:00
delete(key) if self[key].empty?
end
2013-08-19 12:32:59 -05:00
end
def cc
self["CC"]
end
def cxx
self["CXX"]
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
2013-08-19 12:32:59 -05:00
# Outputs the current compiler.
# @return [Symbol]
# <pre># Do something only for the system clang
# if ENV.compiler == :clang
# # modify CFLAGS CXXFLAGS OBJCFLAGS OBJCXXFLAGS in one go:
# ENV.append_to_cflags "-I ./missing/includes"
# end</pre>
def compiler
@compiler ||= if (cc = ARGV.cc)
warn_about_non_apple_gcc($&) if cc =~ GNU_GCC_REGEXP
fetch_compiler(cc, "--cc")
elsif (cc = homebrew_cc)
warn_about_non_apple_gcc($&) if cc =~ GNU_GCC_REGEXP
compiler = fetch_compiler(cc, "HOMEBREW_CC")
if @formula
compilers = [compiler] + CompilerSelector.compilers
compiler = CompilerSelector.select_for(@formula, compilers)
end
compiler
elsif @formula
CompilerSelector.select_for(@formula)
else
DevelopmentTools.default_compiler
end
end
# @private
def determine_cc
COMPILER_SYMBOL_MAP.invert.fetch(compiler, compiler)
end
2014-06-22 18:43:01 -05:00
COMPILERS.each do |compiler|
define_method(compiler) do
@compiler = compiler
self.cc = determine_cc
self.cxx = determine_cxx
end
end
# Snow Leopard defines an NCURSES value the opposite of most distros.
# @see https://bugs.python.org/issue6848
# Currently only used by aalib in core.
2013-08-19 12:32:59 -05:00
def ncurses_define
append "CPPFLAGS", "-DNCURSES_OPAQUE=0"
2013-08-19 12:32:59 -05:00
end
# @private
2013-08-19 12:32:59 -05:00
def userpaths!
2017-04-28 15:07:46 +02:00
path = PATH.new(self["PATH"]).select do |p|
# put Superenv.bin and opt path at the first
p.start_with?("#{HOMEBREW_REPOSITORY}/Library/ENV", "#{HOMEBREW_PREFIX}/opt")
end
path.append(HOMEBREW_PREFIX/"bin") # XXX hot fix to prefer brewed stuff (e.g. python) over /usr/bin.
path.append(self["PATH"]) # reset of self["PATH"]
path.append(
# user paths
ORIGINAL_PATHS.map do |p|
begin
p.realpath.to_s
rescue
nil
end
end - %w[/usr/X11/bin /opt/X11/bin],
)
self["PATH"] = path
2013-08-19 12:32:59 -05:00
end
def fortran
# Ignore repeated calls to this function as it will misleadingly warn about
# building with an alternative Fortran compiler without optimization flags,
# despite it often being the Homebrew-provided one set up in the first call.
return if @fortran_setup_done
2018-09-17 02:45:00 +02:00
@fortran_setup_done = true
flags = []
2013-08-30 19:00:19 -05:00
if fc
2013-08-19 12:32:59 -05:00
ohai "Building with an alternative Fortran compiler"
puts "This is unsupported."
self["F77"] ||= fc
2013-08-19 12:32:59 -05:00
if ARGV.include? "--default-fortran-flags"
flags = FC_FLAG_VARS.reject { |key| self[key] }
2013-08-30 18:59:58 -05:00
elsif values_at(*FC_FLAG_VARS).compact.empty?
2017-10-15 02:28:32 +02:00
opoo <<~EOS
2013-08-19 12:32:59 -05:00
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
else
if (gfortran = which("gfortran", (HOMEBREW_PREFIX/"bin").to_s))
2019-04-08 12:47:15 -04:00
ohai "Using Homebrew-provided Fortran compiler."
2017-04-27 10:44:44 +02:00
elsif (gfortran = which("gfortran", PATH.new(ORIGINAL_PATHS)))
2019-04-08 12:47:15 -04:00
ohai "Using a Fortran compiler found at #{gfortran}."
end
if gfortran
puts "This may be changed by setting the FC environment variable."
self["FC"] = self["F77"] = gfortran
flags = FC_FLAG_VARS
end
2013-08-19 12:32:59 -05:00
end
flags.each { |key| self[key] = cflags }
set_cpu_flags(flags)
2013-08-19 12:32:59 -05:00
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]
gcc_version_name = "gcc@#{version}"
gcc = Formulary.factory("gcc")
if gcc.version_suffix == version
gcc
else
2015-06-21 21:18:24 -04:00
Formulary.factory(gcc_version_name)
end
end
# @private
def warn_about_non_apple_gcc(name)
begin
gcc_formula = gcc_version_formula(name)
2015-06-22 21:08:27 -04:00
rescue FormulaUnavailableError => e
2017-10-15 02:28:32 +02:00
raise <<~EOS
Homebrew GCC requested, but formula #{e.name} not found!
2015-06-21 21:18:23 -04:00
EOS
end
2016-09-23 22:02:23 +02:00
return if gcc_formula.opt_prefix.exist?
2018-09-17 02:45:00 +02:00
2017-10-15 02:28:32 +02:00
raise <<~EOS
The requested Homebrew GCC was not installed. You must:
brew install #{gcc_formula.full_name}
2016-09-23 22:02:23 +02:00
EOS
end
def permit_arch_flags; end
2014-05-18 14:34:31 -05:00
# A no-op until we enable this by default again (which we may never do).
def permit_weak_imports; end
# @private
def compiler_any_clang?(cc = compiler)
%w[clang llvm_clang].include?(cc.to_s)
end
2014-05-18 14:34:31 -05:00
private
def cc=(val)
2014-05-18 14:34:31 -05:00
self["CC"] = self["OBJC"] = val.to_s
end
def cxx=(val)
2014-05-18 14:34:31 -05:00
self["CXX"] = self["OBJCXX"] = val.to_s
end
2014-05-18 14:34:31 -05:00
def homebrew_cc
self["HOMEBREW_CC"]
end
def fetch_compiler(value, source)
COMPILER_SYMBOL_MAP.fetch(value) do |other|
case other
when GNU_GCC_REGEXP
other
else
raise "Invalid value for #{source}: #{other}"
end
end
end
def check_for_compiler_universal_support
return unless GNU_GCC_REGEXP.match?(homebrew_cc)
2018-09-17 02:45:00 +02:00
2016-09-23 22:02:23 +02:00
raise "Non-Apple GCC can't build universal binaries"
end
2013-08-19 12:32:59 -05:00
end
2016-08-16 13:47:22 +01:00
require "extend/os/extend/ENV/shared"