202 lines
5.6 KiB
Ruby
Raw Normal View History

2020-11-20 14:20:38 +01:00
# typed: strict
# frozen_string_literal: true
require "hardware"
require "extend/ENV/shared"
2013-08-19 12:32:59 -05:00
module Stdenv
include SharedEnvExtension
SAFE_CFLAGS_FLAGS = "-w -pipe"
private_constant :SAFE_CFLAGS_FLAGS
2013-08-19 12:32:59 -05:00
sig {
2020-11-20 14:20:38 +01:00
params(
formula: T.nilable(Formula),
cc: T.nilable(String),
build_bottle: T.nilable(T::Boolean),
bottle_arch: T.nilable(String),
testing_formula: T::Boolean,
2022-07-26 19:36:43 +01:00
debug_symbols: T.nilable(T::Boolean),
2020-11-20 14:20:38 +01:00
).void
}
2022-07-30 11:10:26 +01:00
def setup_build_environment(formula: nil, cc: nil, build_bottle: false, bottle_arch: nil, testing_formula: false,
debug_symbols: false)
2020-11-20 14:20:38 +01:00
super
2013-08-19 12:32:59 -05:00
self["HOMEBREW_ENV"] = "std"
ORIGINAL_PATHS.reverse_each { |p| prepend_path "PATH", p }
prepend_path "PATH", HOMEBREW_SHIMS_PATH/"shared"
2013-08-19 12:32:59 -05:00
# Set the default pkg-config search path, overriding the built-in paths
# Anything in PKG_CONFIG_PATH is searched before paths in this variable
self["PKG_CONFIG_LIBDIR"] = determine_pkg_config_libdir
2013-08-19 12:32:59 -05:00
self["MAKEFLAGS"] = "-j#{make_jobs}"
2023-06-14 17:26:01 +01:00
self["RUSTFLAGS"] = Hardware.rustflags_target_cpu
2013-08-19 12:32:59 -05:00
2023-04-18 15:06:50 -07:00
if HOMEBREW_PREFIX.to_s != "/usr/local"
2013-08-19 12:32:59 -05:00
# /usr/local is already an -isystem and -L directory so we skip it
self["CPPFLAGS"] = "-isystem#{HOMEBREW_PREFIX}/include"
self["LDFLAGS"] = "-L#{HOMEBREW_PREFIX}/lib"
2013-08-19 12:32:59 -05:00
# CMake ignores the variables above
self["CMAKE_PREFIX_PATH"] = HOMEBREW_PREFIX.to_s
2013-08-19 12:32:59 -05:00
end
frameworks = HOMEBREW_PREFIX.join("Frameworks")
if frameworks.directory?
append "CPPFLAGS", "-F#{frameworks}"
append "LDFLAGS", "-F#{frameworks}"
self["CMAKE_FRAMEWORK_PATH"] = frameworks.to_s
2013-08-19 12:32:59 -05:00
end
# Os is the default Apple uses for all its stuff so let's trust them
2016-09-24 17:59:14 +02:00
define_cflags "-Os #{SAFE_CFLAGS_FLAGS}"
2013-08-19 12:32:59 -05:00
begin
send(compiler)
rescue CompilerSelectionError
# We don't care if our compiler fails to build the formula during `brew test`.
raise unless testing_formula
send(DevelopmentTools.default_compiler)
end
2020-11-20 14:20:38 +01:00
return unless cc&.match?(GNU_GCC_REGEXP)
2018-09-17 02:45:00 +02:00
2020-09-18 22:31:23 +02:00
gcc_formula = gcc_version_formula(cc)
2016-09-23 22:02:23 +02:00
append_path "PATH", gcc_formula.opt_bin.to_s
2016-07-12 11:39:39 +01:00
end
2016-09-23 18:13:48 +02:00
alias generic_setup_build_environment setup_build_environment
2020-10-20 12:03:48 +02:00
sig { returns(T.nilable(PATH)) }
2013-08-19 12:32:59 -05:00
def determine_pkg_config_libdir
2017-04-27 09:56:16 +02:00
PATH.new(
HOMEBREW_PREFIX/"lib/pkgconfig",
HOMEBREW_PREFIX/"share/pkgconfig",
homebrew_extra_pkg_config_paths,
"/usr/lib/pkgconfig",
).existing
2013-08-19 12:32:59 -05:00
end
# 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.
2020-11-20 14:20:38 +01:00
sig { params(block: T.proc.returns(T.untyped)).returns(T.untyped) }
def deparallelize(&block)
old = self["MAKEFLAGS"]
remove "MAKEFLAGS", /-j\d+/
2020-11-20 14:20:38 +01:00
if block
begin
yield
ensure
self["MAKEFLAGS"] = old
end
end
old
2013-08-19 12:32:59 -05:00
end
2013-11-12 12:00:18 -06:00
%w[O1 O0].each do |opt|
define_method opt do
send(:remove_from_cflags, /-O./)
send(:append_to_cflags, "-#{opt}")
end
end
2020-11-20 14:20:38 +01:00
sig { returns(T.any(String, Pathname)) }
def determine_cc
s = super
2020-11-20 14:20:38 +01:00
DevelopmentTools.locate(s) || Pathname(s)
end
2020-11-20 14:20:38 +01:00
private :determine_cc
2020-11-20 14:20:38 +01:00
sig { returns(Pathname) }
def determine_cxx
2020-11-20 14:20:38 +01:00
dir, base = Pathname(determine_cc).split
2017-06-01 16:06:51 +02:00
dir/base.to_s.sub("gcc", "g++").sub("clang", "clang++")
end
2020-11-20 14:20:38 +01:00
private :determine_cxx
GNU_GCC_VERSIONS.each do |n|
define_method(:"gcc-#{n}") do
super()
2020-11-20 14:20:38 +01:00
send(:set_cpu_cflags)
end
end
2020-11-20 14:20:38 +01:00
sig { void }
2013-08-19 12:32:59 -05:00
def clang
super()
2013-08-19 12:32:59 -05:00
replace_in_cflags(/-Xarch_#{Hardware::CPU.arch_32_bit} (-march=\S*)/, '\1')
map = Hardware::CPU.optimization_flags.dup
if DevelopmentTools.clang_build_version < 700
# Clang mistakenly enables AES-NI on plain Nehalem
map[:nehalem] = "-march=nehalem -Xclang -target-feature -Xclang -aes"
end
set_cpu_cflags(map)
2013-08-19 12:32:59 -05:00
end
2020-11-20 14:20:38 +01:00
sig { void }
def cxx11
append "CXX", "-std=c++11"
libcxx
end
2020-11-20 14:20:38 +01:00
sig { void }
def libcxx
2016-09-23 11:01:40 +02:00
append "CXX", "-stdlib=libc++" if compiler == :clang
end
sig { returns(Integer) }
def make_jobs
Homebrew::EnvConfig.make_jobs.to_i
end
# This method does nothing in {Stdenv} since there is no argument refurbishment.
sig { void }
def refurbish_args; end
private
2020-11-20 14:20:38 +01:00
sig { params(before: Regexp, after: String).void }
def replace_in_cflags(before, after)
2013-08-19 12:32:59 -05:00
CC_FLAG_VARS.each do |key|
2020-11-20 14:20:38 +01:00
self[key] = fetch(key).sub(before, after) if key?(key)
2013-08-19 12:32:59 -05:00
end
end
# Convenience method to set all C compiler flags in one shot.
2020-11-20 14:20:38 +01:00
sig { params(val: String).void }
2016-09-24 17:59:14 +02:00
def define_cflags(val)
2013-08-19 12:32:59 -05:00
CC_FLAG_VARS.each { |key| self[key] = val }
end
# Sets architecture-specific flags for every environment variable
# given in the list `flags`.
2020-11-20 14:20:38 +01:00
sig { params(flags: T::Array[String], map: T::Hash[Symbol, String]).void }
2020-04-05 15:44:50 +01:00
def set_cpu_flags(flags, map = Hardware::CPU.optimization_flags)
cflags =~ /(-Xarch_#{Hardware::CPU.arch_32_bit} )-march=/
xarch = Regexp.last_match(1).to_s
remove flags, /(-Xarch_#{Hardware::CPU.arch_32_bit} )?-march=\S*/
remove flags, /( -Xclang \S+)+/
remove flags, /-mssse3/
remove flags, /-msse4(\.\d)?/
2013-08-19 12:32:59 -05:00
append flags, xarch unless xarch.empty?
append flags, map.fetch(effective_arch)
end
2013-08-19 12:32:59 -05:00
sig { returns(T::Array[Pathname]) }
def homebrew_extra_pkg_config_paths
[]
end
2020-11-20 14:20:38 +01:00
sig { params(map: T::Hash[Symbol, String]).void }
2023-01-02 19:18:51 +00:00
def set_cpu_cflags(map = Hardware::CPU.optimization_flags)
set_cpu_flags(CC_FLAG_VARS, map)
2013-08-19 12:32:59 -05:00
end
end
2016-07-12 11:39:39 +01:00
require "extend/os/extend/ENV/std"