brew/Library/Homebrew/development_tools.rb

166 lines
4.7 KiB
Ruby
Raw Normal View History

2021-09-29 15:12:53 -07:00
# typed: true
# frozen_string_literal: true
2021-09-29 15:12:53 -07:00
require "version"
# @private
class DevelopmentTools
class << self
sig { params(tool: T.any(String, Symbol)).returns(T.nilable(Pathname)) }
def locate(tool)
# Don't call tools (cc, make, strip, etc.) directly!
# Give the name of the binary you look for as a string to this method
# in order to get the full path back as a Pathname.
(@locate ||= {}).fetch(tool) do |key|
2024-04-08 19:23:33 +01:00
@locate[key] = if File.executable?((path = "/usr/bin/#{tool}"))
Pathname.new path
# Homebrew GCCs most frequently; much faster to check this before xcrun
elsif (path = HOMEBREW_PREFIX/"bin/#{tool}").executable?
path
end
end
end
2021-09-29 15:12:53 -07:00
sig { returns(T::Boolean) }
def installed?
2021-09-29 15:12:53 -07:00
locate("clang").present? || locate("gcc").present?
end
2021-09-29 15:12:53 -07:00
sig { returns(String) }
def installation_instructions
2019-04-05 12:24:10 -04:00
"Install Clang or run `brew install gcc`."
end
2021-09-29 15:12:53 -07:00
sig { returns(String) }
def custom_installation_instructions
installation_instructions
end
sig { params(resource: String).returns(String) }
def insecure_download_warning(resource)
package = curl_handles_most_https_certificates? ? "ca-certificates" : "curl"
"Using `--insecure` with curl to download #{resource} because we need it to run " \
"`brew install #{package}` in order to download securely from now on. " \
"Checksums will still be verified."
end
2021-09-29 15:12:53 -07:00
sig { returns(Symbol) }
def default_compiler
:clang
end
2021-09-29 15:12:53 -07:00
sig { returns(Version) }
def clang_version
2021-03-26 14:11:03 +00:00
@clang_version ||= if (path = locate("clang")) &&
2021-05-09 17:07:00 +01:00
(build_version = `#{path} --version`[/(?:clang|LLVM) version (\d+\.\d(?:\.\d)?)/, 1])
2021-03-26 14:11:03 +00:00
Version.new build_version
else
Version::NULL
end
end
2021-09-29 15:12:53 -07:00
sig { returns(Version) }
def clang_build_version
2021-03-26 14:11:03 +00:00
@clang_build_version ||= if (path = locate("clang")) &&
(build_version = `#{path} --version`[
%r{clang(-| version [^ ]+ \(tags/RELEASE_)(\d{2,})}, 2])
Version.new build_version
else
Version::NULL
end
end
2021-09-29 15:12:53 -07:00
sig { returns(Version) }
def llvm_clang_build_version
@llvm_clang_build_version ||= begin
path = Formulary.factory("llvm").opt_prefix/"bin/clang"
if path.executable? &&
(build_version = `#{path} --version`[/clang version (\d+\.\d\.\d)/, 1])
Version.new build_version
else
Version::NULL
end
end
end
2024-04-22 21:05:48 +02:00
# Get the GCC version.
#
# @api internal
2021-09-29 15:12:53 -07:00
sig { params(cc: String).returns(Version) }
def gcc_version(cc)
(@gcc_version ||= {}).fetch(cc) do
path = HOMEBREW_PREFIX/"opt/#{CompilerSelector.preferred_gcc}/bin"/cc
path = locate(cc) unless path.exist?
version = if path &&
(build_version = `#{path} --version`[/gcc(?:(?:-\d+(?:\.\d)?)? \(.+\))? (\d+\.\d\.\d)/, 1])
Version.new build_version
else
Version::NULL
end
@gcc_version[cc] = version
end
end
2021-09-29 15:12:53 -07:00
sig { void }
def clear_version_cache
@clang_version = @clang_build_version = nil
@gcc_version = {}
end
sig { returns(T::Boolean) }
2022-11-09 14:45:43 +00:00
def needs_build_formulae?
needs_libc_formula? || needs_compiler_formula?
end
sig { returns(T::Boolean) }
def needs_libc_formula?
false
end
sig { returns(T::Boolean) }
2022-11-09 14:45:43 +00:00
def needs_compiler_formula?
false
end
sig { returns(T::Boolean) }
def ca_file_handles_most_https_certificates?
# The system CA file is too old for some modern HTTPS certificates on
# older OS versions.
ENV["HOMEBREW_SYSTEM_CA_CERTIFICATES_TOO_OLD"].nil?
end
2021-09-29 15:12:53 -07:00
sig { returns(T::Boolean) }
def curl_handles_most_https_certificates?
true
end
sig { returns(T::Boolean) }
def ca_file_substitution_required?
(!ca_file_handles_most_https_certificates? || ENV["HOMEBREW_FORCE_BREWED_CA_CERTIFICATES"].present?) &&
!(HOMEBREW_PREFIX/"etc/ca-certificates/cert.pem").exist?
end
sig { returns(T::Boolean) }
def curl_substitution_required?
!curl_handles_most_https_certificates? && !HOMEBREW_BREWED_CURL_PATH.exist?
end
2021-09-29 15:12:53 -07:00
sig { returns(T::Boolean) }
def subversion_handles_most_https_certificates?
true
end
2020-04-06 13:04:48 +01:00
sig { returns(T::Hash[String, T.nilable(String)]) }
2020-04-06 13:04:48 +01:00
def build_system_info
{
"os" => HOMEBREW_SYSTEM,
2020-04-06 13:04:48 +01:00
"os_version" => OS_VERSION,
"cpu_family" => Hardware::CPU.family.to_s,
2020-04-06 13:04:48 +01:00
}
end
alias generic_build_system_info build_system_info
end
end
require "extend/os/development_tools"