From 433908e5338af5c9865e0c1e432d681efefa3d57 Mon Sep 17 00:00:00 2001 From: Kuba Sunderland-Ober Date: Tue, 20 Jun 2023 00:10:15 -0400 Subject: [PATCH] Detect and use MSVC compiler if available for preprocessing. Autogen has to be invoked from the MSVC command line so that cl.exe is in path. --- hpy/tools/autogen/parse.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/hpy/tools/autogen/parse.py b/hpy/tools/autogen/parse.py index ce40e2633..a7d83b0fa 100644 --- a/hpy/tools/autogen/parse.py +++ b/hpy/tools/autogen/parse.py @@ -202,16 +202,26 @@ class HPyAPI: re.DOTALL | re.MULTILINE) def __init__(self, filename): - cpp_cmd = get_config_var('CC').split(' ') + cpp_cmd = get_config_var('CC') + if cpp_cmd: + cpp_cmd = cpp_cmd.split(' ') + elif sys.platform == 'win32': + cpp_cmd = [shutil.which("cl.exe")] if sys.platform == 'win32': cpp_cmd += ['/E', '/I%s' % CURRENT_DIR] else: cpp_cmd += ['-E', '-I%s' % CURRENT_DIR] + msvc = "cl.exe" in cpp_cmd[0].casefold() + csource = pycparser.preprocess_file(filename, cpp_path=str(cpp_cmd[0]), cpp_args=cpp_cmd[1:]) + # MSVC preprocesses _Pragma(foo) to __pragma(foo) + if msvc: + csource = re.sub(r'__pragma\(([^)]+)\)', r'#pragma \1\n', csource) + # Remove comments. NOTE: this assumes that comments are never inside # string literals, but there shouldn't be any here. def replace_keeping_newlines(m):