277 lines
8.4 KiB
Ruby
Raw Normal View History

module OS
module Mac
module Xcode
2016-09-24 20:11:54 +02:00
module_function
DEFAULT_BUNDLE_PATH = Pathname.new("/Applications/Xcode.app").freeze
2017-01-09 21:30:20 +00:00
BUNDLE_ID = "com.apple.dt.Xcode".freeze
OLD_BUNDLE_ID = "com.apple.Xcode".freeze
def latest_version
case MacOS.version
2014-08-09 22:00:06 -05:00
when "10.4" then "2.5"
when "10.5" then "3.1.4"
when "10.6" then "3.2.6"
when "10.7" then "4.6.3"
when "10.8" then "5.1.1"
when "10.9" then "6.2"
2016-02-07 19:30:51 +00:00
when "10.10" then "7.2.1"
when "10.11" then "8.2.1"
2017-04-18 20:40:20 -04:00
when "10.12" then "8.3.2"
else
2016-09-23 22:02:23 +02:00
raise "macOS '#{MacOS.version}' is invalid" unless OS::Mac.prerelease?
# Default to newest known version of Xcode for unreleased macOS versions.
2017-04-18 20:40:20 -04:00
"8.3.2"
end
end
def minimum_version
case MacOS.version
when "10.12" then "8.0"
else "2.0"
end
end
def below_minimum_version?
version < minimum_version
end
def outdated?
Version.new(version) < latest_version
end
2013-05-22 22:26:09 -05:00
def without_clt?
installed? && Version.new(version) >= "4.3" && !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
def prefix
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
def toolchain_path
return unless installed?
return if Version.new(version) < "4.3"
Pathname.new("#{prefix}/Toolchains/XcodeDefault.xctoolchain")
end
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
def installed?
!prefix.nil?
end
2016-07-16 21:01:34 +01:00
def update_instructions
if MacOS.version >= "10.9" && !OS::Mac.prerelease?
2016-07-16 21:01:34 +01:00
<<-EOS.undent
Xcode can be updated from the App Store.
EOS
else
<<-EOS.undent
Xcode can be updated from
https://developer.apple.com/download/more/
2016-07-16 21:01:34 +01:00
EOS
end
end
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.
@version ||= uncached_version
end
def uncached_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 "0" unless OS.mac?
return nil 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
xcodebuild_output = Utils.popen_read(xcodebuild_path, "-version")
next unless $?.success?
2016-09-11 17:49:27 +01:00
xcode_version = xcodebuild_output[/Xcode (\d(\.\d)*)/, 1]
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 "515.0" then return "2.0"
when "798.0" then return "2.5"
2014-04-15 14:18:45 -05:00
end
end
# The remaining 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.
case (DevelopmentTools.clang_version.to_f * 10).to_i
2016-09-11 17:49:27 +01:00
when 0 then "dunno"
when 1..14 then "3.2.2"
when 15 then "3.2.4"
when 16 then "3.2.5"
when 17..20 then "4.0"
when 21 then "4.1"
when 22..30 then "4.2"
when 31 then "4.3"
when 40 then "4.4"
when 41 then "4.5"
when 42 then "4.6"
when 50 then "5.0"
when 51 then "5.1"
when 60 then "6.0"
when 61 then "6.1"
when 70 then "7.0"
when 73 then "7.3"
when 80 then "8.0"
when 81 then "8.3"
else "8.3"
end
end
def provides_gcc?
installed? && Version.new(version) < "4.3"
end
2013-12-04 01:10:11 -06:00
def provides_cvs?
installed? && Version.new(version) < "5.0"
2013-12-04 01:10:11 -06:00
end
def default_prefix?
if Version.new(version) < "4.3"
prefix.to_s.start_with? "/Developer"
else
prefix.to_s == "/Applications/Xcode.app/Contents/Developer"
end
end
class Version < ::Version
def <=>(other)
super(Version.new(other))
end
end
end
module CLT
extend self
2016-09-11 17:49:27 +01:00
STANDALONE_PKG_ID = "com.apple.pkg.DeveloperToolsCLILeo".freeze
FROM_XCODE_PKG_ID = "com.apple.pkg.DeveloperToolsCLI".freeze
MAVERICKS_PKG_ID = "com.apple.pkg.CLTools_Executables".freeze
MAVERICKS_NEW_PKG_ID = "com.apple.pkg.CLTools_Base".freeze # obsolete
2017-01-09 21:30:20 +00:00
PKG_PATH = "/Library/Developer/CommandLineTools".freeze
# Returns true even if outdated tools are installed, e.g.
# tools from Xcode 4.x on 10.9
def installed?
2016-09-20 22:24:31 +02:00
!detect_version.nil?
end
2013-07-21 16:41:50 -05:00
2016-07-16 21:01:34 +01:00
def update_instructions
if MacOS.version >= "10.9"
2016-07-16 21:01:34 +01:00
<<-EOS.undent
Update them from Software Update in the App Store.
EOS
elsif MacOS.version == "10.8" || MacOS.version == "10.7"
<<-EOS.undent
The standalone package can be obtained from
https://developer.apple.com/download/more/
2016-07-16 21:01:34 +01:00
or it can be installed via Xcode's preferences.
EOS
end
end
def latest_version
# As of Xcode 8 CLT releases are no longer in sync with Xcode releases
# on the older supported platform for that Xcode release, i.e there's no
# CLT package for 10.11 that contains the Clang version from Xcode 8.
case MacOS.version
when "10.12" then "802.0.38"
when "10.11" then "800.0.42.1"
2015-12-09 05:06:00 +00:00
when "10.10" then "700.1.81"
when "10.9" then "600.0.57"
when "10.8" then "503.0.40"
else
"425.0.28"
end
end
def minimum_version
case MacOS.version
when "10.12" then "8.0.0"
else "1.0.0"
end
end
def below_minimum_version?
# Lion was the first version of OS X to ship with a CLT
return false if MacOS.version < :lion
version < minimum_version
end
def outdated?
# Lion was the first version of OS X to ship with a CLT
return false if MacOS.version < :lion
if MacOS.version >= :mavericks
2017-01-09 21:30:20 +00:00
version = Utils.popen_read("#{PKG_PATH}/usr/bin/clang --version")
else
2016-09-22 17:51:04 +01:00
version = Utils.popen_read("/usr/bin/clang --version")
end
version = version[/clang-(\d+\.\d+\.\d+(\.\d+)?)/, 1] || "0"
Xcode::Version.new(version) < latest_version
end
2013-05-22 22:26:09 -05:00
2014-03-22 10:14:20 +00:00
# Version string (a pretty long one) of the CLT package.
# Note, that different ways to install the CLTs lead to different
# version numbers.
def version
@version ||= detect_version
end
2013-07-21 20:09:55 -05:00
def detect_version
# CLT isn't a distinct entity pre-4.3, and pkgutil doesn't exist
# at all on Tiger, so just count it as installed if Xcode is installed
if MacOS::Xcode.installed? && Xcode::Version.new(MacOS::Xcode.version) < "3.0"
return MacOS::Xcode.version
end
[MAVERICKS_PKG_ID, MAVERICKS_NEW_PKG_ID, STANDALONE_PKG_ID, FROM_XCODE_PKG_ID].find do |id|
2016-11-26 13:59:55 -05:00
if MacOS.version >= :mavericks
2017-01-09 21:30:20 +00:00
next unless File.exist?("#{PKG_PATH}/usr/bin/clang")
end
version = MacOS.pkgutil_info(id)[/version: (.+)$/, 1]
return version if version
end
end
2013-07-21 20:09:55 -05:00
end
end
end