421 lines
12 KiB
Ruby
Raw Normal View History

2021-09-29 15:12:53 -07:00
# typed: true
# frozen_string_literal: true
module OS
module Mac
2020-08-25 00:38:56 +02:00
# Helper module for querying Xcode information.
#
# @api private
module Xcode
2020-10-20 12:03:48 +02:00
extend T::Sig
2016-09-24 20:11:54 +02:00
module_function
2020-10-20 12:03:48 +02:00
DEFAULT_BUNDLE_PATH = Pathname("/Applications/Xcode.app").freeze
BUNDLE_ID = "com.apple.dt.Xcode"
OLD_BUNDLE_ID = "com.apple.Xcode"
APPLE_DEVELOPER_DOWNLOAD_URL = "https://developer.apple.com/download/all/"
# Bump these when a new version is available from the App Store and our
# CI systems have been updated.
# This may be a beta version for a beta macOS.
sig { params(macos: MacOS::Version).returns(String) }
def latest_version(macos: MacOS.version)
2022-05-26 18:11:29 +01:00
latest_stable = "13.4"
case macos
when "12" then latest_stable
when "11" then "13.2.1"
2021-01-29 17:08:29 +11:00
when "10.15" then "12.4"
when "10.14" then "11.3.1"
when "10.13" then "10.1"
when "10.12" then "9.2"
when "10.11" then "8.2.1"
when "10.10" then "7.2.1"
when "10.9" then "6.2"
else
raise "macOS '#{MacOS.version}' is invalid" unless OS::Mac.version.prerelease?
2016-09-23 22:02:23 +02:00
# Default to newest known version of Xcode for unreleased macOS versions.
latest_stable
end
end
# Bump these if things are badly broken (e.g. no SDK for this macOS)
# without this. Generally this will be the first Xcode release on that
# macOS version (which may initially be a beta if that version of macOS is
# also in beta).
2020-10-20 12:03:48 +02:00
sig { returns(String) }
def minimum_version
case MacOS.version
when "12" then "13.1"
when "11" then "12.2"
when "10.15" then "11.0"
when "10.14" then "10.2"
2017-06-06 04:33:55 +01:00
when "10.13" then "9.0"
when "10.12" then "8.0"
else "2.0"
end
end
sig { returns(T::Boolean) }
def below_minimum_version?
return false unless installed?
2018-09-17 02:45:00 +02:00
version < minimum_version
end
sig { returns(T::Boolean) }
def latest_sdk_version?
OS::Mac.full_version >= OS::Mac.latest_sdk_version
end
sig { returns(T::Boolean) }
def needs_clt_installed?
return false if latest_sdk_version?
2018-09-17 02:45:00 +02:00
without_clt?
end
sig { returns(T::Boolean) }
def outdated?
return false unless installed?
2018-09-17 02:45:00 +02:00
version < latest_version
end
2013-05-22 22:26:09 -05:00
sig { returns(T::Boolean) }
def without_clt?
!MacOS::CLT.installed?
end
2016-03-16 22:51:39 -07:00
# Returns a Pathname object corresponding to Xcode.app's Developer
# directory or nil if Xcode.app is not installed.
sig { returns(T.nilable(Pathname)) }
def prefix
return @prefix if defined?(@prefix)
@prefix = T.let(@prefix, T.nilable(Pathname))
2014-05-02 16:48:59 -05:00
@prefix ||=
begin
dir = MacOS.active_developer_dir
2017-01-09 21:30:20 +00:00
if dir.empty? || dir == CLT::PKG_PATH || !File.directory?(dir)
2014-05-02 16:48:59 -05:00
path = bundle_path
2017-01-09 21:30:20 +00:00
path/"Contents/Developer" if path
2014-05-02 16:48:59 -05:00
else
# Use cleanpath to avoid pathological trailing slash
Pathname.new(dir).cleanpath
2014-05-02 16:48:59 -05:00
end
end
end
2020-10-20 12:03:48 +02:00
sig { returns(Pathname) }
def toolchain_path
2020-10-20 12:03:48 +02:00
Pathname("#{prefix}/Toolchains/XcodeDefault.xctoolchain")
end
2021-09-29 15:12:53 -07:00
sig { returns(T.nilable(Pathname)) }
def bundle_path
# Use the default location if it exists.
return DEFAULT_BUNDLE_PATH if DEFAULT_BUNDLE_PATH.exist?
# Ask Spotlight where Xcode is. If the user didn't install the
# helper tools and installed Xcode in a non-conventional place, this
# is our only option. See: https://superuser.com/questions/390757
MacOS.app_with_bundle_id(BUNDLE_ID, OLD_BUNDLE_ID)
end
2020-10-20 12:03:48 +02:00
sig { returns(T::Boolean) }
def installed?
!prefix.nil?
end
2021-09-29 15:12:53 -07:00
sig { returns(XcodeSDKLocator) }
2020-07-01 16:02:29 +01:00
def sdk_locator
@sdk_locator ||= XcodeSDKLocator.new
end
2021-09-29 15:12:53 -07:00
sig { params(v: T.nilable(MacOS::Version)).returns(T.nilable(SDK)) }
2020-07-01 16:02:29 +01:00
def sdk(v = nil)
sdk_locator.sdk_if_applicable(v)
end
2021-09-29 15:12:53 -07:00
sig { params(v: T.nilable(MacOS::Version)).returns(T.nilable(Pathname)) }
def sdk_path(v = nil)
sdk(v)&.path
end
2021-09-29 15:12:53 -07:00
sig { returns(String) }
def installation_instructions
if OS::Mac.version.prerelease?
<<~EOS
Xcode can be installed from:
#{Formatter.url(APPLE_DEVELOPER_DOWNLOAD_URL)}
EOS
else
<<~EOS
Xcode can be installed from the App Store.
EOS
end
end
2020-10-20 12:03:48 +02:00
sig { returns(String) }
2016-07-16 21:01:34 +01:00
def update_instructions
if OS::Mac.version.prerelease?
2017-10-15 02:28:32 +02:00
<<~EOS
Xcode can be updated from:
#{Formatter.url(APPLE_DEVELOPER_DOWNLOAD_URL)}
2016-07-16 21:01:34 +01:00
EOS
else
2017-10-15 02:28:32 +02:00
<<~EOS
Xcode can be updated from the App Store.
2016-07-16 21:01:34 +01:00
EOS
end
end
2021-09-29 15:12:53 -07:00
sig { returns(::Version) }
def version
# may return a version string
# that is guessed based on the compiler, so do not
# use it in order to check if Xcode is installed.
if @version ||= detect_version
::Version.new @version
else
::Version::NULL
end
end
2021-09-29 15:12:53 -07:00
sig { returns(T.nilable(String)) }
def detect_version
# This is a separate function as you can't cache the value out of a block
# if return is used in the middle, which we do many times in here.
return if !MacOS::Xcode.installed? && !MacOS::CLT.installed?
%W[
#{prefix}/usr/bin/xcodebuild
#{which("xcodebuild")}
].uniq.each do |xcodebuild_path|
2016-09-11 17:49:27 +01:00
next unless File.executable? xcodebuild_path
2018-09-17 02:45:00 +02:00
2016-09-11 17:49:27 +01:00
xcodebuild_output = Utils.popen_read(xcodebuild_path, "-version")
next unless $CHILD_STATUS.success?
xcode_version = xcodebuild_output[/Xcode (\d+(\.\d+)*)/, 1]
2016-09-11 17:49:27 +01:00
return xcode_version if xcode_version
2016-09-11 17:49:27 +01:00
# Xcode 2.x's xcodebuild has a different version string
case xcodebuild_output[/DevToolsCore-(\d+\.\d)/, 1]
when "798.0" then return "2.5"
when "515.0" then return "2.0"
2014-04-15 14:18:45 -05:00
end
end
detect_version_from_clang_version
end
2020-10-20 12:03:48 +02:00
sig { returns(String) }
def detect_version_from_clang_version
2021-05-09 17:07:00 +01:00
version = DevelopmentTools.clang_version
return "dunno" if version.null?
2018-09-17 02:45:00 +02:00
# This logic provides a fake Xcode version based on the
# installed CLT version. This is useful as they are packaged
# simultaneously so workarounds need to apply to both based on their
# comparable version.
2021-05-09 17:07:00 +01:00
case version
when "6.0.0" then "6.2"
when "6.1.0" then "6.4"
when "7.0.0" then "7.1"
when "7.0.2" then "7.2.1"
when "7.3.0" then "7.3.1"
when "8.0.0" then "8.2.1"
when "8.1.0" then "8.3.3"
when "9.0.0" then "9.2"
when "9.1.0" then "9.4.1"
when "10.0.0" then "10.1"
when "10.0.1" then "10.3"
when "11.0.0" then "11.3.1"
when "11.0.3" then "11.7"
when "12.0.0" then "12.4"
2021-09-26 16:57:43 +01:00
when "12.0.5" then "12.5.1"
2022-03-23 23:35:06 +00:00
when "13.0.0" then "13.2.1"
2022-05-26 18:11:29 +01:00
else "13.4"
end
end
2021-09-29 15:12:53 -07:00
sig { returns(T::Boolean) }
def default_prefix?
prefix.to_s == "/Applications/Xcode.app/Contents/Developer"
end
end
2020-08-25 00:40:10 +02:00
# Helper module for querying macOS Command Line Tools information.
#
# @api private
module CLT
2020-10-20 12:03:48 +02:00
extend T::Sig
module_function
2018-07-06 15:15:23 -07:00
# The original Mavericks CLT package ID
EXECUTABLE_PKG_ID = "com.apple.pkg.CLTools_Executables"
MAVERICKS_NEW_PKG_ID = "com.apple.pkg.CLTools_Base" # obsolete
PKG_PATH = "/Library/Developer/CommandLineTools"
# Returns true even if outdated tools are installed.
2020-10-20 12:03:48 +02:00
sig { returns(T::Boolean) }
def installed?
!version.null?
end
2013-07-21 16:41:50 -05:00
2021-09-29 15:12:53 -07:00
sig { returns(T::Boolean) }
2018-06-12 14:30:27 -07:00
def separate_header_package?
version >= "10" && MacOS.version >= "10.14"
2018-06-12 14:30:27 -07:00
end
2021-09-29 15:12:53 -07:00
sig { returns(T::Boolean) }
def provides_sdk?
version >= "8"
end
2021-09-29 15:12:53 -07:00
sig { returns(CLTSDKLocator) }
2020-07-01 16:02:29 +01:00
def sdk_locator
@sdk_locator ||= CLTSDKLocator.new
end
2021-09-29 15:12:53 -07:00
sig { params(v: T.nilable(MacOS::Version)).returns(T.nilable(SDK)) }
2020-07-01 16:02:29 +01:00
def sdk(v = nil)
sdk_locator.sdk_if_applicable(v)
end
2021-09-29 15:12:53 -07:00
sig { params(v: T.nilable(MacOS::Version)).returns(T.nilable(Pathname)) }
def sdk_path(v = nil)
sdk(v)&.path
end
2021-09-29 15:12:53 -07:00
sig { returns(String) }
def installation_instructions
if MacOS.version == "10.14"
# This is not available from `xcode-select`
<<~EOS
Install the Command Line Tools for Xcode 11.3.1 from:
#{Formatter.url(MacOS::Xcode::APPLE_DEVELOPER_DOWNLOAD_URL)}
EOS
else
<<~EOS
Install the Command Line Tools:
xcode-select --install
EOS
end
end
2020-10-20 12:03:48 +02:00
sig { returns(String) }
2016-07-16 21:01:34 +01:00
def update_instructions
software_update_location = if MacOS.version >= "10.14"
"System Preferences"
else
"the App Store"
2016-07-16 21:01:34 +01:00
end
<<~EOS
Update them from Software Update in #{software_update_location} or run:
softwareupdate --all --install --force
2021-01-26 15:21:24 -05:00
If that doesn't show you any updates, run:
sudo rm -rf /Library/Developer/CommandLineTools
sudo xcode-select --install
Alternatively, manually download them from:
#{Formatter.url(MacOS::Xcode::APPLE_DEVELOPER_DOWNLOAD_URL)}.
2021-05-18 16:19:31 +02:00
You should download the Command Line Tools for Xcode #{MacOS::Xcode.latest_version}.
EOS
2016-07-16 21:01:34 +01:00
end
# Bump these when the new version is distributed through Software Update
# and our CI systems have been updated.
2020-10-20 12:03:48 +02:00
sig { returns(String) }
def latest_clang_version
case MacOS.version
2022-05-26 18:11:29 +01:00
when "12" then "1316.0.21.2.5"
when "11" then "1300.0.29.30"
when "10.15" then "1200.0.32.29"
when "10.14" then "1100.0.33.17"
when "10.13" then "1000.10.44.2"
when "10.12" then "900.0.39.2"
when "10.11" then "800.0.42.1"
when "10.10" then "700.1.81"
else "600.0.57"
end
end
# Bump these if things are badly broken (e.g. no SDK for this macOS)
# without this. Generally this will be the first stable CLT release on
# that macOS version.
2020-10-20 12:03:48 +02:00
sig { returns(String) }
def minimum_version
case MacOS.version
2021-06-08 19:28:20 +02:00
when "12" then "13.0.0"
when "11" then "12.5.0"
when "10.15" then "11.0.0"
when "10.14" then "10.0.0"
when "10.13" then "9.0.0"
when "10.12" then "8.0.0"
else "1.0.0"
end
end
2021-09-29 15:12:53 -07:00
sig { returns(T::Boolean) }
def below_minimum_version?
return false unless installed?
2018-09-17 02:45:00 +02:00
version < minimum_version
end
2020-10-20 12:03:48 +02:00
sig { returns(T::Boolean) }
def outdated?
clang_version = detect_clang_version
return false unless clang_version
2018-09-17 02:45:00 +02:00
::Version.new(clang_version) < latest_clang_version
end
2021-09-29 15:12:53 -07:00
sig { returns(T.nilable(String)) }
def detect_clang_version
2021-03-17 15:34:20 +00:00
version_output = Utils.popen_read("#{PKG_PATH}/usr/bin/clang", "--version")
version_output[/clang-(\d+\.\d+\.\d+(\.\d+)?)/, 1]
end
2013-05-22 22:26:09 -05:00
2021-09-29 15:12:53 -07:00
sig { returns(T.nilable(String)) }
def detect_version_from_clang_version
detect_clang_version&.sub(/^(\d+)0(\d)\./, "\\1.\\2.")
end
2014-03-22 10:14:20 +00:00
# Version string (a pretty long one) of the CLT package.
# Note that the different ways of installing the CLTs lead to different
# version numbers.
2021-09-29 15:12:53 -07:00
sig { returns(::Version) }
def version
if @version ||= detect_version
::Version.new @version
else
::Version::NULL
end
end
2013-07-21 20:09:55 -05:00
2021-09-29 15:12:53 -07:00
sig { returns(T.nilable(String)) }
def detect_version
2021-09-29 15:12:53 -07:00
version = T.let(nil, T.nilable(String))
[EXECUTABLE_PKG_ID, MAVERICKS_NEW_PKG_ID].each do |id|
next unless File.exist?("#{PKG_PATH}/usr/bin/clang")
version = MacOS.pkgutil_info(id)[/version: (.+)$/, 1]
return version if version
end
detect_version_from_clang_version
end
2013-07-21 20:09:55 -05:00
end
end
end