-
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
/
Copy pathxcode.rb
444 lines (383 loc) · 13.9 KB
/
xcode.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
module OS
module Mac
# Helper module for querying Xcode information.
module Xcode
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: MacOSVersion).returns(String) }
def self.latest_version(macos: MacOS.version)
latest_stable = "15.4"
case macos
when "15" then "16.0"
when "14" then latest_stable
when "13" then "15.2"
when "12" then "14.2"
when "11" then "13.2.1"
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"
else
raise "macOS '#{MacOS.version}' is invalid" unless OS::Mac.version.prerelease?
# 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).
sig { returns(String) }
def self.minimum_version
case MacOS.version
when "15" then "16.0"
when "14" then "15.0"
when "13" then "14.1"
when "12" then "13.1"
when "11" then "12.2"
when "10.15" then "11.0"
when "10.14" then "10.2"
when "10.13" then "9.0"
when "10.12" then "8.0"
else "7.3"
end
end
sig { returns(T::Boolean) }
def self.below_minimum_version?
return false unless installed?
version < minimum_version
end
sig { returns(T::Boolean) }
def self.latest_sdk_version?
OS::Mac.full_version >= OS::Mac.latest_sdk_version
end
sig { returns(T::Boolean) }
def self.needs_clt_installed?
return false if latest_sdk_version?
# With fake El Capitan for Portable Ruby, we want the full 10.11 SDK so that we can link
# against the correct set of libraries in the SDK sysroot rather than the system's copies.
# We therefore do not use the CLT under this setup, which installs to /usr/include.
return false if ENV["HOMEBREW_FAKE_MACOS"]
without_clt?
end
sig { returns(T::Boolean) }
def self.outdated?
return false unless installed?
version < latest_version
end
sig { returns(T::Boolean) }
def self.without_clt?
!MacOS::CLT.installed?
end
# 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 self.prefix
@prefix ||= begin
dir = MacOS.active_developer_dir
if dir.empty? || dir == CLT::PKG_PATH || !File.directory?(dir)
path = bundle_path
path/"Contents/Developer" if path
else
# Use cleanpath to avoid pathological trailing slash
Pathname.new(dir).cleanpath
end
end
end
sig { returns(Pathname) }
def self.toolchain_path
Pathname("#{prefix}/Toolchains/XcodeDefault.xctoolchain")
end
sig { returns(T.nilable(Pathname)) }
def self.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
sig { returns(T::Boolean) }
def self.installed?
!prefix.nil?
end
sig { returns(XcodeSDKLocator) }
def self.sdk_locator
@sdk_locator ||= XcodeSDKLocator.new
end
sig { params(version: T.nilable(MacOSVersion)).returns(T.nilable(SDK)) }
def self.sdk(version = nil)
sdk_locator.sdk_if_applicable(version)
end
sig { params(version: T.nilable(MacOSVersion)).returns(T.nilable(Pathname)) }
def self.sdk_path(version = nil)
sdk(version)&.path
end
sig { returns(String) }
def self.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
sig { returns(String) }
def self.update_instructions
if OS::Mac.version.prerelease?
<<~EOS
Xcode can be updated from:
#{Formatter.url(APPLE_DEVELOPER_DOWNLOAD_URL)}
EOS
else
<<~EOS
Xcode can be updated from the App Store.
EOS
end
end
# Get the Xcode version.
#
# @api internal
sig { returns(::Version) }
def self.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
sig { returns(T.nilable(String)) }
def self.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?
if MacOS::Xcode.installed?
# Fast path that will probably almost always work unless `xcode-select -p` is misconfigured
version_plist = T.must(prefix).parent/"version.plist"
if version_plist.file?
data = Plist.parse_xml(version_plist, marshal: false)
version = data["CFBundleShortVersionString"] if data
return version if version
end
%W[
#{prefix}/usr/bin/xcodebuild
#{which("xcodebuild")}
].uniq.each do |xcodebuild_path|
next unless File.executable? xcodebuild_path
xcodebuild_output = Utils.popen_read(xcodebuild_path, "-version")
next unless $CHILD_STATUS.success?
xcode_version = xcodebuild_output[/Xcode (\d+(\.\d+)*)/, 1]
return xcode_version if xcode_version
# 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"
end
end
end
detect_version_from_clang_version
end
sig { returns(String) }
def self.detect_version_from_clang_version
version = ::DevelopmentTools.clang_version
return "dunno" if version.null?
# 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.
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"
when "12.0.5" then "12.5.1"
when "13.0.0" then "13.2.1"
when "13.1.6" then "13.4.1"
when "14.0.0" then "14.2"
when "14.0.3" then "14.3.1"
when "16.0.0" then "16.0"
else "15.4"
end
end
sig { returns(T::Boolean) }
def self.default_prefix?
prefix.to_s == "/Applications/Xcode.app/Contents/Developer"
end
end
# Helper module for querying macOS Command Line Tools information.
module CLT
# 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.
sig { returns(T::Boolean) }
def self.installed?
!version.null?
end
sig { returns(T::Boolean) }
def self.separate_header_package?
version >= "10" && MacOS.version >= "10.14"
end
sig { returns(T::Boolean) }
def self.provides_sdk?
version >= "8"
end
sig { returns(CLTSDKLocator) }
def self.sdk_locator
@sdk_locator ||= CLTSDKLocator.new
end
sig { params(version: T.nilable(MacOSVersion)).returns(T.nilable(SDK)) }
def self.sdk(version = nil)
sdk_locator.sdk_if_applicable(version)
end
sig { params(version: T.nilable(MacOSVersion)).returns(T.nilable(Pathname)) }
def self.sdk_path(version = nil)
sdk(version)&.path
end
sig { returns(String) }
def self.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
elsif OS::Mac.version.prerelease?
<<~EOS
Install the Command Line Tools for Xcode #{minimum_version.split(".").first} from:
#{Formatter.url(MacOS::Xcode::APPLE_DEVELOPER_DOWNLOAD_URL)}
EOS
else
<<~EOS
Install the Command Line Tools:
xcode-select --install
EOS
end
end
sig { returns(String) }
def self.update_instructions
return installation_instructions if OS::Mac.version.prerelease?
software_update_location = if MacOS.version >= "13"
"System Settings"
elsif MacOS.version >= "10.14"
"System Preferences"
else
"the App Store"
end
<<~EOS
Update them from Software Update in #{software_update_location}.
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)}.
You should download the Command Line Tools for Xcode #{MacOS::Xcode.latest_version}.
EOS
end
# Bump these when the new version is distributed through Software Update
# and our CI systems have been updated.
sig { returns(String) }
def self.latest_clang_version
case MacOS.version
when "15" then "1600.0.20.10"
when "14" then "1500.3.9.4"
when "13" then "1500.1.0.2.5"
when "12" then "1400.0.29.202"
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"
else "800.0.42.1"
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.
sig { returns(String) }
def self.minimum_version
case MacOS.version
when "15" then "16.0.0"
when "14" then "15.0.0"
when "13" then "14.0.0"
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 "7.3.0"
end
end
sig { returns(T::Boolean) }
def self.below_minimum_version?
return false unless installed?
version < minimum_version
end
sig { returns(T::Boolean) }
def self.outdated?
clang_version = detect_clang_version
return false unless clang_version
::Version.new(clang_version) < latest_clang_version
end
sig { returns(T.nilable(String)) }
def self.detect_clang_version
version_output = Utils.popen_read("#{PKG_PATH}/usr/bin/clang", "--version")
version_output[/clang-(\d+(\.\d+)+)/, 1]
end
sig { returns(T.nilable(String)) }
def self.detect_version_from_clang_version
detect_clang_version&.sub(/^(\d+)0(\d)\./, "\\1.\\2.")
end
# Version string (a pretty long one) of the CLT package.
# Note that the different ways of installing the CLTs lead to different
# version numbers.
#
# @api internal
sig { returns(::Version) }
def self.version
if @version ||= detect_version
::Version.new @version
else
::Version::NULL
end
end
sig { returns(T.nilable(String)) }
def self.detect_version
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
end
end
end