brew/Library/Homebrew/development_tools.rb
2021-09-29 15:12:53 -07:00

137 lines
3.6 KiB
Ruby

# typed: true
# frozen_string_literal: true
require "version"
# @private
class DevelopmentTools
class << self
extend T::Sig
sig { params(tool: String).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|
@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
sig { returns(T::Boolean) }
def installed?
locate("clang").present? || locate("gcc").present?
end
sig { returns(String) }
def installation_instructions
"Install Clang or run `brew install gcc`."
end
sig { returns(String) }
def custom_installation_instructions
installation_instructions
end
# TODO: This method appears to be unused. Can it be deleted?
sig { returns(T.nilable(String)) }
def default_cc
cc = DevelopmentTools.locate "cc"
return if cc.nil?
begin
cc.realpath.basename.to_s
rescue
nil
end
end
sig { returns(Symbol) }
def default_compiler
:clang
end
sig { returns(Version) }
def clang_version
@clang_version ||= if (path = locate("clang")) &&
(build_version = `#{path} --version`[/(?:clang|LLVM) version (\d+\.\d(?:\.\d)?)/, 1])
Version.new build_version
else
Version::NULL
end
end
sig { returns(Version) }
def clang_build_version
@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
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
sig { params(cc: String).returns(Version) }
def non_apple_gcc_version(cc)
(@non_apple_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
@non_apple_gcc_version[cc] = version
end
end
sig { void }
def clear_version_cache
@clang_version = @clang_build_version = nil
@non_apple_gcc_version = {}
end
sig { returns(T::Boolean) }
def curl_handles_most_https_certificates?
true
end
sig { returns(T::Boolean) }
def subversion_handles_most_https_certificates?
true
end
sig { returns(T::Hash[String, T.untyped]) }
def build_system_info
{
"os" => ENV["HOMEBREW_SYSTEM"],
"os_version" => OS_VERSION,
"cpu_family" => Hardware::CPU.family,
}
end
alias generic_build_system_info build_system_info
end
end
require "extend/os/development_tools"