Returns a tuple of version and build version of installed Xcode.
()
| 1480 | |
| 1481 | |
| 1482 | def XcodeVersion(): |
| 1483 | """Returns a tuple of version and build version of installed Xcode.""" |
| 1484 | # `xcodebuild -version` output looks like |
| 1485 | # Xcode 4.6.3 |
| 1486 | # Build version 4H1503 |
| 1487 | # or like |
| 1488 | # Xcode 3.2.6 |
| 1489 | # Component versions: DevToolsCore-1809.0; DevToolsSupport-1806.0 |
| 1490 | # BuildVersion: 10M2518 |
| 1491 | # Convert that to ('0463', '4H1503') or ('0326', '10M2518'). |
| 1492 | global XCODE_VERSION_CACHE |
| 1493 | if XCODE_VERSION_CACHE: |
| 1494 | return XCODE_VERSION_CACHE |
| 1495 | version = "" |
| 1496 | build = "" |
| 1497 | try: |
| 1498 | version_list = GetStdoutQuiet(["xcodebuild", "-version"]).splitlines() |
| 1499 | # In some circumstances xcodebuild exits 0 but doesn't return |
| 1500 | # the right results; for example, a user on 10.7 or 10.8 with |
| 1501 | # a bogus path set via xcode-select |
| 1502 | # In that case this may be a CLT-only install so fall back to |
| 1503 | # checking that version. |
| 1504 | if len(version_list) < 2: |
| 1505 | raise GypError("xcodebuild returned unexpected results") |
| 1506 | version = version_list[0].split()[-1] # Last word on first line |
| 1507 | build = version_list[-1].split()[-1] # Last word on last line |
| 1508 | except (GypError, OSError): |
| 1509 | # Xcode not installed so look for XCode Command Line Tools |
| 1510 | version = CLTVersion() # macOS Catalina returns 11.0.0.0.1.1567737322 |
| 1511 | if not version: |
| 1512 | raise GypError("No Xcode or CLT version detected!") |
| 1513 | # Be careful to convert "4.2.3" to "0423" and "11.0.0" to "1100": |
| 1514 | version = version.split(".")[:3] # Just major, minor, micro |
| 1515 | version[0] = version[0].zfill(2) # Add a leading zero if major is one digit |
| 1516 | version = ("".join(version) + "00")[:4] # Limit to exactly four characters |
| 1517 | XCODE_VERSION_CACHE = (version, build) |
| 1518 | return XCODE_VERSION_CACHE |
| 1519 | |
| 1520 | |
| 1521 | # This function ported from the logic in Homebrew's CLT version check |
no test coverage detected