Skip to content

Commit

Permalink
Detect and use MSVC compiler if available for preprocessing.
Browse files Browse the repository at this point in the history
Autogen has to be invoked from the MSVC command line so that cl.exe is in path.
  • Loading branch information
KubaO committed Jun 27, 2023
1 parent 82774bd commit ace5fed
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion hpy/tools/autogen/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
import py
import pycparser
import shutil
from pycparser import c_ast
from pycparser.c_generator import CGenerator
from distutils.sysconfig import get_config_var
Expand Down Expand Up @@ -202,16 +203,27 @@ 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),
# but cparser needs to see a #pragma, not __pragma.
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):
Expand Down

0 comments on commit ace5fed

Please sign in to comment.