Created
October 15, 2019 20:25
-
-
Save nickhutchinson/955e80ad6929536dfcc5c672ea0baf3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import os | |
import subprocess | |
import json | |
import six | |
from collections import namedtuple | |
from distutils.version import LooseVersion | |
MacOsSdk = namedtuple("MacOsSdk", ["xcodeVersion", "xcodePath", "sdkRoot"]) | |
def getMacSDKs(): | |
sdkDicts = [] | |
# Find Xcode paths | |
p = subprocess.Popen( | |
["mdfind", "kMDItemCFBundleIdentifier == 'com.apple.dt.Xcode'"], | |
stdout=subprocess.PIPE, | |
) | |
try: | |
for line in iter(p.stdout.readline, b""): | |
sdkDicts.append({"xcodePath": line.decode("utf-8").rstrip()}) | |
finally: | |
p.wait() | |
ret = [] | |
# Populate Xcode versions | |
for sdkDict in sdkDicts: | |
p = subprocess.Popen( | |
[ | |
"plutil", | |
"-convert", | |
"json", | |
os.path.join(sdkDict["xcodePath"], "Contents/Info.plist"), | |
"-o", | |
"-", | |
], | |
stdout=subprocess.PIPE, | |
) | |
try: | |
version = json.load(p.stdout, encoding="utf-8")[ | |
"CFBundleShortVersionString" | |
] | |
sdkDict["xcodeVersion"] = LooseVersion(version) | |
finally: | |
p.wait() | |
# Populate macOS SDK paths | |
for sdkDict in sdkDicts: | |
cmd = ["xcrun", "-sdk", "macosx", "--show-sdk-path"] | |
env = dict(os.environ) | |
env["DEVELOPER_DIR"] = os.path.join( | |
sdkDict["xcodePath"], "Contents", "Developer" | |
) | |
sdkDict["sdkRoot"] = ( | |
subprocess.check_output(cmd, env=env).decode("utf-8").rstrip() | |
) | |
return [MacOsSdk(**sdkDict) for sdkDict in sdkDicts] | |
if __name__ == "__main__": | |
print(getMacSDKs()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment