Skip to content

Commit

Permalink
Merge pull request #440 from KubaO/feature/fix-msvc-autogen
Browse files Browse the repository at this point in the history
Make autogen work under Windows+MSVC
  • Loading branch information
fangerer authored Mar 7, 2024
2 parents 95c51b9 + ace5fed commit 176724c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion hpy/devel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ def write_stub(self, output_dir, ext, compile=False):
ext_file = os.path.basename(ext._file_name)
module_name = ext_file.split(".")[0]
if not self.dry_run:
with open(stub_file, 'w') as f:
with open(stub_file, 'w', encoding='utf-8') as f:
f.write(_HPY_UNIVERSAL_MODULE_STUB_TEMPLATE.format(
ext_file=ext_file, module_name=module_name)
)
Expand Down
2 changes: 1 addition & 1 deletion hpy/tools/autogen/autogenfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def generate(self):
def write(self, root):
cls = self.__class__
clsname = '%s.%s' % (cls.__module__, cls.__name__)
with root.join(self.PATH).open('w') as f:
with root.join(self.PATH).open('w', encoding='utf-8') as f:
if self.DISCLAIMER is not None:
f.write(self.DISCLAIMER.format(clsname=clsname))
f.write('\n')
Expand Down
4 changes: 2 additions & 2 deletions hpy/tools/autogen/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def write(self, root):
if not self.BEGIN_MARKER or not self.END_MARKER:
raise RuntimeError("missing BEGIN_MARKER or END_MARKER")
n_begin = len(self.BEGIN_MARKER)
with root.join(self.PATH).open('r') as f:
with root.join(self.PATH).open('r', encoding='utf-8') as f:
content = f.read()
start = content.find(self.BEGIN_MARKER)
if start < 0:
Expand All @@ -75,7 +75,7 @@ def write(self, root):
raise RuntimeError(f'end marker "{self.END_MARKER}" not found in'
f'file {self.PATH}')
new_content = self.generate(content[(start+n_begin):end])
with root.join(self.PATH).open('w') as f:
with root.join(self.PATH).open('w', encoding='utf-8') as f:
f.write(content[:start + n_begin] + new_content + content[end:])


Expand Down
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 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 176724c

Please sign in to comment.