215 lines
6.4 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: false
# frozen_string_literal: true
require "os/mac/version"
require "os/mac/xcode"
require "os/mac/sdk"
require "os/mac/keg"
2013-10-18 12:56:51 -05:00
module OS
2020-08-26 03:06:19 +02:00
# Helper module for querying system information on macOS.
2013-10-18 12:56:51 -05:00
module Mac
2020-10-20 12:03:48 +02:00
extend T::Sig
2016-09-24 20:11:54 +02:00
module_function
2013-10-18 12:56:51 -05:00
2022-06-29 11:29:46 -04:00
::MacOS = OS::Mac
raise "Loaded OS::Mac on generic OS!" if ENV["HOMEBREW_TEST_GENERIC_OS"]
VERSION = ENV.fetch("HOMEBREW_MACOS_VERSION").chomp.freeze
private_constant :VERSION
2013-10-18 12:56:51 -05:00
# This can be compared to numerics, strings, or symbols
# using the standard Ruby Comparable methods.
sig { returns(Version) }
2013-10-18 12:56:51 -05:00
def version
@version ||= full_version.strip_patch
end
# This can be compared to numerics, strings, or symbols
# using the standard Ruby Comparable methods.
sig { returns(Version) }
def full_version
@full_version ||= if ENV["HOMEBREW_FAKE_EL_CAPITAN"] # for Portable Ruby building
Version.new("10.11.6")
else
Version.new(VERSION)
end
2013-10-18 12:56:51 -05:00
end
sig { params(version: Version).void }
2017-01-20 09:00:53 +01:00
def full_version=(version)
@full_version = Version.new(version.chomp)
@version = nil
end
sig { returns(::Version) }
def latest_sdk_version
# TODO: bump version when new Xcode macOS SDK is released
# NOTE: We only track the major version of the SDK.
2022-10-20 20:04:42 +01:00
::Version.new("13")
end
private :latest_sdk_version
sig { returns(String) }
def preferred_perl_version
if version >= :big_sur
"5.30"
else
"5.18"
end
end
def languages
return @languages if @languages
os_langs = Utils.popen_read("defaults", "read", "-g", "AppleLanguages")
if os_langs.blank?
# User settings don't exist so check the system-wide one.
os_langs = Utils.popen_read("defaults", "read", "/Library/Preferences/.GlobalPreferences", "AppleLanguages")
end
os_langs = os_langs.scan(/[^ \n"(),]+/)
@languages = os_langs
end
2016-09-09 00:11:43 +02:00
def language
languages.first
2016-09-09 00:11:43 +02:00
end
sig { returns(String) }
def active_developer_dir
@active_developer_dir ||= Utils.popen_read("/usr/bin/xcode-select", "-print-path").strip
end
2020-10-20 12:03:48 +02:00
sig { returns(T::Boolean) }
def sdk_root_needed?
if MacOS::CLT.installed?
# If there's no CLT SDK, return false
return false unless MacOS::CLT.provides_sdk?
# If the CLT is installed and headers are provided by the system, return false
return false unless MacOS::CLT.separate_header_package?
end
true
end
# If a specific SDK is requested:
#
# 1. The requested SDK is returned, if it's installed.
# 2. If the requested SDK is not installed, the newest SDK (if any SDKs
# are available) is returned.
# 3. If no SDKs are available, nil is returned.
#
# If no specific SDK is requested, the SDK matching the OS version is returned,
# if available. Otherwise, the latest SDK is returned.
2020-07-01 16:02:29 +01:00
def sdk_locator
if CLT.installed? && CLT.provides_sdk?
CLT.sdk_locator
2020-03-08 20:09:06 +00:00
else
2020-07-01 16:02:29 +01:00
Xcode.sdk_locator
2018-06-12 14:55:31 -07:00
end
2020-07-01 16:02:29 +01:00
end
2018-06-12 14:55:31 -07:00
def sdk(version = nil)
sdk_locator.sdk_if_applicable(version)
2013-10-18 12:56:51 -05:00
end
def sdk_for_formula(f, version = nil, check_only_runtime_requirements: false)
# If the formula requires Xcode, don't return the CLT SDK
# If check_only_runtime_requirements is true, don't necessarily return the
2021-05-08 11:20:01 +10:00
# Xcode SDK if the XcodeRequirement is only a build or test requirement.
return Xcode.sdk if f.requirements.any? do |req|
next false unless req.is_a? XcodeRequirement
next false if check_only_runtime_requirements && req.build? && !req.test?
true
end
sdk(version)
end
# Returns the path to an SDK or nil, following the rules set by {sdk}.
def sdk_path(version = nil)
s = sdk(version)
2017-09-24 19:24:46 +01:00
s&.path
end
def sdk_path_if_needed(version = nil)
2020-03-08 20:09:06 +00:00
# Prefer CLT SDK when both Xcode and the CLT are installed.
2018-08-07 17:56:42 -07:00
# Expected results:
# 1. On Xcode-only systems, return the Xcode SDK.
# 2. On Xcode-and-CLT systems where headers are provided by the system, return nil.
# 3. On CLT-only systems with no CLT SDK, return nil.
# 4. On CLT-only systems with a CLT SDK, where headers are provided by the system, return nil.
# 5. On CLT-only systems with a CLT SDK, where headers are not provided by the system, return the CLT SDK.
return unless sdk_root_needed?
2018-08-07 17:56:42 -07:00
sdk_path(version)
2018-07-24 09:32:55 -07:00
end
2013-10-18 12:56:51 -05:00
# See these issues for some history:
#
2020-11-03 12:39:26 -05:00
# - {https://github.com/Homebrew/legacy-homebrew/issues/13}
# - {https://github.com/Homebrew/legacy-homebrew/issues/41}
# - {https://github.com/Homebrew/legacy-homebrew/issues/48}
2013-10-18 12:56:51 -05:00
def macports_or_fink
paths = []
# First look in the path because MacPorts is relocatable and Fink
# may become relocatable in the future.
%w[port fink].each do |ponk|
2013-10-18 12:56:51 -05:00
path = which(ponk)
paths << path unless path.nil?
end
# Look in the standard locations, because even if port or fink are
# not in the path they can still break builds if the build scripts
# have these paths baked in.
%w[/sw/bin/fink /opt/local/bin/port].each do |ponk|
2013-10-18 12:56:51 -05:00
path = Pathname.new(ponk)
paths << path if path.exist?
end
2017-09-10 16:31:56 +00:00
# Finally, some users make their MacPorts or Fink directories
2017-09-10 16:32:09 +00:00
# read-only in order to try out Homebrew, but this doesn't work as
2013-10-18 12:56:51 -05:00
# some build scripts error out when trying to read from these now
# unreadable paths.
%w[/sw /opt/local].map { |p| Pathname.new(p) }.each do |path|
2013-10-18 12:56:51 -05:00
paths << path if path.exist? && !path.readable?
end
paths.uniq
end
2021-09-29 15:12:53 -07:00
sig { params(ids: String).returns(T.nilable(Pathname)) }
2014-04-01 20:47:26 -05:00
def app_with_bundle_id(*ids)
path = mdfind(*ids)
.reject { |p| p.include?("/Backups.backupdb/") }
.first
2020-12-01 17:04:59 +00:00
Pathname.new(path) if path.present?
2013-10-18 12:56:51 -05:00
end
2021-09-29 15:12:53 -07:00
sig { params(ids: String).returns(T::Array[String]) }
2014-04-01 20:47:26 -05:00
def mdfind(*ids)
(@mdfind ||= {}).fetch(ids) do
@mdfind[ids] = Utils.popen_read("/usr/bin/mdfind", mdfind_query(*ids)).split("\n")
2013-10-18 12:56:51 -05:00
end
end
2014-04-01 20:47:26 -05:00
def pkgutil_info(id)
(@pkginfo ||= {}).fetch(id) do |key|
@pkginfo[key] = Utils.popen_read("/usr/sbin/pkgutil", "--pkg-info", key).strip
2013-10-18 12:56:51 -05:00
end
end
2014-04-01 20:47:26 -05:00
2021-09-29 15:12:53 -07:00
sig { params(ids: String).returns(String) }
2014-04-01 20:47:26 -05:00
def mdfind_query(*ids)
ids.map! { |id| "kMDItemCFBundleIdentifier == #{id}" }.join(" || ")
end
2013-10-18 12:56:51 -05:00
end
end