Skip to content
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
36 changes: 22 additions & 14 deletions mypyc/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from mypyc.namegen import exported_name
from mypyc.options import CompilerOptions
from mypyc.errors import Errors
from mypyc.common import shared_lib_name
from mypyc.common import BUILD_DIR, shared_lib_name
from mypyc.ops import format_modules

from mypyc import emitmodule
Expand Down Expand Up @@ -79,7 +79,8 @@ def fail(message: str) -> NoReturn:


def get_mypy_config(paths: List[str],
mypy_options: Optional[List[str]]) -> Tuple[List[BuildSource], Options]:
mypy_options: Optional[List[str]],
compiler_options: CompilerOptions) -> Tuple[List[BuildSource], Options]:
"""Construct mypy BuildSources and Options from file and options lists"""
# It is kind of silly to do this but oh well
mypy_options = mypy_options or []
Expand All @@ -99,8 +100,8 @@ def get_mypy_config(paths: List[str],
options.show_traceback = True
# Needed to get types for all AST nodes
options.export_types = True
# TODO: Support incremental checking
options.incremental = False
# We use mypy incremental mode when doing separate/incremental mypyc compilation
options.incremental = compiler_options.separate
options.preserve_asts = True

for source in sources:
Expand Down Expand Up @@ -184,7 +185,7 @@ def generate_c(sources: List[BuildSource],
# Do the actual work now
t0 = time.time()
try:
result = emitmodule.parse_and_typecheck(sources, options)
result = emitmodule.parse_and_typecheck(sources, options, groups)
except CompileError as e:
for line in e.messages:
print(line)
Expand Down Expand Up @@ -283,14 +284,17 @@ def write_file(path: str, contents: str) -> None:
want to write, skip writing so as to preserve the mtime
and avoid triggering recompilation.
"""
# We encode it ourselves and open the files as binary to avoid windows
# newline translation
encoded_contents = contents.encode('utf-8')
try:
with open(path, 'r', encoding='utf-8') as f:
old_contents = f.read() # type: Optional[str]
with open(path, 'rb') as f:
old_contents = f.read() # type: Optional[bytes]
except IOError:
old_contents = None
if old_contents != contents:
with open(path, 'w', encoding='utf-8') as f:
f.write(contents)
if old_contents != encoded_contents:
with open(path, 'wb') as f:
f.write(encoded_contents)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this is relevant, but long time ago we had problems on Windows with incremental mode (see for example #3215). This is why we have some non-trivial logic in and around FilesystemMetadataStore.write(). Maybe you can use the same here?


# Fudge the mtime forward because otherwise when two builds happen close
# together (like in a test) setuptools might not realize the source is newer
Expand Down Expand Up @@ -400,8 +404,12 @@ def mypycify(
"""

setup_mypycify_vars()
compiler_options = CompilerOptions(strip_asserts=strip_asserts,
multi_file=multi_file, verbose=verbose)
compiler_options = CompilerOptions(
strip_asserts=strip_asserts,
multi_file=multi_file,
verbose=verbose,
separate=separate is not False,
)

# Create a compiler object so we can make decisions based on what
# compiler is being used. typeshed is missing some attribues on the
Expand All @@ -413,13 +421,13 @@ def mypycify(
for path in paths:
expanded_paths.extend(glob.glob(path))

build_dir = 'build' # TODO: can this be overridden??
build_dir = BUILD_DIR # TODO: can this be overridden??
try:
os.mkdir(build_dir)
except FileExistsError:
pass

sources, options = get_mypy_config(expanded_paths, mypy_options)
sources, options = get_mypy_config(expanded_paths, mypy_options, compiler_options)
# We generate a shared lib if there are multiple modules or if any
# of the modules are in package. (Because I didn't want to fuss
# around with making the single module code handle packages.)
Expand Down
2 changes: 2 additions & 0 deletions mypyc/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
if MYPY:
from typing_extensions import Final

BUILD_DIR = 'build'

PREFIX = 'CPyPy_' # type: Final # Python wrappers
NATIVE_PREFIX = 'CPyDef_' # type: Final # Native functions etc.
DUNDER_PREFIX = 'CPyDunder_' # type: Final # Wrappers for exposing dunder methods to the API
Expand Down
Loading