Detect MSVC version and architecture via preprocessor #137

Merged
MartinFouilleul merged 1 commits from msvc-version into main 2023-09-21 06:54:03 +00:00
2 changed files with 23 additions and 6 deletions

View File

@ -408,22 +408,30 @@ def ensure_programs():
if platform.system() == "Windows":
MSVC_MAJOR, MSVC_MINOR = 19, 35
try:
result = subprocess.run(["cl"], capture_output=True, text=True)
if "for x64" not in result.stderr:
cl_only = subprocess.run(["cl"], capture_output=True, text=True)
desc = cl_only.stderr.splitlines()[0]
detect = subprocess.run(["cl", "/EP", "scripts\\msvc_version.txt"], capture_output=True, text=True)
parts = [x for x in detect.stdout.splitlines() if x]
version, arch = int(parts[0]), parts[1]
major, minor = int(version / 100), version % 100
if arch != "x64":
msg = log_error("MSVC is not running in 64-bit mode. Make sure you are running in")
msg.more("an x64 Visual Studio command prompt, such as the \"x64 Native Tools")
msg.more("Command Prompt\" from your Start Menu.")
msg.more()
msg.more("MSVC reported itself as:")
msg.more(result.stderr.splitlines()[0])
msg.more(desc)
exit(1)
m = re.search(r"Version (\d+)\.(\d+).(\d+)", result.stderr)
major, minor, patch = int(m.group(1)), int(m.group(2)), int(m.group(3))
if major < MSVC_MAJOR or minor < MSVC_MINOR:
msg = log_error(f"Your version of MSVC is too old. You have version {major}.{minor}.{patch},")
msg = log_error(f"Your version of MSVC is too old. You have version {major}.{minor},")
msg.more(f"but version {MSVC_MAJOR}.{MSVC_MINOR} or greater is required.")
msg.more()
msg.more("MSVC reported itself as:")
msg.more(desc)
msg.more()
msg.more("Please update Visual Studio to the latest version and try again.")
exit(1)
except FileNotFoundError:

9
scripts/msvc_version.txt Normal file
View File

@ -0,0 +1,9 @@
_MSC_VER
#if defined(__x86_64__) || defined(_M_X64)
x64
#elif defined(i386) || defined(__i386__) || defined(__i386) || defined(_M_IX86)
x86
#else
unknown
#endif