Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make autogen work under Windows+MSVC #440

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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':
KubaO marked this conversation as resolved.
Show resolved Hide resolved
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()
KubaO marked this conversation as resolved.
Show resolved Hide resolved

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
Loading