Last active
October 15, 2019 21:48
-
-
Save nickhutchinson/ad2a6c2cc1e77dea379d74d2aa83a020 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 | |
from collections import namedtuple | |
from distutils.version import LooseVersion | |
try: | |
from typing import List, Mapping, Sequence | |
except ImportError: | |
pass | |
MacOsSdk = namedtuple("MacOsSdk", ["xcodeVersion", "xcodePath", "sdkRoot"]) | |
def getMacSDKs(): # type: () -> Sequence[MacOsSdk] | |
sdkDicts = [] # type: List[dict] | |
# Find Xcode paths | |
cmd = ["mdfind", "kMDItemCFBundleIdentifier == 'com.apple.dt.Xcode'"] | |
p = subprocess.Popen(cmd, stdout=subprocess.PIPE) | |
try: | |
for line in iter(p.stdout.readline, b""): | |
sdkDicts.append({"xcodePath": line.decode("utf-8").rstrip()}) | |
finally: | |
p.communicate() | |
if p.returncode != 0: | |
raise subprocess.CalledProcessError(p.returncode, cmd) | |
# Populate Xcode versions | |
for sdkDict in sdkDicts: | |
cmd = [ | |
"plutil", | |
"-convert", | |
"json", | |
os.path.join(sdkDict["xcodePath"], "Contents/Info.plist"), | |
"-o", | |
"-", | |
] | |
p = subprocess.Popen(cmd, stdout=subprocess.PIPE) | |
try: | |
infoPlist = json.load(p.stdout, encoding="utf-8") | |
sdkDict["xcodeVersion"] = infoPlist["CFBundleShortVersionString"] | |
finally: | |
p.communicate() | |
if p.returncode != 0: | |
raise subprocess.CalledProcessError(p.returncode, cmd) | |
# 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] | |
def getMSVCToolchains(): # type: () -> Sequence[Mapping] | |
toolchainDicts = [] # type: Sequence[Mapping] | |
cmd = [ | |
os.path.join( | |
os.environ["ProgramFiles(x86)"], | |
"Microsoft Visual Studio\\Installer\\vswhere.exe", | |
), | |
"-products", | |
"*", | |
"-requires", | |
"Microsoft.VisualStudio.Component.VC.Tools.x86.x64", | |
"-format", | |
"json", | |
"-utf8", | |
] | |
p = subprocess.Popen(cmd, stdout=subprocess.PIPE) | |
try: | |
toolchainDicts = json.load(p.stdout) | |
finally: | |
p.communicate() | |
if p.returncode != 0: | |
raise subprocess.CalledProcessError(p.returncode, cmd) | |
return toolchainDicts | |
if __name__ == "__main__": | |
# print(getMacSDKs()) | |
print(getMSVCToolchains()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment