302 lines
7.3 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: false
# frozen_string_literal: true
require "extend/ENV/shared"
require "development_tools"
# ### 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 (fewer 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 formulae that *just work*
# 8. Build-system agnostic configuration of the toolchain
module Superenv
include SharedEnvExtension
# @private
2020-07-07 11:29:33 +01:00
attr_accessor :keg_only_deps, :deps, :run_time_deps, :x11
2013-08-19 12:32:56 -05:00
def self.extended(base)
base.keg_only_deps = []
base.deps = []
2018-05-16 11:34:12 -07:00
base.run_time_deps = []
2013-08-19 12:32:56 -05:00
end
# @private
def self.bin; end
2013-08-19 12:32:56 -05:00
def reset
super
# 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
# @private
def setup_build_environment(**options)
super(**options)
send(compiler)
self["HOMEBREW_ENV"] = "super"
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.to_s
self["HOMEBREW_PREFIX"] = HOMEBREW_PREFIX.to_s
self["HOMEBREW_CELLAR"] = HOMEBREW_CELLAR.to_s
self["HOMEBREW_OPT"] = "#{HOMEBREW_PREFIX}/opt"
self["HOMEBREW_TEMP"] = HOMEBREW_TEMP.to_s
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"] = DevelopmentTools.locate("m4") if deps.any? { |d| d.name == "autoconf" }
self["HOMEBREW_ISYSTEM_PATHS"] = determine_isystem_paths
self["HOMEBREW_INCLUDE_PATHS"] = determine_include_paths
self["HOMEBREW_LIBRARY_PATHS"] = determine_library_paths
self["HOMEBREW_DEPENDENCIES"] = determine_dependencies
self["HOMEBREW_FORMULA_PREFIX"] = @formula.prefix unless @formula.nil?
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.
#
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.
# K - Don't strip -arch <arch>, -m32, or -m64
2016-08-17 22:26:42 -07:00
# w - Pass -no_weak_imports to the linker
2013-08-01 11:36:44 -07:00
#
# These flags will also be present:
2013-08-01 11:36:44 -07:00
# s - apply fix for sed's Unicode support
# a - apply fix for apr-1-config path
end
2016-09-23 18:13:48 +02:00
alias generic_setup_build_environment setup_build_environment
private
def cc=(val)
self["HOMEBREW_CC"] = super
end
def cxx=(val)
self["HOMEBREW_CXX"] = super
end
def determine_cxx
determine_cc.to_s.gsub("gcc", "g++").gsub("clang", "clang++")
end
2016-07-12 12:01:20 +01:00
def homebrew_extra_paths
[]
end
def determine_path
2017-04-27 09:56:16 +02:00
path = PATH.new(Superenv.bin)
# Formula dependencies can override standard tools.
2017-04-27 10:44:44 +02:00
path.append(deps.map(&:opt_bin))
2017-04-27 09:56:16 +02:00
path.append(homebrew_extra_paths)
path.append("/usr/bin", "/bin", "/usr/sbin", "/sbin")
begin
2020-09-18 22:31:23 +02:00
path.append(gcc_version_formula(homebrew_cc).opt_bin) if homebrew_cc.match?(GNU_GCC_REGEXP)
rescue FormulaUnavailableError
# Don't fail and don't add these formulae to the path if they don't exist.
nil
end
path.existing
end
2016-07-12 12:01:20 +01:00
def homebrew_extra_pkg_config_paths
[]
end
def determine_pkg_config_path
2017-04-27 09:56:16 +02:00
PATH.new(
deps.map { |d| d.opt_lib/"pkgconfig" },
deps.map { |d| d.opt_share/"pkgconfig" },
).existing
end
def determine_pkg_config_libdir
2017-04-27 09:56:16 +02:00
PATH.new(
homebrew_extra_pkg_config_paths,
).existing
end
2016-07-12 12:01:20 +01:00
def homebrew_extra_aclocal_paths
[]
end
def determine_aclocal_path
2017-04-27 09:56:16 +02:00
PATH.new(
keg_only_deps.map { |d| d.opt_share/"aclocal" },
HOMEBREW_PREFIX/"share/aclocal",
homebrew_extra_aclocal_paths,
).existing
end
2016-07-12 12:01:20 +01:00
def homebrew_extra_isystem_paths
[]
end
def determine_isystem_paths
2017-04-27 09:56:16 +02:00
PATH.new(
HOMEBREW_PREFIX/"include",
homebrew_extra_isystem_paths,
).existing
end
def determine_include_paths
PATH.new(keg_only_deps.map(&:opt_include)).existing
end
2016-07-12 12:01:20 +01:00
def homebrew_extra_library_paths
[]
end
def determine_library_paths
2018-03-06 08:44:47 +00:00
paths = [
keg_only_deps.map(&:opt_lib),
HOMEBREW_PREFIX/"lib",
]
2018-03-07 15:00:08 +00:00
paths += homebrew_extra_library_paths
2018-03-06 08:44:47 +00:00
PATH.new(paths).existing
end
def determine_dependencies
deps.map(&:name).join(",")
end
def determine_cmake_prefix_path
2017-04-27 09:56:16 +02:00
PATH.new(
keg_only_deps.map(&:opt_prefix),
HOMEBREW_PREFIX.to_s,
).existing
end
2016-07-12 12:01:20 +01:00
def homebrew_extra_cmake_include_paths
[]
end
def determine_cmake_include_path
PATH.new(homebrew_extra_cmake_include_paths).existing
end
2016-07-12 12:01:20 +01:00
def homebrew_extra_cmake_library_paths
[]
end
def determine_cmake_library_path
PATH.new(homebrew_extra_cmake_library_paths).existing
end
2016-07-12 12:01:20 +01:00
def homebrew_extra_cmake_frameworks_paths
[]
end
def determine_cmake_frameworks_path
2017-04-27 09:56:16 +02:00
PATH.new(
deps.map(&:opt_frameworks),
homebrew_extra_cmake_frameworks_paths,
).existing
end
2012-08-23 16:15:26 -04:00
def determine_make_jobs
2020-04-05 15:44:50 +01:00
Homebrew::EnvConfig.make_jobs
2012-08-23 16:15:26 -04:00
end
def determine_optflags
Hardware::CPU.optimization_flags.fetch(effective_arch)
end
def determine_cccfg
2016-07-12 12:01:20 +01:00
""
end
public
# 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.
def deparallelize
old = delete("MAKEFLAGS")
if block_given?
begin
yield
ensure
self["MAKEFLAGS"] = old
end
end
old
end
2013-11-11 18:33:47 -06:00
def make_jobs
self["MAKEFLAGS"] =~ /-\w*j(\d+)/
[Regexp.last_match(1).to_i, 1].max
end
2013-11-11 18:33:47 -06:00
def universal_binary
check_for_compiler_universal_support
self["HOMEBREW_ARCHFLAGS"] = Hardware::CPU.universal_archs.as_arch_flags
2013-11-11 18:33:47 -06:00
end
def permit_arch_flags
append_to_cccfg "K"
end
2014-05-22 09:18:34 -05:00
def m32
append "HOMEBREW_ARCHFLAGS", "-m32"
end
2014-05-22 09:18:34 -05:00
def m64
append "HOMEBREW_ARCHFLAGS", "-m64"
end
2013-11-11 18:33:47 -06:00
def cxx11
append_to_cccfg "x"
append_to_cccfg "g" if homebrew_cc == "clang"
2013-11-11 18:33:47 -06:00
end
def libcxx
append_to_cccfg "g" if compiler == :clang
2013-11-11 18:33:47 -06:00
end
def libstdcxx
append_to_cccfg "h" if compiler == :clang
2013-11-11 18:33:47 -06:00
end
# @private
def refurbish_args
append_to_cccfg "O"
end
%w[O3 O2 O1 O0 Os].each do |opt|
define_method opt do
self["HOMEBREW_OPTIMIZATION_LEVEL"] = opt
end
end
def set_x11_env_if_installed; end
def set_cpu_flags(_arg0, _arg1 = "", _arg2 = {}); end
end
2016-07-12 12:01:20 +01:00
require "extend/os/extend/ENV/super"