Skip to content

[mypyc] Postpone when decide setuptools or distutils #7897

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

Merged
merged 1 commit into from
Nov 7, 2019
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
50 changes: 22 additions & 28 deletions mypyc/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
import re

from typing import List, Tuple, Any, Optional, Dict, Union, Set, cast
MYPY = False
if MYPY:
from typing import NoReturn
from typing_extensions import TYPE_CHECKING, NoReturn, Type

from mypy.main import process_options
from mypy.errors import CompileError
Expand All @@ -42,21 +40,23 @@

from mypyc import emitmodule

if TYPE_CHECKING:
from distutils.core import Extension # noqa

# We can work with either setuptools or distutils, and pick setuptools
# if it has been imported.
assert 'setuptools' in sys.modules or 'distutils' in sys.modules, (
"'setuptools' or 'distutils' must be imported before mypyc.build")
USE_SETUPTOOLS = 'setuptools' in sys.modules
from distutils import sysconfig, ccompiler

if not USE_SETUPTOOLS:
from distutils.core import setup, Extension
from distutils.command.build_ext import build_ext # type: ignore
else:
from setuptools import setup, Extension # type: ignore # noqa
from setuptools.command.build_ext import build_ext # type: ignore

from distutils import sysconfig, ccompiler
def get_extension() -> Type['Extension']:
# We can work with either setuptools or distutils, and pick setuptools
# if it has been imported.
use_setuptools = 'setuptools' in sys.modules

if not use_setuptools:
from distutils.core import Extension
else:
from setuptools import Extension # type: ignore # noqa

return Extension


def setup_mypycify_vars() -> None:
Expand All @@ -73,7 +73,7 @@ def setup_mypycify_vars() -> None:
vars['CFLAGS'] = vars['CFLAGS'].replace('-arch i386', '')


def fail(message: str) -> 'NoReturn':
def fail(message: str) -> NoReturn:
# TODO: Is there something else we should do to fail?
sys.exit(message)

Expand Down Expand Up @@ -221,7 +221,7 @@ def build_using_shared_lib(sources: List[BuildSource],
deps: List[str],
build_dir: str,
extra_compile_args: List[str],
) -> List[Extension]:
) -> List['Extension']:
"""Produce the list of extension modules when a shared library is needed.

This creates one shared library extension module that all of the
Expand All @@ -233,7 +233,7 @@ def build_using_shared_lib(sources: List[BuildSource],
extension module that exports the real initialization functions in
Capsules stored in module attributes.
"""
extensions = [Extension(
extensions = [get_extension()(
shared_lib_name(group_name),
sources=cfiles,
include_dirs=[include_dir()],
Expand All @@ -251,7 +251,7 @@ def build_using_shared_lib(sources: List[BuildSource],
assert source.path
if os.path.split(source.path)[1] == '__init__.py':
full_module_name += '.__init__'
extensions.append(Extension(
extensions.append(get_extension()(
full_module_name,
sources=[shim_file],
extra_compile_args=extra_compile_args,
Expand All @@ -263,12 +263,12 @@ def build_using_shared_lib(sources: List[BuildSource],
def build_single_module(sources: List[BuildSource],
cfiles: List[str],
extra_compile_args: List[str],
) -> List[Extension]:
) -> List['Extension']:
"""Produce the list of extension modules for a standalone extension.

This contains just one module, since there is no need for a shared module.
"""
return [Extension(
return [get_extension()(
sources[0].module,
sources=cfiles,
include_dirs=[include_dir()],
Expand Down Expand Up @@ -367,7 +367,7 @@ def mypycify(
multi_file: bool = False,
separate: Union[bool, List[Tuple[List[str], Optional[str]]]] = False,
skip_cgen_input: Optional[Any] = None
) -> List[Extension]:
) -> List['Extension']:
"""Main entry point to building using mypyc.

This produces a list of Extension objects that should be passed as the
Expand Down Expand Up @@ -500,9 +500,3 @@ def mypycify(
group_sources, cfilenames + shared_cfilenames, cflags))

return extensions


# For backwards compatibility we define this as an alias. Previous
# versions used to require using it, and it is conceivable that future
# versions will need it also.
MypycifyBuildExt = build_ext
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def run(self):
del sys.modules['mypy.git']
sys.path.insert(0, use_other_mypyc)

from mypyc.build import mypycify, MypycifyBuildExt
from mypyc.build import mypycify
opt_level = os.getenv('MYPYC_OPT_LEVEL', '3')
force_multifile = os.getenv('MYPYC_MULTI_FILE', '') == '1'
ext_modules = mypycify(
Expand All @@ -151,7 +151,6 @@ def run(self):
# our Appveyor builds run out of memory sometimes.
multi_file=sys.platform == 'win32' or force_multifile,
)
cmdclass['build_ext'] = MypycifyBuildExt
else:
ext_modules = []

Expand Down