os/mac/sdk: refactor out sdk_version

This commit is contained in:
carlocab 2020-12-26 01:06:12 +00:00
parent bc4f5556c7
commit a51b105b1d
No known key found for this signature in database
GPG Key ID: C74D447FC549A1D0

View File

@ -31,13 +31,6 @@ module OS
SDK.new v, path, source SDK.new v, path, source
end end
def latest_sdk
return if sdk_paths.empty?
v, path = sdk_paths.max { |(v1, _), (v2, _)| v1 <=> v2 }
SDK.new v, path, source
end
def all_sdks def all_sdks
sdk_paths.map { |v, p| SDK.new v, p, source } sdk_paths.map { |v, p| SDK.new v, p, source }
end end
@ -45,7 +38,7 @@ module OS
def sdk_if_applicable(v = nil) def sdk_if_applicable(v = nil)
sdk = begin sdk = begin
if v.blank? if v.blank?
sdk_for OS::Mac.sdk_version sdk_for OS::Mac.version
else else
sdk_for v sdk_for v
end end
@ -54,16 +47,7 @@ module OS
end end
return if sdk.blank? return if sdk.blank?
# Accept an SDK for another OS version if it shares a major version # On OSs lower than 11, whenever the major versions don't match,
# with the current OS - for example, the 11.0 SDK on 11.1,
# or vice versa.
# Note that this only applies on macOS 11
# or greater, given the way the versioning has changed.
# This shortcuts the below check, since we *do* accept an older version
# on macOS 11 or greater if the major version matches.
return sdk if OS::Mac.version >= :big_sur && sdk.version.major == OS::Mac.version.major
# On OSs lower than 11, or where the major versions don't match,
# only return an SDK older than the OS version if it was specifically requested # only return an SDK older than the OS version if it was specifically requested
return if v.blank? && sdk.version < OS::Mac.version return if v.blank? && sdk.version < OS::Mac.version
@ -93,11 +77,15 @@ module OS
# Use unversioned SDK path on Big Sur to avoid issues such as: # Use unversioned SDK path on Big Sur to avoid issues such as:
# https://github.com/Homebrew/homebrew-core/issues/67075 # https://github.com/Homebrew/homebrew-core/issues/67075
sdk_path = File.join(sdk_prefix, "MacOSX.sdk") # This creates an entry in `paths` whose key is the OS major version
if OS::Mac.version >= :big_sur && File.directory?(sdk_path) sdk_path = Pathname.new("#{sdk_prefix}/MacOSX.sdk")
sdk_settings = File.join(sdk_path, "SDKSettings.json") sdk_settings = sdk_path/"SDKSettings.json"
version = JSON.parse(File.read(sdk_settings))["Version"] if File.exist?(sdk_settings) if sdk_settings.exist? &&
paths[OS::Mac::Version.new(version)] = sdk_path if version.present? (sdk_settings_string = sdk_settings.read.presence) &&
(sdk_settings_json = JSON.parse(sdk_settings_string).presence) &&
(version_string = sdk_settings_json.fetch("Version", nil).presence) &&
(version = version_string[/(\d+)\./, 1].presence)
paths[OS::Mac::Version.new(version)] = sdk_path
end end
paths paths
@ -106,6 +94,14 @@ module OS
end end
end end
end end
# NOTE: This returns a versioned SDK path, even on Big Sur
def latest_sdk
return if sdk_paths.empty?
v, path = sdk_paths.max { |(v1, _), (v2, _)| v1 <=> v2 }
SDK.new v, path, source
end
end end
private_constant :BaseSDKLocator private_constant :BaseSDKLocator