184 lines
4.8 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: true
# frozen_string_literal: true
require "os/mac/version"
module OS
module Mac
2020-08-25 00:33:34 +02:00
# Class representing a macOS SDK.
#
# @api private
class SDK
VERSIONED_SDK_REGEX = /MacOSX(\d+\.\d+)\.sdk$/.freeze
2020-04-07 16:43:32 +01:00
attr_reader :version, :path, :source
2020-04-07 16:43:32 +01:00
def initialize(version, path, source)
2020-10-05 14:46:43 +02:00
@version = version
@path = Pathname.new(path)
2020-04-07 16:43:32 +01:00
@source = source
end
end
2020-08-25 00:33:34 +02:00
# Base class for SDK locators.
#
# @api private
2018-06-12 14:55:31 -07:00
class BaseSDKLocator
class NoSDKError < StandardError; end
def sdk_for(v)
sdk = all_sdks.find { |s| s.version == v }
raise NoSDKError if sdk.nil?
sdk
end
2020-07-01 16:02:29 +01:00
def all_sdks
return @all_sdks if @all_sdks
@all_sdks = []
# Bail out if there is no SDK prefix at all
return @all_sdks unless File.directory? sdk_prefix
# Use unversioned SDK path on Big Sur to avoid issues such as:
# https://github.com/Homebrew/homebrew-core/issues/67075
unversioned_sdk_path = Pathname.new("#{sdk_prefix}/MacOSX.sdk")
version = read_sdk_version(unversioned_sdk_path)
if version && version >= :big_sur
unversioned_sdk_version = version
@all_sdks << SDK.new(unversioned_sdk_version, unversioned_sdk_path, source)
end
Dir["#{sdk_prefix}/MacOSX*.sdk"].each do |sdk_path|
next unless sdk_path.match?(SDK::VERSIONED_SDK_REGEX)
version = read_sdk_version(Pathname.new(sdk_path))
next if version.nil? || version == unversioned_sdk_version
@all_sdks << SDK.new(version, sdk_path, source)
end
@all_sdks
2020-07-01 16:02:29 +01:00
end
def sdk_if_applicable(v = nil)
2018-07-27 15:44:22 -07:00
sdk = begin
if v.blank?
2020-12-26 01:06:12 +00:00
sdk_for OS::Mac.version
else
sdk_for v
end
2020-08-25 00:33:34 +02:00
rescue NoSDKError
2018-07-27 15:44:22 -07:00
latest_sdk
end
2020-11-30 12:58:38 +00:00
return if sdk.blank?
2020-12-26 01:06:12 +00:00
# On OSs lower than 11, whenever the major versions don't match,
# only return an SDK older than the OS version if it was specifically requested
2020-11-30 12:58:38 +00:00
return if v.blank? && sdk.version < OS::Mac.version
2018-09-17 02:45:00 +02:00
2018-07-27 15:44:22 -07:00
sdk
end
2020-04-07 16:43:32 +01:00
def source
nil
end
2020-07-01 16:02:29 +01:00
private
2018-06-12 14:55:31 -07:00
def sdk_prefix
""
end
def latest_sdk
all_sdks.max_by(&:version)
end
2020-12-26 01:06:12 +00:00
def read_sdk_version(sdk_path)
sdk_settings = sdk_path/"SDKSettings.json"
sdk_settings_string = sdk_settings.read if sdk_settings.exist?
2020-12-26 01:06:12 +00:00
# Pre-10.14 SDKs
sdk_settings = sdk_path/"SDKSettings.plist"
if sdk_settings_string.blank? && sdk_settings.exist?
result = system_command("plutil", args: ["-convert", "json", "-o", "-", sdk_settings])
sdk_settings_string = result.stdout if result.success?
end
return if sdk_settings_string.blank?
sdk_settings_json = JSON.parse(sdk_settings_string)
return if sdk_settings_json.blank?
version_string = sdk_settings_json.fetch("Version", nil)
return if version_string.blank?
begin
OS::Mac::Version.new(version_string).strip_patch
rescue MacOSVersionError
nil
end
2020-12-26 01:06:12 +00:00
end
end
2020-08-25 00:33:34 +02:00
private_constant :BaseSDKLocator
2018-06-12 14:55:31 -07:00
2020-08-25 00:33:34 +02:00
# Helper class for locating the Xcode SDK.
#
# @api private
2018-06-12 14:55:31 -07:00
class XcodeSDKLocator < BaseSDKLocator
2020-10-20 12:03:48 +02:00
extend T::Sig
sig { returns(Symbol) }
2020-04-07 16:43:32 +01:00
def source
:xcode
end
2020-07-01 16:02:29 +01:00
private
2018-06-12 14:55:31 -07:00
def sdk_prefix
@sdk_prefix ||= begin
# Xcode.prefix is pretty smart, so let's look inside to find the sdk
sdk_prefix = "#{Xcode.prefix}/Platforms/MacOSX.platform/Developer/SDKs"
# Finally query Xcode itself (this is slow, so check it last)
2018-07-27 15:44:22 -07:00
sdk_platform_path = Utils.popen_read(DevelopmentTools.locate("xcrun"), "--show-sdk-platform-path").chomp
sdk_prefix = File.join(sdk_platform_path, "Developer", "SDKs") unless File.directory? sdk_prefix
2018-06-12 14:55:31 -07:00
sdk_prefix
end
end
end
2020-08-25 00:33:34 +02:00
# Helper class for locating the macOS Command Line Tools SDK.
#
# @api private
2018-06-12 14:55:31 -07:00
class CLTSDKLocator < BaseSDKLocator
2020-10-20 12:03:48 +02:00
extend T::Sig
sig { returns(Symbol) }
2020-04-07 16:43:32 +01:00
def source
:clt
end
2020-07-01 16:02:29 +01:00
private
2018-06-12 14:55:31 -07:00
# While CLT SDKs existed prior to Xcode 10, those packages also
# installed a traditional Unix-style header layout and we prefer
# using that.
2018-06-12 14:55:31 -07:00
# As of Xcode 10, the Unix-style headers are installed via a
# separate package, so we can't rely on their being present.
# This will only look up SDKs on Xcode 10 or newer, and still
# return nil SDKs for Xcode 9 and older.
def sdk_prefix
@sdk_prefix ||= begin
if CLT.provides_sdk?
2018-06-12 14:55:31 -07:00
"#{CLT::PKG_PATH}/SDKs"
else
""
2018-06-12 14:55:31 -07:00
end
end
end
end
end
end